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 java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Iterator;
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;
21 public class InterfacesHelper {
23 private InterfacesHelper() {
26 public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
27 if (clazz.isInterface()) {
28 throw new IllegalArgumentException(clazz
29 + " should not be an interface");
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()));
36 clazz = clazz.getSuperclass();
38 return getAllSuperInterfaces(toBeInspected);
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();
50 if (ifc.isInterface() == false) {
51 throw new IllegalArgumentException(ifc + " should be an interface");
53 interfaces.addAll(Arrays.asList(ifc.getInterfaces()));
60 * Get interfaces that this class is derived from that are JMX interfaces.
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)) {
75 * Get all implemented interfaces that have
76 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
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) {
95 public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(final Class<? extends Module> configBeanClass) {
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);
103 return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
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()}
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());
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());
129 return Collections.unmodifiableSet(qNames);
132 public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(final ModuleFactory factory) {
133 Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections.unmodifiableSet(factory.getImplementedServiceIntefaces());
134 return getServiceInterfaceAnnotations(implementedServiceIntefaces);
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);
147 return Collections.unmodifiableSet(result);
150 static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
151 final Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
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);