2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.applications.frsync.impl.strategy;
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;
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;
66 * Test for {@link SyncPlanPushStrategyIncrementalImpl}.
68 @RunWith(MockitoJUnitRunner.class)
69 public class SyncPlanPushStrategyIncrementalImplTest {
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);
75 private SyncPlanPushStrategyIncrementalImpl syncPlanPushStrategy;
78 private DataBroker db;
80 private GroupForwarder groupCommitter;
82 private FlowForwarder flowCommitter;
84 private MeterForwarder meterCommitter;
86 private TableForwarder tableCommitter;
88 private FlowCapableTransactionService flowCapableTxService;
91 private ArgumentCaptor<Group> groupCaptor;
93 private ArgumentCaptor<Group> groupUpdateCaptor;
95 private ArgumentCaptor<Flow> flowCaptor;
97 private ArgumentCaptor<Flow> flowUpdateCaptor;
99 private ArgumentCaptor<Meter> meterCaptor;
101 private ArgumentCaptor<Meter> meterUpdateCaptor;
103 private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
105 private SyncCrudCounters counters;
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;
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));
120 metersToAddOrUpdate = DiffInputFactory.createMeterSyncBoxWithUpdates(1, 2, 3);
121 metersToRemove = DiffInputFactory.createMeterSyncBox(1, 2, 3);
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"));
132 public void testExecuteSyncStrategy() throws Exception {
133 final SynchronizationDiffInput diffInput = new SynchronizationDiffInput(NODE_IDENT,
134 groupsToAddOrUpdate, metersToAddOrUpdate, flowsToAddOrUpdate, flowsToRemove,
135 metersToRemove, groupsToRemove);
137 final SyncCrudCounters syncCounters = new SyncCrudCounters();
138 final ListenableFuture<RpcResult<Void>> rpcResult = syncPlanPushStrategy.executeSyncStrategy(
139 RpcResultBuilder.<Void>success().buildFuture(), diffInput, syncCounters);
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());
160 Assert.assertTrue(rpcResult.isDone());
161 Assert.assertTrue(rpcResult.get().isSuccessful());
163 Assert.assertEquals(6, syncCounters.getFlowCrudCounts().getAdded());
164 Assert.assertEquals(3, syncCounters.getFlowCrudCounts().getUpdated());
165 Assert.assertEquals(6, syncCounters.getFlowCrudCounts().getRemoved());
167 Assert.assertEquals(6, syncCounters.getGroupCrudCounts().getAdded());
168 Assert.assertEquals(3, syncCounters.getGroupCrudCounts().getUpdated());
169 Assert.assertEquals(6, syncCounters.getGroupCrudCounts().getRemoved());
171 Assert.assertEquals(3, syncCounters.getMeterCrudCounts().getAdded());
172 Assert.assertEquals(3, syncCounters.getMeterCrudCounts().getUpdated());
173 Assert.assertEquals(3, syncCounters.getMeterCrudCounts().getRemoved());
177 public void setUp() throws Exception {
178 Mockito.when(flowCapableTxService.sendBarrier(Matchers.<SendBarrierInput>any()))
179 .thenReturn(RpcResultBuilder.success((SendBarrierOutput) null).buildFuture());
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());
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());
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());
211 Mockito.doAnswer(createSalServiceFutureAnswer()).when(tableCommitter).update(
212 Matchers.<InstanceIdentifier<TableFeatures>>any(), Matchers.<TableFeatures>any(),
213 Matchers.<TableFeatures>any(), Matchers.<InstanceIdentifier<FlowCapableNode>>any());
215 syncPlanPushStrategy = new SyncPlanPushStrategyIncrementalImpl()
216 .setMeterForwarder(meterCommitter)
217 .setTableForwarder(tableCommitter)
218 .setGroupForwarder(groupCommitter)
219 .setFlowForwarder(flowCommitter)
220 .setTransactionService(flowCapableTxService);
222 counters = new SyncCrudCounters();
225 private <O> Answer<Future<RpcResult<O>>> createSalServiceFutureAnswer() {
226 return invocation -> RpcResultBuilder.<O>success().buildFuture();
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());
235 final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
236 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
237 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
239 final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
240 flowBoxMap.put(new TableKey((short) 0), flowBox);
242 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingFlows(
243 NODE_ID, NODE_IDENT, flowBoxMap, counters);
245 Assert.assertTrue(result.isDone());
246 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
267 final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
268 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
269 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
271 final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
272 flowBoxMap.put(new TableKey((short) 0), flowBox);
274 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantFlows(
275 NODE_ID, NODE_IDENT, flowBoxMap, counters);
277 Assert.assertTrue(result.isDone());
278 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
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());
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)));
310 final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
311 flowBoxMap.put(new TableKey((short) 0), flowBox);
315 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingFlows(
316 NODE_ID, NODE_IDENT, flowBoxMap, counters);
318 Assert.assertTrue(result.isDone());
319 Assert.assertTrue(result.get().isSuccessful());
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());
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());
331 final InOrder inOrderFlow = Mockito.inOrder(flowCapableTxService, flowCommitter);
333 inOrderFlow.verify(flowCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Flow>>any(),
334 Matchers.<Flow>any(), Matchers.eq(NODE_IDENT));
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());
341 inOrderFlow.verifyNoMoreInteractions();
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());
350 final ItemSyncBox<Meter> meterSyncBox = new ItemSyncBox<>();
351 meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(2L));
352 meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(4L));
354 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingMeters(
355 NODE_ID, NODE_IDENT, meterSyncBox, counters);
357 Assert.assertTrue(result.isDone());
358 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
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());
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)));
389 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingMeters(
390 NODE_ID, NODE_IDENT, meterSyncBox, counters);
392 Assert.assertTrue(result.isDone());
393 Assert.assertTrue(result.get().isSuccessful());
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());
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());
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());
414 inOrderMeters.verifyNoMoreInteractions();
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());
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)));
429 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantMeters(
430 NODE_ID, NODE_IDENT, meterSyncBox, counters);
432 Assert.assertTrue(result.isDone());
433 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
454 ItemSyncBox<Group> groupBox1 = new ItemSyncBox<>();
455 groupBox1.getItemsToPush().add(DSInputFactory.createGroup(2L));
457 ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
458 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
459 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
461 ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
462 groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
464 final List<ItemSyncBox<Group>> groupBoxLot = Lists.newArrayList(groupBox1, groupBox2, groupBox3);
466 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingGroups(
467 NODE_ID, NODE_IDENT, groupBoxLot, counters);
469 Assert.assertTrue(result.isDone());
470 Assert.assertTrue(result.get().isSuccessful());
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());
479 final InOrder inOrderGroups = Mockito.inOrder(flowCapableTxService, groupCommitter);
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());
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());
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());
493 inOrderGroups.verifyNoMoreInteractions();
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());
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());
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)));
512 ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
513 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
514 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
516 ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
517 groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
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);
523 Assert.assertTrue(result.isDone());
524 Assert.assertTrue(result.get().isSuccessful());
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());
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());
538 final InOrder inOrderGroups = Mockito.inOrder(flowCapableTxService, groupCommitter);
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());
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());
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());
556 inOrderGroups.verifyNoMoreInteractions();
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());
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)));
570 ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
571 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
572 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
574 ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
575 groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
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);
581 Assert.assertTrue(result.isDone());
582 Assert.assertTrue(result.get().isSuccessful());
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());
591 final InOrder inOrderGroup = Mockito.inOrder(flowCapableTxService, groupCommitter);
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());
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());
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());
605 inOrderGroup.verifyNoMoreInteractions();
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());
615 final FlowCapableNode operational = new FlowCapableNodeBuilder()
616 .setTable(Collections.singletonList(new TableBuilder()
619 .setTableFeatures(Collections.singletonList(new TableFeaturesBuilder()
620 .setName("test table features")
621 .setTableId((short) 1)
625 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.updateTableFeatures(
626 NODE_IDENT, operational);
628 Assert.assertTrue(result.isDone());
629 Assert.assertTrue(result.get().isSuccessful());
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());
637 Mockito.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());