Fix checkstyle warnings in netconf-cli
[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.Collections;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15 import javax.management.JMX;
16 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
17 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
18 import org.opendaylight.controller.config.spi.Module;
19 import org.opendaylight.controller.config.spi.ModuleFactory;
20
21 public class InterfacesHelper {
22
23     private InterfacesHelper() {
24     }
25
26     public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
27         if (clazz.isInterface()) {
28             throw new IllegalArgumentException(clazz
29                     + " 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) == false) {
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() == false)  {
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     public static Set<Class<?>> getMXInterfaces(
63             final Class<? extends Module> configBeanClass) {
64         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
65         Set<Class<?>> result = new HashSet<>();
66         for (Class<?> clazz : allInterfaces) {
67             if (JMX.isMXBeanInterface(clazz)) {
68                 result.add(clazz);
69             }
70         }
71         return result;
72     }
73
74     /**
75      * Get all implemented interfaces that have
76      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
77      * annotation.
78      */
79     public static Set<Class<?>> getServiceInterfaces(
80             final Class<? extends Module> configBeanClass) {
81         Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
82         Set<Class<?>> result = new HashSet<>();
83         for (Class<?> clazz : allInterfaces) {
84             if (AbstractServiceInterface.class.isAssignableFrom(clazz)) {
85                 ServiceInterfaceAnnotation annotation = clazz
86                         .getAnnotation(ServiceInterfaceAnnotation.class);
87                 if (annotation != null) {
88                     result.add(clazz);
89                 }
90             }
91         }
92         return result;
93     }
94
95     public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(final Class<? extends Module> configBeanClass) {
96
97         Set<Class<? extends AbstractServiceInterface>> foundGeneratedSIClasses = new HashSet<>();
98         for (Class<?> clazz : getAllInterfaces(configBeanClass)) {
99             if (AbstractServiceInterface.class.isAssignableFrom(clazz) && AbstractServiceInterface.class.equals(clazz) == false) {
100                 foundGeneratedSIClasses.add((Class<? extends AbstractServiceInterface>) clazz);
101             }
102         }
103         return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
104     }
105
106
107     /**
108      * Get OSGi registration types under which config bean instance should be
109      * registered. This is specified in
110      * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()}
111      */
112     public static Set<Class<?>> getOsgiRegistrationTypes(
113             final Class<? extends Module> configBeanClass) {
114         Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
115         Set<Class<?>> result = new HashSet<>();
116         for (Class<?> clazz : serviceInterfaces) {
117             ServiceInterfaceAnnotation annotation = clazz
118                     .getAnnotation(ServiceInterfaceAnnotation.class);
119             result.add(annotation.osgiRegistrationType());
120         }
121         return result;
122     }
123
124     public static Set<String> getQNames(final Set<ServiceInterfaceAnnotation> siAnnotations) {
125         Set<String> qNames = new HashSet<>();
126         for (ServiceInterfaceAnnotation sia: siAnnotations) {
127             qNames.add(sia.value());
128         }
129         return Collections.unmodifiableSet(qNames);
130     }
131
132     public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(final ModuleFactory factory) {
133         Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections.unmodifiableSet(factory.getImplementedServiceIntefaces());
134         return getServiceInterfaceAnnotations(implementedServiceIntefaces);
135     }
136
137     private static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(final Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces) {
138         Set<Class<? extends AbstractServiceInterface>> inspected = getAllAbstractServiceInterfaceClasses(implementedServiceIntefaces);
139         Set<ServiceInterfaceAnnotation> result = new HashSet<>();
140         // SIs can form hierarchies, inspect superclass until it does not extend AbstractSI
141         for (Class<?> clazz : inspected) {
142             ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
143             if (annotation != null) {
144                 result.add(annotation);
145             }
146         }
147         return Collections.unmodifiableSet(result);
148     }
149
150     static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
151             final Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
152
153         Set<Class<?>> allInterfaces = getAllSuperInterfaces(directlyImplementedAbstractSIs);
154         Set<Class<? extends AbstractServiceInterface>> result = new HashSet<>();
155         for(Class<?> ifc: allInterfaces){
156             if (AbstractServiceInterface.class.isAssignableFrom(ifc) &&
157                     ifc.equals(AbstractServiceInterface.class) == false) {
158                 result.add((Class<? extends AbstractServiceInterface>) ifc);
159             }
160
161         }
162         return result;
163     }
164 }