aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/main/java/org/opendaylight/aaa/authn/mdsal/store/IDMObject2MDSAL.java
blob: 0b58ced7528db1e2d28423a4df0501b6a5167667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.aaa.authn.mdsal.store;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.opendaylight.aaa.api.model.Domain;
import org.opendaylight.aaa.api.model.Grant;
import org.opendaylight.aaa.api.model.Role;
import org.opendaylight.aaa.api.model.User;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.DomainBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.GrantBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.RoleBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.UserBuilder;
import org.opendaylight.yangtools.yang.binding.DataObject;
/**
 *
 * @author saichler@gmail.com
 *
 * 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
 * Object and the IDM model object has the same method names.
 */
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Sharon Aicler - saichler@cisco.com
 *
 */
public abstract class IDMObject2MDSAL {
    private static final Logger LOG = LoggerFactory.getLogger(IDMObject2MDSAL.class);
    // this is a Map mapping between the class type of the IDM Model object to a
    // structure containing the corresponding setters and getter methods
    // in MDSAL object
    private static Map<Class<?>, ConvertionMethods> typesMethods = new HashMap<Class<?>, ConvertionMethods>();

    // This method generically via reflection receive a MDSAL object and the
    // corresponding IDM model object class type and
    // creates an IDM model element from the MDSAL element
    private static Object fromMDSALObject(Object mdsalObject, Class<?> type) throws Exception {
        if (mdsalObject == null)
            return null;
        Object result = type.newInstance();
        ConvertionMethods cm = typesMethods.get(type);
        if (cm == null) {
            cm = new ConvertionMethods();
            typesMethods.put(type, cm);
            Method methods[] = type.getMethods();
            for (Method m : methods) {
                if (m.getName().startsWith("set")) {
                    cm.setMethods.add(m);
                    Method gm = null;
                    if (m.getParameterTypes()[0].equals(Boolean.class)
                            || m.getParameterTypes()[0].equals(boolean.class))
                        gm = ((DataObject) mdsalObject).getImplementedInterface().getMethod(
                                "is" + m.getName().substring(3), (Class[]) null);
                    else {
                        try {
                            gm = ((DataObject) mdsalObject).getImplementedInterface().getMethod(
                                    "get" + m.getName().substring(3), (Class[]) null);
                        } catch (Exception err) {
                            LOG.error("Error associating get call", err);
                        }
                    }
                    cm.getMethods.put(m.getName(), gm);
                }
            }
        }
        for (Method m : cm.setMethods) {
            try {
                m.invoke(
                        result,
                        new Object[] { cm.getMethods.get(m.getName()).invoke(mdsalObject,
                                (Object[]) null) });
            } catch (Exception err) {
                LOG.error("Error invoking reflection method", err);
            }
        }
        return result;
    }

    // This method generically use reflection to receive an IDM model object and
    // the corresponsing MDSAL object and creates
    // a MDSAL object out of the IDM model object
    private static Object toMDSALObject(Object object, Class<?> mdSalBuilderType) throws Exception {
        if (object == null)
            return null;
        Object result = mdSalBuilderType.newInstance();
        ConvertionMethods cm = typesMethods.get(mdSalBuilderType);
        if (cm == null) {
            cm = new ConvertionMethods();
            typesMethods.put(mdSalBuilderType, cm);
            Method methods[] = mdSalBuilderType.getMethods();
            for (Method m : methods) {
                if (m.getName().startsWith("set")) {
                    try {
                        Method gm = null;
                        if (m.getParameterTypes()[0].equals(Boolean.class)
                                || m.getParameterTypes()[0].equals(boolean.class))
                            gm = object.getClass().getMethod("is" + m.getName().substring(3),
                                    (Class[]) null);
                        else
                            gm = object.getClass().getMethod("get" + m.getName().substring(3),
                                    (Class[]) null);
                        cm.getMethods.put(m.getName(), gm);
                        cm.setMethods.add(m);
                    } catch (NoSuchMethodException err) {
                    }
                }
            }
            cm.builderMethod = mdSalBuilderType.getMethod("build", (Class[]) null);
        }
        for (Method m : cm.setMethods) {
            m.invoke(result,
                    new Object[] { cm.getMethods.get(m.getName()).invoke(object, (Object[]) null) });
        }

        return cm.builderMethod.invoke(result, (Object[]) null);
    }

    // A struccture class to hold the getters & setters of each type to speed
    // things up
    private static class ConvertionMethods {
        private List<Method> setMethods = new ArrayList<Method>();
        private Map<String, Method> getMethods = new HashMap<String, Method>();
        private Method builderMethod = null;
    }

    // Convert Domain
    public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain toMDSALDomain(
            Domain domain) {
        try {
            return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain) toMDSALObject(
                    domain, DomainBuilder.class);
        } catch (Exception err) {
            LOG.error("Error converting domain to MDSAL object", err);
            return null;
        }
    }

    public static Domain toIDMDomain(
            org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain domain) {
        try {
            return (Domain) fromMDSALObject(domain, Domain.class);
        } catch (Exception err) {
            LOG.error("Error converting domain from MDSAL to IDM object", err);
            return null;
        }
    }

    // Convert Role
    public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role toMDSALRole(
            Role role) {
        try {
            return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role) toMDSALObject(
                    role, RoleBuilder.class);
        } catch (Exception err) {
            LOG.error("Error converting role to MDSAL object", err);
            return null;
        }
    }

    public static Role toIDMRole(
            org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role role) {
        try {
            return (Role) fromMDSALObject(role, Role.class);
        } catch (Exception err) {
            LOG.error("Error converting role fom MDSAL to IDM object", err);
            return null;
        }
    }

    // Convert User
    public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User toMDSALUser(
            User user) {
        try {
            return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User) toMDSALObject(
                    user, UserBuilder.class);
        } catch (Exception err) {
            LOG.error("Error converting user to MDSAL object", err);
            return null;
        }
    }

    public static User toIDMUser(
            org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User user) {
        try {
            return (User) fromMDSALObject(user, User.class);
        } catch (Exception err) {
            LOG.error("Error converting user from MDSAL to IDM object", err);
            return null;
        }
    }

    // Convert Grant
    public static org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant toMDSALGrant(
            Grant grant) {
        try {
            return (org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant) toMDSALObject(
                    grant, GrantBuilder.class);
        } catch (Exception err) {
            LOG.error("Error converting grant to MDSAL object", err);
            return null;
        }
    }

    public static Grant toIDMGrant(
            org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant grant) {
        try {
            return (Grant) fromMDSALObject(grant, Grant.class);
        } catch (Exception err) {
            LOG.error("Error converting grant from MDSAL to IDM object", err);
            return null;
        }
    }
}