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