OPNFLWPLUG-1010 Adopt mdsal changes proposed through weather item TSC-99
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / SyncPlanPushStrategyIncrementalImplTest.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.frsync.impl.strategy;
10
11 import com.google.common.collect.Lists;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.LinkedHashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.Future;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.InOrder;
26 import org.mockito.Matchers;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.runners.MockitoJUnitRunner;
30 import org.mockito.stubbing.Answer;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.openflowplugin.applications.frsync.impl.DSInputFactory;
33 import org.opendaylight.openflowplugin.applications.frsync.util.ItemSyncBox;
34 import org.opendaylight.openflowplugin.applications.frsync.util.SyncCrudCounters;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
64
65 /**
66  * Test for {@link SyncPlanPushStrategyIncrementalImpl}.
67  */
68 @RunWith(MockitoJUnitRunner.class)
69 public class SyncPlanPushStrategyIncrementalImplTest {
70
71     private static final NodeId NODE_ID = new NodeId("unit-nodeId");
72     private static final InstanceIdentifier<FlowCapableNode> NODE_IDENT = InstanceIdentifier.create(Nodes.class)
73             .child(Node.class, new NodeKey(NODE_ID)).augmentation(FlowCapableNode.class);
74
75     private SyncPlanPushStrategyIncrementalImpl syncPlanPushStrategy;
76
77     @Mock
78     private DataBroker db;
79     @Mock
80     private GroupForwarder groupCommitter;
81     @Mock
82     private FlowForwarder flowCommitter;
83     @Mock
84     private MeterForwarder meterCommitter;
85     @Mock
86     private TableForwarder tableCommitter;
87     @Mock
88     private FlowCapableTransactionService flowCapableTxService;
89
90     @Captor
91     private ArgumentCaptor<Group> groupCaptor;
92     @Captor
93     private ArgumentCaptor<Group> groupUpdateCaptor;
94     @Captor
95     private ArgumentCaptor<Flow> flowCaptor;
96     @Captor
97     private ArgumentCaptor<Flow> flowUpdateCaptor;
98     @Captor
99     private ArgumentCaptor<Meter> meterCaptor;
100     @Captor
101     private ArgumentCaptor<Meter> meterUpdateCaptor;
102     @Captor
103     private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
104
105     private SyncCrudCounters counters;
106
107     private final List<ItemSyncBox<Group>> groupsToAddOrUpdate;
108     private final List<ItemSyncBox<Group>> groupsToRemove;
109     private final ItemSyncBox<Meter> metersToAddOrUpdate;
110     private final ItemSyncBox<Meter> metersToRemove;
111     private final Map<TableKey, ItemSyncBox<Flow>> flowsToAddOrUpdate;
112     private final Map<TableKey, ItemSyncBox<Flow>> flowsToRemove;
113
114     public SyncPlanPushStrategyIncrementalImplTest() {
115         groupsToAddOrUpdate = Lists.newArrayList(DiffInputFactory.createGroupSyncBox(1, 2, 3),
116                 DiffInputFactory.createGroupSyncBoxWithUpdates(4, 5, 6));
117         groupsToRemove = Lists.newArrayList(DiffInputFactory.createGroupSyncBox(1, 2, 3),
118                 DiffInputFactory.createGroupSyncBox(4, 5, 6));
119
120         metersToAddOrUpdate = DiffInputFactory.createMeterSyncBoxWithUpdates(1, 2, 3);
121         metersToRemove = DiffInputFactory.createMeterSyncBox(1, 2, 3);
122
123         flowsToAddOrUpdate = new HashMap<>();
124         flowsToAddOrUpdate.put(new TableKey((short) 0), DiffInputFactory.createFlowSyncBox("1", "2", "3"));
125         flowsToAddOrUpdate.put(new TableKey((short) 1), DiffInputFactory.createFlowSyncBoxWithUpdates("4", "5", "6"));
126         flowsToRemove = new HashMap<>();
127         flowsToRemove.put(new TableKey((short) 0), DiffInputFactory.createFlowSyncBox("1", "2", "3"));
128         flowsToRemove.put(new TableKey((short) 1), DiffInputFactory.createFlowSyncBox("4", "5", "6"));
129     }
130
131     @Test
132     public void testExecuteSyncStrategy() throws Exception {
133         final SynchronizationDiffInput diffInput = new SynchronizationDiffInput(NODE_IDENT,
134                 groupsToAddOrUpdate, metersToAddOrUpdate, flowsToAddOrUpdate, flowsToRemove,
135                 metersToRemove, groupsToRemove);
136
137         final SyncCrudCounters syncCounters = new SyncCrudCounters();
138         final ListenableFuture<RpcResult<Void>> rpcResult = syncPlanPushStrategy.executeSyncStrategy(
139                 RpcResultBuilder.<Void>success().buildFuture(), diffInput, syncCounters);
140
141         Mockito.verify(groupCommitter, Mockito.times(6)).add(Matchers.<InstanceIdentifier<Group>>any(),
142                 Matchers.<Group>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
143         Mockito.verify(groupCommitter, Mockito.times(3)).update(Matchers.<InstanceIdentifier<Group>>any(),
144                 Matchers.<Group>any(), Matchers.<Group>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
145         Mockito.verify(groupCommitter, Mockito.times(6)).remove(Matchers.<InstanceIdentifier<Group>>any(),
146                 Matchers.<Group>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
147         Mockito.verify(flowCommitter, Mockito.times(6)).add(Matchers.<InstanceIdentifier<Flow>>any(),
148                 Matchers.<Flow>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
149         Mockito.verify(flowCommitter, Mockito.times(3)).update(Matchers.<InstanceIdentifier<Flow>>any(),
150                 Matchers.<Flow>any(), Matchers.<Flow>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
151         Mockito.verify(flowCommitter, Mockito.times(6)).remove(Matchers.<InstanceIdentifier<Flow>>any(),
152                 Matchers.<Flow>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
153         Mockito.verify(meterCommitter, Mockito.times(3)).add(Matchers.<InstanceIdentifier<Meter>>any(),
154                 Matchers.<Meter>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
155         Mockito.verify(meterCommitter, Mockito.times(3)).update(Matchers.<InstanceIdentifier<Meter>>any(),
156                 Matchers.<Meter>any(), Matchers.<Meter>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
157         Mockito.verify(meterCommitter, Mockito.times(3)).remove(Matchers.<InstanceIdentifier<Meter>>any(),
158                 Matchers.<Meter>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
159
160         Assert.assertTrue(rpcResult.isDone());
161         Assert.assertTrue(rpcResult.get().isSuccessful());
162
163         Assert.assertEquals(6, syncCounters.getFlowCrudCounts().getAdded());
164         Assert.assertEquals(3, syncCounters.getFlowCrudCounts().getUpdated());
165         Assert.assertEquals(6, syncCounters.getFlowCrudCounts().getRemoved());
166
167         Assert.assertEquals(6, syncCounters.getGroupCrudCounts().getAdded());
168         Assert.assertEquals(3, syncCounters.getGroupCrudCounts().getUpdated());
169         Assert.assertEquals(6, syncCounters.getGroupCrudCounts().getRemoved());
170
171         Assert.assertEquals(3, syncCounters.getMeterCrudCounts().getAdded());
172         Assert.assertEquals(3, syncCounters.getMeterCrudCounts().getUpdated());
173         Assert.assertEquals(3, syncCounters.getMeterCrudCounts().getRemoved());
174     }
175
176     @Before
177     public void setUp() throws Exception {
178         Mockito.when(flowCapableTxService.sendBarrier(Matchers.<SendBarrierInput>any()))
179                 .thenReturn(RpcResultBuilder.success((SendBarrierOutput) null).buildFuture());
180
181         Mockito.doAnswer(createSalServiceFutureAnswer()).when(groupCommitter).add(
182                 Matchers.<InstanceIdentifier<Group>>any(), Matchers.<Group>any(),
183                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
184         Mockito.doAnswer(createSalServiceFutureAnswer()).when(groupCommitter).update(
185                 Matchers.<InstanceIdentifier<Group>>any(), Matchers.<Group>any(), Matchers.<Group>any(),
186                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
187         Mockito.doAnswer(createSalServiceFutureAnswer()).when(groupCommitter).remove(
188                 Matchers.<InstanceIdentifier<Group>>any(), Matchers.<Group>any(),
189                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
190
191         Mockito.doAnswer(createSalServiceFutureAnswer()).when(flowCommitter).add(
192                 Matchers.<InstanceIdentifier<Flow>>any(), Matchers.<Flow>any(),
193                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
194         Mockito.doAnswer(createSalServiceFutureAnswer()).when(flowCommitter).update(
195                 Matchers.<InstanceIdentifier<Flow>>any(), Matchers.<Flow>any(), Matchers.<Flow>any(),
196                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
197         Mockito.doAnswer(createSalServiceFutureAnswer()).when(flowCommitter).remove(
198                 Matchers.<InstanceIdentifier<Flow>>any(), Matchers.<Flow>any(),
199                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
200
201         Mockito.doAnswer(createSalServiceFutureAnswer()).when(meterCommitter).add(
202                 Matchers.<InstanceIdentifier<Meter>>any(), Matchers.<Meter>any(),
203                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
204         Mockito.doAnswer(createSalServiceFutureAnswer()).when(meterCommitter).update(
205                 Matchers.<InstanceIdentifier<Meter>>any(), Matchers.<Meter>any(), Matchers.<Meter>any(),
206                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
207         Mockito.doAnswer(createSalServiceFutureAnswer()).when(meterCommitter).remove(
208                 Matchers.<InstanceIdentifier<Meter>>any(), Matchers.<Meter>any(),
209                 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
210
211         Mockito.doAnswer(createSalServiceFutureAnswer()).when(tableCommitter).update(
212                 Matchers.<InstanceIdentifier<TableFeatures>>any(), Matchers.<TableFeatures>any(),
213                 Matchers.<TableFeatures>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
214
215         syncPlanPushStrategy = new SyncPlanPushStrategyIncrementalImpl()
216                 .setMeterForwarder(meterCommitter)
217                 .setTableForwarder(tableCommitter)
218                 .setGroupForwarder(groupCommitter)
219                 .setFlowForwarder(flowCommitter)
220                 .setTransactionService(flowCapableTxService);
221
222         counters = new SyncCrudCounters();
223     }
224
225     private <O> Answer<Future<RpcResult<O>>> createSalServiceFutureAnswer() {
226         return invocation -> RpcResultBuilder.<O>success().buildFuture();
227     }
228
229     @Test
230     public void testAddMissingFlows() throws Exception {
231         Mockito.when(flowCommitter.add(Matchers.<InstanceIdentifier<Flow>>any(), flowCaptor.capture(),
232                 Matchers.same(NODE_IDENT)))
233                 .thenReturn(RpcResultBuilder.success(new AddFlowOutputBuilder().build()).buildFuture());
234
235         final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
236         flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
237         flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
238
239         final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
240         flowBoxMap.put(new TableKey((short) 0), flowBox);
241
242         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingFlows(
243                 NODE_ID, NODE_IDENT, flowBoxMap, counters);
244
245         Assert.assertTrue(result.isDone());
246         Assert.assertTrue(result.get().isSuccessful());
247
248         final List<Flow> flowCaptorAllValues = flowCaptor.getAllValues();
249         Assert.assertEquals(2, flowCaptorAllValues.size());
250         Assert.assertEquals("f3", flowCaptorAllValues.get(0).getId().getValue());
251         Assert.assertEquals("f4", flowCaptorAllValues.get(1).getId().getValue());
252
253         final InOrder inOrderFlow = Mockito.inOrder(flowCapableTxService, flowCommitter);
254         inOrderFlow.verify(flowCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Flow>>any(),
255                 Matchers.<Flow>any(), Matchers.eq(NODE_IDENT));
256         //TODO: uncomment when enabled in impl
257 //        inOrderFlow.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
258         inOrderFlow.verifyNoMoreInteractions();
259     }
260
261     @Test
262     public void testRemoveRedundantFlows() throws Exception {
263         Mockito.when(flowCommitter.remove(Matchers.<InstanceIdentifier<Flow>>any(), flowCaptor.capture(),
264                 Matchers.same(NODE_IDENT)))
265                 .thenReturn(RpcResultBuilder.success(new RemoveFlowOutputBuilder().build()).buildFuture());
266
267         final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
268         flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
269         flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
270
271         final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
272         flowBoxMap.put(new TableKey((short) 0), flowBox);
273
274         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantFlows(
275                 NODE_ID, NODE_IDENT, flowBoxMap, counters);
276
277         Assert.assertTrue(result.isDone());
278         Assert.assertTrue(result.get().isSuccessful());
279
280         final List<Flow> flowCaptorAllValues = flowCaptor.getAllValues();
281         Assert.assertEquals(2, flowCaptorAllValues.size());
282         Assert.assertEquals("f3", flowCaptorAllValues.get(0).getId().getValue());
283         Assert.assertEquals("f4", flowCaptorAllValues.get(1).getId().getValue());
284
285         final InOrder inOrderFlow = Mockito.inOrder(flowCapableTxService, flowCommitter);
286         inOrderFlow.verify(flowCommitter, Mockito.times(2)).remove(Matchers.<InstanceIdentifier<Flow>>any(),
287                 Matchers.<Flow>any(), Matchers.eq(NODE_IDENT));
288         inOrderFlow.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
289         inOrderFlow.verifyNoMoreInteractions();
290     }
291
292
293     @Test
294     public void testAddMissingFlows_withUpdate() throws Exception {
295         Mockito.when(flowCommitter.add(Matchers.<InstanceIdentifier<Flow>>any(), flowCaptor.capture(),
296                 Matchers.same(NODE_IDENT)))
297                 .thenReturn(RpcResultBuilder.success(new AddFlowOutputBuilder().build()).buildFuture());
298
299         Mockito.when(flowCommitter.update(Matchers.<InstanceIdentifier<Flow>>any(),
300                 flowUpdateCaptor.capture(), flowUpdateCaptor.capture(),
301                 Matchers.same(NODE_IDENT)))
302                 .thenReturn(RpcResultBuilder.success(new UpdateFlowOutputBuilder().build()).buildFuture());
303
304         final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
305         flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
306         flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
307         flowBox.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(
308                 DSInputFactory.createFlow("f1", 1), DSInputFactory.createFlowWithInstruction("f1", 1)));
309
310         final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
311         flowBoxMap.put(new TableKey((short) 0), flowBox);
312
313
314         //TODO: replace null
315         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingFlows(
316                 NODE_ID, NODE_IDENT, flowBoxMap, counters);
317
318         Assert.assertTrue(result.isDone());
319         Assert.assertTrue(result.get().isSuccessful());
320
321         final List<Flow> flowCaptorAllValues = flowCaptor.getAllValues();
322         Assert.assertEquals(2, flowCaptorAllValues.size());
323         Assert.assertEquals("f3", flowCaptorAllValues.get(0).getId().getValue());
324         Assert.assertEquals("f4", flowCaptorAllValues.get(1).getId().getValue());
325
326         final List<Flow> flowUpdateCaptorAllValues = flowUpdateCaptor.getAllValues();
327         Assert.assertEquals(2, flowUpdateCaptorAllValues.size());
328         Assert.assertEquals("f1", flowUpdateCaptorAllValues.get(0).getId().getValue());
329         Assert.assertEquals("f1", flowUpdateCaptorAllValues.get(1).getId().getValue());
330
331         final InOrder inOrderFlow = Mockito.inOrder(flowCapableTxService, flowCommitter);
332         // add f3, f4
333         inOrderFlow.verify(flowCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Flow>>any(),
334                 Matchers.<Flow>any(), Matchers.eq(NODE_IDENT));
335         // update f1
336         inOrderFlow.verify(flowCommitter).update(Matchers.<InstanceIdentifier<Flow>>any(),
337                 Matchers.<Flow>any(), Matchers.<Flow>any(), Matchers.eq(NODE_IDENT));
338         //TODO: uncomment when enabled in impl
339 //        inOrderFlow.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
340
341         inOrderFlow.verifyNoMoreInteractions();
342     }
343
344     @Test
345     public void testAddMissingMeters() throws Exception {
346         Mockito.when(meterCommitter.add(Matchers.<InstanceIdentifier<Meter>>any(), meterCaptor.capture(),
347                 Matchers.same(NODE_IDENT)))
348                 .thenReturn(RpcResultBuilder.success(new AddMeterOutputBuilder().build()).buildFuture());
349
350         final ItemSyncBox<Meter> meterSyncBox = new ItemSyncBox<>();
351         meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(2L));
352         meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(4L));
353
354         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingMeters(
355                 NODE_ID, NODE_IDENT, meterSyncBox, counters);
356
357         Assert.assertTrue(result.isDone());
358         Assert.assertTrue(result.get().isSuccessful());
359
360         final List<Meter> metercaptorAllValues = meterCaptor.getAllValues();
361         Assert.assertEquals(2, metercaptorAllValues.size());
362         Assert.assertEquals(2L, metercaptorAllValues.get(0).getMeterId().getValue().longValue());
363         Assert.assertEquals(4L, metercaptorAllValues.get(1).getMeterId().getValue().longValue());
364
365         final InOrder inOrderMeter = Mockito.inOrder(flowCapableTxService, meterCommitter);
366         inOrderMeter.verify(meterCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Meter>>any(),
367                 Matchers.<Meter>any(), Matchers.eq(NODE_IDENT));
368         //TODO: uncomment when enabled in impl
369 //        inOrderMeter.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
370         inOrderMeter.verifyNoMoreInteractions();
371     }
372
373     @Test
374     public void testAddMissingMeters_withUpdate() throws Exception {
375         Mockito.when(meterCommitter.add(Matchers.<InstanceIdentifier<Meter>>any(), meterCaptor.capture(),
376                 Matchers.same(NODE_IDENT)))
377                 .thenReturn(RpcResultBuilder.success(new AddMeterOutputBuilder().build()).buildFuture());
378
379         Mockito.when(meterCommitter.update(Matchers.<InstanceIdentifier<Meter>>any(),
380                 meterUpdateCaptor.capture(), meterUpdateCaptor.capture(), Matchers.same(NODE_IDENT)))
381                 .thenReturn(RpcResultBuilder.success(new UpdateMeterOutputBuilder().build()).buildFuture());
382
383         final ItemSyncBox<Meter> meterSyncBox = new ItemSyncBox<>();
384         meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(2L));
385         meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(4L));
386         meterSyncBox.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(
387                 DSInputFactory.createMeter(1L), DSInputFactory.createMeterWithBody(1L)));
388
389         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingMeters(
390                 NODE_ID, NODE_IDENT, meterSyncBox, counters);
391
392         Assert.assertTrue(result.isDone());
393         Assert.assertTrue(result.get().isSuccessful());
394
395         final List<Meter> meterCaptorAllValues = meterCaptor.getAllValues();
396         Assert.assertEquals(2, meterCaptorAllValues.size());
397         Assert.assertEquals(2L, meterCaptorAllValues.get(0).getMeterId().getValue().longValue());
398         Assert.assertEquals(4L, meterCaptorAllValues.get(1).getMeterId().getValue().longValue());
399
400
401         final List<Meter> meterUpdateCaptorAllValues = meterUpdateCaptor.getAllValues();
402         Assert.assertEquals(2, meterUpdateCaptorAllValues.size());
403         Assert.assertEquals(1L, meterUpdateCaptorAllValues.get(0).getMeterId().getValue().longValue());
404         Assert.assertEquals(1L, meterUpdateCaptorAllValues.get(1).getMeterId().getValue().longValue());
405
406         final InOrder inOrderMeters = Mockito.inOrder(flowCapableTxService, meterCommitter);
407         inOrderMeters.verify(meterCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Meter>>any(),
408                 Matchers.<Meter>any(), Matchers.eq(NODE_IDENT));
409         inOrderMeters.verify(meterCommitter).update(Matchers.<InstanceIdentifier<Meter>>any(),
410                 Matchers.<Meter>any(), Matchers.<Meter>any(), Matchers.eq(NODE_IDENT));
411         //TODO: uncomment when enabled in impl
412 //        inOrderMeters.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
413
414         inOrderMeters.verifyNoMoreInteractions();
415     }
416
417     @Test
418     public void testRemoveRedundantMeters() throws Exception {
419         Mockito.when(meterCommitter.remove(Matchers.<InstanceIdentifier<Meter>>any(), meterCaptor.capture(),
420                 Matchers.same(NODE_IDENT)))
421                 .thenReturn(RpcResultBuilder.success(new RemoveMeterOutputBuilder().build()).buildFuture());
422
423         final ItemSyncBox<Meter> meterSyncBox = new ItemSyncBox<>();
424         meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(2L));
425         meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(4L));
426         meterSyncBox.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(
427                 DSInputFactory.createMeter(1L), DSInputFactory.createMeterWithBody(1L)));
428
429         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantMeters(
430                 NODE_ID, NODE_IDENT, meterSyncBox, counters);
431
432         Assert.assertTrue(result.isDone());
433         Assert.assertTrue(result.get().isSuccessful());
434
435         final List<Meter> metercaptorAllValues = meterCaptor.getAllValues();
436         Assert.assertEquals(2, metercaptorAllValues.size());
437         Assert.assertEquals(2L, metercaptorAllValues.get(0).getMeterId().getValue().longValue());
438         Assert.assertEquals(4L, metercaptorAllValues.get(1).getMeterId().getValue().longValue());
439
440         final InOrder inOrderMeter = Mockito.inOrder(flowCapableTxService, meterCommitter);
441         inOrderMeter.verify(meterCommitter, Mockito.times(2)).remove(Matchers.<InstanceIdentifier<Meter>>any(),
442                 Matchers.<Meter>any(), Matchers.eq(NODE_IDENT));
443         //TODO: uncomment when enabled in impl
444 //        inOrderMeter.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
445         inOrderMeter.verifyNoMoreInteractions();
446     }
447
448     @Test
449     public void testAddMissingGroups() throws Exception {
450         Mockito.when(groupCommitter.add(Matchers.<InstanceIdentifier<Group>>any(), groupCaptor.capture(),
451                 Matchers.same(NODE_IDENT)))
452                 .thenReturn(RpcResultBuilder.success(new AddGroupOutputBuilder().build()).buildFuture());
453
454         ItemSyncBox<Group> groupBox1 = new ItemSyncBox<>();
455         groupBox1.getItemsToPush().add(DSInputFactory.createGroup(2L));
456
457         ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
458         groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
459         groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
460
461         ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
462         groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
463
464         final List<ItemSyncBox<Group>> groupBoxLot = Lists.newArrayList(groupBox1, groupBox2, groupBox3);
465
466         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingGroups(
467                 NODE_ID, NODE_IDENT, groupBoxLot, counters);
468
469         Assert.assertTrue(result.isDone());
470         Assert.assertTrue(result.get().isSuccessful());
471
472         final List<Group> groupCaptorAllValues = groupCaptor.getAllValues();
473         Assert.assertEquals(4, groupCaptorAllValues.size());
474         Assert.assertEquals(2L, groupCaptorAllValues.get(0).getGroupId().getValue().longValue());
475         Assert.assertEquals(3L, groupCaptorAllValues.get(1).getGroupId().getValue().longValue());
476         Assert.assertEquals(4L, groupCaptorAllValues.get(2).getGroupId().getValue().longValue());
477         Assert.assertEquals(5L, groupCaptorAllValues.get(3).getGroupId().getValue().longValue());
478
479         final InOrder inOrderGroups = Mockito.inOrder(flowCapableTxService, groupCommitter);
480         // add 2
481         inOrderGroups.verify(groupCommitter).add(Matchers.<InstanceIdentifier<Group>>any(),
482                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
483         inOrderGroups.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
484         // add 3, 4
485         inOrderGroups.verify(groupCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Group>>any(),
486                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
487         inOrderGroups.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
488         // add 5
489         inOrderGroups.verify(groupCommitter).add(Matchers.<InstanceIdentifier<Group>>any(),
490                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
491         inOrderGroups.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
492
493         inOrderGroups.verifyNoMoreInteractions();
494     }
495
496     @Test
497     public void testAddMissingGroups_withUpdate() throws Exception {
498         Mockito.when(groupCommitter.add(Matchers.<InstanceIdentifier<Group>>any(), groupCaptor.capture(),
499                 Matchers.same(NODE_IDENT)))
500                 .thenReturn(RpcResultBuilder.success(new AddGroupOutputBuilder().build()).buildFuture());
501
502         Mockito.when(groupCommitter.update(Matchers.<InstanceIdentifier<Group>>any(),
503                 groupUpdateCaptor.capture(), groupUpdateCaptor.capture(),
504                 Matchers.same(NODE_IDENT)))
505                 .thenReturn(RpcResultBuilder.success(new UpdateGroupOutputBuilder().build()).buildFuture());
506
507         ItemSyncBox<Group> groupBox1 = new ItemSyncBox<>();
508         groupBox1.getItemsToPush().add(DSInputFactory.createGroup(2L));
509         groupBox1.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(
510                 DSInputFactory.createGroup(1L), DSInputFactory.createGroupWithAction(1L)));
511
512         ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
513         groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
514         groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
515
516         ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
517         groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
518
519         final List<ItemSyncBox<Group>> groupBoxLot = Lists.newArrayList(groupBox1, groupBox2, groupBox3);
520         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingGroups(
521                 NODE_ID, NODE_IDENT, groupBoxLot, counters);
522
523         Assert.assertTrue(result.isDone());
524         Assert.assertTrue(result.get().isSuccessful());
525
526         final List<Group> groupCaptorAllValues = groupCaptor.getAllValues();
527         Assert.assertEquals(4, groupCaptorAllValues.size());
528         Assert.assertEquals(2L, groupCaptorAllValues.get(0).getGroupId().getValue().longValue());
529         Assert.assertEquals(3L, groupCaptorAllValues.get(1).getGroupId().getValue().longValue());
530         Assert.assertEquals(4L, groupCaptorAllValues.get(2).getGroupId().getValue().longValue());
531         Assert.assertEquals(5L, groupCaptorAllValues.get(3).getGroupId().getValue().longValue());
532
533         final List<Group> groupUpdateCaptorAllValues = groupUpdateCaptor.getAllValues();
534         Assert.assertEquals(2, groupUpdateCaptorAllValues.size());
535         Assert.assertEquals(1L, groupUpdateCaptorAllValues.get(0).getGroupId().getValue().longValue());
536         Assert.assertEquals(1L, groupUpdateCaptorAllValues.get(1).getGroupId().getValue().longValue());
537
538         final InOrder inOrderGroups = Mockito.inOrder(flowCapableTxService, groupCommitter);
539
540         // add 2, update 1
541         inOrderGroups.verify(groupCommitter).add(Matchers.<InstanceIdentifier<Group>>any(),
542                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
543         inOrderGroups.verify(groupCommitter).update(Matchers.<InstanceIdentifier<Group>>any(),
544                 Matchers.<Group>any(), Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
545         inOrderGroups.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
546
547         // add 3, 4
548         inOrderGroups.verify(groupCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Group>>any(),
549                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
550         inOrderGroups.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
551         // add 5
552         inOrderGroups.verify(groupCommitter).add(Matchers.<InstanceIdentifier<Group>>any(),
553                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
554         inOrderGroups.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
555
556         inOrderGroups.verifyNoMoreInteractions();
557     }
558
559     @Test
560     public void testRemoveRedundantGroups() throws Exception {
561         Mockito.when(groupCommitter.remove(Matchers.<InstanceIdentifier<Group>>any(), groupCaptor.capture(),
562                 Matchers.same(NODE_IDENT)))
563                 .thenReturn(RpcResultBuilder.success(new RemoveGroupOutputBuilder().build()).buildFuture());
564
565         ItemSyncBox<Group> groupBox1 = new ItemSyncBox<>();
566         groupBox1.getItemsToPush().add(DSInputFactory.createGroup(2L));
567         groupBox1.getItemsToUpdate().add(new ItemSyncBox.ItemUpdateTuple<>(
568                 DSInputFactory.createGroup(1L), DSInputFactory.createGroupWithAction(1L)));
569
570         ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
571         groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
572         groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
573
574         ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
575         groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
576
577         final List<ItemSyncBox<Group>> groupBoxLot = Lists.newArrayList(groupBox1, groupBox2, groupBox3);
578         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantGroups(
579                 NODE_ID, NODE_IDENT, groupBoxLot, counters);
580
581         Assert.assertTrue(result.isDone());
582         Assert.assertTrue(result.get().isSuccessful());
583
584         final List<Group> groupCaptorAllValues = groupCaptor.getAllValues();
585         Assert.assertEquals(4, groupCaptorAllValues.size());
586         Assert.assertEquals(5L, groupCaptorAllValues.get(0).getGroupId().getValue().longValue());
587         Assert.assertEquals(3L, groupCaptorAllValues.get(1).getGroupId().getValue().longValue());
588         Assert.assertEquals(4L, groupCaptorAllValues.get(2).getGroupId().getValue().longValue());
589         Assert.assertEquals(2L, groupCaptorAllValues.get(3).getGroupId().getValue().longValue());
590
591         final InOrder inOrderGroup = Mockito.inOrder(flowCapableTxService, groupCommitter);
592         // remove 5
593         inOrderGroup.verify(groupCommitter).remove(Matchers.<InstanceIdentifier<Group>>any(),
594                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
595         inOrderGroup.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
596         // remove 3, 4
597         inOrderGroup.verify(groupCommitter, Mockito.times(2)).remove(Matchers.<InstanceIdentifier<Group>>any(),
598                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
599         inOrderGroup.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
600         // remove 2
601         inOrderGroup.verify(groupCommitter).remove(Matchers.<InstanceIdentifier<Group>>any(),
602                 Matchers.<Group>any(), Matchers.eq(NODE_IDENT));
603         inOrderGroup.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
604
605         inOrderGroup.verifyNoMoreInteractions();
606     }
607
608     @Test
609     public void testUpdateTableFeatures() throws Exception {
610         Mockito.when(tableCommitter.update(Matchers.<InstanceIdentifier<TableFeatures>>any(),
611                 Matchers.isNull(TableFeatures.class), tableFeaturesCaptor.capture(),
612                 Matchers.same(NODE_IDENT)))
613                 .thenReturn(RpcResultBuilder.success(new UpdateTableOutputBuilder().build()).buildFuture());
614
615         final FlowCapableNode operational = new FlowCapableNodeBuilder()
616                 .setTable(Collections.singletonList(new TableBuilder()
617                         .setId((short) 1)
618                         .build()))
619                 .setTableFeatures(Collections.singletonList(new TableFeaturesBuilder()
620                         .setName("test table features")
621                         .setTableId((short) 1)
622                         .build()))
623                 .build();
624
625         final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.updateTableFeatures(
626                 NODE_IDENT, operational);
627
628         Assert.assertTrue(result.isDone());
629         Assert.assertTrue(result.get().isSuccessful());
630
631         final List<TableFeatures> groupCaptorAllValues = tableFeaturesCaptor.getAllValues();
632         //TODO: uncomment when enabled in impl
633 //        Assert.assertEquals(1, groupCaptorAllValues.size());
634 //        Assert.assertEquals("test table features", groupCaptorAllValues.get(0).getName());
635 //        Assert.assertEquals(1, groupCaptorAllValues.get(0).getTableId().intValue());
636
637         Mockito.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());
638     }
639 }