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