52507d69b6fee64157f449e0e0b2316f92c7856c
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPDispatcherImplModuleTest.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.bgp.rib.impl;
9
10 import javax.management.InstanceAlreadyExistsException;
11 import javax.management.ObjectName;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.api.jmx.CommitStatus;
16 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
17 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
18 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
19 import org.opendaylight.controller.config.yang.bgp.parser.spi.SimpleBGPExtensionProviderContextModuleFactory;
20 import org.opendaylight.controller.config.yang.bgp.parser.spi.SimpleBGPExtensionProviderContextModuleMXBean;
21 import org.opendaylight.controller.config.yang.bgp.rib.spi.RIBExtensionsImplModuleFactory;
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 import org.opendaylight.controller.config.yang.netty.timer.HashedWheelTimerModuleMXBean;
26
27 public class BGPDispatcherImplModuleTest extends AbstractConfigTest {
28
29     private static final String INSTANCE_NAME = "bgp-message-fct";
30     private static final String FACTORY_NAME = BGPDispatcherImplModuleFactory.NAME;
31
32     private static final String TIMER_INSTANCE_NAME = "timer-impl";
33     private static final String BGP_EXTENSION_INSTANCE_NAME = "bgp-extension-impl";
34     private static final String BOSS_TG_INSTANCE_NAME = "boss-threadgroup-impl";
35     private static final String WORKER_TG_INSTANCE_NAME = "worker-threadgroup-impl";
36
37     @Before
38     public void setUp() throws Exception {
39         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, new BGPDispatcherImplModuleFactory(), new NettyThreadgroupModuleFactory(), new RIBExtensionsImplModuleFactory(), new SimpleBGPExtensionProviderContextModuleFactory(), new HashedWheelTimerModuleFactory()));
40     }
41
42     @Test
43     public void testCreateBean() throws Exception {
44         CommitStatus status = createInstance();
45         assertBeanCount(1, FACTORY_NAME);
46         assertStatus(status, 5, 0, 0);
47     }
48
49     @Test
50     public void testReusingOldInstance() throws Exception {
51         createInstance();
52         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
53         assertBeanCount(1, FACTORY_NAME);
54         CommitStatus status = transaction.commit();
55         assertBeanCount(1, FACTORY_NAME);
56         assertStatus(status, 0, 0, 5);
57     }
58
59     private CommitStatus createInstance() throws Exception {
60         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
61         createInstance(transaction);
62         return transaction.commit();
63     }
64
65     public static ObjectName createInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
66         ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
67         BGPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPDispatcherImplModuleMXBean.class);
68         mxBean.setBossGroup(createThreadgroupInstance(transaction, BOSS_TG_INSTANCE_NAME, 10));
69         mxBean.setWorkerGroup(createThreadgroupInstance(transaction, WORKER_TG_INSTANCE_NAME, 10));
70         mxBean.setBgpExtensions(createBgpExtensionsInstance(transaction));
71         mxBean.setTimer(createTimerInstance(transaction));
72         return nameCreated;
73     }
74
75     private static ObjectName createThreadgroupInstance(final ConfigTransactionJMXClient transaction, final String instanceName,
76             final Integer threadCount) throws InstanceAlreadyExistsException {
77         ObjectName nameCreated = transaction.createModule(NettyThreadgroupModuleFactory.NAME, instanceName);
78         NettyThreadgroupModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NettyThreadgroupModuleMXBean.class);
79         mxBean.setThreadCount(threadCount);
80         return nameCreated;
81     }
82
83     private static ObjectName createTimerInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
84         ObjectName nameCreated = transaction.createModule(HashedWheelTimerModuleFactory.NAME, TIMER_INSTANCE_NAME);
85         transaction.newMXBeanProxy(nameCreated, HashedWheelTimerModuleMXBean.class);
86         return nameCreated;
87
88     }
89
90     private static ObjectName createBgpExtensionsInstance(final ConfigTransactionJMXClient transaction)
91             throws InstanceAlreadyExistsException {
92         ObjectName nameCreated = transaction.createModule(SimpleBGPExtensionProviderContextModuleFactory.NAME, BGP_EXTENSION_INSTANCE_NAME);
93         transaction.newMXBeanProxy(nameCreated, SimpleBGPExtensionProviderContextModuleMXBean.class);
94         return nameCreated;
95     }
96 }