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.factoriesresolver.HardcodedModuleFactoriesResolver;
15 import org.opendaylight.controller.config.manager.impl.jmx.ServiceReferenceMXBean;
16 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
17 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.test.AbstractParallelAPSPTest;
18 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
19 import org.opendaylight.controller.config.manager.testingservices.seviceinterface.TestingThreadPoolServiceInterface;
20 import org.opendaylight.controller.config.manager.testingservices.threadpool.TestingFixedThreadPoolModuleFactory;
21 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
23 import javax.management.Attribute;
24 import javax.management.AttributeNotFoundException;
25 import javax.management.InstanceNotFoundException;
26 import javax.management.JMX;
27 import javax.management.MBeanException;
28 import javax.management.ObjectName;
29 import javax.management.ReflectionException;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertTrue;
36 import static org.opendaylight.controller.config.api.jmx.ObjectNameUtil.withoutTransactionName;
38 public class ServiceReferenceRegistryImplTest extends AbstractParallelAPSPTest {
43 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
44 new TestingFixedThreadPoolModuleFactory(),
45 new TestingParallelAPSPModuleFactory(),
46 new TestingScheduledThreadPoolModuleFactory()));
50 protected String getThreadPoolImplementationName() {
51 return TestingFixedThreadPoolModuleFactory.NAME;
55 public void test() throws Exception {
56 ConfigTransactionJMXClient transaction1 = configRegistryClient.createTransaction();
58 int fixedNrOfThreads = 20, scheduledNrOfThreads = 30;
60 ObjectName fixedTPTransactionON = transaction1.createModule(getThreadPoolImplementationName(), fixed1);
61 platformMBeanServer.setAttribute(fixedTPTransactionON, new Attribute("ThreadCount", fixedNrOfThreads));
63 ObjectName scheduledTPTransactionON = transaction1.createModule(
64 TestingScheduledThreadPoolModuleFactory.NAME, "scheduled1");
65 platformMBeanServer.setAttribute(scheduledTPTransactionON, new Attribute("ThreadCount",
66 scheduledNrOfThreads));
68 String refName = "ref";
69 ObjectName serviceReference = transaction1.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
70 fixedTPTransactionON);
71 // create apsp-parallel
72 createParallelAPSP(transaction1, serviceReference);
73 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 // switch reference to scheduled
81 ConfigTransactionJMXClient transaction2 = configRegistryClient.createTransaction();
82 transaction2.saveServiceReference(TestingThreadPoolServiceInterface.QNAME, refName,
83 ObjectNameUtil.withTransactionName(scheduledTPTransactionON, transaction2.getTransactionName()));
84 transaction2.commit();
85 // check scheduled is used
86 checkApspThreadCount(scheduledNrOfThreads);
87 // check that dummy MXBean points to scheduled
88 assertEquals(withoutTransactionName(scheduledTPTransactionON), serviceReferenceMXBean.getCurrentImplementation());
91 configRegistryClient.createTransaction().commit();
93 // get service mapping
94 Map<String,Map<String,ObjectName>> serviceMapping = configRegistryClient.getServiceMapping();
95 Map<String,Map<String,ObjectName>> expectedMapping = ImmutableMap.of(TestingThreadPoolServiceInterface.QNAME,
96 (Map<String, ObjectName>)ImmutableMap.of(refName, withoutTransactionName(scheduledTPTransactionON)));
97 assertEquals(expectedMapping, serviceMapping);
100 ConfigTransactionJMXClient transaction4 = configRegistryClient.createTransaction();
101 Set<ObjectName> objectNames = transaction4.lookupConfigBeans();
102 for(ObjectName on: objectNames) {
103 transaction4.destroyModule(on);
105 transaction4.commit();
107 serviceMapping = configRegistryClient.getServiceMapping();
108 assertTrue(serviceMapping.isEmpty());
111 private void checkApspThreadCount(int fixedNrOfThreads) throws MBeanException, AttributeNotFoundException,
112 InstanceNotFoundException, ReflectionException {
113 ObjectName apspON = ObjectNameUtil.createReadOnlyModuleON(TestingParallelAPSPModuleFactory.NAME, apsp1);
114 assertEquals(fixedNrOfThreads, platformMBeanServer.getAttribute(apspON, "MaxNumberOfThreads"));