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.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;
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;
96 * Execute CRUD API for flow + group + meter involving flat-batch strategy.
98 public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
100 private static final Logger LOG = LoggerFactory.getLogger(SyncPlanPushStrategyFlatBatchImpl.class);
102 private SalFlatBatchService flatBatchService;
103 private TableForwarder tableForwarder;
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);
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()));
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()));
121 counters.getMeterCrudCounts().setAdded(diffInput.getMetersToAddOrUpdate().getItemsToPush().size());
122 counters.getMeterCrudCounts().setUpdated(diffInput.getMetersToAddOrUpdate().getItemsToUpdate().size());
123 counters.getMeterCrudCounts().setRemoved(diffInput.getMetersToRemove().getItemsToPush().size());
125 /* Tables - have to be pushed before groups */
126 // TODO enable table-update when ready
127 //resultVehicle = updateTableFeatures(nodeIdent, configTree);
129 resultVehicle = Futures.transform(resultVehicle, new AsyncFunction<RpcResult<Void>, RpcResult<Void>>() {
131 public ListenableFuture<RpcResult<Void>> apply(final RpcResult<Void> input) throws Exception {
132 final List<Batch> batchBag = new ArrayList<>();
135 batchOrder = assembleAddOrUpdateGroups(batchBag, batchOrder, diffInput.getGroupsToAddOrUpdate());
136 batchOrder = assembleAddOrUpdateMeters(batchBag, batchOrder, diffInput.getMetersToAddOrUpdate());
137 batchOrder = assembleAddOrUpdateFlows(batchBag, batchOrder, diffInput.getFlowsToAddOrUpdate());
139 batchOrder = assembleRemoveFlows(batchBag, batchOrder, diffInput.getFlowsToRemove());
140 batchOrder = assembleRemoveMeters(batchBag, batchOrder, diffInput.getMetersToRemove());
141 batchOrder = assembleRemoveGroups(batchBag, batchOrder, diffInput.getGroupsToRemove());
143 LOG.trace("Index of last batch step: {}", batchOrder);
145 final ProcessFlatBatchInput flatBatchInput = new ProcessFlatBatchInputBuilder()
146 .setNode(new NodeRef(PathUtil.digNodePath(diffInput.getNodeIdent())))
147 .setExitOnFirstError(false) // TODO: propagate from input
151 final Future<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture = flatBatchService.processFlatBatch(flatBatchInput);
153 final int failureIndexLimit = batchOrder;
155 if (LOG.isDebugEnabled()) {
156 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(rpcResultFuture),
157 createCounterCallback(batchBag, failureIndexLimit, counters));
160 return Futures.transform(JdkFutureAdapters.listenInPoolThread(rpcResultFuture),
161 ReconcileUtil.<ProcessFlatBatchOutput>createRpcResultToVoidFunction("flat-batch"));
165 Futures.addCallback(resultVehicle, FxChainUtil.logResultCallback(nodeId, "flat-batch"));
166 return resultVehicle;
169 private FutureCallback<RpcResult<ProcessFlatBatchOutput>> createCounterCallback(final List<Batch> inputBatchBag,
170 final int failureIndexLimit,
171 final SyncCrudCounters counters) {
172 return new FutureCallback<RpcResult<ProcessFlatBatchOutput>>() {
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);
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);
192 public void onFailure(final Throwable t) {
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();
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()
228 batchMap.put(Range.closed(batch.getBatchOrder(), nextBatchOrder - 1), batch);
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();
240 if (!flowItemSyncBox.getItemsToPush().isEmpty()) {
241 final List<FlatBatchRemoveFlow> flatBatchRemoveFlowBag =
242 new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
244 for (Flow flow : flowItemSyncBox.getItemsToPush()) {
245 flatBatchRemoveFlowBag.add(new FlatBatchRemoveFlowBuilder(flow)
246 .setBatchOrder(itemOrder++)
247 .setFlowId(flow.getId())
250 final Batch batch = new BatchBuilder()
251 .setBatchChoice(new FlatBatchRemoveFlowCaseBuilder()
252 .setFlatBatchRemoveFlow(flatBatchRemoveFlowBag)
254 .setBatchOrder(batchOrder)
256 batchOrder += itemOrder;
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());
273 for (Group group : groupItemSyncBox.getItemsToPush()) {
274 flatBatchAddGroupBag.add(new FlatBatchAddGroupBuilder(group).setBatchOrder(itemOrder++).build());
276 final Batch batch = new BatchBuilder()
277 .setBatchChoice(new FlatBatchAddGroupCaseBuilder()
278 .setFlatBatchAddGroup(flatBatchAddGroupBag)
280 .setBatchOrder(batchOrder)
282 batchOrder += itemOrder;
286 if (!groupItemSyncBox.getItemsToUpdate().isEmpty()) {
287 final List<FlatBatchUpdateGroup> flatBatchUpdateGroupBag =
288 new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
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())
297 final Batch batch = new BatchBuilder()
298 .setBatchChoice(new FlatBatchUpdateGroupCaseBuilder()
299 .setFlatBatchUpdateGroup(flatBatchUpdateGroupBag)
301 .setBatchOrder(batchOrder)
303 batchOrder += itemOrder;
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());
320 for (Group group : groupItemSyncBox.getItemsToPush()) {
321 flatBatchRemoveGroupBag.add(new FlatBatchRemoveGroupBuilder(group).setBatchOrder(itemOrder++).build());
323 final Batch batch = new BatchBuilder()
324 .setBatchChoice(new FlatBatchRemoveGroupCaseBuilder()
325 .setFlatBatchRemoveGroup(flatBatchRemoveGroupBag)
327 .setBatchOrder(batchOrder)
329 batchOrder += itemOrder;
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());
345 for (Meter meter : meterItemSyncBox.getItemsToPush()) {
346 flatBatchAddMeterBag.add(new FlatBatchAddMeterBuilder(meter).setBatchOrder(itemOrder++).build());
348 final Batch batch = new BatchBuilder()
349 .setBatchChoice(new FlatBatchAddMeterCaseBuilder()
350 .setFlatBatchAddMeter(flatBatchAddMeterBag)
352 .setBatchOrder(batchOrder)
354 batchOrder += itemOrder;
358 if (!meterItemSyncBox.getItemsToUpdate().isEmpty()) {
359 final List<FlatBatchUpdateMeter> flatBatchUpdateMeterBag =
360 new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
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())
369 final Batch batch = new BatchBuilder()
370 .setBatchChoice(new FlatBatchUpdateMeterCaseBuilder()
371 .setFlatBatchUpdateMeter(flatBatchUpdateMeterBag)
373 .setBatchOrder(batchOrder)
375 batchOrder += itemOrder;
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());
390 for (Meter meter : meterItemSyncBox.getItemsToPush()) {
391 flatBatchRemoveMeterBag.add(new FlatBatchRemoveMeterBuilder(meter).setBatchOrder(itemOrder++).build());
393 final Batch batch = new BatchBuilder()
394 .setBatchChoice(new FlatBatchRemoveMeterCaseBuilder()
395 .setFlatBatchRemoveMeter(flatBatchRemoveMeterBag)
397 .setBatchOrder(batchOrder)
399 batchOrder += itemOrder;
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();
413 if (!flowItemSyncBox.getItemsToPush().isEmpty()) {
414 final List<FlatBatchAddFlow> flatBatchAddFlowBag =
415 new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
417 for (Flow flow : flowItemSyncBox.getItemsToPush()) {
418 flatBatchAddFlowBag.add(new FlatBatchAddFlowBuilder(flow)
419 .setBatchOrder(itemOrder++)
420 .setFlowId(flow.getId())
423 final Batch batch = new BatchBuilder()
424 .setBatchChoice(new FlatBatchAddFlowCaseBuilder()
425 .setFlatBatchAddFlow(flatBatchAddFlowBag)
427 .setBatchOrder(batchOrder)
429 batchOrder += itemOrder;
433 if (!flowItemSyncBox.getItemsToUpdate().isEmpty()) {
434 final List<FlatBatchUpdateFlow> flatBatchUpdateFlowBag =
435 new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
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())
445 final Batch batch = new BatchBuilder()
446 .setBatchChoice(new FlatBatchUpdateFlowCaseBuilder()
447 .setFlatBatchUpdateFlow(flatBatchUpdateFlowBag)
449 .setBatchOrder(batchOrder)
451 batchOrder += itemOrder;
459 public SyncPlanPushStrategyFlatBatchImpl setFlatBatchService(final SalFlatBatchService flatBatchService) {
460 this.flatBatchService = flatBatchService;
464 public SyncPlanPushStrategyFlatBatchImpl setTableForwarder(final TableForwarder tableForwarder) {
465 this.tableForwarder = tableForwarder;