Removed rib dependency on linkstate.
[bgpcep.git] / integration-tests / src / test / java / org / opendaylight / protocol / integration / pcep / PcepRpcServicesRoutingTest.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.protocol.integration.pcep;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNotSame;
12 import static org.junit.Assert.assertSame;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.verifyZeroInteractions;
16
17 import java.lang.reflect.Method;
18
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyContext;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.NetworkTopologyPcepProgrammingService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitAddLspInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitAddLspInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitUpdateLspInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev131106.SubmitUpdateLspInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.NetworkTopologyPcepService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepCreateP2pTunnelInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepCreateP2pTunnelInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepDestroyTunnelInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.PcepDestroyTunnelInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.programming.rev131030.TopologyTunnelPcepProgrammingService;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.ops4j.pax.exam.junit.PaxExam;
46 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
47
48 @RunWith(PaxExam.class)
49 @ExamReactorStrategy(org.ops4j.pax.exam.spi.reactors.PerClass.class)
50 public class PcepRpcServicesRoutingTest extends AbstractPcepOsgiTest {
51
52     @Test
53     public void testRoutedRpcNetworkTopologyPcepService() throws Exception {
54         final NetworkTopologyPcepService pcepService1 = mock(NetworkTopologyPcepService.class, "First pcep Service");
55         final NetworkTopologyPcepService pcepService2 = mock(NetworkTopologyPcepService.class, "Second pcep Service");
56
57         assertNotNull(getBroker());
58
59         final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
60                 new TopologyKey(getTopologyId("Topo1"))).toInstance();
61
62         BindingAwareProvider provider1 = new AbstractTestProvider() {
63
64             @Override
65             public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
66                 assertNotNull(session);
67                 BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> firstReg = session.addRoutedRpcImplementation(
68                         NetworkTopologyPcepService.class, pcepService1);
69                 assertNotNull("Registration should not be null", firstReg);
70                 assertSame(pcepService1, firstReg.getInstance());
71
72                 firstReg.registerPath(NetworkTopologyContext.class, topology);
73             }
74         };
75
76         broker.registerProvider(provider1, getBundleContext());
77
78         final InstanceIdentifier<Topology> topology2 = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
79                 new TopologyKey(getTopologyId("Topo2"))).toInstance();
80
81         BindingAwareProvider provider2 = new AbstractTestProvider() {
82
83             @Override
84             public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
85                 assertNotNull(session);
86                 BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepService> secondReg = session.addRoutedRpcImplementation(
87                         NetworkTopologyPcepService.class, pcepService2);
88                 secondReg.registerPath(NetworkTopologyContext.class, topology2);
89             }
90         };
91
92         broker.registerProvider(provider2, getBundleContext());
93
94         BindingAwareConsumer consumer = new BindingAwareConsumer() {
95             @Override
96             public void onSessionInitialized(final BindingAwareBroker.ConsumerContext session) {
97                 NetworkTopologyPcepService consumerPcepService = session.getRpcService(NetworkTopologyPcepService.class);
98
99                 assertNotNull(consumerPcepService);
100                 assertNotSame(pcepService1, consumerPcepService);
101                 assertNotSame(pcepService2, consumerPcepService);
102
103                 testAddLspRpce(consumerPcepService);
104                 testEnsureLspRpce(consumerPcepService);
105             }
106
107             private void testAddLspRpce(NetworkTopologyPcepService consumerPcepService) {
108                 AddLspInput addLspInput = getInputForRpc(topology, AddLspInputBuilder.class, AddLspInput.class);
109                 consumerPcepService.addLsp(addLspInput);
110
111                 verify(pcepService1).addLsp(addLspInput);
112                 verifyZeroInteractions(pcepService2);
113
114                 addLspInput = getInputForRpc(topology2, AddLspInputBuilder.class, AddLspInput.class);
115
116                 consumerPcepService.addLsp(addLspInput);
117
118                 verifyZeroInteractions(pcepService1);
119                 verify(pcepService2).addLsp(addLspInput);
120             }
121
122             private void testEnsureLspRpce(NetworkTopologyPcepService consumerPcepService) {
123                 EnsureLspOperationalInput ensureInput = getInputForRpc(topology, EnsureLspOperationalInputBuilder.class,
124                         EnsureLspOperationalInput.class);
125
126                 consumerPcepService.ensureLspOperational(ensureInput);
127
128                 verify(pcepService1).ensureLspOperational(ensureInput);
129                 verifyZeroInteractions(pcepService2);
130
131                 ensureInput = getInputForRpc(topology2, EnsureLspOperationalInputBuilder.class, EnsureLspOperationalInput.class);
132
133                 consumerPcepService.ensureLspOperational(ensureInput);
134
135                 verifyZeroInteractions(pcepService1);
136                 verify(pcepService2).ensureLspOperational(ensureInput);
137             }
138         };
139         broker.registerConsumer(consumer, getBundleContext());
140     }
141
142     private static <T> T getInputForRpc(final InstanceIdentifier<Topology> topology, Class<?> builderClass, Class<T> builtObjectClass) {
143         try {
144             Object builderInstance = builderClass.newInstance();
145             Method method = builderClass.getMethod("setNetworkTopologyRef", NetworkTopologyRef.class);
146             method.invoke(builderInstance, new NetworkTopologyRef(topology));
147             return builtObjectClass.cast(builderClass.getMethod("build").invoke(builderInstance));
148         } catch (Exception e) {
149             throw new RuntimeException("Unable to create instance from " + builderClass, e);
150         }
151     }
152
153     @Test
154     public void testRoutedRpcNetworkTopologyPcepProgrammingService() throws Exception {
155         final NetworkTopologyPcepProgrammingService pcepService1 = mock(NetworkTopologyPcepProgrammingService.class,
156                 "First pcep program Service");
157         final NetworkTopologyPcepProgrammingService pcepService2 = mock(NetworkTopologyPcepProgrammingService.class,
158                 "Second pcep program Service");
159
160         assertNotNull(getBroker());
161
162         final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
163                 new TopologyKey(getTopologyId("Topo1"))).toInstance();
164
165         BindingAwareProvider provider1 = new AbstractTestProvider() {
166
167             @Override
168             public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
169                 assertNotNull(session);
170                 BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> firstReg = session.addRoutedRpcImplementation(
171                         NetworkTopologyPcepProgrammingService.class, pcepService1);
172                 assertNotNull("Registration should not be null", firstReg);
173                 assertSame(pcepService1, firstReg.getInstance());
174
175                 firstReg.registerPath(NetworkTopologyContext.class, topology);
176             }
177         };
178
179         broker.registerProvider(provider1, getBundleContext());
180
181         final InstanceIdentifier<Topology> topology2 = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
182                 new TopologyKey(getTopologyId("Topo2"))).toInstance();
183
184         BindingAwareProvider provider2 = new AbstractTestProvider() {
185
186             @Override
187             public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
188                 assertNotNull(session);
189                 BindingAwareBroker.RoutedRpcRegistration<NetworkTopologyPcepProgrammingService> secondReg = session.addRoutedRpcImplementation(
190                         NetworkTopologyPcepProgrammingService.class, pcepService2);
191                 secondReg.registerPath(NetworkTopologyContext.class, topology2);
192             }
193         };
194
195         broker.registerProvider(provider2, getBundleContext());
196
197         BindingAwareConsumer consumer = new BindingAwareConsumer() {
198             @Override
199             public void onSessionInitialized(final BindingAwareBroker.ConsumerContext session) {
200                 NetworkTopologyPcepProgrammingService consumerPcepService = session.getRpcService(NetworkTopologyPcepProgrammingService.class);
201
202                 assertNotNull(consumerPcepService);
203                 assertNotSame(pcepService1, consumerPcepService);
204                 assertNotSame(pcepService2, consumerPcepService);
205
206                 testSubmitAddLspRpc(consumerPcepService);
207                 testSubmitUpdateLspRpc(consumerPcepService);
208             }
209
210             private void testSubmitAddLspRpc(NetworkTopologyPcepProgrammingService consumerPcepService) {
211                 SubmitAddLspInput addLspInput = getInputForRpc(topology, SubmitAddLspInputBuilder.class, SubmitAddLspInput.class);
212                 consumerPcepService.submitAddLsp(addLspInput);
213
214                 verify(pcepService1).submitAddLsp(addLspInput);
215                 verifyZeroInteractions(pcepService2);
216
217                 addLspInput = getInputForRpc(topology2, SubmitAddLspInputBuilder.class, SubmitAddLspInput.class);
218
219                 consumerPcepService.submitAddLsp(addLspInput);
220
221                 verifyZeroInteractions(pcepService1);
222                 verify(pcepService2).submitAddLsp(addLspInput);
223             }
224
225             private void testSubmitUpdateLspRpc(NetworkTopologyPcepProgrammingService consumerPcepService) {
226                 SubmitUpdateLspInput submitLspInput = getInputForRpc(topology, SubmitUpdateLspInputBuilder.class,
227                         SubmitUpdateLspInput.class);
228                 consumerPcepService.submitUpdateLsp(submitLspInput);
229
230                 verify(pcepService1).submitUpdateLsp(submitLspInput);
231                 verifyZeroInteractions(pcepService2);
232
233                 submitLspInput = getInputForRpc(topology2, SubmitUpdateLspInputBuilder.class, SubmitUpdateLspInput.class);
234
235                 consumerPcepService.submitUpdateLsp(submitLspInput);
236
237                 verifyZeroInteractions(pcepService1);
238                 verify(pcepService2).submitUpdateLsp(submitLspInput);
239             }
240         };
241         broker.registerConsumer(consumer, getBundleContext());
242     }
243
244     @Test
245     public void testRoutedRpcTopologyTunnelPcepProgrammingService() throws Exception {
246         final TopologyTunnelPcepProgrammingService pcepService1 = mock(TopologyTunnelPcepProgrammingService.class,
247                 "First pcep program Service");
248         final TopologyTunnelPcepProgrammingService pcepService2 = mock(TopologyTunnelPcepProgrammingService.class,
249                 "Second pcep program Service");
250
251         assertNotNull(getBroker());
252
253         final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
254                 new TopologyKey(getTopologyId("Topo1"))).toInstance();
255
256         BindingAwareProvider provider1 = new AbstractTestProvider() {
257
258             @Override
259             public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
260                 assertNotNull(session);
261                 BindingAwareBroker.RoutedRpcRegistration<TopologyTunnelPcepProgrammingService> firstReg = session.addRoutedRpcImplementation(
262                         TopologyTunnelPcepProgrammingService.class, pcepService1);
263                 assertNotNull("Registration should not be null", firstReg);
264                 assertSame(pcepService1, firstReg.getInstance());
265
266                 firstReg.registerPath(NetworkTopologyContext.class, topology);
267             }
268         };
269
270         broker.registerProvider(provider1, getBundleContext());
271
272         final InstanceIdentifier<Topology> topology2 = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
273                 new TopologyKey(getTopologyId("Topo2"))).toInstance();
274
275         BindingAwareProvider provider2 = new AbstractTestProvider() {
276
277             @Override
278             public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
279                 assertNotNull(session);
280                 BindingAwareBroker.RoutedRpcRegistration<TopologyTunnelPcepProgrammingService> secondReg = session.addRoutedRpcImplementation(
281                         TopologyTunnelPcepProgrammingService.class, pcepService2);
282                 secondReg.registerPath(NetworkTopologyContext.class, topology2);
283             }
284         };
285
286         broker.registerProvider(provider2, getBundleContext());
287
288         BindingAwareConsumer consumer = new BindingAwareConsumer() {
289             @Override
290             public void onSessionInitialized(final BindingAwareBroker.ConsumerContext session) {
291                 TopologyTunnelPcepProgrammingService consumerPcepService = session.getRpcService(TopologyTunnelPcepProgrammingService.class);
292
293                 assertNotNull(consumerPcepService);
294                 assertNotSame(pcepService1, consumerPcepService);
295                 assertNotSame(pcepService2, consumerPcepService);
296
297                 testCreateP2pTunnel(consumerPcepService);
298                 testDestroyP2pTunnel(consumerPcepService);
299             }
300
301             private void testCreateP2pTunnel(TopologyTunnelPcepProgrammingService consumerPcepService) {
302                 PcepCreateP2pTunnelInput addLspInput = getInputForRpc(topology, PcepCreateP2pTunnelInputBuilder.class,
303                         PcepCreateP2pTunnelInput.class);
304                 consumerPcepService.pcepCreateP2pTunnel(addLspInput);
305
306                 verify(pcepService1).pcepCreateP2pTunnel(addLspInput);
307                 verifyZeroInteractions(pcepService2);
308
309                 addLspInput = getInputForRpc(topology2, PcepCreateP2pTunnelInputBuilder.class, PcepCreateP2pTunnelInput.class);
310
311                 consumerPcepService.pcepCreateP2pTunnel(addLspInput);
312
313                 verifyZeroInteractions(pcepService1);
314                 verify(pcepService2).pcepCreateP2pTunnel(addLspInput);
315             }
316
317             private void testDestroyP2pTunnel(TopologyTunnelPcepProgrammingService consumerPcepService) {
318                 PcepDestroyTunnelInput addLspInput = getInputForRpc(topology, PcepDestroyTunnelInputBuilder.class,
319                         PcepDestroyTunnelInput.class);
320                 consumerPcepService.pcepDestroyTunnel(addLspInput);
321
322                 verify(pcepService1).pcepDestroyTunnel(addLspInput);
323                 verifyZeroInteractions(pcepService2);
324
325                 addLspInput = getInputForRpc(topology2, PcepDestroyTunnelInputBuilder.class, PcepDestroyTunnelInput.class);
326
327                 consumerPcepService.pcepDestroyTunnel(addLspInput);
328
329                 verifyZeroInteractions(pcepService1);
330                 verify(pcepService2).pcepDestroyTunnel(addLspInput);
331             }
332         };
333         broker.registerConsumer(consumer, getBundleContext());
334     }
335
336 }