CDS: Add stress test RPC to the cars model
[controller.git] / opendaylight / config / netconf-config-dispatcher / src / test / java / org / opendaylight / controller / config / yang / config / netconf / client / dispatcher / NetconfClientDispatcherModuleTest.java
1 package org.opendaylight.controller.config.yang.config.netconf.client.dispatcher;
2
3 import javax.management.InstanceAlreadyExistsException;
4 import javax.management.InstanceNotFoundException;
5 import javax.management.ObjectName;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.opendaylight.controller.config.api.ConflictingVersionException;
9 import org.opendaylight.controller.config.api.ValidationException;
10 import org.opendaylight.controller.config.api.jmx.CommitStatus;
11 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
12 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
13 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
14 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleFactory;
15 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleMXBean;
16 import org.opendaylight.controller.config.yang.netty.timer.HashedWheelTimerModuleFactory;
17
18 public class NetconfClientDispatcherModuleTest extends AbstractConfigTest{
19
20     private NetconfClientDispatcherModuleFactory factory;
21     private final String instanceName = "dispatch";
22
23     @Before
24     public void setUp() {
25         factory = new NetconfClientDispatcherModuleFactory();
26         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,factory,
27                 new NettyThreadgroupModuleFactory(),
28                 new HashedWheelTimerModuleFactory()));
29     }
30
31     @Test
32     public void testCreateBean() throws InstanceAlreadyExistsException, ValidationException, ConflictingVersionException {
33         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
34
35         createInstance(transaction, instanceName, "timer", "thGroup");
36         createInstance(transaction, instanceName + 2, "timer2", "thGroup2");
37         transaction.validateConfig();
38         CommitStatus status = transaction.commit();
39
40         assertBeanCount(2, factory.getImplementationName());
41         assertStatus(status, 2 + 4, 0, 0);
42     }
43
44     @Test
45     public void testReusingOldInstance() throws InstanceAlreadyExistsException, ConflictingVersionException, ValidationException {
46
47         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
48         createInstance(transaction, instanceName, "timer", "thGroup");
49
50         transaction.commit();
51
52         transaction = configRegistryClient.createTransaction();
53         assertBeanCount(1, factory.getImplementationName());
54         CommitStatus status = transaction.commit();
55
56         assertBeanCount(1, factory.getImplementationName());
57         assertStatus(status, 0, 0, 3);
58     }
59
60     @Test
61     public void testReconfigure() throws InstanceAlreadyExistsException, ConflictingVersionException,
62             ValidationException, InstanceNotFoundException {
63
64         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
65         createInstance(transaction, instanceName, "timer", "thGroup");
66
67         transaction.commit();
68
69         transaction = configRegistryClient.createTransaction();
70         assertBeanCount(1, factory.getImplementationName());
71         NetconfClientDispatcherModuleMXBean mxBean = transaction.newMBeanProxy(
72                 transaction.lookupConfigBean(NetconfClientDispatcherModuleFactory.NAME, instanceName),
73                 NetconfClientDispatcherModuleMXBean.class);
74         mxBean.setBossThreadGroup(getThreadGroup(transaction, "group2"));
75         CommitStatus status = transaction.commit();
76
77         assertBeanCount(1, factory.getImplementationName());
78         assertStatus(status, 1, 1, 2);
79     }
80
81     private ObjectName createInstance(ConfigTransactionJMXClient transaction, String instanceName, String timerName, String threadGroupName)
82             throws InstanceAlreadyExistsException {
83         ObjectName nameCreated = transaction.createModule(factory.getImplementationName(), instanceName);
84         NetconfClientDispatcherModuleMXBean mxBean = transaction.newMBeanProxy(nameCreated, NetconfClientDispatcherModuleMXBean.class);
85         ObjectName thGroup = getThreadGroup(transaction, threadGroupName);
86         mxBean.setBossThreadGroup(thGroup);
87         mxBean.setWorkerThreadGroup(thGroup);
88         mxBean.setTimer(getTimer(transaction, timerName));
89         return nameCreated;
90     }
91
92     private ObjectName getTimer(ConfigTransactionJMXClient transaction, String name) throws InstanceAlreadyExistsException {
93         return transaction.createModule(HashedWheelTimerModuleFactory.NAME, name);
94     }
95
96     private ObjectName getThreadGroup(ConfigTransactionJMXClient transaction, String name) throws InstanceAlreadyExistsException {
97         ObjectName nameCreated = transaction.createModule(NettyThreadgroupModuleFactory.NAME, name);
98         NettyThreadgroupModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NettyThreadgroupModuleMXBean.class);
99         mxBean.setThreadCount(1);
100         return nameCreated;
101     }
102 }