Merge "Make neutron a simple osgi app"
[controller.git] / opendaylight / config / config-manager / src / test / java / org / opendaylight / controller / config / manager / testingservices / scheduledthreadpool / test / TwoInterfacesExportTest.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.testingservices.scheduledthreadpool.test;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16
17 import javax.annotation.Nullable;
18 import javax.management.InstanceAlreadyExistsException;
19 import javax.management.InstanceNotFoundException;
20 import javax.management.IntrospectionException;
21 import javax.management.ObjectName;
22 import javax.management.ReflectionException;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
26 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPConfigMXBean;
27 import org.opendaylight.controller.config.manager.testingservices.parallelapsp.TestingParallelAPSPModuleFactory;
28 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolImpl;
29 import org.opendaylight.controller.config.manager.testingservices.scheduledthreadpool.TestingScheduledThreadPoolModuleFactory;
30 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
31
32 public class TwoInterfacesExportTest extends AbstractScheduledTest {
33
34     private void assertExists(final String moduleName, final String instanceName)
35             throws Exception {
36         assertExists(null, moduleName, instanceName);
37     }
38
39     private void assertExists(@Nullable final ConfigTransactionJMXClient transaction,
40             final String moduleName, final String instanceName)
41             throws InstanceNotFoundException, IntrospectionException, ReflectionException {
42         if (transaction != null) {
43             transaction.lookupConfigBean(moduleName, instanceName);
44             // make a dummy call
45             platformMBeanServer.getMBeanInfo(ObjectNameUtil.createTransactionModuleON(
46                     transaction.getTransactionName(), moduleName, instanceName));
47         } else {
48             configRegistryClient.lookupConfigBean(moduleName, instanceName);
49             // make a dummy call
50             platformMBeanServer.getMBeanInfo(ObjectNameUtil.createReadOnlyModuleON(moduleName,
51                     instanceName));
52         }
53     }
54
55     private void assertNotExists(final String moduleName, final String instanceName) {
56         assertNotExists(null, moduleName, instanceName);
57     }
58
59     private void assertNotExists(
60             @Nullable final ConfigTransactionJMXClient transaction,
61             final String moduleName, final String instanceName) {
62
63         if (transaction != null) {
64             try {
65                 transaction.lookupConfigBean(moduleName, instanceName);
66                 fail();
67             } catch (InstanceNotFoundException e) {
68
69             }
70         } else {
71             try {
72                 configRegistryClient.lookupConfigBean(moduleName, instanceName);
73                 fail();
74             } catch (InstanceNotFoundException e) {
75
76             }
77         }
78     }
79
80     @Test
81     public void twoInterfaceNamesAfterCreatingConfigBean() throws Exception {
82         ConfigTransactionJMXClient transaction = configRegistryClient
83                 .createTransaction();
84
85         // create using TestingThreadPoolIfc:
86         ObjectName scheduled1name = transaction.createModule(
87                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
88
89         ObjectName retrievedName = transaction.lookupConfigBean(
90                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
91         assertEquals(scheduled1name, retrievedName);
92
93         // getExistingConfigBean should resolve moduleName
94         String moduleName = TestingScheduledThreadPoolModuleFactory.NAME;
95         retrievedName = transaction.lookupConfigBean(moduleName, scheduled1);
96         ObjectName expected = ObjectNameUtil.createTransactionModuleON(
97                 transaction.getTransactionName(), moduleName, scheduled1);
98         assertEquals(expected, retrievedName);
99
100         // commit
101         transaction.commit();
102         assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
103         assertFalse(TestingScheduledThreadPoolImpl.allExecutors.get(0)
104                 .isTerminated());
105         assertEquals(0,
106                 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
107
108         assertExists(moduleName, scheduled1);
109
110         // destroy using ThreadPool ifc
111         transaction = configRegistryClient.createTransaction();
112         transaction.destroyModule(ObjectNameUtil.createTransactionModuleON(
113                 transaction.getTransactionName(), moduleName, scheduled1));
114         transaction.commit();
115         assertEquals(1, TestingScheduledThreadPoolImpl.allExecutors.size());
116         assertTrue(TestingScheduledThreadPoolImpl.allExecutors.get(0)
117                 .isTerminated());
118         assertEquals(1,
119                 TestingScheduledThreadPoolImpl.getNumberOfCloseMethodCalls());
120
121         // should not be in platform:
122
123         assertNotExists(moduleName, scheduled1);
124
125         transaction = configRegistryClient.createTransaction();
126         // should not be in transaction
127         assertNotExists(transaction, moduleName, scheduled1);
128     }
129
130     @Test
131     public void tryToRegisterThreadPoolWithSameName()
132             throws InstanceAlreadyExistsException {
133         ConfigTransactionJMXClient transaction = configRegistryClient
134                 .createTransaction();
135
136         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
137                 scheduled1);
138         try {
139             transaction.createModule(
140                     TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
141             fail();
142         } catch (InstanceAlreadyExistsException e) {
143             assertThat(
144                     e.getMessage(),
145                     containsString("There is an instance registered with name ModuleIdentifier{factoryName='scheduled', instanceName='scheduled1'}"));
146         }
147     }
148
149     // --
150     @Test
151     public void testRegisteringAllIfcNames() throws Exception {
152         ConfigTransactionJMXClient transaction = configRegistryClient
153                 .createTransaction();
154         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
155                 scheduled1);
156         transaction.commit();
157         assertExists(TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
158         // another transaction
159         transaction = configRegistryClient.createTransaction();
160         assertExists(transaction, TestingScheduledThreadPoolModuleFactory.NAME,
161                 scheduled1);
162     }
163
164     @Test
165     public void testWithAPSP_useScheduledNames()
166             throws InstanceAlreadyExistsException, ValidationException {
167         ConfigTransactionJMXClient transaction = configRegistryClient
168                 .createTransaction();
169         ObjectName scheduledName = transaction.createModule(
170                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1);
171
172         ObjectName apspName = transaction.createModule(
173                 TestingParallelAPSPModuleFactory.NAME, "apsp1");
174         TestingParallelAPSPConfigMXBean apspProxy = transaction.newMXBeanProxy(
175                 apspName, TestingParallelAPSPConfigMXBean.class);
176         apspProxy.setThreadPool(scheduledName);
177         apspProxy.setSomeParam("someParam");
178         transaction.validateConfig();
179
180     }
181
182     @Test
183     public void testWithAPSP_useIfcNameMismatch() throws Exception {
184         ConfigTransactionJMXClient transaction = configRegistryClient
185                 .createTransaction();
186         transaction.createModule(TestingScheduledThreadPoolModuleFactory.NAME,
187                 scheduled1);
188
189         ObjectName apspName = transaction.createModule(
190                 TestingParallelAPSPModuleFactory.NAME, "apsp1");
191         TestingParallelAPSPConfigMXBean apspProxy = transaction.newMXBeanProxy(
192                 apspName, TestingParallelAPSPConfigMXBean.class);
193         apspProxy.setThreadPool(ObjectNameUtil.createReadOnlyModuleON(
194                 TestingScheduledThreadPoolModuleFactory.NAME, scheduled1));
195         apspProxy.setSomeParam("someParam");
196         transaction.validateConfig();
197         transaction.commit();
198
199     }
200
201 }