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