d385a7db5a2d007bb6d4320f31d7247a4ecde1c1
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / threadpool / TestingFixedThreadPoolModuleFactory.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.testingservices.threadpool;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.HashSet;
12 import java.util.Set;
13 import org.opendaylight.controller.config.api.DependencyResolver;
14 import org.opendaylight.controller.config.api.DependencyResolverFactory;
15 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
18 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.ModifiableThreadPoolServiceInterface;
19 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
20 import org.opendaylight.controller.config.spi.Module;
21 import org.opendaylight.controller.config.spi.ModuleFactory;
22 import org.osgi.framework.BundleContext;
23
24 public class TestingFixedThreadPoolModuleFactory extends AbstractTestingFixedThreadPoolModuleFactory implements ModuleFactory {
25     public static final String NAME = "fixed";
26
27     private static final Set<Class<? extends AbstractServiceInterface>> ifc = ImmutableSet.of(
28             (Class<? extends AbstractServiceInterface>) ModifiableThreadPoolServiceInterface.class,
29             TestingThreadPoolServiceInterface.class);
30
31     @Override
32     public String getImplementationName() {
33         return NAME;
34     }
35
36     @Override
37     public TestingFixedThreadPoolModule createModule(final String instanceName,
38             final DependencyResolver dependencyResolver, final BundleContext bundleContext) {
39         return new TestingFixedThreadPoolModule(new ModuleIdentifier(NAME,
40                 instanceName), null, null);
41     }
42
43     @Override
44     public Module createModule(final String instanceName,
45             final DependencyResolver dependencyResolver, final DynamicMBeanWithInstance old, final BundleContext bundleContext)
46             throws Exception {
47         int threadCount = (Integer) old.getAttribute("ThreadCount");
48         // is the instance compatible?
49         TestingFixedThreadPool oldInstance;
50         try {
51             // reconfigure existing instance
52             oldInstance = (TestingFixedThreadPool) old.getInstance();
53         } catch (ClassCastException e) {
54             // old instance will be closed, new needs to be created
55             oldInstance = null;
56         }
57         TestingFixedThreadPoolModule result = new TestingFixedThreadPoolModule(
58                 new ModuleIdentifier(NAME, instanceName), old.getInstance(),
59                 oldInstance);
60         result.setThreadCount(threadCount);
61         return result;
62     }
63
64     @Override
65     public boolean isModuleImplementingServiceInterface(
66             final Class<? extends AbstractServiceInterface> serviceInterface) {
67         return ifc.contains(serviceInterface);
68     }
69
70     @Override
71     public Set<Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory, final BundleContext bundleContext) {
72         return new HashSet<Module>();
73     }
74
75     @Override
76     public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
77         return ifc;
78     }
79 }