Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / util / InterfacesHelper.java
1 /*
2  * Copyright (c) 2013 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.controller.config.manager.impl.util;
9
10 import java.util.Arrays;
11 import java.util.HashSet;
12 import java.util.Iterator;
13 import java.util.Set;
14
15 import javax.management.JMX;
16
17 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
18 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
19 import org.opendaylight.controller.config.spi.Module;
20
21 public class InterfacesHelper {
22
23     public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
24         if (clazz.isInterface()) {
25             throw new IllegalArgumentException(clazz
26                     + " should not be an interface");
27         }
28         // getInterfaces gets interfaces implemented directly by this class
29         Set<Class<?>> toBeInspected = new HashSet<>();
30         while (clazz.equals(Object.class) == false) {
31             toBeInspected.addAll(Arrays.asList(clazz.getInterfaces()));
32             // get parent class
33             clazz = clazz.getSuperclass();
34         }
35         // each interface can extend other interfaces
36         Set<Class<?>> inspected = new HashSet<>();
37         while (toBeInspected.size() > 0) {
38             Iterator<Class<?>> iterator = toBeInspected.iterator();
39             Class<?> ifc = iterator.next();
40             iterator.remove();
41             toBeInspected.addAll(Arrays.asList(ifc.getInterfaces()));
42             inspected.add(ifc);
43         }
44         return inspected;
45     }
46
47     /**
48      * Get interfaces that this class is derived from that are JMX interfaces.
49      */
50     public static Set<Class<?>> getMXInterfaces(
51             Class<? extends Module> configBeanClass) {
52         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
53         Set<Class<?>> result = new HashSet<>();
54         for (Class<?> clazz : allInterfaces) {
55             if (JMX.isMXBeanInterface(clazz)) {
56                 result.add(clazz);
57             }
58         }
59         return result;
60     }
61
62     /**
63      * Get all implemented interfaces that have
64      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
65      * annotation.
66      */
67     public static Set<Class<?>> getServiceInterfaces(
68             Class<? extends Module> configBeanClass) {
69         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
70         Set<Class<?>> result = new HashSet<>();
71         for (Class<?> clazz : allInterfaces) {
72             if (AbstractServiceInterface.class.isAssignableFrom(clazz)) {
73                 ServiceInterfaceAnnotation annotation = clazz
74                         .getAnnotation(ServiceInterfaceAnnotation.class);
75                 if (annotation != null) {
76                     result.add(clazz);
77                 }
78             }
79         }
80         return result;
81     }
82
83     /**
84      * Get OSGi registration types under which config bean instance should be
85      * registered. This is specified in
86      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()}
87      */
88     public static Set<Class<?>> getOsgiRegistrationTypes(
89             Class<? extends Module> configBeanClass) {
90         Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
91         Set<Class<?>> result = new HashSet<>();
92         for (Class<?> clazz : serviceInterfaces) {
93             ServiceInterfaceAnnotation annotation = clazz
94                     .getAnnotation(ServiceInterfaceAnnotation.class);
95             result.add(annotation.osgiRegistrationType());
96         }
97         return result;
98     }
99
100 }