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 org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * covers routed rpc creation, registration, invocation, unregistration
39 public class RoutedServiceTest extends AbstractTest {
41 private static final Logger LOG = LoggerFactory
42 .getLogger(RoutedServiceTest.class);
44 protected OpendaylightTestRoutedRpcService odlRoutedService1;
45 protected OpendaylightTestRoutedRpcService odlRoutedService2;
47 protected OpendaylightTestRoutedRpcService consumerService;
49 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg;
50 protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg;
57 odlRoutedService1 = mock(OpendaylightTestRoutedRpcService.class, "First Flow Service");
58 odlRoutedService2 = mock(OpendaylightTestRoutedRpcService.class, "Second Flow Service");
62 public void testServiceRegistration() {
64 assertNotNull(getBroker());
66 BindingAwareProvider provider1 = new AbstractTestProvider() {
68 public void onSessionInitiated(ProviderContext session) {
69 assertNotNull(session);
70 firstReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
74 LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
75 broker.registerProvider(provider1);
76 assertNotNull("Registration should not be null", firstReg);
77 assertSame(odlRoutedService1, firstReg.getInstance());
79 BindingAwareProvider provider2 = new AbstractTestProvider() {
81 public void onSessionInitiated(ProviderContext session) {
82 assertNotNull(session);
83 secondReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
87 LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
88 broker.registerProvider(provider2);
89 assertNotNull("Registration should not be null", firstReg);
90 assertSame(odlRoutedService2, secondReg.getInstance());
91 assertNotSame(secondReg, firstReg);
93 BindingAwareConsumer consumer = new BindingAwareConsumer() {
95 public void onSessionInitialized(ConsumerContext session) {
96 consumerService = session.getRpcService(OpendaylightTestRoutedRpcService.class);
99 LOG.info("Register routeService consumer");
100 broker.registerConsumer(consumer);
102 assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
103 assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1, consumerService);
105 InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
107 LOG.info("Provider 1 registers path of node 1");
108 firstReg.registerPath(TestContext.class, nodeOnePath);
111 * Consumer creates addFlow message for node one and sends it to the
114 RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
115 consumerService.routedSimpleRoute(simpleRouteFirstFoo);
118 * Verifies that implementation of the first provider received the same
119 * message from MD-SAL.
121 verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
123 * Verifies that second instance was not invoked with first message
125 verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
127 LOG.info("Provider 2 registers path of node 2");
128 InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
129 secondReg.registerPath(TestContext.class, nodeTwo);
132 * Consumer sends message to nodeTwo for three times. Should be
133 * processed by second instance.
135 RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
136 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
137 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
138 consumerService.routedSimpleRoute(simpleRouteSecondFoo);
141 * Verifies that second instance was invoked 3 times with second message
142 * and first instance wasn't invoked.
145 verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
146 verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
148 LOG.info("Unregistration of the path for the node one in the first provider");
149 firstReg.unregisterPath(TestContext.class, nodeOnePath);
151 LOG.info("Provider 2 registers path of node 1");
152 secondReg.registerPath(TestContext.class, nodeOnePath);
155 * A consumer sends third message to node 1
157 RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
158 consumerService.routedSimpleRoute(simpleRouteThirdFoo);
161 * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
163 * TODO: fix unregister path
165 //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
166 verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
171 * Returns node reference from string which represents path
174 * string with key(path)
175 * @return instance identifier to {@link UnorderedList}
177 private static InstanceIdentifier<UnorderedList> createNodeRef(String string) {
178 UnorderedListKey key = new UnorderedListKey(string);
179 InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
180 .child(UnorderedContainer.class)
181 .child(UnorderedList.class, key)
188 * Creates flow AddFlowInput for which only node and cookie are set
192 * @return simpleRouteInput instance
194 static RoutedSimpleRouteInput createSimpleRouteInput(InstanceIdentifier<UnorderedList> node) {
195 RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();