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 org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.OpendaylightTestRoutedRpcService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.RoutedSimpleRouteInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.rpc.routing.rev140701.TestContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * covers routed rpc creation, registration, invocation, unregistration
42 public class RoutedServiceTest extends AbstractTest {
44 private static final Logger LOG = LoggerFactory
45 .getLogger(RoutedServiceTest.class);
47 protected OpendaylightTestRoutedRpcService odlRoutedService1;
48 protected OpendaylightTestRoutedRpcService odlRoutedService2;
50 protected OpendaylightTestRoutedRpcService consumerService;
52 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg;
53 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg;
60 odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service");
61 odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service");
62 Mockito.when(odlRoutedService1.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
63 .thenReturn(Futures.<RpcResult<Void>>immediateFuture(null));
64 Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
65 .thenReturn(Futures.<RpcResult<Void>>immediateFuture(null));
69 public void testServiceRegistration() {
71 assertNotNull(getBroker());
73 final BindingAwareProvider provider1 = new AbstractTestProvider() {
75 public void onSessionInitiated(final ProviderContext session) {
76 assertNotNull(session);
77 firstReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
81 LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
82 broker.registerProvider(provider1);
83 assertNotNull("Registration should not be null", firstReg);
84 assertSame(odlRoutedService1, firstReg.getInstance());
86 final BindingAwareProvider provider2 = new AbstractTestProvider() {
88 public void onSessionInitiated(final ProviderContext session) {
89 assertNotNull(session);
90 secondReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
94 LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
95 broker.registerProvider(provider2);
96 assertNotNull("Registration should not be null", firstReg);
97 assertSame(odlRoutedService2, secondReg.getInstance());
98 assertNotSame(secondReg, firstReg);
100 final BindingAwareConsumer consumer = new BindingAwareConsumer() {
102 public void onSessionInitialized(final ConsumerContext session) {
103 consumerService = session.getRpcService(OpendaylightTestRoutedRpcService.class);
106 LOG.info("Register routeService consumer");
107 broker.registerConsumer(consumer);
109 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
110 assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1, consumerService);
112 final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
114 LOG.info("Provider 1 registers path of node 1");
115 firstReg.registerPath(TestContext.class, nodeOnePath);
118 * Consumer creates addFlow message for node one and sends it to the
121 final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
122 consumerService.routedSimpleRoute(simpleRouteFirstFoo);
125 * Verifies that implementation of the first provider received the same
126 * message from MD-SAL.
128 verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
130 * Verifies that second instance was not invoked with first message
132 verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
134 LOG.info("Provider 2 registers path of node 2");
135 final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
136 secondReg.registerPath(TestContext.class, nodeTwo);
139 * Consumer sends message to nodeTwo for three times. Should be
140 * processed by second instance.
142 final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
143 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
144 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
145 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
148 * Verifies that second instance was invoked 3 times with second message
149 * and first instance wasn't invoked.
152 verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
153 verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
155 LOG.info("Unregistration of the path for the node one in the first provider");
156 firstReg.unregisterPath(TestContext.class, nodeOnePath);
158 LOG.info("Provider 2 registers path of node 1");
159 secondReg.registerPath(TestContext.class, nodeOnePath);
162 * A consumer sends third message to node 1
164 final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
165 consumerService.routedSimpleRoute(simpleRouteThirdFoo);
168 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
170 * TODO: fix unregister path
172 //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
173 verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
178 * Returns node reference from string which represents path
181 * string with key(path)
182 * @return instance identifier to {@link UnorderedList}
184 private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
185 final UnorderedListKey key = new UnorderedListKey(string);
186 final InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
187 .child(UnorderedContainer.class)
188 .child(UnorderedList.class, key)
195 * Creates flow AddFlowInput for which only node and cookie are set
199 * @return simpleRouteInput instance
201 static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
202 final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();