X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Futil%2FInterfacesHelper.java;fp=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Futil%2FInterfacesHelper.java;h=6cd855450ddec8df16df9431701598fb9084ecd8;hb=9fb64948564e252018f9b1e13e7cea2c92f991aa;hp=0000000000000000000000000000000000000000;hpb=1742b3894614be478c333a1043ced8ef1bc5dc84;p=controller.git diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/InterfacesHelper.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/InterfacesHelper.java new file mode 100644 index 0000000000..6cd855450d --- /dev/null +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/util/InterfacesHelper.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.config.manager.impl.util; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import javax.management.JMX; + +import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface; +import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation; +import org.opendaylight.controller.config.spi.Module; + +public class InterfacesHelper { + + public static Set> getAllInterfaces(Class clazz) { + if (clazz.isInterface()) { + throw new IllegalArgumentException(clazz + + " should not be an interface"); + } + // getInterfaces gets interfaces implemented directly by this class + Set> toBeInspected = new HashSet<>(); + while (clazz.equals(Object.class) == false) { + toBeInspected.addAll(Arrays.asList(clazz.getInterfaces())); + // get parent class + clazz = clazz.getSuperclass(); + } + // each interface can extend other interfaces + Set> inspected = new HashSet<>(); + while (toBeInspected.size() > 0) { + Iterator> iterator = toBeInspected.iterator(); + Class ifc = iterator.next(); + iterator.remove(); + toBeInspected.addAll(Arrays.asList(ifc.getInterfaces())); + inspected.add(ifc); + } + return inspected; + } + + /** + * Get interfaces that this class is derived from that are JMX interfaces. + */ + public static Set> getMXInterfaces( + Class configBeanClass) { + Set> allInterfaces = getAllInterfaces(configBeanClass); + Set> result = new HashSet<>(); + for (Class clazz : allInterfaces) { + if (JMX.isMXBeanInterface(clazz)) { + result.add(clazz); + } + } + return result; + } + + /** + * Get all implemented interfaces that have + * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation} + * annotation. + */ + public static Set> getServiceInterfaces( + Class configBeanClass) { + Set> allInterfaces = getAllInterfaces(configBeanClass); + Set> result = new HashSet<>(); + for (Class clazz : allInterfaces) { + if (AbstractServiceInterface.class.isAssignableFrom(clazz)) { + ServiceInterfaceAnnotation annotation = clazz + .getAnnotation(ServiceInterfaceAnnotation.class); + if (annotation != null) { + result.add(clazz); + } + } + } + return result; + } + + /** + * Get OSGi registration types under which config bean instance should be + * registered. This is specified in + * {@link org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation#osgiRegistrationType()} + */ + public static Set> getOsgiRegistrationTypes( + Class configBeanClass) { + Set> serviceInterfaces = getServiceInterfaces(configBeanClass); + Set> result = new HashSet<>(); + for (Class clazz : serviceInterfaces) { + ServiceInterfaceAnnotation annotation = clazz + .getAnnotation(ServiceInterfaceAnnotation.class); + result.add(annotation.osgiRegistrationType()); + } + return result; + } + +}