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.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;
70 * Test for {@link SyncPlanPushStrategyIncrementalImpl}.
72 @RunWith(MockitoJUnitRunner.class)
73 public class SyncPlanPushStrategyIncrementalImplTest {
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);
79 private SyncPlanPushStrategyIncrementalImpl syncPlanPushStrategy;
82 private DataBroker db;
84 private GroupForwarder groupCommitter;
86 private FlowForwarder flowCommitter;
88 private MeterForwarder meterCommitter;
90 private TableForwarder tableCommitter;
92 private FlowCapableTransactionService flowCapableTxService;
95 private ArgumentCaptor<Group> groupCaptor;
97 private ArgumentCaptor<Group> groupUpdateCaptor;
99 private ArgumentCaptor<Flow> flowCaptor;
101 private ArgumentCaptor<Flow> flowUpdateCaptor;
103 private ArgumentCaptor<Meter> meterCaptor;
105 private ArgumentCaptor<Meter> meterUpdateCaptor;
107 private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
109 private SyncCrudCounters counters;
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;
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));
124 metersToAddOrUpdate = DiffInputFactory.createMeterSyncBoxWithUpdates(1, 2, 3);
125 metersToRemove = DiffInputFactory.createMeterSyncBox(1, 2, 3);
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"));
136 public void testExecuteSyncStrategy() throws Exception {
137 final SynchronizationDiffInput diffInput = new SynchronizationDiffInput(NODE_IDENT,
138 groupsToAddOrUpdate, metersToAddOrUpdate, flowsToAddOrUpdate, flowsToRemove, metersToRemove, groupsToRemove);
140 final SyncCrudCounters counters = new SyncCrudCounters();
141 final ListenableFuture<RpcResult<Void>> rpcResult = syncPlanPushStrategy.executeSyncStrategy(
142 RpcResultBuilder.<Void>success().buildFuture(), diffInput, counters);
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());
163 Assert.assertTrue(rpcResult.isDone());
164 Assert.assertTrue(rpcResult.get().isSuccessful());
166 Assert.assertEquals(6, counters.getFlowCrudCounts().getAdded());
167 Assert.assertEquals(3, counters.getFlowCrudCounts().getUpdated());
168 Assert.assertEquals(6, counters.getFlowCrudCounts().getRemoved());
170 Assert.assertEquals(6, counters.getGroupCrudCounts().getAdded());
171 Assert.assertEquals(3, counters.getGroupCrudCounts().getUpdated());
172 Assert.assertEquals(6, counters.getGroupCrudCounts().getRemoved());
174 Assert.assertEquals(3, counters.getMeterCrudCounts().getAdded());
175 Assert.assertEquals(3, counters.getMeterCrudCounts().getUpdated());
176 Assert.assertEquals(3, counters.getMeterCrudCounts().getRemoved());
180 public void setUp() throws Exception {
181 Mockito.when(flowCapableTxService.sendBarrier(Matchers.<SendBarrierInput>any()))
182 .thenReturn(RpcResultBuilder.success((Void) null).buildFuture());
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());
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());
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());
214 Mockito.doAnswer(createSalServiceFutureAnswer()).when(tableCommitter).update(
215 Matchers.<InstanceIdentifier<TableFeatures>>any(), Matchers.<TableFeatures>any(), Matchers.<TableFeatures>any(),
216 Matchers.<InstanceIdentifier<FlowCapableNode>>any());
218 syncPlanPushStrategy = new SyncPlanPushStrategyIncrementalImpl()
219 .setMeterForwarder(meterCommitter)
220 .setTableForwarder(tableCommitter)
221 .setGroupForwarder(groupCommitter)
222 .setFlowForwarder(flowCommitter)
223 .setTransactionService(flowCapableTxService);
225 counters = new SyncCrudCounters();
228 private <O> Answer<Future<RpcResult<O>>> createSalServiceFutureAnswer() {
229 return new Answer<Future<RpcResult<O>>>() {
231 public Future<RpcResult<O>> answer(final InvocationOnMock invocation) throws Throwable {
232 return RpcResultBuilder.<O>success().buildFuture();
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());
243 final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
244 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
245 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
247 final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
248 flowBoxMap.put(new TableKey((short) 0), flowBox);
250 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingFlows(
251 NODE_ID, NODE_IDENT, flowBoxMap, counters);
253 Assert.assertTrue(result.isDone());
254 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
275 final ItemSyncBox<Flow> flowBox = new ItemSyncBox<>();
276 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f3", 3));
277 flowBox.getItemsToPush().add(DSInputFactory.createFlow("f4", 4));
279 final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
280 flowBoxMap.put(new TableKey((short) 0), flowBox);
282 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantFlows(
283 NODE_ID, NODE_IDENT, flowBoxMap, counters);
285 Assert.assertTrue(result.isDone());
286 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
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());
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)));
318 final Map<TableKey, ItemSyncBox<Flow>> flowBoxMap = new LinkedHashMap<>();
319 flowBoxMap.put(new TableKey((short) 0), flowBox);
323 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingFlows(
324 NODE_ID, NODE_IDENT, flowBoxMap, counters);
326 Assert.assertTrue(result.isDone());
327 Assert.assertTrue(result.get().isSuccessful());
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());
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());
339 final InOrder inOrderFlow = Mockito.inOrder(flowCapableTxService, flowCommitter);
341 inOrderFlow.verify(flowCommitter, Mockito.times(2)).add(Matchers.<InstanceIdentifier<Flow>>any(),
342 Matchers.<Flow>any(), Matchers.eq(NODE_IDENT));
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());
349 inOrderFlow.verifyNoMoreInteractions();
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());
358 final ItemSyncBox<Meter> meterSyncBox = new ItemSyncBox<>();
359 meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(2L));
360 meterSyncBox.getItemsToPush().add(DSInputFactory.createMeter(4L));
362 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingMeters(
363 NODE_ID, NODE_IDENT, meterSyncBox, counters);
365 Assert.assertTrue(result.isDone());
366 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
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());
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)));
397 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingMeters(
398 NODE_ID, NODE_IDENT, meterSyncBox, counters);
400 Assert.assertTrue(result.isDone());
401 Assert.assertTrue(result.get().isSuccessful());
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());
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());
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());
422 inOrderMeters.verifyNoMoreInteractions();
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());
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)));
437 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.removeRedundantMeters(
438 NODE_ID, NODE_IDENT, meterSyncBox, counters);
440 Assert.assertTrue(result.isDone());
441 Assert.assertTrue(result.get().isSuccessful());
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());
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();
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());
462 ItemSyncBox<Group> groupBox1 = new ItemSyncBox<>();
463 groupBox1.getItemsToPush().add(DSInputFactory.createGroup(2L));
465 ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
466 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
467 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
469 ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
470 groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
472 final List<ItemSyncBox<Group>> groupBoxLot = Lists.newArrayList(groupBox1, groupBox2, groupBox3);
474 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.addMissingGroups(
475 NODE_ID, NODE_IDENT, groupBoxLot, counters);
477 Assert.assertTrue(result.isDone());
478 Assert.assertTrue(result.get().isSuccessful());
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());
487 final InOrder inOrderGroups = Mockito.inOrder(flowCapableTxService, groupCommitter);
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.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());
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());
501 inOrderGroups.verifyNoMoreInteractions();
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());
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());
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)));
520 ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
521 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
522 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
524 ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
525 groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
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);
531 Assert.assertTrue(result.isDone());
532 Assert.assertTrue(result.get().isSuccessful());
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());
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());
546 final InOrder inOrderGroups = Mockito.inOrder(flowCapableTxService, groupCommitter);
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());
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());
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());
564 inOrderGroups.verifyNoMoreInteractions();
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());
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)));
578 ItemSyncBox<Group> groupBox2 = new ItemSyncBox<>();
579 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(3L, 2L));
580 groupBox2.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(4L, 2L));
582 ItemSyncBox<Group> groupBox3 = new ItemSyncBox<>();
583 groupBox3.getItemsToPush().add(DSInputFactory.createGroupWithPreconditions(5L, 3L, 4L));
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);
589 Assert.assertTrue(result.isDone());
590 Assert.assertTrue(result.get().isSuccessful());
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());
599 final InOrder inOrderGroup = Mockito.inOrder(flowCapableTxService, groupCommitter);
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.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());
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());
613 inOrderGroup.verifyNoMoreInteractions();
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());
623 final FlowCapableNode operational = new FlowCapableNodeBuilder()
624 .setTable(Collections.singletonList(new TableBuilder()
627 .setTableFeatures(Collections.singletonList(new TableFeaturesBuilder()
628 .setName("test table features")
629 .setTableId((short) 1)
633 final ListenableFuture<RpcResult<Void>> result = syncPlanPushStrategy.updateTableFeatures(
634 NODE_IDENT, operational);
636 Assert.assertTrue(result.isDone());
637 Assert.assertTrue(result.get().isSuccessful());
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());
645 Mockito.verify(flowCapableTxService).sendBarrier(Matchers.<SendBarrierInput>any());