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