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.HashSet;
12 import java.util.Iterator;
15 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;
21 public class InterfacesHelper {
23 public static Set<Class<?>> getAllInterfaces(Class<?> clazz) {
24 if (clazz.isInterface()) {
25 throw new IllegalArgumentException(clazz
26 + " should not be an interface");
28 // getInterfaces gets interfaces implemented directly by this class
29 Set<Class<?>> toBeInspected = new HashSet<>();
30 while (clazz.equals(Object.class) == false) {
31 toBeInspected.addAll(Arrays.asList(clazz.getInterfaces()));
33 clazz = clazz.getSuperclass();
35 // each interface can extend other interfaces
36 Set<Class<?>> inspected = new HashSet<>();
37 while (toBeInspected.size() > 0) {
38 Iterator<Class<?>> iterator = toBeInspected.iterator();
39 Class<?> ifc = iterator.next();
41 toBeInspected.addAll(Arrays.asList(ifc.getInterfaces()));
48 * Get interfaces that this class is derived from that are JMX interfaces.
50 public static Set<Class<?>> getMXInterfaces(
51 Class<? extends Module> configBeanClass) {
52 Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
53 Set<Class<?>> result = new HashSet<>();
54 for (Class<?> clazz : allInterfaces) {
55 if (JMX.isMXBeanInterface(clazz)) {
63 * Get all implemented interfaces that have
64 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation}
67 public static Set<Class<?>> getServiceInterfaces(
68 Class<? extends Module> configBeanClass) {
69 Set<Class<?>> allInterfaces = getAllInterfaces(configBeanClass);
70 Set<Class<?>> result = new HashSet<>();
71 for (Class<?> clazz : allInterfaces) {
72 if (AbstractServiceInterface.class.isAssignableFrom(clazz)) {
73 ServiceInterfaceAnnotation annotation = clazz
74 .getAnnotation(ServiceInterfaceAnnotation.class);
75 if (annotation != null) {
84 * Get OSGi registration types under which config bean instance should be
85 * registered. This is specified in
86 * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()}
88 public static Set<Class<?>> getOsgiRegistrationTypes(
89 Class<? extends Module> configBeanClass) {
90 // TODO test with service interface hierarchy
91 Set<Class<?>> serviceInterfaces = getServiceInterfaces(configBeanClass);
92 Set<Class<?>> result = new HashSet<>();
93 for (Class<?> clazz : serviceInterfaces) {
94 ServiceInterfaceAnnotation annotation = clazz
95 .getAnnotation(ServiceInterfaceAnnotation.class);
96 result.add(annotation.osgiRegistrationType());