2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl.util;
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;
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;
22 public class InterfacesHelper {
24 private InterfacesHelper() {
27 public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
28 if (clazz.isInterface()) {
29 throw new IllegalArgumentException(clazz
30 + " should not be an interface");
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()));
37 clazz = clazz.getSuperclass();
39 return getAllSuperInterfaces(toBeInspected);
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();
51 if (ifc.isInterface() == false) {
52 throw new IllegalArgumentException(ifc + " should be an interface");
54 interfaces.addAll(Arrays.asList(ifc.getInterfaces()));
61 * Get interfaces that this class is derived from that are JMX interfaces.
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)) {
76 * Get all implemented interfaces that have
77 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
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) {
96 public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(Class<? extends Module> configBeanClass) {
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);
104 return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
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()}
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());
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());
130 return Collections.unmodifiableSet(qNames);
133 public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(ModuleFactory factory) {
134 Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections.unmodifiableSet(factory.getImplementedServiceIntefaces());
135 return getServiceInterfaceAnnotations(implementedServiceIntefaces);
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);
148 return Collections.unmodifiableSet(result);
151 static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
152 Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
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);