2 * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.bulk.o.matic;
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;
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;
75 * Test for {@link SalBulkFlowServiceImpl}.
77 @RunWith(MockitoJUnitRunner.class)
78 public class SalBulkFlowServiceImplTest {
81 private DataBroker mockDataBroker;
83 private SalFlowService mockSalFlowService;
85 private WriteTransaction writeTransaction;
87 private ReadTransaction readOnlyTransaction;
89 private Nodes mockNodes;
91 private Node mockNode;
93 private ArgumentCaptor<Flow> flowArgumentCaptor;
95 private SalBulkFlowServiceImpl salBulkFlowService;
99 when(mockDataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
100 when(mockDataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
102 lenient().doReturn(FluentFutures.immediateFluentFuture(Optional.of(mockNode))).when(readOnlyTransaction)
103 .read(any(LogicalDatastoreType.class), any());
104 salBulkFlowService = new SalBulkFlowServiceImpl(mockSalFlowService, mockDataBroker);
108 public void testAddRemoveFlowsDs() {
109 doReturn(CommitInfo.emptyFluentFuture()).when(writeTransaction).commit();
111 final BulkFlowDsItemBuilder bulkFlowDsItemBuilder = new BulkFlowDsItemBuilder().setFlowId(new FlowId("1"))
112 .setTableId((short) 2);
114 final InstanceIdentifier<Node> nodeId = BulkOMaticUtils.getFlowCapableNodeId("1");
115 bulkFlowDsItemBuilder.setNode(new NodeRef(nodeId));
116 final BulkFlowDsItem bulkFlowDsItem = bulkFlowDsItemBuilder.build();
118 final List<BulkFlowDsItem> bulkFlowDsItems = new ArrayList<>();
119 bulkFlowDsItems.add(bulkFlowDsItem);
121 final AddFlowsDsInputBuilder addFlowsDsInputBuilder = new AddFlowsDsInputBuilder();
122 addFlowsDsInputBuilder.setBulkFlowDsItem(bulkFlowDsItems);
124 final AddFlowsDsInput addFlowsDsInput = addFlowsDsInputBuilder.build();
125 salBulkFlowService.addFlowsDs(addFlowsDsInput);
127 verify(writeTransaction).commit();
128 verify(writeTransaction).mergeParentStructurePut(ArgumentMatchers.any(), ArgumentMatchers.any(),
129 flowArgumentCaptor.capture());
131 Flow flow = flowArgumentCaptor.getValue();
132 Assert.assertEquals("1", flow.getId().getValue());
133 Assert.assertEquals((short) 2, flow.getTableId().shortValue());
135 final RemoveFlowsDsInputBuilder removeFlowsDsInputBuilder = new RemoveFlowsDsInputBuilder();
136 removeFlowsDsInputBuilder.setBulkFlowDsItem(bulkFlowDsItems);
138 final RemoveFlowsDsInput removeFlowsDsInput = removeFlowsDsInputBuilder.build();
140 salBulkFlowService.removeFlowsDs(removeFlowsDsInput);
141 verify(writeTransaction).delete(ArgumentMatchers.any(),
142 ArgumentMatchers.<InstanceIdentifier<Flow>>any());
143 verify(writeTransaction, times(2)).commit();
147 public void testAddRemoveFlowsRpc() {
148 Mockito.when(mockSalFlowService.addFlow(ArgumentMatchers.any()))
149 .thenReturn(RpcResultBuilder.success(new AddFlowOutputBuilder().build()).buildFuture());
151 Mockito.when(mockSalFlowService.removeFlow(ArgumentMatchers.any()))
152 .thenReturn(RpcResultBuilder.success(new RemoveFlowOutputBuilder().build()).buildFuture());
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();
159 final List<BulkFlowItem> bulkFlowItems = new ArrayList<>();
160 bulkFlowItems.add(bulkFlowItem);
162 final AddFlowsRpcInputBuilder addFlowsRpcInputBuilder = new AddFlowsRpcInputBuilder();
163 addFlowsRpcInputBuilder.setBulkFlowItem(bulkFlowItems);
165 final AddFlowsRpcInput addFlowsRpcInput = addFlowsRpcInputBuilder.build();
166 salBulkFlowService.addFlowsRpc(addFlowsRpcInput);
168 verify(mockSalFlowService).addFlow(ArgumentMatchers.any());
170 final RemoveFlowsRpcInputBuilder removeFlowsRpcInputBuilder = new RemoveFlowsRpcInputBuilder();
171 removeFlowsRpcInputBuilder.setBulkFlowItem(bulkFlowItems);
173 final RemoveFlowsRpcInput removeFlowsRpcInput = removeFlowsRpcInputBuilder.build();
174 salBulkFlowService.removeFlowsRpc(removeFlowsRpcInput);
176 verify(mockSalFlowService).removeFlow(ArgumentMatchers.any());
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);
184 final ReadFlowTestInput readFlowTestInput = readFlowTestInputBuilder.build();
185 final ListenableFuture<RpcResult<ReadFlowTestOutput>> resultFuture
186 = salBulkFlowService.readFlowTest(readFlowTestInput);
188 Assert.assertTrue(resultFuture.get().isSuccessful());
192 public void testFlowRpcAddTest() throws Exception {
193 doReturn(FluentFutures.immediateFluentFuture(Optional.of(mockNodes))).when(readOnlyTransaction)
194 .read(any(LogicalDatastoreType.class), any());
196 final FlowRpcAddTestInputBuilder flowRpcAddTestInputBuilder = new FlowRpcAddTestInputBuilder().setFlowCount(1L)
197 .setDpnId("1").setRpcBatchSize(1L);
199 final FlowRpcAddTestInput flowRpcAddTestInput = flowRpcAddTestInputBuilder.build();
200 final ListenableFuture<RpcResult<FlowRpcAddTestOutput>> resultFuture
201 = salBulkFlowService.flowRpcAddTest(flowRpcAddTestInput);
203 Assert.assertTrue(resultFuture.get().isSuccessful());
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);
212 FlowTestInput flowTestInput = flowTestInputBuilder.build();
214 Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
216 flowTestInputBuilder.setIsAdd(false);
217 flowTestInput = flowTestInputBuilder.build();
219 Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
221 flowTestInputBuilder.setTxChain(false);
222 flowTestInput = flowTestInputBuilder.build();
224 Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
226 flowTestInputBuilder.setIsAdd(true);
227 flowTestInput = flowTestInputBuilder.build();
229 Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
231 flowTestInputBuilder.setSeq(false);
232 flowTestInput = flowTestInputBuilder.build();
234 Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
236 flowTestInputBuilder.setIsAdd(false);
237 flowTestInput = flowTestInputBuilder.build();
239 Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
243 public void testFlowRpcAddMultiple() throws Exception {
244 doReturn(FluentFutures.immediateFluentFuture(Optional.of(mockNodes))).when(readOnlyTransaction)
245 .read(any(LogicalDatastoreType.class), any());
247 final FlowRpcAddMultipleInputBuilder flowRpcAddMultipleInputBuilder = new FlowRpcAddMultipleInputBuilder()
248 .setFlowCount(1L).setRpcBatchSize(1L);
250 final FlowRpcAddMultipleInput flowRpcAddMultipleInput = flowRpcAddMultipleInputBuilder.build();
252 Assert.assertTrue(salBulkFlowService.flowRpcAddMultiple(flowRpcAddMultipleInput).get().isSuccessful());
256 public void testTableTest() throws Exception {
257 final TableTestInputBuilder tableTestInputBuilder = new TableTestInputBuilder().setStartTableId(0L)
258 .setEndTableId(99L).setDpnCount(1L).setOperation(Operation.Add);
260 TableTestInput tableTestInput = tableTestInputBuilder.build();
262 Assert.assertTrue(salBulkFlowService.tableTest(tableTestInput).get().isSuccessful());
264 tableTestInputBuilder.setOperation(Operation.Delete);
265 tableTestInput = tableTestInputBuilder.build();
267 Assert.assertTrue(salBulkFlowService.tableTest(tableTestInput).get().isSuccessful());