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.ConsumerContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
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.TestContext;
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.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
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;
49 protected OpendaylightTestRoutedRpcService consumerService;
51 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg;
52 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg;
59 odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service");
60 odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service");
61 Mockito.when(odlRoutedService1.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
62 .thenReturn(Futures.<RpcResult<Void>>immediateFuture(null));
63 Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
64 .thenReturn(Futures.<RpcResult<Void>>immediateFuture(null));
68 public void testServiceRegistration() {
70 assertNotNull(broker);
72 final BindingAwareProvider provider1 = new AbstractTestProvider() {
74 public void onSessionInitiated(final ProviderContext session) {
75 assertNotNull(session);
76 firstReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
80 LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
81 broker.registerProvider(provider1);
82 assertNotNull("Registration should not be null", firstReg);
83 assertSame(odlRoutedService1, firstReg.getInstance());
85 final BindingAwareProvider provider2 = new AbstractTestProvider() {
87 public void onSessionInitiated(final ProviderContext session) {
88 assertNotNull(session);
89 secondReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
93 LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
94 broker.registerProvider(provider2);
95 assertNotNull("Registration should not be null", firstReg);
96 assertSame(odlRoutedService2, secondReg.getInstance());
97 assertNotSame(secondReg, firstReg);
99 final BindingAwareConsumer consumer = new BindingAwareConsumer() {
101 public void onSessionInitialized(final ConsumerContext session) {
102 consumerService = session.getRpcService(OpendaylightTestRoutedRpcService.class);
105 LOG.info("Register routeService consumer");
106 broker.registerConsumer(consumer);
108 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
109 assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1, consumerService);
111 final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
113 LOG.info("Provider 1 registers path of node 1");
114 firstReg.registerPath(TestContext.class, nodeOnePath);
117 * Consumer creates addFlow message for node one and sends it to the
120 final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
121 consumerService.routedSimpleRoute(simpleRouteFirstFoo);
124 * Verifies that implementation of the first provider received the same
125 * message from MD-SAL.
127 verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
129 * Verifies that second instance was not invoked with first message
131 verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
133 LOG.info("Provider 2 registers path of node 2");
134 final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
135 secondReg.registerPath(TestContext.class, nodeTwo);
138 * Consumer sends message to nodeTwo for three times. Should be
139 * processed by second instance.
141 final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
142 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
143 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
144 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
147 * Verifies that second instance was invoked 3 times with second message
148 * and first instance wasn't invoked.
151 verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
152 verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
154 LOG.info("Unregistration of the path for the node one in the first provider");
155 firstReg.unregisterPath(TestContext.class, nodeOnePath);
157 LOG.info("Provider 2 registers path of node 1");
158 secondReg.registerPath(TestContext.class, nodeOnePath);
161 * A consumer sends third message to node 1
163 final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
164 consumerService.routedSimpleRoute(simpleRouteThirdFoo);
167 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
169 * TODO: fix unregister path
171 //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
172 verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
177 * Returns node reference from string which represents path
180 * string with key(path)
181 * @return instance identifier to {@link UnorderedList}
183 private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
184 final UnorderedListKey key = new UnorderedListKey(string);
185 final InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
186 .child(UnorderedContainer.class)
187 .child(UnorderedList.class, key)
194 * Creates flow AddFlowInput for which only node and cookie are set
198 * @return simpleRouteInput instance
200 static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
201 final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();