Move ServiceReferenceMXBean to config-api
[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 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.api.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;
24
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;
33 import java.util.Map;
34 import java.util.Set;
35
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;
39
40 public class ServiceReferenceRegistryImplTest extends AbstractParallelAPSPTest {
41
42
43     @Before
44     public void setUp() {
45         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(
46                 mockedContext,
47                 new TestingFixedThreadPoolModuleFactory(),
48                 new TestingParallelAPSPModuleFactory(),
49                 new TestingScheduledThreadPoolModuleFactory()));
50     }
51
52     @Override
53     protected String getThreadPoolImplementationName() {
54         return TestingFixedThreadPoolModuleFactory.NAME;
55     }
56
57     @Test
58     public void test() throws Exception {
59         ConfigTransactionJMXClient transaction1 = configRegistryClient.createTransaction();
60         // create fixed1
61         int fixedNrOfThreads = 20, scheduledNrOfThreads = 30;
62
63         ObjectName fixedTPTransactionON = transaction1.createModule(getThreadPoolImplementationName(), fixed1);
64         platformMBeanServer.setAttribute(fixedTPTransactionON, new Attribute("ThreadCount", fixedNrOfThreads));
65
66         ObjectName scheduledTPTransactionON = transaction1.createModule(
67                 TestingScheduledThreadPoolModuleFactory.NAME, "scheduled1");
68         platformMBeanServer.setAttribute(scheduledTPTransactionON, new Attribute("ThreadCount",
69                 scheduledNrOfThreads));
70
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();
77
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);
83         // check OSGi SR
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);
90
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());
100
101         // empty transaction
102         configRegistryClient.createTransaction().commit();
103
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);
109
110         // destroy all
111         ConfigTransactionJMXClient transaction4 = configRegistryClient.createTransaction();
112         Set<ObjectName> objectNames = transaction4.lookupConfigBeans();
113         for(ObjectName on: objectNames) {
114             transaction4.destroyModule(on);
115         }
116         transaction4.commit();
117
118         serviceMapping = configRegistryClient.getServiceMapping();
119         assertTrue(serviceMapping.isEmpty());
120     }
121
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"));
126     }
127 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.