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.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
18 import com.google.common.util.concurrent.Futures;
20 import javax.inject.Inject;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.api.RpcProviderService;
24 import org.opendaylight.mdsal.binding.api.RpcService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRoute;
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.Registration;
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 {
44 private static final Logger LOG = LoggerFactory.getLogger(RoutedServiceIT.class);
46 protected RoutedSimpleRoute routedSimpleRouteRpc1;
47 protected RoutedSimpleRoute routedSimpleRouteRpc2;
50 @Filter(timeout = 120 * 1000)
51 RpcProviderService rpcProviderService;
54 @Filter(timeout = 120 * 1000)
55 RpcService rpcService;
62 routedSimpleRouteRpc1 = mock(RoutedSimpleRoute.class, "First Flow Rpc");
63 doReturn(RoutedSimpleRoute.class).when(routedSimpleRouteRpc1).implementedInterface();
64 doReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null)).when(routedSimpleRouteRpc1)
67 routedSimpleRouteRpc2 = mock(RoutedSimpleRoute.class, "Second Flow Rpc");
68 doReturn(RoutedSimpleRoute.class).when(routedSimpleRouteRpc2).implementedInterface();
69 doReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null)).when(routedSimpleRouteRpc2)
74 public void testServiceRegistration() {
75 LOG.info("Register provider 1 with first implementation of routeSimpleService - rpc1 of node 1");
76 final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
77 final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
79 Registration firstReg = rpcProviderService.registerRpcImplementation(routedSimpleRouteRpc1,
81 assertNotNull("Registration should not be null", firstReg);
83 LOG.info("Register provider 2 with second implementation of routeSimpleService - rpc2 of node 2");
85 Registration secondReg = rpcProviderService.registerRpcImplementation(routedSimpleRouteRpc2, Set.of(nodeTwo));
86 assertNotNull("Registration should not be null", firstReg);
87 assertNotSame(secondReg, firstReg);
89 RoutedSimpleRoute consumerService = rpcService.getRpc(RoutedSimpleRoute.class);
90 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
91 assertNotSame("Provider instance and consumer instance should not be same.", routedSimpleRouteRpc1,
95 * Consumer creates addFlow message for node one and sends it to the MD-SAL.
97 final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
98 consumerService.invoke(simpleRouteFirstFoo);
101 * Verifies that implementation of the first instance received the same message from MD-SAL.
103 verify(routedSimpleRouteRpc1).invoke(simpleRouteFirstFoo);
105 * Verifies that second instance was not invoked with first message
107 verify(routedSimpleRouteRpc2, times(0)).invoke(simpleRouteFirstFoo);
110 * Consumer sends message to nodeTwo for three times. Should be processed by second instance.
112 final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
113 consumerService.invoke(simpleRouteSecondFoo);
114 consumerService.invoke(simpleRouteSecondFoo);
115 consumerService.invoke(simpleRouteSecondFoo);
118 * Verifies that second instance was invoked 3 times with second message and first instance wasn't invoked.
120 verify(routedSimpleRouteRpc2, times(3)).invoke(simpleRouteSecondFoo);
121 verify(routedSimpleRouteRpc1, times(0)).invoke(simpleRouteSecondFoo);
123 LOG.info("Unregistration of the path for the node one in the first provider");
126 LOG.info("Provider 2 registers path of node 1");
128 secondReg = rpcProviderService.registerRpcImplementation(routedSimpleRouteRpc2, Set.of(nodeOnePath));
131 * A consumer sends third message to node 1.
133 final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
134 consumerService.invoke(simpleRouteThirdFoo);
137 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1 time.
138 * TODO: fix unregister path
140 verify(routedSimpleRouteRpc2).invoke(simpleRouteThirdFoo);
144 * Returns node reference from string which represents path.
146 * @param string string with key(path)
147 * @return instance identifier to {@link UnorderedList}
149 private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
150 return InstanceIdentifier.builder(Lists.class)
151 .child(UnorderedContainer.class)
152 .child(UnorderedList.class, new UnorderedListKey(string))
157 * Creates flow AddFlowInput for which only node and cookie are set.
159 * @param node NodeRef value
160 * @return simpleRouteInput instance
162 static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
163 return new RoutedSimpleRouteInputBuilder().setRoute(node).build();