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