2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.test.sal.binding.it;
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.times;
15 import static org.mockito.Mockito.verify;
17 import com.google.common.util.concurrent.Futures;
19 import javax.inject.Inject;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
24 import org.opendaylight.mdsal.binding.api.RpcProviderService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
33 import org.opendaylight.yangtools.concepts.ObjectRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.ops4j.pax.exam.util.Filter;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Covers routed rpc creation, registration, invocation, unregistration.
43 public class RoutedServiceIT extends AbstractIT {
45 private static final Logger LOG = LoggerFactory
46 .getLogger(RoutedServiceIT.class);
48 protected OpendaylightTestRoutedRpcService odlRoutedService1;
49 protected OpendaylightTestRoutedRpcService odlRoutedService2;
52 @Filter(timeout = 120 * 1000)
53 RpcProviderService rpcProviderService;
56 @Filter(timeout = 120 * 1000)
57 RpcConsumerRegistry rpcConsumerRegistry;
64 odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service");
65 odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service");
66 Mockito.when(odlRoutedService1.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
67 .thenReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null));
68 Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
69 .thenReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null));
73 public void testServiceRegistration() {
74 LOG.info("Register provider 1 with first implementation of routeSimpleService - service1 of node 1");
75 final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
76 final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
78 ObjectRegistration<OpendaylightTestRoutedRpcService> firstReg = rpcProviderService.registerRpcImplementation(
79 OpendaylightTestRoutedRpcService.class, odlRoutedService1, Set.of(nodeOnePath));
80 assertNotNull("Registration should not be null", firstReg);
81 assertSame(odlRoutedService1, firstReg.getInstance());
83 LOG.info("Register provider 2 with second implementation of routeSimpleService - service2 of node 2");
85 ObjectRegistration<OpendaylightTestRoutedRpcService> secondReg = rpcProviderService.registerRpcImplementation(
86 OpendaylightTestRoutedRpcService.class, odlRoutedService2, Set.of(nodeTwo));
87 assertNotNull("Registration should not be null", firstReg);
88 assertSame(odlRoutedService2, secondReg.getInstance());
89 assertNotSame(secondReg, firstReg);
91 OpendaylightTestRoutedRpcService consumerService =
92 rpcConsumerRegistry.getRpcService(OpendaylightTestRoutedRpcService.class);
93 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
94 assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1,
98 * Consumer creates addFlow message for node one and sends it to the MD-SAL.
100 final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
101 consumerService.routedSimpleRoute(simpleRouteFirstFoo);
104 * Verifies that implementation of the first provider received the same message from MD-SAL.
106 verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
108 * Verifies that second instance was not invoked with first message
110 verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
113 * Consumer sends message to nodeTwo for three times. Should be processed by second instance.
115 final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
116 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
117 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
118 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
121 * Verifies that second instance was invoked 3 times with second message and first instance wasn't invoked.
123 verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
124 verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
126 LOG.info("Unregistration of the path for the node one in the first provider");
129 LOG.info("Provider 2 registers path of node 1");
131 secondReg = rpcProviderService.registerRpcImplementation(
132 OpendaylightTestRoutedRpcService.class, odlRoutedService2, Set.of(nodeOnePath));
135 * A consumer sends third message to node 1.
137 final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
138 consumerService.routedSimpleRoute(simpleRouteThirdFoo);
141 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1 time.
142 * TODO: fix unregister path
144 //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
145 verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
149 * Returns node reference from string which represents path.
151 * @param string string with key(path)
152 * @return instance identifier to {@link UnorderedList}
154 private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
155 return InstanceIdentifier.builder(Lists.class)
156 .child(UnorderedContainer.class)
157 .child(UnorderedList.class, new UnorderedListKey(string))
162 * Creates flow AddFlowInput for which only node and cookie are set.
164 * @param node NodeRef value
165 * @return simpleRouteInput instance
167 static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
168 return new RoutedSimpleRouteInputBuilder().setRoute(node).build();