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 static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.controller.config.api.jmx.ObjectNameUtil.withoutTransactionName;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.List;
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;
39 public class ServiceReferenceRegistryImplTest extends AbstractParallelAPSPTest {
44 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
46 new TestingFixedThreadPoolModuleFactory(),
47 new TestingParallelAPSPModuleFactory(),
48 new TestingScheduledThreadPoolModuleFactory()));
52 protected String getThreadPoolImplementationName() {
53 return TestingFixedThreadPoolModuleFactory.NAME;
57 public void test() throws Exception {
58 ConfigTransactionJMXClient transaction1 = configRegistryClient.createTransaction();
60 int fixedNrOfThreads = 20, scheduledNrOfThreads = 30;
62 ObjectName fixedTPTransactionON = transaction1.createModule(getThreadPoolImplementationName(), fixed1);
63 platformMBeanServer.setAttribute(fixedTPTransactionON, new Attribute("ThreadCount", fixedNrOfThreads));
65 ObjectName scheduledTPTransactionON = transaction1.createModule(
66 TestingScheduledThreadPoolModuleFactory.NAME, "scheduled1");
67 platformMBeanServer.setAttribute(scheduledTPTransactionON, new Attribute("ThreadCount",
68 scheduledNrOfThreads));
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();
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);
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"), (Map<String, String>) record.props);
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());
101 configRegistryClient.createTransaction().commit();
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);
110 ConfigTransactionJMXClient transaction4 = configRegistryClient.createTransaction();
111 Set<ObjectName> objectNames = transaction4.lookupConfigBeans();
112 for(ObjectName on: objectNames) {
113 transaction4.destroyModule(on);
115 transaction4.commit();
117 serviceMapping = configRegistryClient.getServiceMapping();
118 assertTrue(serviceMapping.isEmpty());
121 private void checkApspThreadCount(int fixedNrOfThreads) throws MBeanException, AttributeNotFoundException,
122 InstanceNotFoundException, ReflectionException {
123 ObjectName apspON = ObjectNameUtil.createReadOnlyModuleON(TestingParallelAPSPModuleFactory.NAME, apsp1);
124 assertEquals(fixedNrOfThreads, platformMBeanServer.getAttribute(apspON, "MaxNumberOfThreads"));