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