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