Removed checkstyle warnings.
[bgpcep.git] / pcep / topology-provider-config / src / test / java / org / opendaylight / controller / config / yang / pcep / topology / provider / PCEPTopologyProviderModuleTest.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 package org.opendaylight.controller.config.yang.pcep.topology.provider;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12 import static org.opendaylight.controller.config.yang.pcep.impl.PCEPDispatcherImplModuleTest.createDispatcherInstance;
13
14 import java.util.List;
15 import java.util.Random;
16
17 import javax.management.ObjectName;
18
19 import org.junit.Test;
20 import org.opendaylight.controller.config.api.ValidationException;
21 import org.opendaylight.controller.config.api.jmx.CommitStatus;
22 import org.opendaylight.controller.config.spi.ModuleFactory;
23 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
24 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleFactory;
25 import org.opendaylight.controller.config.yang.pcep.impl.PCEPDispatcherImplModuleFactory;
26 import org.opendaylight.controller.config.yang.pcep.impl.PCEPSessionProposalFactoryImplModuleFactory;
27 import org.opendaylight.controller.config.yang.pcep.spi.SimplePCEPExtensionProviderContextModuleFactory;
28 import org.opendaylight.controller.config.yang.pcep.stateful02.cfg.Stateful02PCEPSessionProposalFactoryModuleFactory;
29 import org.opendaylight.controller.config.yang.programming.impl.AbstractInstructionSchedulerTest;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
33
34 public class PCEPTopologyProviderModuleTest extends AbstractInstructionSchedulerTest {
35
36     private static final String FACTORY_NAME = PCEPTopologyProviderModuleFactory.NAME;
37     private static final String INSTANCE_NAME = "pcep-topology-provider-instance";
38     private static final String STATEFUL02_TOPOLOGY_INSTANCE_NAME = "pcep-topology-stateful02-instance";
39
40     private static final String LISTEN_ADDRESS = "0.0.0.0";
41     private static final PortNumber LISTEN_PORT = new PortNumber(4189);
42     private static final TopologyId TOPOLOGY_ID = new TopologyId("pcep-topology");
43
44     @Test
45     public void testValidationExceptionListenAddressNotSet() throws Exception {
46         try {
47             createInstance(null, LISTEN_PORT, TOPOLOGY_ID);
48             fail();
49         } catch (ValidationException e) {
50             assertTrue(e.getMessage().contains("ListenAddress is not set"));
51         }
52     }
53
54     @Test
55     public void testValidationExceptionListenPortNotSet() throws Exception {
56         try {
57             createInstance(LISTEN_ADDRESS, null, TOPOLOGY_ID);
58             fail();
59         } catch (ValidationException e) {
60             assertTrue(e.getMessage().contains("ListenPort is not set"));
61         }
62     }
63
64     @Test
65     public void testValidationExceptionTopologyIdNotSet() throws Exception {
66         try {
67             createInstance(LISTEN_ADDRESS, LISTEN_PORT, null);
68             fail();
69         } catch (ValidationException e) {
70             assertTrue(e.getMessage().contains("TopologyId is not set"));
71         }
72     }
73
74     @Test
75     public void testCreateBean() throws Exception {
76         CommitStatus status = createInstance();
77         assertBeanCount(1, FACTORY_NAME);
78         assertStatus(status, 16, 0, 0);
79     }
80
81     @Test
82     public void testReusingOldInstance() throws Exception {
83         createInstance();
84         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
85         assertBeanCount(1, FACTORY_NAME);
86         CommitStatus status = transaction.commit();
87         assertBeanCount(1, FACTORY_NAME);
88         assertStatus(status, 0, 0, 16);
89     }
90
91     @Test
92     public void testReconfigure() throws Exception {
93         createInstance();
94         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
95         assertBeanCount(1, FACTORY_NAME);
96         final PCEPTopologyProviderModuleMXBean mxBean = transaction.newMXBeanProxy(
97                 transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME), PCEPTopologyProviderModuleMXBean.class);
98         mxBean.setTopologyId(new TopologyId("new-pcep-topology"));
99         final CommitStatus status = transaction.commit();
100         assertBeanCount(1, FACTORY_NAME);
101         assertStatus(status, 0, 1, 15);
102     }
103
104     private CommitStatus createInstance(final String listenAddress, final PortNumber listenPort, final TopologyId topologyId)
105             throws Exception {
106         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
107         createPCEPTopologyProviderModuleInstance(transaction, listenAddress, listenPort, topologyId);
108         return transaction.commit();
109     }
110
111     private CommitStatus createInstance() throws Exception {
112         return createInstance(LISTEN_ADDRESS, getRandomPortNumber(), TOPOLOGY_ID);
113     }
114
115     public static ObjectName createPCEPTopologyProviderModuleInstance(final ConfigTransactionJMXClient transaction,
116             final ObjectName dataBrokerON, final ObjectName bindingBrokerON, final ObjectName schedulerON) throws Exception {
117         final ObjectName objectName = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
118         final PCEPTopologyProviderModuleMXBean mxBean = transaction.newMXBeanProxy(objectName, PCEPTopologyProviderModuleMXBean.class);
119         mxBean.setDataProvider(dataBrokerON);
120         mxBean.setDispatcher(createDispatcherInstance(transaction, 5));
121         mxBean.setListenAddress(new IpAddress(LISTEN_ADDRESS.toCharArray()));
122         mxBean.setListenPort(getRandomPortNumber());
123         mxBean.setRpcRegistry(bindingBrokerON);
124         mxBean.setScheduler(schedulerON);
125         mxBean.setStatefulPlugin(transaction.createModule(Stateful02TopologySessionListenerModuleFactory.NAME,
126                 STATEFUL02_TOPOLOGY_INSTANCE_NAME));
127         mxBean.setTopologyId(TOPOLOGY_ID);
128         return objectName;
129     }
130
131     private ObjectName createPCEPTopologyProviderModuleInstance(final ConfigTransactionJMXClient transaction, final String listenAddress,
132             final PortNumber listenPort, final TopologyId topologyId) throws Exception {
133         final ObjectName objectName = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
134         final ObjectName dataBrokerON = createDataBrokerInstance(transaction);
135         final ObjectName notificationBrokerON = createNotificationBrokerInstance(transaction);
136         final ObjectName bindingBrokerON = createBindingBrokerImpl(transaction, dataBrokerON, notificationBrokerON);
137
138         final PCEPTopologyProviderModuleMXBean mxBean = transaction.newMXBeanProxy(objectName, PCEPTopologyProviderModuleMXBean.class);
139         mxBean.setDataProvider(dataBrokerON);
140         mxBean.setDispatcher(createDispatcherInstance(transaction, 5));
141         mxBean.setListenAddress(listenAddress == null ? null : new IpAddress(listenAddress.toCharArray()));
142         mxBean.setListenPort(listenPort);
143         mxBean.setRpcRegistry(bindingBrokerON);
144         mxBean.setScheduler(createInstructionSchedulerModuleInstance(transaction, dataBrokerON, bindingBrokerON, notificationBrokerON));
145         mxBean.setStatefulPlugin(transaction.createModule(Stateful02TopologySessionListenerModuleFactory.NAME,
146                 STATEFUL02_TOPOLOGY_INSTANCE_NAME));
147         mxBean.setTopologyId(topologyId);
148         return objectName;
149     }
150
151     @Override
152     public List<ModuleFactory> getModuleFactories() {
153         final List<ModuleFactory> moduleFactories = super.getModuleFactories();
154         moduleFactories.add(new PCEPTopologyProviderModuleFactory());
155         moduleFactories.add(new PCEPDispatcherImplModuleFactory());
156         moduleFactories.add(new PCEPSessionProposalFactoryImplModuleFactory());
157         moduleFactories.add(new NettyThreadgroupModuleFactory());
158         moduleFactories.add(new SimplePCEPExtensionProviderContextModuleFactory());
159         moduleFactories.add(new Stateful02TopologySessionListenerModuleFactory());
160         moduleFactories.add(new Stateful02PCEPSessionProposalFactoryModuleFactory());
161         return moduleFactories;
162     }
163
164     private static PortNumber getRandomPortNumber() {
165         final Random random = new Random();
166         return new PortNumber(random.nextInt(65000 - 30000 + 1) + 30000);
167     }
168
169     @Override
170     public List<String> getYangModelsPaths() {
171         final List<String> paths = super.getYangModelsPaths();
172         paths.add("/META-INF/yang/network-topology@2013-10-21.yang");
173         return paths;
174     }
175 }