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