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