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