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;
16 import com.google.common.util.concurrent.Futures;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
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.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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * covers routed rpc creation, registration, invocation, unregistration
40 public class RoutedServiceIT extends AbstractIT {
42 private static final Logger LOG = LoggerFactory
43 .getLogger(RoutedServiceIT.class);
45 protected OpendaylightTestRoutedRpcService odlRoutedService1;
46 protected OpendaylightTestRoutedRpcService odlRoutedService2;
48 protected OpendaylightTestRoutedRpcService consumerService;
50 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg;
51 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg;
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() {
69 assertNotNull(broker);
71 final BindingAwareProvider provider1 = new AbstractTestProvider() {
73 public void onSessionInitiated(final ProviderContext session) {
74 assertNotNull(session);
75 firstReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
79 LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
80 broker.registerProvider(provider1);
81 assertNotNull("Registration should not be null", firstReg);
82 assertSame(odlRoutedService1, firstReg.getInstance());
84 final BindingAwareProvider provider2 = new AbstractTestProvider() {
86 public void onSessionInitiated(final ProviderContext session) {
87 assertNotNull(session);
88 secondReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
92 LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
93 broker.registerProvider(provider2);
94 assertNotNull("Registration should not be null", firstReg);
95 assertSame(odlRoutedService2, secondReg.getInstance());
96 assertNotSame(secondReg, firstReg);
98 final BindingAwareConsumer consumer =
99 session -> consumerService = session.getRpcService(OpendaylightTestRoutedRpcService.class);
100 LOG.info("Register routeService consumer");
101 broker.registerConsumer(consumer);
103 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
104 assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1, consumerService);
106 final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
108 LOG.info("Provider 1 registers path of node 1");
109 firstReg.registerPath(TestContext.class, nodeOnePath);
112 * Consumer creates addFlow message for node one and sends it to the
115 final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
116 consumerService.routedSimpleRoute(simpleRouteFirstFoo);
119 * Verifies that implementation of the first provider received the same
120 * message from MD-SAL.
122 verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
124 * Verifies that second instance was not invoked with first message
126 verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
128 LOG.info("Provider 2 registers path of node 2");
129 final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
130 secondReg.registerPath(TestContext.class, nodeTwo);
133 * Consumer sends message to nodeTwo for three times. Should be
134 * processed by second instance.
136 final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
137 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
138 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
139 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
142 * Verifies that second instance was invoked 3 times with second message
143 * and first instance wasn't invoked.
146 verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
147 verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
149 LOG.info("Unregistration of the path for the node one in the first provider");
150 firstReg.unregisterPath(TestContext.class, nodeOnePath);
152 LOG.info("Provider 2 registers path of node 1");
153 secondReg.registerPath(TestContext.class, nodeOnePath);
156 * A consumer sends third message to node 1
158 final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
159 consumerService.routedSimpleRoute(simpleRouteThirdFoo);
162 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
164 * TODO: fix unregister path
166 //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
167 verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
172 * Returns node reference from string which represents path
175 * string with key(path)
176 * @return instance identifier to {@link UnorderedList}
178 private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
179 final UnorderedListKey key = new UnorderedListKey(string);
180 final InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
181 .child(UnorderedContainer.class)
182 .child(UnorderedList.class, key)
189 * Creates flow AddFlowInput for which only node and cookie are set
193 * @return simpleRouteInput instance
195 static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
196 final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();