2 * Copyright (c) 2013, 2017 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 {
43 super.initConfigTransactionManagerImpl(
44 new HardcodedModuleFactoriesResolver(mockedContext, new TestingFixedThreadPoolModuleFactory(),
45 new TestingParallelAPSPModuleFactory(), new TestingScheduledThreadPoolModuleFactory()));
49 protected String getThreadPoolImplementationName() {
50 return TestingFixedThreadPoolModuleFactory.NAME;
54 public void test() throws Exception {
55 ConfigTransactionJMXClient transaction1 = configRegistryClient.createTransaction();
57 int fixedNrOfThreads = 20;
58 int scheduledNrOfThreads = 30;
60 ObjectName fixedTPTransactionON = transaction1.createModule(getThreadPoolImplementationName(), fixed1);
61 platformMBeanServer.setAttribute(fixedTPTransactionON, new Attribute("ThreadCount", fixedNrOfThreads));
63 ObjectName scheduledTPTransactionON = transaction1.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
65 platformMBeanServer.setAttribute(scheduledTPTransactionON, new Attribute("ThreadCount", scheduledNrOfThreads));
67 String refName = "ref";
68 ObjectName serviceReference = transaction1.saveServiceReference(TestingThreadPoolServiceInterface.QNAME,
69 refName, fixedTPTransactionON);
70 // create apsp-parallel
71 createParallelAPSP(transaction1, serviceReference);
72 transaction1.commit();
74 // check fixed1 is used
75 ServiceReferenceMXBean serviceReferenceMXBean = JMX.newMXBeanProxy(platformMBeanServer,
76 withoutTransactionName(serviceReference), ServiceReferenceMXBean.class);
77 assertEquals(withoutTransactionName(fixedTPTransactionON), serviceReferenceMXBean.getCurrentImplementation());
78 checkApspThreadCount(fixedNrOfThreads);
80 List<RegistrationHolder> registrations =
81 ((RecordingBundleContextServiceRegistrationHandler) currentBundleContextServiceRegistrationHandler)
83 assertEquals(1, registrations.size());
84 RegistrationHolder record = registrations.get(0);
85 assertEquals(TestingThreadPoolIfc.class, record.clazz);
86 assertEquals(ImmutableMap.of("name", "ref"), record.props);
88 // switch reference to scheduled
89 ConfigTransactionJMXClient transaction2 = configRegistryClient.createTransaction();
90 transaction2.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
91 ObjectNameUtil.withTransactionName(scheduledTPTransactionON, transaction2.getTransactionName()));
92 transaction2.commit();
93 // check scheduled is used
94 checkApspThreadCount(scheduledNrOfThreads);
95 // check that dummy MXBean points to scheduled
96 assertEquals(withoutTransactionName(scheduledTPTransactionON),
97 serviceReferenceMXBean.getCurrentImplementation());
100 configRegistryClient.createTransaction().commit();
102 // get service mapping
103 Map<String, Map<String, ObjectName>> serviceMapping = configRegistryClient.getServiceMapping();
104 Map<String, Map<String, ObjectName>> expectedMapping = ImmutableMap.of(TestingThreadPoolServiceInterface.QNAME,
105 (Map<String, ObjectName>) ImmutableMap.of(refName, withoutTransactionName(scheduledTPTransactionON)));
106 assertEquals(expectedMapping, serviceMapping);
109 ConfigTransactionJMXClient transaction4 = configRegistryClient.createTransaction();
110 Set<ObjectName> objectNames = transaction4.lookupConfigBeans();
111 for (ObjectName on : objectNames) {
112 transaction4.destroyModule(on);
114 transaction4.commit();
116 serviceMapping = configRegistryClient.getServiceMapping();
117 assertTrue(serviceMapping.isEmpty());
120 private void checkApspThreadCount(final int fixedNrOfThreads)
121 throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
122 ObjectName apspON = ObjectNameUtil.createReadOnlyModuleON(TestingParallelAPSPModuleFactory.NAME, apsp1);
123 assertEquals(fixedNrOfThreads, platformMBeanServer.getAttribute(apspON, "MaxNumberOfThreads"));