Cleaning and preparation before bug 6170
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / SyncPlanPushStrategyFlatBatchImpl.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.annotations.VisibleForTesting;
12 import com.google.common.collect.Iterators;
13 import com.google.common.collect.PeekingIterator;
14 import com.google.common.collect.Range;
15 import com.google.common.util.concurrent.AsyncFunction;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.JdkFutureAdapters;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.concurrent.Future;
25 import javax.annotation.Nullable;
26 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
27 import org.opendaylight.openflowplugin.applications.frsync.impl.TableForwarder;
28 import org.opendaylight.openflowplugin.applications.frsync.util.FxChainUtil;
29 import org.opendaylight.openflowplugin.applications.frsync.util.ItemSyncBox;
30 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
31 import org.opendaylight.openflowplugin.applications.frsync.util.ReconcileUtil;
32 import org.opendaylight.openflowplugin.applications.frsync.util.SyncCrudCounters;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.Batch;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.BatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.BatchChoice;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCase;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCaseBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlow;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroup;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroupBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeter;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeterBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlow;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlowBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroup;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroupBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeter;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeterBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlow;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlowBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroup;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroupBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeter;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeterBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.input.update.grouping.OriginalBatchedFlowBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.flows.service.rev160314.batch.flow.input.update.grouping.UpdatedBatchedFlowBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.batch.group.input.update.grouping.OriginalBatchedGroupBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.batch.group.input.update.grouping.UpdatedBatchedGroupBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.input.update.grouping.OriginalBatchedMeterBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.input.update.grouping.UpdatedBatchedMeterBuilder;
90 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
91 import org.opendaylight.yangtools.yang.common.RpcResult;
92 import org.slf4j.Logger;
93 import org.slf4j.LoggerFactory;
94
95 /**
96  * Execute CRUD API for flow + group + meter involving flat-batch strategy.
97  */
98 public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
99
100     private static final Logger LOG = LoggerFactory.getLogger(SyncPlanPushStrategyFlatBatchImpl.class);
101
102     private SalFlatBatchService flatBatchService;
103     private TableForwarder tableForwarder;
104
105     @Override
106     public ListenableFuture<RpcResult<Void>> executeSyncStrategy(ListenableFuture<RpcResult<Void>> resultVehicle,
107                                                                  final SynchronizationDiffInput diffInput,
108                                                                  final SyncCrudCounters counters) {
109         final InstanceIdentifier<FlowCapableNode> nodeIdent = diffInput.getNodeIdent();
110         final NodeId nodeId = PathUtil.digNodeId(nodeIdent);
111
112         // prepare default (full) counts
113         counters.getGroupCrudCounts().setAdded(ReconcileUtil.countTotalPushed(diffInput.getGroupsToAddOrUpdate()));
114         counters.getGroupCrudCounts().setUpdated(ReconcileUtil.countTotalUpdated(diffInput.getGroupsToAddOrUpdate()));
115         counters.getGroupCrudCounts().setRemoved(ReconcileUtil.countTotalPushed(diffInput.getGroupsToRemove()));
116
117         counters.getFlowCrudCounts().setAdded(ReconcileUtil.countTotalPushed(diffInput.getFlowsToAddOrUpdate().values()));
118         counters.getFlowCrudCounts().setUpdated(ReconcileUtil.countTotalUpdated(diffInput.getFlowsToAddOrUpdate().values()));
119         counters.getFlowCrudCounts().setRemoved(ReconcileUtil.countTotalPushed(diffInput.getFlowsToRemove().values()));
120
121         counters.getMeterCrudCounts().setAdded(diffInput.getMetersToAddOrUpdate().getItemsToPush().size());
122         counters.getMeterCrudCounts().setUpdated(diffInput.getMetersToAddOrUpdate().getItemsToUpdate().size());
123         counters.getMeterCrudCounts().setRemoved(diffInput.getMetersToRemove().getItemsToPush().size());
124
125         /* Tables - have to be pushed before groups */
126         // TODO enable table-update when ready
127         //resultVehicle = updateTableFeatures(nodeIdent, configTree);
128
129         resultVehicle = Futures.transform(resultVehicle, new AsyncFunction<RpcResult<Void>, RpcResult<Void>>() {
130             @Override
131             public ListenableFuture<RpcResult<Void>> apply(final RpcResult<Void> input) throws Exception {
132                 final List<Batch> batchBag = new ArrayList<>();
133                 int batchOrder = 0;
134
135                 batchOrder = assembleAddOrUpdateGroups(batchBag, batchOrder, diffInput.getGroupsToAddOrUpdate());
136                 batchOrder = assembleAddOrUpdateMeters(batchBag, batchOrder, diffInput.getMetersToAddOrUpdate());
137                 batchOrder = assembleAddOrUpdateFlows(batchBag, batchOrder, diffInput.getFlowsToAddOrUpdate());
138
139                 batchOrder = assembleRemoveFlows(batchBag, batchOrder, diffInput.getFlowsToRemove());
140                 batchOrder = assembleRemoveMeters(batchBag, batchOrder, diffInput.getMetersToRemove());
141                 batchOrder = assembleRemoveGroups(batchBag, batchOrder, diffInput.getGroupsToRemove());
142
143                 LOG.trace("Index of last batch step: {}", batchOrder);
144
145                 final ProcessFlatBatchInput flatBatchInput = new ProcessFlatBatchInputBuilder()
146                         .setNode(new NodeRef(PathUtil.digNodePath(diffInput.getNodeIdent())))
147                         .setExitOnFirstError(false) // TODO: propagate from input
148                         .setBatch(batchBag)
149                         .build();
150
151                 final Future<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture = flatBatchService.processFlatBatch(flatBatchInput);
152
153                 final int failureIndexLimit = batchOrder;
154
155                 if (LOG.isDebugEnabled()) {
156                     Futures.addCallback(JdkFutureAdapters.listenInPoolThread(rpcResultFuture),
157                             createCounterCallback(batchBag, failureIndexLimit, counters));
158                 }
159
160                 return Futures.transform(JdkFutureAdapters.listenInPoolThread(rpcResultFuture),
161                         ReconcileUtil.<ProcessFlatBatchOutput>createRpcResultToVoidFunction("flat-batch"));
162             }
163         });
164
165         Futures.addCallback(resultVehicle, FxChainUtil.logResultCallback(nodeId, "flat-batch"));
166         return resultVehicle;
167     }
168
169     private FutureCallback<RpcResult<ProcessFlatBatchOutput>> createCounterCallback(final List<Batch> inputBatchBag,
170                                                                                     final int failureIndexLimit,
171                                                                                     final SyncCrudCounters counters) {
172         return new FutureCallback<RpcResult<ProcessFlatBatchOutput>>() {
173             @Override
174             public void onSuccess(@Nullable final RpcResult<ProcessFlatBatchOutput> result) {
175                 if (!result.isSuccessful() && result.getResult() != null && !result.getResult().getBatchFailure().isEmpty()) {
176                     Map<Range<Integer>, Batch> batchMap = mapBatchesToRanges(inputBatchBag, failureIndexLimit);
177
178                     for (BatchFailure batchFailure : result.getResult().getBatchFailure()) {
179                         for (Map.Entry<Range<Integer>, Batch> rangeBatchEntry : batchMap.entrySet()) {
180                             if (rangeBatchEntry.getKey().contains(batchFailure.getBatchOrder())) {
181                                 // get type and decrease
182                                 final BatchChoice batchChoice = rangeBatchEntry.getValue().getBatchChoice();
183                                 decrementCounters(batchChoice, counters);
184                                 break;
185                             }
186                         }
187                     }
188                 }
189             }
190
191             @Override
192             public void onFailure(final Throwable t) {
193                 counters.resetAll();
194             }
195         };
196     }
197
198     static void decrementCounters(final BatchChoice batchChoice, final SyncCrudCounters counters) {
199         if (batchChoice instanceof FlatBatchAddFlowCase) {
200             counters.getFlowCrudCounts().decAdded();
201         } else if (batchChoice instanceof FlatBatchUpdateFlowCase) {
202             counters.getFlowCrudCounts().decUpdated();
203         } else if (batchChoice instanceof FlatBatchRemoveFlowCase) {
204             counters.getFlowCrudCounts().decRemoved();
205         } else if (batchChoice instanceof FlatBatchAddGroupCase) {
206             counters.getGroupCrudCounts().decAdded();
207         } else if (batchChoice instanceof FlatBatchUpdateGroupCase) {
208             counters.getGroupCrudCounts().decUpdated();
209         } else if (batchChoice instanceof FlatBatchRemoveGroupCase) {
210             counters.getGroupCrudCounts().decRemoved();
211         } else if (batchChoice instanceof FlatBatchAddMeterCase) {
212             counters.getMeterCrudCounts().decAdded();
213         } else if (batchChoice instanceof FlatBatchUpdateMeterCase) {
214             counters.getMeterCrudCounts().decUpdated();
215         } else if (batchChoice instanceof FlatBatchRemoveMeterCase) {
216             counters.getMeterCrudCounts().decRemoved();
217         }
218     }
219
220     static Map<Range<Integer>, Batch> mapBatchesToRanges(final List<Batch> inputBatchBag, final int failureIndexLimit) {
221         final Map<Range<Integer>, Batch> batchMap = new LinkedHashMap<>();
222         final PeekingIterator<Batch> batchPeekingIterator = Iterators.peekingIterator(inputBatchBag.iterator());
223         while (batchPeekingIterator.hasNext()) {
224             final Batch batch = batchPeekingIterator.next();
225             final int nextBatchOrder = batchPeekingIterator.hasNext()
226                     ? batchPeekingIterator.peek().getBatchOrder()
227                     : failureIndexLimit;
228             batchMap.put(Range.closed(batch.getBatchOrder(), nextBatchOrder - 1), batch);
229         }
230         return batchMap;
231     }
232
233     @VisibleForTesting
234     static int assembleRemoveFlows(final List<Batch> batchBag, int batchOrder, final Map<TableKey, ItemSyncBox<Flow>> flowItemSyncTableMap) {
235         // process flow remove
236         if (flowItemSyncTableMap != null) {
237             for (Map.Entry<TableKey, ItemSyncBox<Flow>> syncBoxEntry : flowItemSyncTableMap.entrySet()) {
238                 final ItemSyncBox<Flow> flowItemSyncBox = syncBoxEntry.getValue();
239
240                 if (!flowItemSyncBox.getItemsToPush().isEmpty()) {
241                     final List<FlatBatchRemoveFlow> flatBatchRemoveFlowBag =
242                             new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
243                     int itemOrder = 0;
244                     for (Flow flow : flowItemSyncBox.getItemsToPush()) {
245                         flatBatchRemoveFlowBag.add(new FlatBatchRemoveFlowBuilder(flow)
246                                 .setBatchOrder(itemOrder++)
247                                 .setFlowId(flow.getId())
248                                 .build());
249                     }
250                     final Batch batch = new BatchBuilder()
251                             .setBatchChoice(new FlatBatchRemoveFlowCaseBuilder()
252                                     .setFlatBatchRemoveFlow(flatBatchRemoveFlowBag)
253                                     .build())
254                             .setBatchOrder(batchOrder)
255                             .build();
256                     batchOrder += itemOrder;
257                     batchBag.add(batch);
258                 }
259             }
260         }
261         return batchOrder;
262     }
263
264     @VisibleForTesting
265     static int assembleAddOrUpdateGroups(final List<Batch> batchBag, int batchOrder, final List<ItemSyncBox<Group>> groupsToAddOrUpdate) {
266         // process group add+update
267         if (groupsToAddOrUpdate != null) {
268             for (ItemSyncBox<Group> groupItemSyncBox : groupsToAddOrUpdate) {
269                 if (!groupItemSyncBox.getItemsToPush().isEmpty()) {
270                     final List<FlatBatchAddGroup> flatBatchAddGroupBag =
271                             new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
272                     int itemOrder = 0;
273                     for (Group group : groupItemSyncBox.getItemsToPush()) {
274                         flatBatchAddGroupBag.add(new FlatBatchAddGroupBuilder(group).setBatchOrder(itemOrder++).build());
275                     }
276                     final Batch batch = new BatchBuilder()
277                             .setBatchChoice(new FlatBatchAddGroupCaseBuilder()
278                                     .setFlatBatchAddGroup(flatBatchAddGroupBag)
279                                     .build())
280                             .setBatchOrder(batchOrder)
281                             .build();
282                     batchOrder += itemOrder;
283                     batchBag.add(batch);
284                 }
285
286                 if (!groupItemSyncBox.getItemsToUpdate().isEmpty()) {
287                     final List<FlatBatchUpdateGroup> flatBatchUpdateGroupBag =
288                             new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
289                     int itemOrder = 0;
290                     for (ItemSyncBox.ItemUpdateTuple<Group> groupUpdate : groupItemSyncBox.getItemsToUpdate()) {
291                         flatBatchUpdateGroupBag.add(new FlatBatchUpdateGroupBuilder()
292                                 .setBatchOrder(itemOrder++)
293                                 .setOriginalBatchedGroup(new OriginalBatchedGroupBuilder(groupUpdate.getOriginal()).build())
294                                 .setUpdatedBatchedGroup(new UpdatedBatchedGroupBuilder(groupUpdate.getUpdated()).build())
295                                 .build());
296                     }
297                     final Batch batch = new BatchBuilder()
298                             .setBatchChoice(new FlatBatchUpdateGroupCaseBuilder()
299                                     .setFlatBatchUpdateGroup(flatBatchUpdateGroupBag)
300                                     .build())
301                             .setBatchOrder(batchOrder)
302                             .build();
303                     batchOrder += itemOrder;
304                     batchBag.add(batch);
305                 }
306             }
307         }
308         return batchOrder;
309     }
310
311     @VisibleForTesting
312     static int assembleRemoveGroups(final List<Batch> batchBag, int batchOrder, final List<ItemSyncBox<Group>> groupsToRemoveOrUpdate) {
313         // process group add+update
314         if (groupsToRemoveOrUpdate != null) {
315             for (ItemSyncBox<Group> groupItemSyncBox : groupsToRemoveOrUpdate) {
316                 if (!groupItemSyncBox.getItemsToPush().isEmpty()) {
317                     final List<FlatBatchRemoveGroup> flatBatchRemoveGroupBag =
318                             new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
319                     int itemOrder = 0;
320                     for (Group group : groupItemSyncBox.getItemsToPush()) {
321                         flatBatchRemoveGroupBag.add(new FlatBatchRemoveGroupBuilder(group).setBatchOrder(itemOrder++).build());
322                     }
323                     final Batch batch = new BatchBuilder()
324                             .setBatchChoice(new FlatBatchRemoveGroupCaseBuilder()
325                                     .setFlatBatchRemoveGroup(flatBatchRemoveGroupBag)
326                                     .build())
327                             .setBatchOrder(batchOrder)
328                             .build();
329                     batchOrder += itemOrder;
330                     batchBag.add(batch);
331                 }
332             }
333         }
334         return batchOrder;
335     }
336
337     @VisibleForTesting
338     static int assembleAddOrUpdateMeters(final List<Batch> batchBag, int batchOrder, final ItemSyncBox<Meter> meterItemSyncBox) {
339         // process meter add+update
340         if (meterItemSyncBox != null) {
341             if (!meterItemSyncBox.getItemsToPush().isEmpty()) {
342                 final List<FlatBatchAddMeter> flatBatchAddMeterBag =
343                         new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
344                 int itemOrder = 0;
345                 for (Meter meter : meterItemSyncBox.getItemsToPush()) {
346                     flatBatchAddMeterBag.add(new FlatBatchAddMeterBuilder(meter).setBatchOrder(itemOrder++).build());
347                 }
348                 final Batch batch = new BatchBuilder()
349                         .setBatchChoice(new FlatBatchAddMeterCaseBuilder()
350                                 .setFlatBatchAddMeter(flatBatchAddMeterBag)
351                                 .build())
352                         .setBatchOrder(batchOrder)
353                         .build();
354                 batchOrder += itemOrder;
355                 batchBag.add(batch);
356             }
357
358             if (!meterItemSyncBox.getItemsToUpdate().isEmpty()) {
359                 final List<FlatBatchUpdateMeter> flatBatchUpdateMeterBag =
360                         new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
361                 int itemOrder = 0;
362                 for (ItemSyncBox.ItemUpdateTuple<Meter> meterUpdate : meterItemSyncBox.getItemsToUpdate()) {
363                     flatBatchUpdateMeterBag.add(new FlatBatchUpdateMeterBuilder()
364                             .setBatchOrder(itemOrder++)
365                             .setOriginalBatchedMeter(new OriginalBatchedMeterBuilder(meterUpdate.getOriginal()).build())
366                             .setUpdatedBatchedMeter(new UpdatedBatchedMeterBuilder(meterUpdate.getUpdated()).build())
367                             .build());
368                 }
369                 final Batch batch = new BatchBuilder()
370                         .setBatchChoice(new FlatBatchUpdateMeterCaseBuilder()
371                                 .setFlatBatchUpdateMeter(flatBatchUpdateMeterBag)
372                                 .build())
373                         .setBatchOrder(batchOrder)
374                         .build();
375                 batchOrder += itemOrder;
376                 batchBag.add(batch);
377             }
378         }
379         return batchOrder;
380     }
381
382     @VisibleForTesting
383     static int assembleRemoveMeters(final List<Batch> batchBag, int batchOrder, final ItemSyncBox<Meter> meterItemSyncBox) {
384         // process meter remove
385         if (meterItemSyncBox != null) {
386             if (!meterItemSyncBox.getItemsToPush().isEmpty()) {
387                 final List<FlatBatchRemoveMeter> flatBatchRemoveMeterBag =
388                         new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
389                 int itemOrder = 0;
390                 for (Meter meter : meterItemSyncBox.getItemsToPush()) {
391                     flatBatchRemoveMeterBag.add(new FlatBatchRemoveMeterBuilder(meter).setBatchOrder(itemOrder++).build());
392                 }
393                 final Batch batch = new BatchBuilder()
394                         .setBatchChoice(new FlatBatchRemoveMeterCaseBuilder()
395                                 .setFlatBatchRemoveMeter(flatBatchRemoveMeterBag)
396                                 .build())
397                         .setBatchOrder(batchOrder)
398                         .build();
399                 batchOrder += itemOrder;
400                 batchBag.add(batch);
401             }
402         }
403         return batchOrder;
404     }
405
406     @VisibleForTesting
407     static int assembleAddOrUpdateFlows(final List<Batch> batchBag, int batchOrder, final Map<TableKey, ItemSyncBox<Flow>> flowItemSyncTableMap) {
408         // process flow add+update
409         if (flowItemSyncTableMap != null) {
410             for (Map.Entry<TableKey, ItemSyncBox<Flow>> syncBoxEntry : flowItemSyncTableMap.entrySet()) {
411                 final ItemSyncBox<Flow> flowItemSyncBox = syncBoxEntry.getValue();
412
413                 if (!flowItemSyncBox.getItemsToPush().isEmpty()) {
414                     final List<FlatBatchAddFlow> flatBatchAddFlowBag =
415                             new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
416                     int itemOrder = 0;
417                     for (Flow flow : flowItemSyncBox.getItemsToPush()) {
418                         flatBatchAddFlowBag.add(new FlatBatchAddFlowBuilder(flow)
419                                 .setBatchOrder(itemOrder++)
420                                 .setFlowId(flow.getId())
421                                 .build());
422                     }
423                     final Batch batch = new BatchBuilder()
424                             .setBatchChoice(new FlatBatchAddFlowCaseBuilder()
425                                     .setFlatBatchAddFlow(flatBatchAddFlowBag)
426                                     .build())
427                             .setBatchOrder(batchOrder)
428                             .build();
429                     batchOrder += itemOrder;
430                     batchBag.add(batch);
431                 }
432
433                 if (!flowItemSyncBox.getItemsToUpdate().isEmpty()) {
434                     final List<FlatBatchUpdateFlow> flatBatchUpdateFlowBag =
435                             new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
436                     int itemOrder = 0;
437                     for (ItemSyncBox.ItemUpdateTuple<Flow> flowUpdate : flowItemSyncBox.getItemsToUpdate()) {
438                         flatBatchUpdateFlowBag.add(new FlatBatchUpdateFlowBuilder()
439                                 .setBatchOrder(itemOrder++)
440                                 .setFlowId(flowUpdate.getUpdated().getId())
441                                 .setOriginalBatchedFlow(new OriginalBatchedFlowBuilder(flowUpdate.getOriginal()).build())
442                                 .setUpdatedBatchedFlow(new UpdatedBatchedFlowBuilder(flowUpdate.getUpdated()).build())
443                                 .build());
444                     }
445                     final Batch batch = new BatchBuilder()
446                             .setBatchChoice(new FlatBatchUpdateFlowCaseBuilder()
447                                     .setFlatBatchUpdateFlow(flatBatchUpdateFlowBag)
448                                     .build())
449                             .setBatchOrder(batchOrder)
450                             .build();
451                     batchOrder += itemOrder;
452                     batchBag.add(batch);
453                 }
454             }
455         }
456         return batchOrder;
457     }
458
459     public SyncPlanPushStrategyFlatBatchImpl setFlatBatchService(final SalFlatBatchService flatBatchService) {
460         this.flatBatchService = flatBatchService;
461         return this;
462     }
463
464     public SyncPlanPushStrategyFlatBatchImpl setTableForwarder(final TableForwarder tableForwarder) {
465         this.tableForwarder = tableForwarder;
466         return this;
467     }
468 }