BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / pcep / topology-provider / 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 io.netty.channel.nio.NioEventLoopGroup;
13 import java.util.Arrays;
14 import java.util.List;
15 import java.util.Random;
16 import javax.management.ObjectName;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.config.api.ValidationException;
20 import org.opendaylight.controller.config.api.jmx.CommitStatus;
21 import org.opendaylight.controller.config.spi.ModuleFactory;
22 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
23 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleFactory;
24 import org.opendaylight.controller.config.yang.pcep.impl.PCEPDispatcherImplModuleFactory;
25 import org.opendaylight.controller.config.yang.pcep.impl.PCEPDispatcherImplModuleMXBean;
26 import org.opendaylight.controller.config.yang.programming.impl.AbstractInstructionSchedulerTest;
27 import org.opendaylight.protocol.pcep.PCEPDispatcher;
28 import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
29 import org.opendaylight.protocol.pcep.ietf.stateful07.PCEPStatefulCapability;
30 import org.opendaylight.protocol.pcep.impl.BasePCEPSessionProposalFactory;
31 import org.opendaylight.protocol.pcep.impl.DefaultPCEPSessionNegotiatorFactory;
32 import org.opendaylight.protocol.pcep.impl.PCEPDispatcherImpl;
33 import org.opendaylight.protocol.pcep.spi.PCEPExtensionProviderContext;
34 import org.opendaylight.protocol.pcep.spi.pojo.SimplePCEPExtensionProviderContext;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
39
40 public class PCEPTopologyProviderModuleTest extends AbstractInstructionSchedulerTest {
41
42     private static final String FACTORY_NAME = PCEPTopologyProviderModuleFactory.NAME;
43     private static final String INSTANCE_NAME = "pcep-topology-provider-instance";
44     private static final String STATEFUL07_TOPOLOGY_INSTANCE_NAME = "pcep-topology-stateful07-instance";
45
46     private static final String LISTEN_ADDRESS = "0.0.0.0";
47     private static final PortNumber LISTEN_PORT = new PortNumber(4189);
48     private static final TopologyId TOPOLOGY_ID = new TopologyId("pcep-topology");
49
50     @Override
51     @Before
52     public void setUp() throws Exception {
53         super.setUp();
54
55         SimplePCEPExtensionProviderContext extContext = new SimplePCEPExtensionProviderContext();
56         setupMockService(PCEPExtensionProviderContext.class, extContext);
57         BasePCEPSessionProposalFactory proposalFactory = new BasePCEPSessionProposalFactory(120, 30,
58                 Arrays.asList(new PCEPStatefulCapability(true, true, true, true, true, true, true)));
59         setupMockService(PCEPSessionProposalFactory.class, proposalFactory);
60         NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
61         setupMockService(PCEPDispatcher.class, new PCEPDispatcherImpl(extContext.getMessageHandlerRegistry(),
62                 new DefaultPCEPSessionNegotiatorFactory(proposalFactory, 5), eventLoopGroup, eventLoopGroup));
63     }
64
65     @Test
66     public void testValidationExceptionListenAddressNotSet() throws Exception {
67         try {
68             createInstance(null, LISTEN_PORT, TOPOLOGY_ID, false);
69             fail();
70         } catch (final ValidationException e) {
71             assertTrue(e.getMessage().contains("ListenAddress is not set"));
72         }
73     }
74
75     @Test
76     public void testValidationExceptionListenPortNotSet() throws Exception {
77         try {
78             createInstance(LISTEN_ADDRESS, null, TOPOLOGY_ID, false);
79             fail();
80         } catch (final ValidationException e) {
81             assertTrue(e.getMessage().contains("ListenPort is not set"));
82         }
83     }
84
85     @Test
86     public void testValidationExceptionTopologyIdNotSet() throws Exception {
87         try {
88             createInstance(LISTEN_ADDRESS, LISTEN_PORT, null, false);
89             fail();
90         } catch (final ValidationException e) {
91             assertTrue(e.getMessage().contains("TopologyId is not set"));
92         }
93     }
94
95     @Test
96     public void testCreateBean() throws Exception {
97         final CommitStatus status = createInstance(false);
98         assertBeanCount(1, FACTORY_NAME);
99         assertStatus(status, 13, 0, 0);
100     }
101
102     @Test
103     public void testReusingOldInstance() throws Exception {
104         createInstance(false);
105         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
106         assertBeanCount(1, FACTORY_NAME);
107         final CommitStatus status = transaction.commit();
108         assertBeanCount(1, FACTORY_NAME);
109         assertStatus(status, 0, 0, 13);
110     }
111
112     @Test
113     public void testReconfigure() throws Exception {
114         createInstance(false);
115         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
116         assertBeanCount(1, FACTORY_NAME);
117         final PCEPTopologyProviderModuleMXBean mxBean = transaction.newMXBeanProxy(
118                 transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME), PCEPTopologyProviderModuleMXBean.class);
119         mxBean.setTopologyId(new TopologyId("new-pcep-topology"));
120         final CommitStatus status = transaction.commit();
121         assertBeanCount(1, FACTORY_NAME);
122         assertStatus(status, 0, 1, 12);
123     }
124
125     private CommitStatus createInstance(final String listenAddress, final PortNumber listenPort,
126                                         final TopologyId topologyId, final boolean addMD5)
127             throws Exception {
128         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
129         createPCEPTopologyProviderModuleInstance(transaction, listenAddress, listenPort, topologyId, addMD5);
130         return transaction.commit();
131     }
132
133     private CommitStatus createInstance(final boolean addMD5) throws Exception {
134         return createInstance(LISTEN_ADDRESS, getRandomPortNumber(), TOPOLOGY_ID, addMD5);
135     }
136
137     public static ObjectName createPCEPTopologyProviderModuleInstance(final ConfigTransactionJMXClient transaction,
138             final ObjectName dataBrokerON, final ObjectName bindingBrokerON, final ObjectName schedulerON) throws Exception {
139         final ObjectName objectName = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
140         final PCEPTopologyProviderModuleMXBean mxBean = transaction.newMXBeanProxy(objectName, PCEPTopologyProviderModuleMXBean.class);
141         mxBean.setDataProvider(dataBrokerON);
142         mxBean.setDispatcher(createDispatcherInstance(transaction));
143
144         mxBean.setListenAddress(new IpAddress(LISTEN_ADDRESS.toCharArray()));
145         mxBean.setListenPort(getRandomPortNumber());
146         mxBean.setRpcRegistry(bindingBrokerON);
147         mxBean.setScheduler(schedulerON);
148         mxBean.setStatefulPlugin(transaction.createModule(Stateful07TopologySessionListenerModuleFactory.NAME,
149                 STATEFUL07_TOPOLOGY_INSTANCE_NAME));
150         mxBean.setTopologyId(TOPOLOGY_ID);
151         return objectName;
152     }
153
154     public static ObjectName createDispatcherInstance(final ConfigTransactionJMXClient transaction)
155             throws Exception {
156         final ObjectName nameCreated = transaction.createModule(PCEPDispatcherImplModuleFactory.NAME, "pcep-dispatcher-impl");
157         transaction.newMXBeanProxy(nameCreated, PCEPDispatcherImplModuleMXBean.class);
158         return nameCreated;
159     }
160
161     private ObjectName createPCEPTopologyProviderModuleInstance(final ConfigTransactionJMXClient transaction, final String listenAddress,
162             final PortNumber listenPort, final TopologyId topologyId, final boolean addMD5) throws Exception {
163         final ObjectName objectName = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
164         final ObjectName notificationBrokerON = createNotificationBrokerInstance(transaction);
165         final ObjectName asyncDataBrokerON = createAsyncDataBrokerInstance(transaction);
166         final ObjectName bindingBrokerON = createBindingBrokerImpl(transaction, createCompatibleDataBrokerInstance(transaction), notificationBrokerON);
167
168         final PCEPTopologyProviderModuleMXBean mxBean = transaction.newMXBeanProxy(objectName, PCEPTopologyProviderModuleMXBean.class);
169         mxBean.setDataProvider(asyncDataBrokerON);
170         mxBean.setDispatcher(createDispatcherInstance(transaction));
171
172         if (addMD5) {
173             // create 1 client
174             final Client client = new Client();
175             client.setPassword(Rfc2385Key.getDefaultInstance("foo"));
176             client.setAddress(new IpAddress("127.0.0.1".toCharArray()));
177             mxBean.setClient(Arrays.asList(client));
178         }
179
180         mxBean.setListenAddress(listenAddress == null ? null : new IpAddress(listenAddress.toCharArray()));
181         mxBean.setListenPort(listenPort);
182         mxBean.setRpcRegistry(bindingBrokerON);
183         mxBean.setScheduler(createInstructionSchedulerModuleInstance(transaction, asyncDataBrokerON, bindingBrokerON,
184                 notificationBrokerON));
185         mxBean.setStatefulPlugin(transaction.createModule(Stateful07TopologySessionListenerModuleFactory.NAME,
186                 STATEFUL07_TOPOLOGY_INSTANCE_NAME));
187         mxBean.setTopologyId(topologyId);
188         return objectName;
189     }
190
191     @Override
192     public List<ModuleFactory> getModuleFactories() {
193         final List<ModuleFactory> moduleFactories = super.getModuleFactories();
194         moduleFactories.add(new PCEPTopologyProviderModuleFactory());
195         moduleFactories.add(new PCEPDispatcherImplModuleFactory());
196         moduleFactories.add(new NettyThreadgroupModuleFactory());
197         moduleFactories.add(new Stateful07TopologySessionListenerModuleFactory());
198         return moduleFactories;
199     }
200
201     private static PortNumber getRandomPortNumber() {
202         final Random random = new Random();
203         return new PortNumber(random.nextInt(65000 - 30000 + 1) + 30000);
204     }
205
206     @Override
207     public List<String> getYangModelsPaths() {
208         final List<String> paths = super.getYangModelsPaths();
209         paths.add("/META-INF/yang/network-topology@2013-10-21.yang");
210         paths.add("/META-INF/yang/network-topology-pcep.yang");
211         paths.add("/META-INF/yang/network-topology-pcep-programming.yang");
212         paths.add("/META-INF/yang/network-topology-programming.yang");
213         paths.add("/META-INF/yang/odl-network-topology.yang");
214         paths.add("/META-INF/yang/yang-ext.yang");
215         paths.add("/META-INF/yang/pcep-types.yang");
216         paths.add("/META-INF/yang/rsvp.yang");
217         paths.add("/META-INF/yang/iana.yang");
218         paths.add("/META-INF/yang/network-concepts.yang");
219         paths.add("/META-INF/yang/ieee754.yang");
220         return paths;
221     }
222 }