2 * Copyright (c) 2013, 2017 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 com.google.common.collect.ImmutableSet;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.Iterator;
16 import javax.management.JMX;
17 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
18 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
19 import org.opendaylight.controller.config.spi.Module;
20 import org.opendaylight.controller.config.spi.ModuleFactory;
22 public final class InterfacesHelper {
24 private InterfacesHelper() {
27 public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
28 if (clazz.isInterface()) {
29 throw new IllegalArgumentException(clazz + " 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)) {
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()) {
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 * @param configBeanClass
64 * @return set containing classes
66 public static Set<Class<?>> getMXInterfaces(final Class<? extends Module> configBeanClass) {
67 Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
68 Set<Class<?>> result = new HashSet<>();
69 for (Class<?> clazz : allInterfaces) {
70 if (JMX.isMXBeanInterface(clazz)) {
78 * Get all implemented interfaces that have
79 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
82 * @param configBeanClass
84 * @return set containing classes
86 public static Set<Class<?>> getServiceInterfaces(final Class<? extends Module> configBeanClass) {
87 Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
88 Set<Class<?>> result = new HashSet<>();
89 for (Class<?> clazz : allInterfaces) {
90 if (AbstractServiceInterface.class.isAssignableFrom(clazz)) {
91 ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
92 if (annotation != null) {
100 public static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceClasses(
101 final Class<? extends Module> configBeanClass) {
103 Set<Class<? extends AbstractServiceInterface>> foundGeneratedSIClasses = new HashSet<>();
104 for (Class<?> clazz : getAllInterfaces(configBeanClass)) {
105 if (AbstractServiceInterface.class.isAssignableFrom(clazz)
106 && !AbstractServiceInterface.class.equals(clazz)) {
107 foundGeneratedSIClasses.add((Class<? extends AbstractServiceInterface>) clazz);
110 return getAllAbstractServiceInterfaceClasses(foundGeneratedSIClasses);
114 * Get OSGi registration types under which config bean instance should be
115 * registered. This is specified in
116 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()}
118 * @param configBeanClass config bean class
119 * @return set of classes
121 public static Set<Class<?>> getOsgiRegistrationTypes(final Class<? extends Module> configBeanClass) {
122 Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
123 Set<Class<?>> result = new HashSet<>();
124 for (Class<?> clazz : serviceInterfaces) {
125 ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
126 result.add(annotation.osgiRegistrationType());
131 public static Set<String> getQNames(final Set<ServiceInterfaceAnnotation> siAnnotations) {
132 Set<String> names = new HashSet<>();
133 for (ServiceInterfaceAnnotation sia : siAnnotations) {
134 names.add(sia.value());
136 return ImmutableSet.copyOf(names);
139 public static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(final ModuleFactory factory) {
140 Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces = Collections
141 .unmodifiableSet(factory.getImplementedServiceIntefaces());
142 return getServiceInterfaceAnnotations(implementedServiceIntefaces);
145 private static Set<ServiceInterfaceAnnotation> getServiceInterfaceAnnotations(
146 final Set<Class<? extends AbstractServiceInterface>> implementedServiceIntefaces) {
147 Set<Class<? extends AbstractServiceInterface>> inspected = getAllAbstractServiceInterfaceClasses(
148 implementedServiceIntefaces);
149 Set<ServiceInterfaceAnnotation> result = new HashSet<>();
150 // SIs can form hierarchies, inspect superclass until it does not extend
152 for (Class<?> clazz : inspected) {
153 ServiceInterfaceAnnotation annotation = clazz.getAnnotation(ServiceInterfaceAnnotation.class);
154 if (annotation != null) {
155 result.add(annotation);
158 return Collections.unmodifiableSet(result);
161 static Set<Class<? extends AbstractServiceInterface>> getAllAbstractServiceInterfaceClasses(
162 final Set<Class<? extends AbstractServiceInterface>> directlyImplementedAbstractSIs) {
164 Set<Class<?>> allInterfaces = getAllSuperInterfaces(directlyImplementedAbstractSIs);
165 Set<Class<? extends AbstractServiceInterface>> result = new HashSet<>();
166 for (Class<?> ifc : allInterfaces) {
167 if (AbstractServiceInterface.class.isAssignableFrom(ifc) && !ifc.equals(AbstractServiceInterface.class)) {
168 result.add((Class<? extends AbstractServiceInterface>) ifc);