Cleanup RpcRoutingStrategy definition
[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 org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
11 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
12 import org.opendaylight.controller.config.spi.Module;
13 import org.opendaylight.controller.config.spi.ModuleFactory;
14
15 import javax.management.JMX;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.Set;
21
22 public class InterfacesHelper {
23
24     public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
25         if (clazz.isInterface()) {
26             throw new IllegalArgumentException(clazz
27                     + " should not be an interface");
28         }
29         // getInterfaces gets interfaces implemented directly by this class
30         Set<Class<?>> toBeInspected = new HashSet<>();
31         while (clazz.equals(Object.class) == false) {
32             toBeInspected.addAll(Arrays.asList(clazz.getInterfaces()));
33             // get parent class
34             clazz = clazz.getSuperclass();
35         }
36         return getAllSuperInterfaces(toBeInspected);
37
38     }
39
40     private static Set<Class<?>> getAllSuperInterfaces(Set<Class<?>> ifcs) {
41         ifcs = new HashSet<>(ifcs); // create copy to modify
42         // each interface can extend other interfaces
43         Set<Class<?>> result = new HashSet<>();
44         while (ifcs.size() > 0) {
45             Iterator<Class<?>> iterator = ifcs.iterator();
46             Class<?> ifc = iterator.next();
47             iterator.remove();
48             if (ifc.isInterface() == false)  {
49                 throw new IllegalArgumentException(ifc + " should be an interface");
50             }
51             ifcs.addAll(Arrays.asList(ifc.getInterfaces()));
52             result.add(ifc);
53         }
54         return result;
55     }
56
57     /**
58      * Get interfaces that this class is derived from that are JMX interfaces.
59      */
60     public static Set<Class<?>> getMXInterfaces(
61             Class<? extends Module> configBeanClass) {
62         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
63         Set<Class<?>> result = new HashSet<>();
64         for (Class<?> clazz : allInterfaces) {
65             if (JMX.isMXBeanInterface(clazz)) {
66                 result.add(clazz);
67             }
68         }
69         return result;
70     }
71
72     /**
73      * Get all implemented interfaces that have
74      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
75      * annotation.
76      */
77     public static Set<Class<?>> getServiceInterfaces(
78             Class<? extends Module> configBeanClass) {
79         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
80         Set<Class<?>> result = new HashSet<>();
81         for (Class<?> clazz : allInterfaces) {
82             if (AbstractServiceInterface.class.isAssignableFrom(clazz)) {
83                 ServiceInterfaceAnnotation annotation = clazz
84                         .getAnnotation(ServiceInterfaceAnnotation.class);
85                 if (annotation != null) {
86                     result.add(clazz);
87                 }
88             }
89         }
90         return result;
91     }
92
93     public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(Class<? extends Module> configBeanClass) {
94
95         Set<Class<? extends AbstractServiceInterface>> foundGeneratedSIClasses = new HashSet<>();
96         for (Class<?> clazz : getAllInterfaces(configBeanClass)) {
97             if (AbstractServiceInterface.class.isAssignableFrom(clazz) && AbstractServiceInterface.class.equals(clazz) == false) {
98                 foundGeneratedSIClasses.add((Class<? extends AbstractServiceInterface>) clazz);
99             }
100         }
101         return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
102     }
103
104
105     /**
106      * Get OSGi registration types under which config bean instance should be
107      * registered. This is specified in
108      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()}
109      */
110     public static Set<Class<?>> getOsgiRegistrationTypes(
111             Class<? extends Module> configBeanClass) {
112         Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
113         Set<Class<?>> result = new HashSet<>();
114         for (Class<?> clazz : serviceInterfaces) {
115             ServiceInterfaceAnnotation annotation = clazz
116                     .getAnnotation(ServiceInterfaceAnnotation.class);
117             result.add(annotation.osgiRegistrationType());
118         }
119         return result;
120     }
121
122     public static Set<String> getQNames(Set<ServiceInterfaceAnnotation> siAnnotations) {
123         Set<String> qNames = new HashSet<>();
124         for (ServiceInterfaceAnnotation sia: siAnnotations) {
125             qNames.add(sia.value());
126         }
127         return Collections.unmodifiableSet(qNames);
128     }
129
130     public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(ModuleFactory factory) {
131         Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections.unmodifiableSet(factory.getImplementedServiceIntefaces());
132         return getServiceInterfaceAnnotations(implementedServiceIntefaces);
133     }
134
135     private static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces) {
136         Set<Class<? extends AbstractServiceInterface>> inspected = getAllAbstractServiceInterfaceClasses(implementedServiceIntefaces);
137         Set<ServiceInterfaceAnnotation> result = new HashSet<>();
138         // SIs can form hierarchies, inspect superclass until it does not extend AbstractSI
139         for (Class<?> clazz : inspected) {
140             ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
141             if (annotation != null) {
142                 result.add(annotation);
143             }
144         }
145         return Collections.unmodifiableSet(result);
146     }
147
148     static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
149             Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
150
151         Set<Class<?>> allInterfaces = getAllSuperInterfaces((Set) directlyImplementedAbstractSIs);
152         Set<Class<? extends AbstractServiceInterface>> result = new HashSet<>();
153         for(Class<?> ifc: allInterfaces){
154             if (AbstractServiceInterface.class.isAssignableFrom(ifc) &&
155                     ifc.equals(AbstractServiceInterface.class) == false) {
156                 result.add((Class<? extends AbstractServiceInterface>) ifc);
157             }
158
159         }
160         return result;
161     }
162 }