2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.aaa.authn.mdsal.store;
10 import java.lang.reflect.Method;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import org.opendaylight.aaa.api.model.Domain;
16 import org.opendaylight.aaa.api.model.Grant;
17 import org.opendaylight.aaa.api.model.Role;
18 import org.opendaylight.aaa.api.model.User;
19 import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.DomainBuilder;
20 import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.GrantBuilder;
21 import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.RoleBuilder;
22 import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.UserBuilder;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
26 * @author saichler@gmail.com
28 * This class is a codec to convert between MDSAL objects and IDM model objects. It is doing so via reflection when it assumes that the MDSAL
29 * Object and the IDM model object has the same method names.
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * @author Sharon Aicler - saichler@cisco.com
38 public abstract class IDMObject2MDSAL {
39 private static final Logger LOG = LoggerFactory.getLogger(IDMObject2MDSAL.class);
40 // this is a Map mapping between the class type of the IDM Model object to a
41 // structure containing the corresponding setters and getter methods
43 private static Map<Class<?>, ConvertionMethods> typesMethods = new HashMap<Class<?>, ConvertionMethods>();
45 // This method generically via reflection receive a MDSAL object and the
46 // corresponding IDM model object class type and
47 // creates an IDM model element from the MDSAL element
48 private static Object fromMDSALObject(Object mdsalObject, Class<?> type) throws Exception {
49 if (mdsalObject == null)
51 Object result = type.newInstance();
52 ConvertionMethods cm = typesMethods.get(type);
54 cm = new ConvertionMethods();
55 typesMethods.put(type, cm);
56 Method methods[] = type.getMethods();
57 for (Method m : methods) {
58 if (m.getName().startsWith("set")) {
61 if (m.getParameterTypes()[0].equals(Boolean.class)
62 || m.getParameterTypes()[0].equals(boolean.class))
63 gm = ((DataObject) mdsalObject).getImplementedInterface().getMethod(
64 "is" + m.getName().substring(3), (Class[]) null);
67 gm = ((DataObject) mdsalObject).getImplementedInterface().getMethod(
68 "get" + m.getName().substring(3), (Class[]) null);
69 } catch (Exception err) {
70 LOG.error("Error associating get call", err);
73 cm.getMethods.put(m.getName(), gm);
77 for (Method m : cm.setMethods) {
81 new Object[] { cm.getMethods.get(m.getName()).invoke(mdsalObject,
83 } catch (Exception err) {
84 LOG.error("Error invoking reflection method", err);
90 // This method generically use reflection to receive an IDM model object and
91 // the corresponsing MDSAL object and creates
92 // a MDSAL object out of the IDM model object
93 private static Object toMDSALObject(Object object, Class<?> mdSalBuilderType) throws Exception {
96 Object result = mdSalBuilderType.newInstance();
97 ConvertionMethods cm = typesMethods.get(mdSalBuilderType);
99 cm = new ConvertionMethods();
100 typesMethods.put(mdSalBuilderType, cm);
101 Method methods[] = mdSalBuilderType.getMethods();
102 for (Method m : methods) {
103 if (m.getName().startsWith("set")) {
106 if (m.getParameterTypes()[0].equals(Boolean.class)
107 || m.getParameterTypes()[0].equals(boolean.class))
108 gm = object.getClass().getMethod("is" + m.getName().substring(3),
111 gm = object.getClass().getMethod("get" + m.getName().substring(3),
113 cm.getMethods.put(m.getName(), gm);
114 cm.setMethods.add(m);
115 } catch (NoSuchMethodException err) {
119 cm.builderMethod = mdSalBuilderType.getMethod("build", (Class[]) null);
121 for (Method m : cm.setMethods) {
123 new Object[] { cm.getMethods.get(m.getName()).invoke(object, (Object[]) null) });
126 return cm.builderMethod.invoke(result, (Object[]) null);
129 // A struccture class to hold the getters & setters of each type to speed
131 private static class ConvertionMethods {
132 private List<Method> setMethods = new ArrayList<Method>();
133 private Map<String, Method> getMethods = new HashMap<String, Method>();
134 private Method builderMethod = null;
138 public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain toMDSALDomain(
141 return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain) toMDSALObject(
142 domain, DomainBuilder.class);
143 } catch (Exception err) {
144 LOG.error("Error converting domain to MDSAL object", err);
149 public static Domain toIDMDomain(
150 org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain domain) {
152 return (Domain) fromMDSALObject(domain, Domain.class);
153 } catch (Exception err) {
154 LOG.error("Error converting domain from MDSAL to IDM object", err);
160 public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role toMDSALRole(
163 return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role) toMDSALObject(
164 role, RoleBuilder.class);
165 } catch (Exception err) {
166 LOG.error("Error converting role to MDSAL object", err);
171 public static Role toIDMRole(
172 org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role role) {
174 return (Role) fromMDSALObject(role, Role.class);
175 } catch (Exception err) {
176 LOG.error("Error converting role fom MDSAL to IDM object", err);
182 public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User toMDSALUser(
185 return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User) toMDSALObject(
186 user, UserBuilder.class);
187 } catch (Exception err) {
188 LOG.error("Error converting user to MDSAL object", err);
193 public static User toIDMUser(
194 org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User user) {
196 return (User) fromMDSALObject(user, User.class);
197 } catch (Exception err) {
198 LOG.error("Error converting user from MDSAL to IDM object", err);
204 public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant toMDSALGrant(
207 return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant) toMDSALObject(
208 grant, GrantBuilder.class);
209 } catch (Exception err) {
210 LOG.error("Error converting grant to MDSAL object", err);
215 public static Grant toIDMGrant(
216 org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant grant) {
218 return (Grant) fromMDSALObject(grant, Grant.class);
219 } catch (Exception err) {
220 LOG.error("Error converting grant from MDSAL to IDM object", err);