BUG-8607: Fix issues in checkstyle enforcement for module bulk-o-matic
[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
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 writeTransaction;
87     @Mock
88     private ReadOnlyTransaction readOnlyTransaction;
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(writeTransaction);
101         when(mockDataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
102         when(readOnlyTransaction.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(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
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).submit();
128         verify(writeTransaction).put(Matchers.<LogicalDatastoreType>any(), Matchers.<InstanceIdentifier<Flow>>any(),
129                 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(writeTransaction).delete(Matchers.<LogicalDatastoreType>any(), Matchers.<InstanceIdentifier<Flow>>any());
142         verify(writeTransaction, 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().setDpnCount(1L)
181                 .setStartTableId(1L).setEndTableId(2L).setIsConfigDs(false).setFlowsPerDpn(1L).setVerbose(true);
182
183         final ReadFlowTestInput readFlowTestInput = readFlowTestInputBuilder.build();
184         final Future<RpcResult<Void>> resultFuture = salBulkFlowService.readFlowTest(readFlowTestInput);
185
186         Assert.assertTrue(resultFuture.get().isSuccessful());
187     }
188
189     @Test
190     public void testFlowRpcAddTest() throws Exception {
191         when(readOnlyTransaction.read(Mockito.any(LogicalDatastoreType.class),
192                 Mockito.<InstanceIdentifier<Nodes>>any()))
193                         .thenReturn(Futures.immediateCheckedFuture(Optional.of(mockNodes)));
194
195         final FlowRpcAddTestInputBuilder flowRpcAddTestInputBuilder = new FlowRpcAddTestInputBuilder().setFlowCount(1L)
196                 .setDpnId("1").setRpcBatchSize(1L);
197
198         final FlowRpcAddTestInput flowRpcAddTestInput = flowRpcAddTestInputBuilder.build();
199         final Future<RpcResult<Void>> resultFuture = salBulkFlowService.flowRpcAddTest(flowRpcAddTestInput);
200
201         Assert.assertTrue(resultFuture.get().isSuccessful());
202     }
203
204     @Test
205     public void testFlowTest() throws Exception {
206         final FlowTestInputBuilder flowTestInputBuilder = new FlowTestInputBuilder().setBatchSize(1L).setDpnCount(1L)
207                 .setEndTableId(2L).setFlowsPerDpn(1L).setIsAdd(true).setSeq(true).setSleepAfter(20L).setSleepFor(1L)
208                 .setStartTableId(1L).setTxChain(true).setCreateParents(true);
209
210         FlowTestInput flowTestInput = flowTestInputBuilder.build();
211
212         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
213
214         flowTestInputBuilder.setIsAdd(false);
215         flowTestInput = flowTestInputBuilder.build();
216
217         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
218
219         flowTestInputBuilder.setTxChain(false);
220         flowTestInput = flowTestInputBuilder.build();
221
222         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
223
224         flowTestInputBuilder.setIsAdd(true);
225         flowTestInput = flowTestInputBuilder.build();
226
227         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
228
229         flowTestInputBuilder.setSeq(false);
230         flowTestInput = flowTestInputBuilder.build();
231
232         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
233
234         flowTestInputBuilder.setIsAdd(false);
235         flowTestInput = flowTestInputBuilder.build();
236
237         Assert.assertTrue(salBulkFlowService.flowTest(flowTestInput).get().isSuccessful());
238     }
239
240     @Test
241     public void testFlowRpcAddMultiple() throws Exception {
242         when(readOnlyTransaction.read(Mockito.any(LogicalDatastoreType.class),
243                 Mockito.<InstanceIdentifier<Nodes>>any()))
244                         .thenReturn(Futures.immediateCheckedFuture(Optional.of(mockNodes)));
245
246         final FlowRpcAddMultipleInputBuilder flowRpcAddMultipleInputBuilder = new FlowRpcAddMultipleInputBuilder()
247                 .setFlowCount(1L).setRpcBatchSize(1L);
248
249         final FlowRpcAddMultipleInput flowRpcAddMultipleInput = flowRpcAddMultipleInputBuilder.build();
250
251         Assert.assertTrue(salBulkFlowService.flowRpcAddMultiple(flowRpcAddMultipleInput).get().isSuccessful());
252     }
253
254     @Test
255     public void testTableTest() throws Exception {
256         final TableTestInputBuilder tableTestInputBuilder = new TableTestInputBuilder().setStartTableId(0L)
257                 .setEndTableId(99L).setDpnCount(1L).setOperation(Operation.Add);
258
259         TableTestInput tableTestInput = tableTestInputBuilder.build();
260
261         Assert.assertTrue(salBulkFlowService.tableTest(tableTestInput).get().isSuccessful());
262
263         tableTestInputBuilder.setOperation(Operation.Delete);
264         tableTestInput = tableTestInputBuilder.build();
265
266         Assert.assertTrue(salBulkFlowService.tableTest(tableTestInput).get().isSuccessful());
267     }
268 }