Update MRI projects for Aluminium
[openflowplugin.git] / applications / bulk-o-matic / src / test / java / org / opendaylight / openflowplugin / applications / bulk / o / matic / SalBulkFlowServiceImplTest.java
1 /*
2  * Copyright (c) 2016, 2017 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.openflowplugin.applications.bulk.o.matic;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.lenient;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Optional;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.ArgumentMatchers;
27 import org.mockito.Captor;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.mdsal.binding.api.ReadTransaction;
33 import org.opendaylight.mdsal.binding.api.WriteTransaction;
34 import org.opendaylight.mdsal.common.api.CommitInfo;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.AddFlowsDsInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.AddFlowsDsInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.AddFlowsRpcInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.AddFlowsRpcInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowRpcAddMultipleInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowRpcAddMultipleInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowRpcAddTestInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowRpcAddTestInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowRpcAddTestOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowTestInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.FlowTestInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.ReadFlowTestInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.ReadFlowTestInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.ReadFlowTestOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.RemoveFlowsDsInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.RemoveFlowsDsInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.RemoveFlowsRpcInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.RemoveFlowsRpcInputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.TableTestInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.TableTestInput.Operation;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.TableTestInputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.bulk.flow.ds.list.grouping.BulkFlowDsItem;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.bulk.flow.ds.list.grouping.BulkFlowDsItemBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.bulk.flow.list.grouping.BulkFlowItem;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.bulk.flow.service.rev150608.bulk.flow.list.grouping.BulkFlowItemBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
69 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
72 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
73
74 /**
75  * Test for {@link SalBulkFlowServiceImpl}.
76  */
77 @RunWith(MockitoJUnitRunner.class)
78 public class SalBulkFlowServiceImplTest {
79
80     @Mock
81     private DataBroker mockDataBroker;
82     @Mock
83     private SalFlowService mockSalFlowService;
84     @Mock
85     private WriteTransaction writeTransaction;
86     @Mock
87     private ReadTransaction readOnlyTransaction;
88     @Mock
89     private Nodes mockNodes;
90     @Mock
91     private Node mockNode;
92     @Captor
93     private ArgumentCaptor<Flow> flowArgumentCaptor;
94
95     private SalBulkFlowServiceImpl salBulkFlowService;
96
97     @Before
98     public void setUp() {
99         when(mockDataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
100         when(mockDataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
101
102         lenient().doReturn(FluentFutures.immediateFluentFuture(Optional.of(mockNode))).when(readOnlyTransaction)
103             .read(any(LogicalDatastoreType.class), any());
104         salBulkFlowService = new SalBulkFlowServiceImpl(mockSalFlowService, mockDataBroker);
105     }
106
107     @Test
108     public void testAddRemoveFlowsDs() {
109         doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
110
111         final BulkFlowDsItemBuilder bulkFlowDsItemBuilder = new BulkFlowDsItemBuilder().setFlowId(new FlowId("1"))
112                 .setTableId((short) 2);
113
114         final InstanceIdentifier<Node> nodeId = BulkOMaticUtils.getFlowCapableNodeId("1");
115         bulkFlowDsItemBuilder.setNode(new NodeRef(nodeId));
116         final BulkFlowDsItem bulkFlowDsItem = bulkFlowDsItemBuilder.build();
117
118         final List<BulkFlowDsItem> bulkFlowDsItems = new ArrayList<>();
119         bulkFlowDsItems.add(bulkFlowDsItem);
120
121         final AddFlowsDsInputBuilder addFlowsDsInputBuilder = new AddFlowsDsInputBuilder();
122         addFlowsDsInputBuilder.setBulkFlowDsItem(bulkFlowDsItems);
123
124         final AddFlowsDsInput addFlowsDsInput = addFlowsDsInputBuilder.build();
125         salBulkFlowService.addFlowsDs(addFlowsDsInput);
126
127         verify(writeTransaction).commit();
128         verify(writeTransaction).mergeParentStructurePut(ArgumentMatchers.any(), ArgumentMatchers.any(),
129                 flowArgumentCaptor.capture());
130
131         Flow flow = flowArgumentCaptor.getValue();
132         Assert.assertEquals("1", flow.getId().getValue());
133         Assert.assertEquals((short) 2, flow.getTableId().shortValue());
134
135         final RemoveFlowsDsInputBuilder removeFlowsDsInputBuilder = new RemoveFlowsDsInputBuilder();
136         removeFlowsDsInputBuilder.setBulkFlowDsItem(bulkFlowDsItems);
137
138         final RemoveFlowsDsInput removeFlowsDsInput = removeFlowsDsInputBuilder.build();
139
140         salBulkFlowService.removeFlowsDs(removeFlowsDsInput);
141         verify(writeTransaction).delete(ArgumentMatchers.any(),
142                 ArgumentMatchers.<InstanceIdentifier<Flow>>any());
143         verify(writeTransaction, times(2)).commit();
144     }
145
146     @Test
147     public void testAddRemoveFlowsRpc() {
148         Mockito.when(mockSalFlowService.addFlow(ArgumentMatchers.any()))
149                 .thenReturn(RpcResultBuilder.success(new AddFlowOutputBuilder().build()).buildFuture());
150
151         Mockito.when(mockSalFlowService.removeFlow(ArgumentMatchers.any()))
152                 .thenReturn(RpcResultBuilder.success(new RemoveFlowOutputBuilder().build()).buildFuture());
153
154         final BulkFlowItemBuilder bulkFlowItemBuilder = new BulkFlowItemBuilder();
155         final InstanceIdentifier<Node> nodeId = BulkOMaticUtils.getFlowCapableNodeId("1");
156         bulkFlowItemBuilder.setNode(new NodeRef(nodeId));
157         final BulkFlowItem bulkFlowItem = bulkFlowItemBuilder.build();
158
159         final List<BulkFlowItem> bulkFlowItems = new ArrayList<>();
160         bulkFlowItems.add(bulkFlowItem);
161
162         final AddFlowsRpcInputBuilder addFlowsRpcInputBuilder = new AddFlowsRpcInputBuilder();
163         addFlowsRpcInputBuilder.setBulkFlowItem(bulkFlowItems);
164
165         final AddFlowsRpcInput addFlowsRpcInput = addFlowsRpcInputBuilder.build();
166         salBulkFlowService.addFlowsRpc(addFlowsRpcInput);
167
168         verify(mockSalFlowService).addFlow(ArgumentMatchers.any());
169
170         final RemoveFlowsRpcInputBuilder removeFlowsRpcInputBuilder = new RemoveFlowsRpcInputBuilder();
171         removeFlowsRpcInputBuilder.setBulkFlowItem(bulkFlowItems);
172
173         final RemoveFlowsRpcInput removeFlowsRpcInput = removeFlowsRpcInputBuilder.build();
174         salBulkFlowService.removeFlowsRpc(removeFlowsRpcInput);
175
176         verify(mockSalFlowService).removeFlow(ArgumentMatchers.any());
177     }
178
179     @Test
180     public void testReadFlowTest() throws Exception {
181         final ReadFlowTestInputBuilder readFlowTestInputBuilder = new ReadFlowTestInputBuilder().setDpnCount(1L)
182                 .setStartTableId(1L).setEndTableId(2L).setIsConfigDs(false).setFlowsPerDpn(1L).setVerbose(true);
183
184         final ReadFlowTestInput readFlowTestInput = readFlowTestInputBuilder.build();
185         final ListenableFuture<RpcResult<ReadFlowTestOutput>> resultFuture
186                 = salBulkFlowService.readFlowTest(readFlowTestInput);
187
188         Assert.assertTrue(resultFuture.get().isSuccessful());
189     }
190
191     @Test
192     public void testFlowRpcAddTest() throws Exception {
193         doReturn(FluentFutures.immediateFluentFuture(Optional.of(mockNodes))).when(readOnlyTransaction)
194             .read(any(LogicalDatastoreType.class), any());
195
196         final FlowRpcAddTestInputBuilder flowRpcAddTestInputBuilder = new FlowRpcAddTestInputBuilder().setFlowCount(1L)
197                 .setDpnId("1").setRpcBatchSize(1L);
198
199         final FlowRpcAddTestInput flowRpcAddTestInput = flowRpcAddTestInputBuilder.build();
200         final ListenableFuture<RpcResult<FlowRpcAddTestOutput>> resultFuture
201                 = salBulkFlowService.flowRpcAddTest(flowRpcAddTestInput);
202
203         Assert.assertTrue(resultFuture.get().isSuccessful());
204     }
205
206     @Test
207     public void testFlowTest() throws Exception {
208         final FlowTestInputBuilder flowTestInputBuilder = new FlowTestInputBuilder().setBatchSize(1L).setDpnCount(1L)
209                 .setEndTableId(2L).setFlowsPerDpn(1L).setIsAdd(true).setSeq(true).setSleepAfter(20L).setSleepFor(1L)
210                 .setStartTableId(1L).setTxChain(true).setCreateParents(true);
211
212         FlowTestInput flowTestInput = flowTestInputBuilder.build();
213
214         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
215
216         flowTestInputBuilder.setIsAdd(false);
217         flowTestInput = flowTestInputBuilder.build();
218
219         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
220
221         flowTestInputBuilder.setTxChain(false);
222         flowTestInput = flowTestInputBuilder.build();
223
224         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
225
226         flowTestInputBuilder.setIsAdd(true);
227         flowTestInput = flowTestInputBuilder.build();
228
229         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
230
231         flowTestInputBuilder.setSeq(false);
232         flowTestInput = flowTestInputBuilder.build();
233
234         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
235
236         flowTestInputBuilder.setIsAdd(false);
237         flowTestInput = flowTestInputBuilder.build();
238
239         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
240     }
241
242     @Test
243     public void testFlowRpcAddMultiple() throws Exception {
244         doReturn(FluentFutures.immediateFluentFuture(Optional.of(mockNodes))).when(readOnlyTransaction)
245             .read(any(LogicalDatastoreType.class), any());
246
247         final FlowRpcAddMultipleInputBuilder flowRpcAddMultipleInputBuilder = new FlowRpcAddMultipleInputBuilder()
248                 .setFlowCount(1L).setRpcBatchSize(1L);
249
250         final FlowRpcAddMultipleInput flowRpcAddMultipleInput = flowRpcAddMultipleInputBuilder.build();
251
252         Assert.assertTrue(salBulkFlowService.flowRpcAddMultiple(flowRpcAddMultipleInput).get().isSuccessful());
253     }
254
255     @Test
256     public void testTableTest() throws Exception {
257         final TableTestInputBuilder tableTestInputBuilder = new TableTestInputBuilder().setStartTableId(0L)
258                 .setEndTableId(99L).setDpnCount(1L).setOperation(Operation.Add);
259
260         TableTestInput tableTestInput = tableTestInputBuilder.build();
261
262         Assert.assertTrue(salBulkFlowService.tableTest(tableTestInput).get().isSuccessful());
263
264         tableTestInputBuilder.setOperation(Operation.Delete);
265         tableTestInput = tableTestInputBuilder.build();
266
267         Assert.assertTrue(salBulkFlowService.tableTest(tableTestInput).get().isSuccessful());
268     }
269 }