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