Adjust for Binding RPC codegen changes
[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
97          * MD-SAL
98          */
99         final RoutedSimpleRouteInput simpleRouteFirstFoo = createSimpleRouteInput(nodeOnePath);
100         consumerService.routedSimpleRoute(simpleRouteFirstFoo);
101
102         /**
103          * Verifies that implementation of the first provider received the same
104          * message from MD-SAL.
105          */
106         verify(odlRoutedService1).routedSimpleRoute(simpleRouteFirstFoo);
107         /**
108          * Verifies that second instance was not invoked with first message
109          */
110         verify(odlRoutedService2, times(0)).routedSimpleRoute(simpleRouteFirstFoo);
111
112         LOG.info("Provider 2 registers path of node 2");
113         final InstanceIdentifier<UnorderedList> nodeTwo = createNodeRef("foo:node:2");
114         secondReg.registerPath(TestContext.class, nodeTwo);
115
116         /**
117          * Consumer sends message to nodeTwo for three times. Should be
118          * processed by second instance.
119          */
120         final RoutedSimpleRouteInput simpleRouteSecondFoo = createSimpleRouteInput(nodeTwo);
121         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
122         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
123         consumerService.routedSimpleRoute(simpleRouteSecondFoo);
124
125         /**
126          * Verifies that second instance was invoked 3 times with second message
127          * and first instance wasn't invoked.
128          *
129          */
130         verify(odlRoutedService2, times(3)).routedSimpleRoute(simpleRouteSecondFoo);
131         verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteSecondFoo);
132
133         LOG.info("Unregistration of the path for the node one in the first provider");
134         firstReg.unregisterPath(TestContext.class, nodeOnePath);
135
136         LOG.info("Provider 2 registers path of node 1");
137         secondReg.registerPath(TestContext.class, nodeOnePath);
138
139         /**
140          * A consumer sends third message to node 1
141          */
142         final RoutedSimpleRouteInput simpleRouteThirdFoo = createSimpleRouteInput(nodeOnePath);
143         consumerService.routedSimpleRoute(simpleRouteThirdFoo);
144
145         /**
146          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
147          * time.
148          * TODO: fix unregister path
149          */
150         //verify(odlRoutedService1, times(0)).routedSimpleRoute(simpleRouteThirdFoo);
151         verify(odlRoutedService2).routedSimpleRoute(simpleRouteThirdFoo);
152
153     }
154
155     /**
156      * Returns node reference from string which represents path
157      *
158      * @param string
159      *            string with key(path)
160      * @return instance identifier to {@link UnorderedList}
161      */
162     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
163         final UnorderedListKey key = new UnorderedListKey(string);
164         final InstanceIdentifier<UnorderedList> path = InstanceIdentifier.builder(Lists.class)
165                 .child(UnorderedContainer.class)
166                 .child(UnorderedList.class, key)
167                 .build();
168
169         return path;
170     }
171
172     /**
173      * Creates flow AddFlowInput for which only node and cookie are set
174      *
175      * @param node
176      *            NodeRef value
177      * @return simpleRouteInput instance
178      */
179     static RoutedSimpleRouteInput createSimpleRouteInput(final InstanceIdentifier<UnorderedList> node) {
180         final RoutedSimpleRouteInputBuilder ret = new RoutedSimpleRouteInputBuilder();
181         ret.setRoute(node);
182         return ret.build();
183     }
184 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.