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;
18 import javax.inject.Inject;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.ops4j.pax.exam.util.Filter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * covers routed rpc creation, registration, invocation, unregistration
41 public class RoutedServiceIT extends AbstractIT {
43 private static final Logger LOG = LoggerFactory
44 .getLogger(RoutedServiceIT.class);
46 protected OpendaylightTestRoutedRpcService odlRoutedService1;
47 protected OpendaylightTestRoutedRpcService odlRoutedService2;
50 @Filter(timeout = 120 * 1000)
51 RpcProviderRegistry rpcProviderRegistry;
58 odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service");
59 odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service");
60 Mockito.when(odlRoutedService1.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
61 .thenReturn(Futures.<RpcResult<Void>>immediateFuture(null));
62 Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
63 .thenReturn(Futures.<RpcResult<Void>>immediateFuture(null));
67 public void testServiceRegistration() {
68 LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
70 RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg = rpcProviderRegistry
71 .addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
72 assertNotNull("Registration should not be null", firstReg);
73 assertSame(odlRoutedService1, firstReg.getInstance());
75 LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
77 RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg = rpcProviderRegistry
78 .addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
79 assertNotNull("Registration should not be null", firstReg);
80 assertSame(odlRoutedService2, secondReg.getInstance());
81 assertNotSame(secondReg, firstReg);
83 OpendaylightTestRoutedRpcService consumerService =
84 rpcProviderRegistry.getRpcService(OpendaylightTestRoutedRpcService.class);
85 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
86 assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1,
89 final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
91 LOG.info("Provider 1 registers path of node 1");
92 firstReg.registerPath(TestContext.class, nodeOnePath);
95 * Consumer creates addFlow message for node one and sends it to the
98 final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
99 consumerService.routedSimpleRoute(simpleRouteFirstFoo);
102 * Verifies that implementation of the first provider received the same
103 * message from MD-SAL.
105 verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
107 * Verifies that second instance was not invoked with first message
109 verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
111 LOG.info("Provider 2 registers path of node 2");
112 final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
113 secondReg.registerPath(TestContext.class, nodeTwo);
116 * Consumer sends message to nodeTwo for three times. Should be
117 * processed by second instance.
119 final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
120 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
121 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
122 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
125 * Verifies that second instance was invoked 3 times with second message
126 * and first instance wasn't invoked.
129 verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
130 verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
132 LOG.info("Unregistration of the path for the node one in the first provider");
133 firstReg.unregisterPath(TestContext.class, nodeOnePath);
135 LOG.info("Provider 2 registers path of node 1");
136 secondReg.registerPath(TestContext.class, nodeOnePath);
139 * A consumer sends third message to node 1
141 final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
142 consumerService.routedSimpleRoute(simpleRouteThirdFoo);
145 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
147 * TODO: fix unregister path
149 //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
150 verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
155 * Returns node reference from string which represents path
158 * string with key(path)
159 * @return instance identifier to {@link UnorderedList}
161 private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
162 final UnorderedListKey key = new UnorderedListKey(string);
163 final InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
164 .child(UnorderedContainer.class)
165 .child(UnorderedList.class, key)
172 * Creates flow AddFlowInput for which only node and cookie are set
176 * @return simpleRouteInput instance
178 static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
179 final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();