Add blueprint XML for bgp-parser-spi
[bgpcep.git] / bgp / bmp-impl / src / test / java / org / opendaylight / controller / config / yang / bmp / impl / BmpDispatcherImplModuleTest.java
1 /*
2  * Copyright (c) 2015 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.bmp.impl;
9
10 import static org.mockito.Mockito.mock;
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.EventExecutor;
14 import javax.management.InstanceAlreadyExistsException;
15 import javax.management.ObjectName;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.config.api.jmx.CommitStatus;
19 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
20 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
21 import org.opendaylight.controller.config.yang.bmp.spi.SimpleBmpExtensionProviderContextModuleFactory;
22 import org.opendaylight.controller.config.yang.bmp.spi.SimpleBmpExtensionProviderContextModuleMXBean;
23 import org.opendaylight.controller.config.yang.netty.eventexecutor.GlobalEventExecutorModuleFactory;
24 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleFactory;
25 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleMXBean;
26
27 public class BmpDispatcherImplModuleTest extends AbstractBmpModuleTest {
28     private static final String INSTANCE_NAME = "bmp-message-fct";
29     private static final String FACTORY_NAME = BmpDispatcherImplModuleFactory.NAME;
30
31     private static final String BMP_EXTENSION_INSTANCE_NAME = "bmp-extension-impl";
32     private static final String BOSS_TG_INSTANCE_NAME = "boss-threadgroup-impl";
33     private static final String WORKER_TG_INSTANCE_NAME = "worker-threadgroup-impl";
34
35     @Override
36     @Before
37     public void setUp() throws Exception {
38         super.setUp();
39         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext,
40             new BmpDispatcherImplModuleFactory(),
41             new NettyThreadgroupModuleFactory(),
42             new GlobalEventExecutorModuleFactory(),
43             new SimpleBmpExtensionProviderContextModuleFactory()));
44
45         setupMockService(EventLoopGroup.class, mock(EventLoopGroup.class));
46         setupMockService(Timer.class, mock(Timer.class));
47         setupMockService(EventExecutor.class, mock(EventExecutor.class));
48     }
49
50     @Test
51     public void testCreateBean() throws Exception {
52         final CommitStatus status = createInstance();
53         assertBeanCount(1, FACTORY_NAME);
54         assertStatus(status, 4, 0, 0);
55     }
56
57     @Test
58     public void testReusingOldInstance() throws Exception {
59         createInstance();
60         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
61         assertBeanCount(1, FACTORY_NAME);
62         final CommitStatus status = transaction.commit();
63         assertBeanCount(1, FACTORY_NAME);
64         assertStatus(status, 0, 0, 4);
65     }
66
67     private CommitStatus createInstance() throws Exception {
68         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
69         createInstance(transaction);
70         return transaction.commit();
71     }
72
73     public static ObjectName createInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
74         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
75         final BmpDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BmpDispatcherImplModuleMXBean.class);
76         mxBean.setBossGroup(createThreadgroupInstance(transaction, BOSS_TG_INSTANCE_NAME, 10));
77         mxBean.setWorkerGroup(createThreadgroupInstance(transaction, WORKER_TG_INSTANCE_NAME, 10));
78         mxBean.setBmpExtensions(createBmpExtensionsInstance(transaction));
79         return nameCreated;
80     }
81
82     private static ObjectName createThreadgroupInstance(final ConfigTransactionJMXClient transaction, final String instanceName,
83             final Integer threadCount) throws InstanceAlreadyExistsException {
84         final ObjectName nameCreated = transaction.createModule(NettyThreadgroupModuleFactory.NAME, instanceName);
85         final NettyThreadgroupModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NettyThreadgroupModuleMXBean.class);
86         mxBean.setThreadCount(threadCount);
87         return nameCreated;
88     }
89
90     private static ObjectName createBmpExtensionsInstance(final ConfigTransactionJMXClient transaction)
91             throws InstanceAlreadyExistsException {
92         final ObjectName nameCreated = transaction.createModule(SimpleBmpExtensionProviderContextModuleFactory.NAME, BMP_EXTENSION_INSTANCE_NAME);
93         transaction.newMXBeanProxy(nameCreated, SimpleBmpExtensionProviderContextModuleMXBean.class);
94         return nameCreated;
95     }
96 }