Fix checkstyle/spotbugs violations
[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
17 import com.google.common.util.concurrent.Futures;
18 import javax.inject.Inject;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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.RoutedSimpleRouteOutput;
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.ops4j.pax.exam.util.Filter;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Covers routed rpc creation, registration, invocation, unregistration.
41  */
42 public class RoutedServiceIT extends AbstractIT {
43
44     private static final Logger LOG = LoggerFactory
45             .getLogger(RoutedServiceIT.class);
46
47     protected OpendaylightTestRoutedRpcService odlRoutedService1;
48     protected OpendaylightTestRoutedRpcService odlRoutedService2;
49
50     @Inject
51     @Filter(timeout = 120 * 1000)
52     RpcProviderRegistry rpcProviderRegistry;
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<RoutedSimpleRouteOutput>>immediateFuture(null));
63         Mockito.when(odlRoutedService2.routedSimpleRoute(Mockito.<RoutedSimpleRouteInput>any()))
64             .thenReturn(Futures.<RpcResult<RoutedSimpleRouteOutput>>immediateFuture(null));
65     }
66
67     @Test
68     public void testServiceRegistration() {
69         LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
70
71         RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg = rpcProviderRegistry
72                 .addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
73         assertNotNull("Registration should not be null", firstReg);
74         assertSame(odlRoutedService1, firstReg.getInstance());
75
76         LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
77
78         RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg = rpcProviderRegistry
79                 .addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
80         assertNotNull("Registration should not be null", firstReg);
81         assertSame(odlRoutedService2, secondReg.getInstance());
82         assertNotSame(secondReg, firstReg);
83
84         OpendaylightTestRoutedRpcService consumerService =
85                 rpcProviderRegistry.getRpcService(OpendaylightTestRoutedRpcService.class);
86         assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
87         assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1,
88                 consumerService);
89
90         final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
91
92         LOG.info("Provider 1 registers path of node 1");
93         firstReg.registerPath(TestContext.class, nodeOnePath);
94
95         /**
96          * Consumer creates addFlow message for node one and sends it to the MD-SAL.
97          */
98         final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
99         consumerService.routedSimpleRoute(simpleRouteFirstFoo);
100
101         /**
102          * Verifies that implementation of the first provider received the same message from MD-SAL.
103          */
104         verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
105         /**
106          * Verifies that second instance was not invoked with first message
107          */
108         verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
109
110         LOG.info("Provider 2 registers path of node 2");
111         final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
112         secondReg.registerPath(TestContext.class, nodeTwo);
113
114         /**
115          * Consumer sends message to nodeTwo for three times. Should be processed by second instance.
116          */
117         final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
118         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
119         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
120         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
121
122         /**
123          * Verifies that second instance was invoked 3 times with second message and first instance wasn't invoked.
124          */
125         verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
126         verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
127
128         LOG.info("Unregistration of the path for the node one in the first provider");
129         firstReg.unregisterPath(TestContext.class, nodeOnePath);
130
131         LOG.info("Provider 2 registers path of node 1");
132         secondReg.registerPath(TestContext.class, nodeOnePath);
133
134         /**
135          * A consumer sends third message to node 1.
136          */
137         final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
138         consumerService.routedSimpleRoute(simpleRouteThirdFoo);
139
140         /**
141          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1 time.
142          * TODO: fix unregister path
143          */
144         //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
145         verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
146     }
147
148     /**
149      * Returns node reference from string which represents path.
150      *
151      * @param string string with key(path)
152      * @return instance identifier to {@link UnorderedList}
153      */
154     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
155         return InstanceIdentifier.builder(Lists.class)
156                 .child(UnorderedContainer.class)
157                 .child(UnorderedList.class, new UnorderedListKey(string))
158                 .build();
159     }
160
161     /**
162      * Creates flow AddFlowInput for which only node and cookie are set.
163      *
164      * @param node NodeRef value
165      * @return simpleRouteInput instance
166      */
167     static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
168         final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();
169         ret.setRoute(node);
170         return ret.build();
171     }
172 }