Merge "Fix for bug #236 and bug #240 Have made changes in opendaylight-table-types...
[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 java.math.BigInteger;
18 import java.util.concurrent.Future;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40
41 public class RoutedServiceTest extends AbstractTest {
42
43     private SalFlowService salFlowService1;
44     private SalFlowService salFlowService2;
45
46     private SalFlowService consumerService;
47
48     private RoutedRpcRegistration<SalFlowService> firstReg;
49     private RoutedRpcRegistration<SalFlowService> secondReg;
50
51     @Before
52     public void setUp() throws Exception {
53         salFlowService1 = mock(SalFlowService.class, "First Flow Service");
54         salFlowService2 = mock(SalFlowService.class, "Second Flow Service");
55     }
56
57     @Test
58     public void testServiceRegistration() {
59
60         assertNotNull(getBroker());
61
62         BindingAwareProvider provider1 = new AbstractTestProvider() {
63
64             @Override
65             public void onSessionInitiated(ProviderContext session) {
66                 assertNotNull(session);
67                 firstReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService1);
68             }
69         };
70
71         /**
72          * Register provider 1 with first implementation of SalFlowService -
73          * service1
74          * 
75          */
76         broker.registerProvider(provider1, getBundleContext());
77         assertNotNull("Registration should not be null", firstReg);
78         assertSame(salFlowService1, firstReg.getInstance());
79
80         BindingAwareProvider provider2 = new AbstractTestProvider() {
81
82             @Override
83             public void onSessionInitiated(ProviderContext session) {
84                 assertNotNull(session);
85                 secondReg = session.addRoutedRpcImplementation(SalFlowService.class, salFlowService2);
86             }
87         };
88
89         /**
90          * Register provider 2 with first implementation of SalFlowService -
91          * service2
92          * 
93          */
94         broker.registerProvider(provider2, getBundleContext());
95         assertNotNull("Registration should not be null", firstReg);
96         assertSame(salFlowService2, secondReg.getInstance());
97         assertNotSame(secondReg, firstReg);
98
99         BindingAwareConsumer consumer = new BindingAwareConsumer() {
100             @Override
101             public void onSessionInitialized(ConsumerContext session) {
102                 consumerService = session.getRpcService(SalFlowService.class);
103             }
104         };
105         broker.registerConsumer(consumer, getBundleContext());
106
107         assertNotNull("MD-SAL instance of Flow Service should be returned", consumerService);
108         assertNotSame("Provider instance and consumer instance should not be same.", salFlowService1, consumerService);
109
110         NodeRef nodeOne = createNodeRef("foo:node:1");
111
112         /**
113          * Provider 1 registers path of node 1
114          */
115         firstReg.registerPath(NodeContext.class, nodeOne.getValue());
116
117         /**
118          * Consumer creates addFlow message for node one and sends it to the
119          * MD-SAL
120          * 
121          */
122         AddFlowInput addFlowFirstMessage = createSampleAddFlow(nodeOne, 1);
123         consumerService.addFlow(addFlowFirstMessage);
124
125         /**
126          * Verifies that implementation of the first provider received the same
127          * message from MD-SAL.
128          * 
129          */
130         verify(salFlowService1).addFlow(addFlowFirstMessage);
131
132         /**
133          * Verifies that second instance was not invoked with first message
134          * 
135          */
136         verify(salFlowService2, times(0)).addFlow(addFlowFirstMessage);
137
138         /**
139          * Provider 2 registers path of node 2
140          * 
141          */
142         NodeRef nodeTwo = createNodeRef("foo:node:2");
143         secondReg.registerPath(NodeContext.class, nodeTwo.getValue());
144
145         /**
146          * Consumer sends message to nodeTwo for three times. Should be
147          * processed by second instance.
148          */
149         AddFlowInput AddFlowSecondMessage = createSampleAddFlow(nodeTwo, 2);
150         consumerService.addFlow(AddFlowSecondMessage);
151         consumerService.addFlow(AddFlowSecondMessage);
152         consumerService.addFlow(AddFlowSecondMessage);
153
154         /**
155          * Verifies that second instance was invoked 3 times with second message
156          * and first instance wasn't invoked.
157          * 
158          */
159         verify(salFlowService2, times(3)).addFlow(AddFlowSecondMessage);
160         verify(salFlowService1, times(0)).addFlow(AddFlowSecondMessage);
161
162         /**
163          * Unregisteration of the path for the node one in the first provider
164          * 
165          */
166         firstReg.unregisterPath(NodeContext.class, nodeOne.getValue());
167
168         /**
169          * Provider 2 registers path of node 1
170          * 
171          */
172         secondReg.registerPath(NodeContext.class, nodeOne.getValue());
173
174         /**
175          * A consumer sends third message to node 1
176          * 
177          */
178         AddFlowInput AddFlowThirdMessage = createSampleAddFlow(nodeOne, 3);
179         consumerService.addFlow(AddFlowThirdMessage);
180
181         /**
182          * Verifies that provider 1 wasn't invoked and provider 2 was invoked 1
183          * time.
184          */
185         verify(salFlowService1, times(0)).addFlow(AddFlowThirdMessage);
186         verify(salFlowService2).addFlow(AddFlowThirdMessage);
187
188     }
189
190     /**
191      * Returns node reference from string which represents path
192      * 
193      * @param string
194      *            string with key(path)
195      * @return instance of the type NodeRef
196      */
197     private static NodeRef createNodeRef(String string) {
198         NodeKey key = new NodeKey(new NodeId(string));
199         InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
200                 .toInstance();
201
202         return new NodeRef(path);
203     }
204
205     /**
206      * Creates flow AddFlowInput for which only node and cookie are set
207      * 
208      * @param node
209      *            NodeRef value
210      * @param cookie
211      *            integer with cookie value
212      * @return AddFlowInput instance
213      */
214     static AddFlowInput createSampleAddFlow(NodeRef node, int cookie) {
215         AddFlowInputBuilder ret = new AddFlowInputBuilder();
216         ret.setNode(node);
217         ret.setCookie(BigInteger.valueOf(cookie));
218         return ret.build();
219     }
220 }