Fix Eclipse warnings in config-manager
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / impl / ServiceReferenceRegistryImplTest.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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.controller.config.api.jmx.ObjectNameUtil.withoutTransactionName;
13
14 import com.google.common.collect.ImmutableMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import javax.management.Attribute;
19 import javax.management.AttributeNotFoundException;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.JMX;
22 import javax.management.MBeanException;
23 import javax.management.ObjectName;
24 import javax.management.ReflectionException;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
28 import org.opendaylight.controller.config.api.jmx.ServiceReferenceMXBean;
29 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest.RecordingBundleContextServiceRegistrationHandler.RegistrationHolder;
30 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
31 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
32 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.test.AbstractParallelAPSPTest;
33 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
34 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
35 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
36 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
37 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
38
39 public class ServiceReferenceRegistryImplTest extends AbstractParallelAPSPTest {
40
41
42     @Before
43     public void setUp() {
44         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
45                 mockedContext,
46                 new TestingFixedThreadPoolModuleFactory(),
47                 new TestingParallelAPSPModuleFactory(),
48                 new TestingScheduledThreadPoolModuleFactory()));
49     }
50
51     @Override
52     protected String getThreadPoolImplementationName() {
53         return TestingFixedThreadPoolModuleFactory.NAME;
54     }
55
56     @Test
57     public void test() throws Exception {
58         ConfigTransactionJMXClient transaction1 = configRegistryClient.createTransaction();
59         // create fixed1
60         int fixedNrOfThreads = 20, scheduledNrOfThreads = 30;
61
62         ObjectName fixedTPTransactionON = transaction1.createModule(getThreadPoolImplementationName(), fixed1);
63         platformMBeanServer.setAttribute(fixedTPTransactionON, new Attribute("ThreadCount", fixedNrOfThreads));
64
65         ObjectName scheduledTPTransactionON = transaction1.createModule(
66                 TestingScheduledThreadPoolModuleFactory.NAME, "scheduled1");
67         platformMBeanServer.setAttribute(scheduledTPTransactionON, new Attribute("ThreadCount",
68                 scheduledNrOfThreads));
69
70         String refName = "ref";
71         ObjectName serviceReference = transaction1.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
72                 fixedTPTransactionON);
73         // create apsp-parallel
74         createParallelAPSP(transaction1, serviceReference);
75         transaction1.commit();
76
77         // check fixed1 is used
78         ServiceReferenceMXBean serviceReferenceMXBean = JMX.newMXBeanProxy(platformMBeanServer,
79                 withoutTransactionName(serviceReference), ServiceReferenceMXBean.class);
80         assertEquals(withoutTransactionName(fixedTPTransactionON), serviceReferenceMXBean.getCurrentImplementation());
81         checkApspThreadCount(fixedNrOfThreads);
82         // check OSGi SR
83         List<RegistrationHolder> registrations =
84                 ((RecordingBundleContextServiceRegistrationHandler) currentBundleContextServiceRegistrationHandler).getRegistrations();
85         assertEquals(1, registrations.size());
86         RegistrationHolder record = registrations.get(0);
87         assertEquals(TestingThreadPoolIfc.class, record.clazz);
88         assertEquals(ImmutableMap.of("name","ref"), record.props);
89
90         // switch reference to scheduled
91         ConfigTransactionJMXClient transaction2 = configRegistryClient.createTransaction();
92         transaction2.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
93                 ObjectNameUtil.withTransactionName(scheduledTPTransactionON, transaction2.getTransactionName()));
94         transaction2.commit();
95         // check scheduled is used
96         checkApspThreadCount(scheduledNrOfThreads);
97         // check that dummy MXBean points to scheduled
98         assertEquals(withoutTransactionName(scheduledTPTransactionON), serviceReferenceMXBean.getCurrentImplementation());
99
100         // empty transaction
101         configRegistryClient.createTransaction().commit();
102
103         // get service mapping
104         Map<String,Map<String,ObjectName>> serviceMapping = configRegistryClient.getServiceMapping();
105         Map<String,Map<String,ObjectName>> expectedMapping = ImmutableMap.of(TestingThreadPoolServiceInterface.QNAME,
106                 (Map<String, ObjectName>)ImmutableMap.of(refName, withoutTransactionName(scheduledTPTransactionON)));
107         assertEquals(expectedMapping, serviceMapping);
108
109         // destroy all
110         ConfigTransactionJMXClient transaction4 = configRegistryClient.createTransaction();
111         Set<ObjectName> objectNames = transaction4.lookupConfigBeans();
112         for(ObjectName on: objectNames) {
113             transaction4.destroyModule(on);
114         }
115         transaction4.commit();
116
117         serviceMapping = configRegistryClient.getServiceMapping();
118         assertTrue(serviceMapping.isEmpty());
119     }
120
121     private void checkApspThreadCount(final int fixedNrOfThreads) throws MBeanException, AttributeNotFoundException,
122             InstanceNotFoundException, ReflectionException {
123         ObjectName apspON = ObjectNameUtil.createReadOnlyModuleON(TestingParallelAPSPModuleFactory.NAME, apsp1);
124         assertEquals(fixedNrOfThreads, platformMBeanServer.getAttribute(apspON, "MaxNumberOfThreads"));
125     }
126 }