66a12edf340d0d46425b995e1642b4a5b718ad11
[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.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.ops4j.pax.exam.util.Filter;
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     @Inject
50     @Filter(timeout = 120 * 1000)
51     RpcProviderRegistry rpcProviderRegistry;
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         LOG.info("Register provider 1 with first implementation of routeSimpleService - service1");
69
70         RoutedRpcRegistration<OpendaylightTestRoutedRpcService> firstReg = rpcProviderRegistry
71                 .addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService1);
72         assertNotNull("Registration should not be null", firstReg);
73         assertSame(odlRoutedService1, firstReg.getInstance());
74
75         LOG.info("Register provider 2 with second implementation of routeSimpleService - service2");
76
77         RoutedRpcRegistration<OpendaylightTestRoutedRpcService> secondReg = rpcProviderRegistry
78                 .addRoutedRpcImplementation(OpendaylightTestRoutedRpcService.class, odlRoutedService2);
79         assertNotNull("Registration should not be null", firstReg);
80         assertSame(odlRoutedService2, secondReg.getInstance());
81         assertNotSame(secondReg, firstReg);
82
83         OpendaylightTestRoutedRpcService consumerService =
84                 rpcProviderRegistry.getRpcService(OpendaylightTestRoutedRpcService.class);
85         assertNotNull("MD-SAL instance of test Service should be returned", consumerService);
86         assertNotSame("Provider instance and consumer instance should not be same.", odlRoutedService1,
87                 consumerService);
88
89         final InstanceIdentifier<UnorderedList> nodeOnePath = createNodeRef("foo:node:1");
90
91         LOG.info("Provider 1 registers path of node 1");
92         firstReg.registerPath(TestContext.class, nodeOnePath);
93
94         /**
95          * Consumer creates addFlow message for node one and sends it to the
96          * 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
103          * message from MD-SAL.
104          */
105         verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
106         /**
107          * Verifies that second instance was not invoked with first message
108          */
109         verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
110
111         LOG.info("Provider 2 registers path of node 2");
112         final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
113         secondReg.registerPath(TestContext.class, nodeTwo);
114
115         /**
116          * Consumer sends message to nodeTwo for three times. Should be
117          * processed by second instance.
118          */
119         final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
120         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
121         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
122         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
123
124         /**
125          * Verifies that second instance was invoked 3 times with second message
126          * and first instance wasn't invoked.
127          *
128          */
129         verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
130         verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
131
132         LOG.info("Unregistration of the path for the node one in the first provider");
133         firstReg.unregisterPath(TestContext.class, nodeOnePath);
134
135         LOG.info("Provider 2 registers path of node 1");
136         secondReg.registerPath(TestContext.class, nodeOnePath);
137
138         /**
139          * A consumer sends third message to node 1
140          */
141         final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
142         consumerService.routedSimpleRoute(simpleRouteThirdFoo);
143
144         /**
145          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
146          * time.
147          * TODO: fix unregister path
148          */
149         //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
150         verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
151
152     }
153
154     /**
155      * Returns node reference from string which represents path
156      *
157      * @param string
158      *            string with key(path)
159      * @return instance identifier to {@link UnorderedList}
160      */
161     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
162         final UnorderedListKey key = new UnorderedListKey(string);
163         final InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
164                 .child(UnorderedContainer.class)
165                 .child(UnorderedList.class, key)
166                 .build();
167
168         return path;
169     }
170
171     /**
172      * Creates flow AddFlowInput for which only node and cookie are set
173      *
174      * @param node
175      *            NodeRef value
176      * @return simpleRouteInput instance
177      */
178     static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
179         final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();
180         ret.setRoute(node);
181         return ret.build();
182     }
183 }