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