fcc86a8d87504e2be88b329eb9dececeb32abe1b
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / controller / config / yang / pcep / impl / PCEPDispatcherImplModuleTest.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.yang.pcep.impl;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15 import org.junit.Before;
16 import org.junit.Test;
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.pcep.spi.SimplePCEPExtensionProviderContextModuleFactory;
25 import org.opendaylight.controller.config.yang.pcep.spi.SimplePCEPExtensionProviderContextModuleMXBean;
26
27 public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
28
29     private static final String INSTANCE_NAME = "pcep-dispatcher-impl";
30     private static final String FACTORY_NAME = PCEPDispatcherImplModuleFactory.NAME;
31
32     private static final String THREADGROUP_FACTORY_NAME = NettyThreadgroupModuleFactory.NAME;
33     private static final String BOSS_TG_INSTANCE_NAME = "boss-group";
34     private static final String WORKER_TG_INSTANCE_NAME = "worker-group";
35
36     private static final String EXTENSION_INSTANCE_NAME = "pcep-extension-privider";
37     private static final String EXTENSION_FACTORYNAME = SimplePCEPExtensionProviderContextModuleFactory.NAME;
38
39     @Before
40     public void setUp() throws Exception {
41         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext, new PCEPDispatcherImplModuleFactory(), new PCEPSessionProposalFactoryImplModuleFactory(), new NettyThreadgroupModuleFactory(), new SimplePCEPExtensionProviderContextModuleFactory()));
42     }
43
44     @Test
45     public void testValidationExceptionMaxUnknownMessagesNotSet() throws Exception {
46         try {
47             createDispatcherInstance(null);
48             fail();
49         } catch (final ValidationException e) {
50             assertTrue(e.getMessage().contains("MaxUnknownMessages value is not set"));
51         }
52     }
53
54     @Test
55     public void testValidationExceptionMaxUnknownMessagesMinValue() throws Exception {
56         try {
57             createDispatcherInstance(0);
58             fail();
59         } catch (final ValidationException e) {
60             assertTrue(e.getMessage().contains("must be greater than 0"));
61         }
62     }
63
64     @Test
65     public void testCreateBean() throws Exception {
66         final CommitStatus status = createDispatcherInstance(5);
67         assertBeanCount(1, FACTORY_NAME);
68         assertStatus(status, 5, 0, 0);
69     }
70
71     @Test
72     public void testReusingOldInstance() throws Exception {
73         createDispatcherInstance(5);
74         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
75         assertBeanCount(1, FACTORY_NAME);
76         final CommitStatus status = transaction.commit();
77         assertBeanCount(1, FACTORY_NAME);
78         assertStatus(status, 0, 0, 5);
79     }
80
81     @Test
82     public void testReconfigure() throws Exception {
83         createDispatcherInstance(5);
84         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
85         assertBeanCount(1, FACTORY_NAME);
86         final PCEPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
87                 PCEPDispatcherImplModuleMXBean.class);
88         mxBean.setMaxUnknownMessages(10);
89         final CommitStatus status = transaction.commit();
90         assertBeanCount(1, FACTORY_NAME);
91         assertStatus(status, 0, 1, 4);
92     }
93
94     private CommitStatus createDispatcherInstance(final Integer maxUnknownMessages) throws Exception {
95         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
96         createDispatcherInstance(transaction, maxUnknownMessages);
97         return transaction.commit();
98     }
99
100     public static ObjectName createDispatcherInstance(final ConfigTransactionJMXClient transaction, final Integer maxUnknownMessages)
101             throws Exception {
102         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
103         final PCEPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, PCEPDispatcherImplModuleMXBean.class);
104         mxBean.setPcepSessionProposalFactory(PCEPSessionProposalFactoryImplModuleTest.createSessionInstance(transaction));
105         mxBean.setMaxUnknownMessages(maxUnknownMessages);
106         mxBean.setBossGroup(createThreadGroupInstance(transaction, 10, BOSS_TG_INSTANCE_NAME));
107         mxBean.setWorkerGroup(createThreadGroupInstance(transaction, 10, WORKER_TG_INSTANCE_NAME));
108         mxBean.setPcepExtensions(createExtensionsInstance(transaction));
109         return nameCreated;
110     }
111
112     private static ObjectName createExtensionsInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
113         final ObjectName nameCreated = transaction.createModule(EXTENSION_FACTORYNAME, EXTENSION_INSTANCE_NAME);
114         transaction.newMXBeanProxy(nameCreated, SimplePCEPExtensionProviderContextModuleMXBean.class);
115
116         return nameCreated;
117     }
118
119     private static ObjectName createThreadGroupInstance(final ConfigTransactionJMXClient transaction, final Integer threadCount,
120             final String instanceName) throws InstanceAlreadyExistsException {
121         final ObjectName nameCreated = transaction.createModule(THREADGROUP_FACTORY_NAME, instanceName);
122         final NettyThreadgroupModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NettyThreadgroupModuleMXBean.class);
123         mxBean.setThreadCount(threadCount);
124         return nameCreated;
125     }
126
127 }