0b365cac49e7444b1ad280322b094f09e0b1613b
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / RoutedServiceIT.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.test.sal.binding.it;
9
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;
37
38 /**
39  * covers routed rpc creation, registration, invocation, unregistration
40  */
41 public class RoutedServiceIT extends AbstractIT {
42
43     private static final Logger LOG = LoggerFactory
44             .getLogger(RoutedServiceIT.class);
45
46     protected OpendaylightTestRoutedRpcService odlRoutedService1;
47     protected OpendaylightTestRoutedRpcService odlRoutedService2;
48
49     protected OpendaylightTestRoutedRpcService consumerService;
50
51     protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg;
52     protected RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg;
53
54     /**
55      * prepare mocks
56      */
57     @Before
58     public void setUp() {
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));
65     }
66
67     @Test
68     public void testServiceRegistration() {
69
70         assertNotNull(broker);
71
72         final BindingAwareProvider provider1 = new AbstractTestProvider() {
73             @Override
74             public void onSessionInitiated(final ProviderContext session) {
75                 assertNotNull(session);
76                 firstReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
77             }
78         };
79
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());
84
85         final BindingAwareProvider provider2 = new AbstractTestProvider() {
86             @Override
87             public void onSessionInitiated(final ProviderContext session) {
88                 assertNotNull(session);
89                 secondReg = session.addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
90             }
91         };
92
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);
98
99         final BindingAwareConsumer consumer = new BindingAwareConsumer() {
100             @Override
101             public void onSessionInitialized(final ConsumerContext session) {
102                 consumerService = session.getRpcService(OpendaylightTestRoutedRpcService.class);
103             }
104         };
105         LOG.info("Register routeService consumer");
106         broker.registerConsumer(consumer);
107
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);
110
111         final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
112
113         LOG.info("Provider 1 registers path of node 1");
114         firstReg.registerPath(TestContext.class, nodeOnePath);
115
116         /**
117          * Consumer creates addFlow message for node one and sends it to the
118          * MD-SAL
119          */
120         final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
121         consumerService.routedSimpleRoute(simpleRouteFirstFoo);
122
123         /**
124          * Verifies that implementation of the first provider received the same
125          * message from MD-SAL.
126          */
127         verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
128         /**
129          * Verifies that second instance was not invoked with first message
130          */
131         verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
132
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);
136
137         /**
138          * Consumer sends message to nodeTwo for three times. Should be
139          * processed by second instance.
140          */
141         final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
142         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
143         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
144         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
145
146         /**
147          * Verifies that second instance was invoked 3 times with second message
148          * and first instance wasn't invoked.
149          *
150          */
151         verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
152         verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
153
154         LOG.info("Unregistration of the path for the node one in the first provider");
155         firstReg.unregisterPath(TestContext.class, nodeOnePath);
156
157         LOG.info("Provider 2 registers path of node 1");
158         secondReg.registerPath(TestContext.class, nodeOnePath);
159
160         /**
161          * A consumer sends third message to node 1
162          */
163         final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
164         consumerService.routedSimpleRoute(simpleRouteThirdFoo);
165
166         /**
167          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
168          * time.
169          * TODO: fix unregister path
170          */
171         //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
172         verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
173
174     }
175
176     /**
177      * Returns node reference from string which represents path
178      *
179      * @param string
180      *            string with key(path)
181      * @return instance identifier to {@link UnorderedList}
182      */
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)
188                 .build();
189
190         return path;
191     }
192
193     /**
194      * Creates flow AddFlowInput for which only node and cookie are set
195      *
196      * @param node
197      *            NodeRef value
198      * @return simpleRouteInput instance
199      */
200     static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
201         final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();
202         ret.setRoute(node);
203         return ret.build();
204     }
205 }