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