0b58ced7528db1e2d28423a4df0501b6a5167667
[moon.git] /
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.aaa.authn.mdsal.store;
9
10 import java.lang.reflect.Method;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
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;
24 /**
25  *
26  * @author saichler@gmail.com
27  *
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.
30  */
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * @author Sharon Aicler - saichler@cisco.com
36  *
37  */
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
42     // in MDSAL object
43     private static Map<Class<?>, ConvertionMethods> typesMethods = new HashMap<Class<?>, ConvertionMethods>();
44
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)
50             return null;
51         Object result = type.newInstance();
52         ConvertionMethods cm = typesMethods.get(type);
53         if (cm == null) {
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")) {
59                     cm.setMethods.add(m);
60                     Method gm = null;
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);
65                     else {
66                         try {
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);
71                         }
72                     }
73                     cm.getMethods.put(m.getName(), gm);
74                 }
75             }
76         }
77         for (Method m : cm.setMethods) {
78             try {
79                 m.invoke(
80                         result,
81                         new Object[] { cm.getMethods.get(m.getName()).invoke(mdsalObject,
82                                 (Object[]) null) });
83             } catch (Exception err) {
84                 LOG.error("Error invoking reflection method", err);
85             }
86         }
87         return result;
88     }
89
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 {
94         if (object == null)
95             return null;
96         Object result = mdSalBuilderType.newInstance();
97         ConvertionMethods cm = typesMethods.get(mdSalBuilderType);
98         if (cm == null) {
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")) {
104                     try {
105                         Method gm = null;
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),
109                                     (Class[]) null);
110                         else
111                             gm = object.getClass().getMethod("get" + m.getName().substring(3),
112                                     (Class[]) null);
113                         cm.getMethods.put(m.getName(), gm);
114                         cm.setMethods.add(m);
115                     } catch (NoSuchMethodException err) {
116                     }
117                 }
118             }
119             cm.builderMethod = mdSalBuilderType.getMethod("build", (Class[]) null);
120         }
121         for (Method m : cm.setMethods) {
122             m.invoke(result,
123                     new Object[] { cm.getMethods.get(m.getName()).invoke(object, (Object[]) null) });
124         }
125
126         return cm.builderMethod.invoke(result, (Object[]) null);
127     }
128
129     // A struccture class to hold the getters & setters of each type to speed
130     // things up
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;
135     }
136
137     // Convert Domain
138     public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain toMDSALDomain(
139             Domain domain) {
140         try {
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);
145             return null;
146         }
147     }
148
149     public static Domain toIDMDomain(
150             org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain domain) {
151         try {
152             return (Domain) fromMDSALObject(domain, Domain.class);
153         } catch (Exception err) {
154             LOG.error("Error converting domain from MDSAL to IDM object", err);
155             return null;
156         }
157     }
158
159     // Convert Role
160     public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role toMDSALRole(
161             Role role) {
162         try {
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);
167             return null;
168         }
169     }
170
171     public static Role toIDMRole(
172             org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role role) {
173         try {
174             return (Role) fromMDSALObject(role, Role.class);
175         } catch (Exception err) {
176             LOG.error("Error converting role fom MDSAL to IDM object", err);
177             return null;
178         }
179     }
180
181     // Convert User
182     public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User toMDSALUser(
183             User user) {
184         try {
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);
189             return null;
190         }
191     }
192
193     public static User toIDMUser(
194             org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User user) {
195         try {
196             return (User) fromMDSALObject(user, User.class);
197         } catch (Exception err) {
198             LOG.error("Error converting user from MDSAL to IDM object", err);
199             return null;
200         }
201     }
202
203     // Convert Grant
204     public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant toMDSALGrant(
205             Grant grant) {
206         try {
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);
211             return null;
212         }
213     }
214
215     public static Grant toIDMGrant(
216             org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant grant) {
217         try {
218             return (Grant) fromMDSALObject(grant, Grant.class);
219         } catch (Exception err) {
220             LOG.error("Error converting grant from MDSAL to IDM object", err);
221             return null;
222         }
223     }
224 }