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