Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalFlowsBatchServiceImplTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.impl.services;
10
11 import com.google.common.collect.Lists;
12 import java.util.List;
13 import java.util.concurrent.Future;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.InOrder;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
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.AddFlowOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.AddFlowsBatchOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.RemoveFlowsBatchOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.UpdateFlowsBatchOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.add.flows.batch.input.BatchAddFlows;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.add.flows.batch.input.BatchAddFlowsBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.input.update.grouping.OriginalBatchedFlowBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.input.update.grouping.UpdatedBatchedFlowBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.remove.flows.batch.input.BatchRemoveFlows;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.remove.flows.batch.input.BatchRemoveFlowsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.update.flows.batch.input.BatchUpdateFlows;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.update.flows.batch.input.BatchUpdateFlowsBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
63 import org.opendaylight.yangtools.yang.common.RpcError;
64 import org.opendaylight.yangtools.yang.common.RpcResult;
65 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 /**
70  * Test for {@link SalFlowsBatchServiceImpl}.
71  */
72 @RunWith(MockitoJUnitRunner.class)
73 public class SalFlowsBatchServiceImplTest {
74
75     private static final Logger LOG = LoggerFactory.getLogger(SalFlowsBatchServiceImplTest.class);
76
77     public static final NodeId NODE_ID = new NodeId("ut-dummy-node");
78     public static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
79     public static final NodeRef NODE_REF = new NodeRef(InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY));
80
81     @Mock
82     private SalFlowService salFlowService;
83     @Mock
84     private FlowCapableTransactionService transactionService;
85     @Captor
86     private ArgumentCaptor<RemoveFlowInput> removeFlowInputCpt;
87     @Captor
88     private ArgumentCaptor<UpdateFlowInput> updateFlowInputCpt;
89     @Captor
90     private ArgumentCaptor<AddFlowInput> addFlowInputCpt;
91
92     private SalFlowsBatchServiceImpl salFlowsBatchService;
93     public static final String FLOW_ID_VALUE_1 = "ut-dummy-flow1";
94     public static final String FLOW_ID_VALUE_2 = "ut-dummy-flow2";
95
96     @Before
97     public void setUp() throws Exception {
98         salFlowsBatchService = new SalFlowsBatchServiceImpl(salFlowService, transactionService);
99
100         Mockito.when(transactionService.sendBarrier(Matchers.<SendBarrierInput>any()))
101                 .thenReturn(RpcResultBuilder.<Void>success().buildFuture());
102     }
103
104     @After
105     public void tearDown() throws Exception {
106         Mockito.verifyNoMoreInteractions(salFlowService, transactionService);
107     }
108
109     @Test
110     public void testRemoveFlowsBatch_success() throws Exception {
111         Mockito.when(salFlowService.removeFlow(Matchers.<RemoveFlowInput>any()))
112                 .thenReturn(RpcResultBuilder.success(new RemoveFlowOutputBuilder().build())
113                         .buildFuture());
114
115         final String flow1IdValue = "ut-dummy-flow1";
116         final String flow2IdValue = "ut-dummy-flow2";
117         final BatchRemoveFlows batchFlow1 = createEmptyBatchRemoveFlow(flow1IdValue, 42);
118         final BatchRemoveFlows batchFlow2 = createEmptyBatchRemoveFlow(flow2IdValue, 43);
119
120         final RemoveFlowsBatchInput input = new RemoveFlowsBatchInputBuilder()
121                 .setNode(NODE_REF)
122                 .setBarrierAfter(true)
123                 .setBatchRemoveFlows(Lists.newArrayList(batchFlow1, batchFlow2))
124                 .build();
125
126         final Future<RpcResult<RemoveFlowsBatchOutput>> resultFuture = salFlowsBatchService.removeFlowsBatch(input);
127
128         Assert.assertTrue(resultFuture.isDone());
129         final RpcResult<RemoveFlowsBatchOutput> rpcResult = resultFuture.get();
130         Assert.assertTrue(rpcResult.isSuccessful());
131         final RemoveFlowsBatchOutput result = rpcResult.getResult();
132         Assert.assertEquals(0, result.getBatchFailedFlowsOutput().size());
133
134         final InOrder inOrder = Mockito.inOrder(salFlowService, transactionService);
135
136         inOrder.verify(salFlowService, Mockito.times(2)).removeFlow(removeFlowInputCpt.capture());
137         final List<RemoveFlowInput> allValues = removeFlowInputCpt.getAllValues();
138         Assert.assertEquals(2, allValues.size());
139         Assert.assertEquals(42, allValues.get(0).getPriority().longValue());
140         Assert.assertEquals(43, allValues.get(1).getPriority().longValue());
141
142         inOrder.verify(transactionService).sendBarrier(Matchers.<SendBarrierInput>any());
143     }
144
145     @Test
146     public void testRemoveFlowsBatch_failed() throws Exception {
147         Mockito.when(salFlowService.removeFlow(Matchers.<RemoveFlowInput>any()))
148                 .thenReturn(RpcResultBuilder.<RemoveFlowOutput>failed()
149                         .withError(RpcError.ErrorType.APPLICATION, "flow-remove-fail-1")
150                         .buildFuture());
151
152         final BatchRemoveFlows batchFlow1 = createEmptyBatchRemoveFlow(FLOW_ID_VALUE_1, 42);
153         final BatchRemoveFlows batchFlow2 = createEmptyBatchRemoveFlow(FLOW_ID_VALUE_2, 43);
154
155         final RemoveFlowsBatchInput input = new RemoveFlowsBatchInputBuilder()
156                 .setNode(NODE_REF)
157                 .setBarrierAfter(true)
158                 .setBatchRemoveFlows(Lists.newArrayList(batchFlow1, batchFlow2))
159                 .build();
160
161         final Future<RpcResult<RemoveFlowsBatchOutput>> resultFuture = salFlowsBatchService.removeFlowsBatch(input);
162
163         Assert.assertTrue(resultFuture.isDone());
164         final RpcResult<RemoveFlowsBatchOutput> rpcResult = resultFuture.get();
165         Assert.assertFalse(rpcResult.isSuccessful());
166         final RemoveFlowsBatchOutput result = rpcResult.getResult();
167         Assert.assertEquals(2, result.getBatchFailedFlowsOutput().size());
168         Assert.assertEquals(FLOW_ID_VALUE_1, result.getBatchFailedFlowsOutput().get(0).getFlowId().getValue());
169         Assert.assertEquals(FLOW_ID_VALUE_2, result.getBatchFailedFlowsOutput().get(1).getFlowId().getValue());
170
171         final InOrder inOrder = Mockito.inOrder(salFlowService, transactionService);
172
173         inOrder.verify(salFlowService, Mockito.times(2)).removeFlow(removeFlowInputCpt.capture());
174         final List<RemoveFlowInput> allValues = removeFlowInputCpt.getAllValues();
175         Assert.assertEquals(2, allValues.size());
176         Assert.assertEquals(42, allValues.get(0).getPriority().longValue());
177         Assert.assertEquals(43, allValues.get(1).getPriority().longValue());
178
179         inOrder.verify(transactionService).sendBarrier(Matchers.<SendBarrierInput>any());
180     }
181
182     private static BatchAddFlows createEmptyBatchAddFlow(final String flowIdValue, final int priority) {
183         return new BatchAddFlowsBuilder()
184                 .setFlowId(new FlowId(flowIdValue))
185                 .setPriority(priority)
186                 .setMatch(new MatchBuilder().build())
187                 .setTableId((short) 0)
188                 .build();
189     }
190
191     private static BatchRemoveFlows createEmptyBatchRemoveFlow(final String flowIdValue, final int priority) {
192         return new BatchRemoveFlowsBuilder()
193                 .setFlowId(new FlowId(flowIdValue))
194                 .setPriority(priority)
195                 .setMatch(new MatchBuilder().build())
196                 .setTableId((short) 0)
197                 .build();
198     }
199
200     private static BatchUpdateFlows createEmptyBatchUpdateFlow(final String flowIdValue, final int priority) {
201         final BatchAddFlows emptyOriginalFlow = createEmptyBatchAddFlow(flowIdValue, priority);
202         final BatchAddFlows emptyUpdatedFlow = createEmptyBatchAddFlow(flowIdValue, priority + 1);
203         return new BatchUpdateFlowsBuilder()
204                 .setFlowId(new FlowId(flowIdValue))
205                 .setOriginalBatchedFlow(new OriginalBatchedFlowBuilder(emptyOriginalFlow).build())
206                 .setUpdatedBatchedFlow(new UpdatedBatchedFlowBuilder(emptyUpdatedFlow).build())
207                 .build();
208     }
209
210     @Test
211     public void testAddFlowsBatch_success() throws Exception {
212         Mockito.when(salFlowService.addFlow(Matchers.<AddFlowInput>any()))
213                 .thenReturn(RpcResultBuilder.success(new AddFlowOutputBuilder().build()).buildFuture());
214
215         final AddFlowsBatchInput input = new AddFlowsBatchInputBuilder()
216                 .setNode(NODE_REF)
217                 .setBarrierAfter(true)
218                 .setBatchAddFlows(Lists.newArrayList(
219                         createEmptyBatchAddFlow("ut-dummy-flow1", 42),
220                         createEmptyBatchAddFlow("ut-dummy-flow2", 43)))
221                 .build();
222
223         final Future<RpcResult<AddFlowsBatchOutput>> resultFuture = salFlowsBatchService.addFlowsBatch(input);
224
225         Assert.assertTrue(resultFuture.isDone());
226         Assert.assertTrue(resultFuture.get().isSuccessful());
227
228         final InOrder inOrder = Mockito.inOrder(salFlowService, transactionService);
229
230         inOrder.verify(salFlowService, Mockito.times(2)).addFlow(addFlowInputCpt.capture());
231         final List<AddFlowInput> allValues = addFlowInputCpt.getAllValues();
232         Assert.assertEquals(2, allValues.size());
233         Assert.assertEquals(42, allValues.get(0).getPriority().longValue());
234         Assert.assertEquals(43, allValues.get(1).getPriority().longValue());
235
236         inOrder.verify(transactionService).sendBarrier(Matchers.<SendBarrierInput>any());
237     }
238
239     @Test
240     public void testAddFlowsBatch_failed() throws Exception {
241         Mockito.when(salFlowService.addFlow(Matchers.<AddFlowInput>any()))
242                 .thenReturn(RpcResultBuilder.<AddFlowOutput>failed().withError(RpcError.ErrorType.APPLICATION, "ut-groupAddError")
243                         .buildFuture());
244
245         final AddFlowsBatchInput input = new AddFlowsBatchInputBuilder()
246                 .setNode(NODE_REF)
247                 .setBarrierAfter(true)
248                 .setBatchAddFlows(Lists.newArrayList(
249                         createEmptyBatchAddFlow(FLOW_ID_VALUE_1, 42),
250                         createEmptyBatchAddFlow(FLOW_ID_VALUE_2, 43)))
251                 .build();
252
253         final Future<RpcResult<AddFlowsBatchOutput>> resultFuture = salFlowsBatchService.addFlowsBatch(input);
254
255         Assert.assertTrue(resultFuture.isDone());
256         Assert.assertFalse(resultFuture.get().isSuccessful());
257         Assert.assertEquals(2, resultFuture.get().getResult().getBatchFailedFlowsOutput().size());
258         Assert.assertEquals(FLOW_ID_VALUE_1, resultFuture.get().getResult().getBatchFailedFlowsOutput().get(0).getFlowId().getValue());
259         Assert.assertEquals(FLOW_ID_VALUE_2, resultFuture.get().getResult().getBatchFailedFlowsOutput().get(1).getFlowId().getValue());
260         Assert.assertEquals(2, resultFuture.get().getErrors().size());
261
262         final InOrder inOrder = Mockito.inOrder(salFlowService, transactionService);
263
264         inOrder.verify(salFlowService, Mockito.times(2)).addFlow(addFlowInputCpt.capture());
265         final List<AddFlowInput> allValues = addFlowInputCpt.getAllValues();
266         Assert.assertEquals(2, allValues.size());
267         Assert.assertEquals(42, allValues.get(0).getPriority().longValue());
268         Assert.assertEquals(43, allValues.get(1).getPriority().longValue());
269
270         inOrder.verify(transactionService).sendBarrier(Matchers.<SendBarrierInput>any());
271     }
272
273     @Test
274     public void testUpdateFlowsBatch_success() throws Exception {
275         Mockito.when(salFlowService.updateFlow(Matchers.<UpdateFlowInput>any()))
276                 .thenReturn(RpcResultBuilder.success(new UpdateFlowOutputBuilder().build()).buildFuture());
277
278         final UpdateFlowsBatchInput input = new UpdateFlowsBatchInputBuilder()
279                 .setNode(NODE_REF)
280                 .setBarrierAfter(true)
281                 .setBatchUpdateFlows(Lists.newArrayList(
282                         createEmptyBatchUpdateFlow(FLOW_ID_VALUE_1, 42),
283                         createEmptyBatchUpdateFlow(FLOW_ID_VALUE_2, 44)))
284                 .build();
285
286         final Future<RpcResult<UpdateFlowsBatchOutput>> resultFuture = salFlowsBatchService.updateFlowsBatch(input);
287
288         Assert.assertTrue(resultFuture.isDone());
289         Assert.assertTrue(resultFuture.get().isSuccessful());
290
291         final InOrder inOrder = Mockito.inOrder(salFlowService, transactionService);
292
293         inOrder.verify(salFlowService, Mockito.times(2)).updateFlow(updateFlowInputCpt.capture());
294         final List<UpdateFlowInput> allValues = updateFlowInputCpt.getAllValues();
295         Assert.assertEquals(2, allValues.size());
296         Assert.assertEquals(42, allValues.get(0).getOriginalFlow().getPriority().longValue());
297         Assert.assertEquals(43, allValues.get(0).getUpdatedFlow().getPriority().longValue());
298         Assert.assertEquals(44, allValues.get(1).getOriginalFlow().getPriority().longValue());
299         Assert.assertEquals(45, allValues.get(1).getUpdatedFlow().getPriority().longValue());
300
301         inOrder.verify(transactionService).sendBarrier(Matchers.<SendBarrierInput>any());
302     }
303
304     @Test
305     public void testUpdateFlowsBatch_failure() throws Exception {
306         Mockito.when(salFlowService.updateFlow(Matchers.<UpdateFlowInput>any()))
307                 .thenReturn(RpcResultBuilder.<UpdateFlowOutput>failed().withError(RpcError.ErrorType.APPLICATION, "ut-flowUpdateError")
308                         .buildFuture());
309
310         final UpdateFlowsBatchInput input = new UpdateFlowsBatchInputBuilder()
311                 .setNode(NODE_REF)
312                 .setBarrierAfter(true)
313                 .setBatchUpdateFlows(Lists.newArrayList(
314                         createEmptyBatchUpdateFlow(FLOW_ID_VALUE_1, 42),
315                         createEmptyBatchUpdateFlow(FLOW_ID_VALUE_2, 44)))
316                 .build();
317
318         final Future<RpcResult<UpdateFlowsBatchOutput>> resultFuture = salFlowsBatchService.updateFlowsBatch(input);
319
320         Assert.assertTrue(resultFuture.isDone());
321         Assert.assertFalse(resultFuture.get().isSuccessful());
322         Assert.assertFalse(resultFuture.get().isSuccessful());
323         Assert.assertEquals(2, resultFuture.get().getResult().getBatchFailedFlowsOutput().size());
324         Assert.assertEquals(FLOW_ID_VALUE_1, resultFuture.get().getResult().getBatchFailedFlowsOutput().get(0).getFlowId().getValue());
325         Assert.assertEquals(FLOW_ID_VALUE_2, resultFuture.get().getResult().getBatchFailedFlowsOutput().get(1).getFlowId().getValue());
326         Assert.assertEquals(2, resultFuture.get().getErrors().size());
327
328         final InOrder inOrder = Mockito.inOrder(salFlowService, transactionService);
329         inOrder.verify(salFlowService, Mockito.times(2)).updateFlow(updateFlowInputCpt.capture());
330         final List<UpdateFlowInput> allValues = updateFlowInputCpt.getAllValues();
331         Assert.assertEquals(2, allValues.size());
332         Assert.assertEquals(42, allValues.get(0).getOriginalFlow().getPriority().longValue());
333         Assert.assertEquals(43, allValues.get(0).getUpdatedFlow().getPriority().longValue());
334         Assert.assertEquals(44, allValues.get(1).getOriginalFlow().getPriority().longValue());
335         Assert.assertEquals(45, allValues.get(1).getUpdatedFlow().getPriority().longValue());
336
337         inOrder.verify(transactionService).sendBarrier(Matchers.<SendBarrierInput>any());
338     }
339 }