Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / ClassBasedModuleFactory.java
index afc79a5c16b53b60d90b79a2717f498d42904c5b..93661699c4712debfc4cba9d4306e4a8e06e2913 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013, 2017 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,
@@ -7,22 +7,28 @@
  */
 package org.opendaylight.controller.config.manager.impl;
 
+import com.google.common.base.Preconditions;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
-
+import java.util.Set;
 import org.opendaylight.controller.config.api.DependencyResolver;
+import org.opendaylight.controller.config.api.DependencyResolverFactory;
 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
+import org.opendaylight.controller.config.api.ModuleIdentifier;
 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
+import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper;
 import org.opendaylight.controller.config.spi.Module;
 import org.opendaylight.controller.config.spi.ModuleFactory;
-
-import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
+import org.osgi.framework.BundleContext;
 
 /**
- * Creates new Config beans by calling {@link Class#newInstance()} on provided
- * config bean class.
+ * Creates new modules by reflection. Provided class must have this constructor:
+ * ctor(DynamicMBeanWithInstance.class, ModuleIdentifier.class). When
+ * reconfiguring, both parameters will be non null. When creating new instance
+ * first parameter will be null.
  *
  */
 public class ClassBasedModuleFactory implements ModuleFactory {
@@ -30,14 +36,15 @@ public class ClassBasedModuleFactory implements ModuleFactory {
     private final Class<? extends Module> configBeanClass;
 
     /**
+     * Module factory constructor.
+     *
      * @param implementationName
+     *            name of the implementation
      * @param configBeanClass
-     *            class that will be instantiated when createModule is called.
-     *            This class must implement Module interface and all exported
-     *            interfaces.
+     *            class that will be instantiated when createModule is called. This
+     *            class must implement Module interface and all exported interfaces.
      */
-    public ClassBasedModuleFactory(String implementationName,
-            Class<? extends Module> configBeanClass) {
+    public ClassBasedModuleFactory(final String implementationName, final Class<? extends Module> configBeanClass) {
         this.implementationName = implementationName;
         this.configBeanClass = configBeanClass;
     }
@@ -48,37 +55,42 @@ public class ClassBasedModuleFactory implements ModuleFactory {
     }
 
     @Override
-    public Module createModule(String instanceName,
-            DependencyResolver dependencyResolver, DynamicMBeanWithInstance old)
-            throws Exception {
-        Preconditions.checkNotNull(dependencyResolver);
+    public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
+            final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
         Preconditions.checkNotNull(old);
-        Constructor<? extends Module> declaredConstructor;
+        return constructModule(instanceName, dependencyResolver, old);
+    }
+
+    @Override
+    public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
+            final BundleContext bundleContext) {
         try {
-            declaredConstructor = configBeanClass
-                    .getDeclaredConstructor(DynamicMBeanWithInstance.class);
-        } catch (NoSuchMethodException e) {
-            throw new IllegalStateException(
-                    "Did not find constructor with parameters (DynamicMBeanWithInstance) in "
-                            + configBeanClass, e);
+            return constructModule(instanceName, dependencyResolver, null);
+        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
+            throw new RuntimeException(e);
         }
-        Preconditions.checkState(declaredConstructor != null);
-        return declaredConstructor.newInstance(old);
     }
 
-    @Override
-    public Module createModule(String instanceName,
-            DependencyResolver dependencyResolver) {
+    private Module constructModule(final String instanceName, final DependencyResolver dependencyResolver,
+            final DynamicMBeanWithInstance old)
+            throws InstantiationException, IllegalAccessException, InvocationTargetException {
+        Preconditions.checkNotNull(dependencyResolver);
+        ModuleIdentifier moduleIdentifier = new ModuleIdentifier(implementationName, instanceName);
+        Constructor<? extends Module> declaredConstructor;
         try {
-            return configBeanClass.newInstance();
-        } catch (Exception e) {
-            throw Throwables.propagate(e);
+            declaredConstructor = configBeanClass.getDeclaredConstructor(DynamicMBeanWithInstance.class,
+                    ModuleIdentifier.class);
+        } catch (final NoSuchMethodException e) {
+            throw new IllegalStateException(
+                    "Did not find constructor with parameters (DynamicMBeanWithInstance) in " + configBeanClass, e);
         }
+        Preconditions.checkState(declaredConstructor != null);
+        return declaredConstructor.newInstance(old, moduleIdentifier);
     }
 
     @Override
     public boolean isModuleImplementingServiceInterface(
-            Class<? extends AbstractServiceInterface> serviceInterface) {
+            final Class<? extends AbstractServiceInterface> serviceInterface) {
         Class<?>[] classes = configBeanClass.getInterfaces();
         List<Class<?>> ifc = Arrays.asList(classes);
         if (ifc.contains(serviceInterface)) {
@@ -92,4 +104,15 @@ public class ClassBasedModuleFactory implements ModuleFactory {
         }
         return false;
     }
+
+    @Override
+    public Set<Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory,
+            final BundleContext bundleContext) {
+        return new HashSet<>();
+    }
+
+    @Override
+    public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
+        return InterfacesHelper.getAllAbstractServiceClasses(configBeanClass);
+    }
 }