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

©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.