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