Added BundleContext reference to generated factories for config subsystem
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / ClassBasedModuleFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.config.api.DependencyResolver;
12 import org.opendaylight.controller.config.api.DependencyResolverFactory;
13 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
14 import org.opendaylight.controller.config.api.ModuleIdentifier;
15 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
16 import org.opendaylight.controller.config.spi.Module;
17 import org.opendaylight.controller.config.spi.ModuleFactory;
18 import org.osgi.framework.BundleContext;
19
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 /**
28  * Creates new modules by reflection. Provided class must have this constructor:
29  * ctor(DynamicMBeanWithInstance.class, ModuleIdentifier.class).
30  * When reconfiguring, both parameters will be non null. When creating new
31  * instance first parameter will be null.
32  *
33  */
34 public class ClassBasedModuleFactory implements ModuleFactory {
35     private final String implementationName;
36     private final Class<? extends Module> configBeanClass;
37
38     /**
39      * @param implementationName
40      * @param configBeanClass
41      *            class that will be instantiated when createModule is called.
42      *            This class must implement Module interface and all exported
43      *            interfaces.
44      */
45     public ClassBasedModuleFactory(String implementationName,
46             Class<? extends Module> configBeanClass) {
47         this.implementationName = implementationName;
48         this.configBeanClass = configBeanClass;
49     }
50
51     @Override
52     public String getImplementationName() {
53         return implementationName;
54     }
55
56     @Override
57     public Module createModule(String instanceName,
58             DependencyResolver dependencyResolver, DynamicMBeanWithInstance old, BundleContext bundleContext)
59             throws Exception {
60         Preconditions.checkNotNull(old);
61         return constructModule(instanceName, dependencyResolver, old);
62     }
63
64     private Module constructModule(String instanceName, DependencyResolver dependencyResolver, DynamicMBeanWithInstance old) throws InstantiationException, IllegalAccessException, InvocationTargetException {
65         Preconditions.checkNotNull(dependencyResolver);
66         ModuleIdentifier moduleIdentifier = new ModuleIdentifier(implementationName, instanceName);
67         Constructor<? extends Module> declaredConstructor;
68         try {
69             declaredConstructor = configBeanClass.getDeclaredConstructor(DynamicMBeanWithInstance.class, ModuleIdentifier.class);
70         } catch (NoSuchMethodException e) {
71             throw new IllegalStateException(
72                     "Did not find constructor with parameters (DynamicMBeanWithInstance) in "
73                             + configBeanClass, e);
74         }
75         Preconditions.checkState(declaredConstructor != null);
76         return declaredConstructor.newInstance(old, moduleIdentifier);
77     }
78
79     @Override
80     public Module createModule(String instanceName,
81             DependencyResolver dependencyResolver, BundleContext bundleContext) {
82         try {
83             return constructModule(instanceName, dependencyResolver, null);
84         } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
85             throw new RuntimeException(e);
86         }
87     }
88
89     @Override
90     public boolean isModuleImplementingServiceInterface(
91             Class<? extends AbstractServiceInterface> serviceInterface) {
92         Class<?>[] classes = configBeanClass.getInterfaces();
93         List<Class<?>> ifc = Arrays.asList(classes);
94         if (ifc.contains(serviceInterface)) {
95             return true;
96         }
97         for (Class<?> c : classes) {
98             ifc = Arrays.asList(c.getInterfaces());
99             if (ifc.contains(serviceInterface)) {
100                 return true;
101             }
102         }
103         return false;
104     }
105
106     @Override
107     public Set<Module> getDefaultModules(DependencyResolverFactory dependencyResolverFactory, BundleContext bundleContext) {
108         return new HashSet<Module>();
109     }
110 }