2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.manager.impl;
10 import com.google.common.collect.ImmutableMap;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
14 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest.RecordingBundleContextServiceRegistrationHandler.RegistrationHolder;
15 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
16 import org.opendaylight.controller.config.manager.impl.jmx.ServiceReferenceMXBean;
17 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
18 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.test.AbstractParallelAPSPTest;
19 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
20 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
21 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
22 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingThreadPoolIfc;
23 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
25 import javax.management.Attribute;
26 import javax.management.AttributeNotFoundException;
27 import javax.management.InstanceNotFoundException;
28 import javax.management.JMX;
29 import javax.management.MBeanException;
30 import javax.management.ObjectName;
31 import javax.management.ReflectionException;
32 import java.util.List;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
38 import static org.opendaylight.controller.config.api.jmx.ObjectNameUtil.withoutTransactionName;
40 public class ServiceReferenceRegistryImplTest extends AbstractParallelAPSPTest {
45 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
47 new TestingFixedThreadPoolModuleFactory(),
48 new TestingParallelAPSPModuleFactory(),
49 new TestingScheduledThreadPoolModuleFactory()));
53 protected String getThreadPoolImplementationName() {
54 return TestingFixedThreadPoolModuleFactory.NAME;
58 public void test() throws Exception {
59 ConfigTransactionJMXClient transaction1 = configRegistryClient.createTransaction();
61 int fixedNrOfThreads = 20, scheduledNrOfThreads = 30;
63 ObjectName fixedTPTransactionON = transaction1.createModule(getThreadPoolImplementationName(), fixed1);
64 platformMBeanServer.setAttribute(fixedTPTransactionON, new Attribute("ThreadCount", fixedNrOfThreads));
66 ObjectName scheduledTPTransactionON = transaction1.createModule(
67 TestingScheduledThreadPoolModuleFactory.NAME, "scheduled1");
68 platformMBeanServer.setAttribute(scheduledTPTransactionON, new Attribute("ThreadCount",
69 scheduledNrOfThreads));
71 String refName = "ref";
72 ObjectName serviceReference = transaction1.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
73 fixedTPTransactionON);
74 // create apsp-parallel
75 createParallelAPSP(transaction1, serviceReference);
76 transaction1.commit();
78 // check fixed1 is used
79 ServiceReferenceMXBean serviceReferenceMXBean = JMX.newMXBeanProxy(platformMBeanServer,
80 withoutTransactionName(serviceReference), ServiceReferenceMXBean.class);
81 assertEquals(withoutTransactionName(fixedTPTransactionON), serviceReferenceMXBean.getCurrentImplementation());
82 checkApspThreadCount(fixedNrOfThreads);
84 List<RegistrationHolder> registrations =
85 ((RecordingBundleContextServiceRegistrationHandler) currentBundleContextServiceRegistrationHandler).getRegistrations();
86 assertEquals(1, registrations.size());
87 RegistrationHolder record = registrations.get(0);
88 assertEquals(TestingThreadPoolIfc.class, record.clazz);
89 assertEquals(ImmutableMap.of("name","ref"), (Map<String, String>) record.props);
91 // switch reference to scheduled
92 ConfigTransactionJMXClient transaction2 = configRegistryClient.createTransaction();
93 transaction2.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
94 ObjectNameUtil.withTransactionName(scheduledTPTransactionON, transaction2.getTransactionName()));
95 transaction2.commit();
96 // check scheduled is used
97 checkApspThreadCount(scheduledNrOfThreads);
98 // check that dummy MXBean points to scheduled
99 assertEquals(withoutTransactionName(scheduledTPTransactionON), serviceReferenceMXBean.getCurrentImplementation());
102 configRegistryClient.createTransaction().commit();
104 // get service mapping
105 Map<String,Map<String,ObjectName>> serviceMapping = configRegistryClient.getServiceMapping();
106 Map<String,Map<String,ObjectName>> expectedMapping = ImmutableMap.of(TestingThreadPoolServiceInterface.QNAME,
107 (Map<String, ObjectName>)ImmutableMap.of(refName, withoutTransactionName(scheduledTPTransactionON)));
108 assertEquals(expectedMapping, serviceMapping);
111 ConfigTransactionJMXClient transaction4 = configRegistryClient.createTransaction();
112 Set<ObjectName> objectNames = transaction4.lookupConfigBeans();
113 for(ObjectName on: objectNames) {
114 transaction4.destroyModule(on);
116 transaction4.commit();
118 serviceMapping = configRegistryClient.getServiceMapping();
119 assertTrue(serviceMapping.isEmpty());
122 private void checkApspThreadCount(int fixedNrOfThreads) throws MBeanException, AttributeNotFoundException,
123 InstanceNotFoundException, ReflectionException {
124 ObjectName apspON = ObjectNameUtil.createReadOnlyModuleON(TestingParallelAPSPModuleFactory.NAME, apsp1);
125 assertEquals(fixedNrOfThreads, platformMBeanServer.getAttribute(apspON, "MaxNumberOfThreads"));