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 public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
25 if (clazz.isInterface()) {
26 throw new IllegalArgumentException(clazz
27 + " should not be an interface");
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()));
34 clazz = clazz.getSuperclass();
36 return getAllSuperInterfaces(toBeInspected);
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();
48 if (ifc.isInterface() == false) {
49 throw new IllegalArgumentException(ifc + " should be an interface");
51 ifcs.addAll(Arrays.asList(ifc.getInterfaces()));
58 * Get interfaces that this class is derived from that are JMX interfaces.
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)) {
73 * Get all implemented interfaces that have
74 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
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) {
93 public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(Class<? extends Module> configBeanClass) {
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);
101 return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
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()}
110 public static Set<Class<?>> getOsgiRegistrationTypes(
111 Class<? extends Module> configBeanClass) {
112 // TODO test with service interface hierarchy
113 Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
114 Set<Class<?>> result = new HashSet<>();
115 for (Class<?> clazz : serviceInterfaces) {
116 ServiceInterfaceAnnotation annotation = clazz
117 .getAnnotation(ServiceInterfaceAnnotation.class);
118 result.add(annotation.osgiRegistrationType());
124 public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(ModuleFactory factory) {
125 Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections.unmodifiableSet(factory.getImplementedServiceIntefaces());
126 return getServiceInterfaceAnnotations(implementedServiceIntefaces);
129 private static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces) {
130 Set<Class<? extends AbstractServiceInterface>> inspected = getAllAbstractServiceInterfaceClasses(implementedServiceIntefaces);
131 Set<ServiceInterfaceAnnotation> result = new HashSet<>();
132 // SIs can form hierarchies, inspect superclass until it does not extend AbstractSI
133 for (Class<?> clazz : inspected) {
134 ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
135 if (annotation != null) {
136 result.add(annotation);
142 static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
143 Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
145 Set<Class<?>> allInterfaces = getAllSuperInterfaces((Set) directlyImplementedAbstractSIs);
146 Set<Class<? extends AbstractServiceInterface>> result = new HashSet<>();
147 for(Class<?> ifc: allInterfaces){
148 if (AbstractServiceInterface.class.isAssignableFrom(ifc) &&
149 ifc.equals(AbstractServiceInterface.class) == false) {
150 result.add((Class<? extends AbstractServiceInterface>) ifc);