Initial code drop of yang model driven configuration system
[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.util.Arrays;
12 import java.util.List;
13
14 import org.opendaylight.controller.config.api.DependencyResolver;
15 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
16 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
17 import org.opendaylight.controller.config.spi.Module;
18 import org.opendaylight.controller.config.spi.ModuleFactory;
19
20 import com.google.common.base.Preconditions;
21 import com.google.common.base.Throwables;
22
23 /**
24  * Creates new Config beans by calling {@link Class#newInstance()} on provided
25  * config bean class.
26  *
27  */
28 public class ClassBasedModuleFactory implements ModuleFactory {
29     private final String implementationName;
30     private final Class<? extends Module> configBeanClass;
31
32     /**
33      * @param implementationName
34      * @param configBeanClass
35      *            class that will be instantiated when createModule is called.
36      *            This class must implement Module interface and all exported
37      *            interfaces.
38      */
39     public ClassBasedModuleFactory(String implementationName,
40             Class<? extends Module> configBeanClass) {
41         this.implementationName = implementationName;
42         this.configBeanClass = configBeanClass;
43     }
44
45     @Override
46     public String getImplementationName() {
47         return implementationName;
48     }
49
50     @Override
51     public Module createModule(String instanceName,
52             DependencyResolver dependencyResolver, DynamicMBeanWithInstance old)
53             throws Exception {
54         Preconditions.checkNotNull(dependencyResolver);
55         Preconditions.checkNotNull(old);
56         Constructor<? extends Module> declaredConstructor;
57         try {
58             declaredConstructor = configBeanClass
59                     .getDeclaredConstructor(DynamicMBeanWithInstance.class);
60         } catch (NoSuchMethodException e) {
61             throw new IllegalStateException(
62                     "Did not find constructor with parameters (DynamicMBeanWithInstance) in "
63                             + configBeanClass, e);
64         }
65         Preconditions.checkState(declaredConstructor != null);
66         return declaredConstructor.newInstance(old);
67     }
68
69     @Override
70     public Module createModule(String instanceName,
71             DependencyResolver dependencyResolver) {
72         try {
73             return configBeanClass.newInstance();
74         } catch (Exception e) {
75             throw Throwables.propagate(e);
76         }
77     }
78
79     @Override
80     public boolean isModuleImplementingServiceInterface(
81             Class<? extends AbstractServiceInterface> serviceInterface) {
82         Class<?>[] classes = configBeanClass.getInterfaces();
83         List<Class<?>> ifc = Arrays.asList(classes);
84         if (ifc.contains(serviceInterface)) {
85             return true;
86         }
87         for (Class<?> c : classes) {
88             ifc = Arrays.asList(c.getInterfaces());
89             if (ifc.contains(serviceInterface)) {
90                 return true;
91             }
92         }
93         return false;
94     }
95 }