Bump MRI upstreams
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / SyncPlanPushStrategyFlatBatchImpl.java
index 54fcf60b8a235e981bf38211ce794ef588162929..d954cf2129263e86fbb59c2183036dfb45c49413 100644 (file)
@@ -16,6 +16,7 @@ import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -28,45 +29,54 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev16032
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.FlatBatchAddFlowCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.FlatBatchAddFlowCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.FlatBatchRemoveFlowCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.FlatBatchRemoveFlowCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.FlatBatchUpdateFlowCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.FlatBatchUpdateFlowCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.add.flow._case.FlatBatchAddFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.add.flow._case.FlatBatchAddFlowBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.add.flow._case.FlatBatchAddFlowKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.remove.flow._case.FlatBatchRemoveFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.remove.flow._case.FlatBatchRemoveFlowBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.remove.flow._case.FlatBatchRemoveFlowKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.update.flow._case.FlatBatchUpdateFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.update.flow._case.FlatBatchUpdateFlowBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.flow.crud._case.aug.flat.batch.update.flow._case.FlatBatchUpdateFlowKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.FlatBatchAddGroupCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.FlatBatchAddGroupCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.FlatBatchRemoveGroupCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.FlatBatchRemoveGroupCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.FlatBatchUpdateGroupCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.FlatBatchUpdateGroupCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.add.group._case.FlatBatchAddGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.add.group._case.FlatBatchAddGroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.add.group._case.FlatBatchAddGroupKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.remove.group._case.FlatBatchRemoveGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.remove.group._case.FlatBatchRemoveGroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.remove.group._case.FlatBatchRemoveGroupKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.update.group._case.FlatBatchUpdateGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.update.group._case.FlatBatchUpdateGroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.group.crud._case.aug.flat.batch.update.group._case.FlatBatchUpdateGroupKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.FlatBatchAddMeterCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.FlatBatchAddMeterCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.FlatBatchRemoveMeterCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.FlatBatchRemoveMeterCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.FlatBatchUpdateMeterCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.FlatBatchUpdateMeterCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.add.meter._case.FlatBatchAddMeter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.add.meter._case.FlatBatchAddMeterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.add.meter._case.FlatBatchAddMeterKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.remove.meter._case.FlatBatchRemoveMeter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.remove.meter._case.FlatBatchRemoveMeterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.remove.meter._case.FlatBatchRemoveMeterKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.update.meter._case.FlatBatchUpdateMeter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.update.meter._case.FlatBatchUpdateMeterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.flat.batch.meter.crud._case.aug.flat.batch.update.meter._case.FlatBatchUpdateMeterKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.Batch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.BatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.BatchChoice;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCaseBuilder;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
-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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
@@ -79,7 +89,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groups.service.rev160315.ba
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.input.update.grouping.OriginalBatchedMeterBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meters.service.rev160316.batch.meter.input.update.grouping.UpdatedBatchedMeterBuilder;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap.Builder;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.Uint16;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -134,7 +147,7 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
                     .setNode(new NodeRef(PathUtil.digNodePath(diffInput.getNodeIdent())))
                     // TODO: propagate from input
                     .setExitOnFirstError(false)
-                    .setBatch(batchBag)
+                    .setBatch(BindingMap.ordered(batchBag))
                     .build();
 
             final ListenableFuture<RpcResult<ProcessFlatBatchOutput>> rpcResultFuture =
@@ -152,16 +165,16 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
         return resultVehicle;
     }
 
-    private FutureCallback<RpcResult<ProcessFlatBatchOutput>> createCounterCallback(final List<Batch> inputBatchBag,
-                                                                                    final int failureIndexLimit,
-                                                                                    final SyncCrudCounters counters) {
-        return new FutureCallback<RpcResult<ProcessFlatBatchOutput>>() {
+    private static FutureCallback<RpcResult<ProcessFlatBatchOutput>> createCounterCallback(
+            final List<Batch> inputBatchBag, final int failureIndexLimit, final SyncCrudCounters counters) {
+        return new FutureCallback<>() {
             @Override
             public void onSuccess(final RpcResult<ProcessFlatBatchOutput> result) {
                 if (!result.isSuccessful() && result.getResult() != null
-                        && !result.getResult().getBatchFailure().isEmpty()) {
-                    Map<Range<Integer>, Batch> batchMap = mapBatchesToRanges(inputBatchBag, failureIndexLimit);
-                    decrementBatchFailuresCounters(result.getResult().getBatchFailure(), batchMap, counters);
+                        && !result.getResult().nonnullBatchFailure().isEmpty()) {
+                    Map<Range<Uint16>, Batch> batchMap = mapBatchesToRanges(inputBatchBag, failureIndexLimit);
+                    decrementBatchFailuresCounters(result.getResult().nonnullBatchFailure().values(), batchMap,
+                            counters);
                 }
             }
 
@@ -172,11 +185,11 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
         };
     }
 
-    private static void decrementBatchFailuresCounters(final List<BatchFailure> batchFailures,
-                                                final Map<Range<Integer>, Batch> batchMap,
+    private static void decrementBatchFailuresCounters(final Collection<BatchFailure> batchFailures,
+                                                final Map<Range<Uint16>, Batch> batchMap,
                                                 final SyncCrudCounters counters) {
         for (BatchFailure batchFailure : batchFailures) {
-            for (Map.Entry<Range<Integer>, Batch> rangeBatchEntry : batchMap.entrySet()) {
+            for (Map.Entry<Range<Uint16>, Batch> rangeBatchEntry : batchMap.entrySet()) {
                 if (rangeBatchEntry.getKey().contains(batchFailure.getBatchOrder())) {
                     // get type and decrease
                     final BatchChoice batchChoice = rangeBatchEntry.getValue().getBatchChoice();
@@ -209,21 +222,21 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
         }
     }
 
-    static Map<Range<Integer>, Batch> mapBatchesToRanges(final List<Batch> inputBatchBag, final int failureIndexLimit) {
-        final Map<Range<Integer>, Batch> batchMap = new LinkedHashMap<>();
+    static Map<Range<Uint16>, Batch> mapBatchesToRanges(final List<Batch> inputBatchBag, final int failureIndexLimit) {
+        final Map<Range<Uint16>, Batch> batchMap = new LinkedHashMap<>();
         final PeekingIterator<Batch> batchPeekingIterator = Iterators.peekingIterator(inputBatchBag.iterator());
         while (batchPeekingIterator.hasNext()) {
             final Batch batch = batchPeekingIterator.next();
             final int nextBatchOrder = batchPeekingIterator.hasNext()
-                    ? batchPeekingIterator.peek().getBatchOrder()
+                    ? batchPeekingIterator.peek().getBatchOrder().toJava()
                     : failureIndexLimit;
-            batchMap.put(Range.closed(batch.getBatchOrder(), nextBatchOrder - 1), batch);
+            batchMap.put(Range.closed(batch.getBatchOrder(), Uint16.valueOf(nextBatchOrder - 1)), batch);
         }
         return batchMap;
     }
 
     @VisibleForTesting
-    static int assembleRemoveFlows(final List<Batch> batchBag, int batchOrder,
+    static int assembleRemoveFlows(final List<Batch> batchBag, final int batchOrder,
             final Map<TableKey, ItemSyncBox<Flow>> flowItemSyncTableMap) {
         // process flow remove
         int order = batchOrder;
@@ -232,20 +245,20 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
                 final ItemSyncBox<Flow> flowItemSyncBox = syncBoxEntry.getValue();
 
                 if (!flowItemSyncBox.getItemsToPush().isEmpty()) {
-                    final List<FlatBatchRemoveFlow> flatBatchRemoveFlowBag =
-                            new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
+                    final Builder<FlatBatchRemoveFlowKey, FlatBatchRemoveFlow> flatBatchRemoveFlowBag =
+                            BindingMap.orderedBuilder(flowItemSyncBox.getItemsToUpdate().size());
                     int itemOrder = 0;
                     for (Flow flow : flowItemSyncBox.getItemsToPush()) {
                         flatBatchRemoveFlowBag.add(new FlatBatchRemoveFlowBuilder(flow)
-                                .setBatchOrder(itemOrder++)
+                                .setBatchOrder(Uint16.valueOf(itemOrder++))
                                 .setFlowId(flow.getId())
                                 .build());
                     }
                     final Batch batch = new BatchBuilder()
                             .setBatchChoice(new FlatBatchRemoveFlowCaseBuilder()
-                                    .setFlatBatchRemoveFlow(flatBatchRemoveFlowBag)
+                                    .setFlatBatchRemoveFlow(flatBatchRemoveFlowBag.build())
                                     .build())
-                            .setBatchOrder(order)
+                            .setBatchOrder(Uint16.valueOf(order))
                             .build();
                     order += itemOrder;
                     batchBag.add(batch);
@@ -256,46 +269,46 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
     }
 
     @VisibleForTesting
-    static int assembleAddOrUpdateGroups(final List<Batch> batchBag, int batchOrder,
+    static int assembleAddOrUpdateGroups(final List<Batch> batchBag, final int batchOrder,
             final List<ItemSyncBox<Group>> groupsToAddOrUpdate) {
         // process group add+update
         int order = batchOrder;
         if (groupsToAddOrUpdate != null) {
             for (ItemSyncBox<Group> groupItemSyncBox : groupsToAddOrUpdate) {
                 if (!groupItemSyncBox.getItemsToPush().isEmpty()) {
-                    final List<FlatBatchAddGroup> flatBatchAddGroupBag =
-                            new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
+                    final Builder<FlatBatchAddGroupKey, FlatBatchAddGroup> flatBatchAddGroupBag =
+                        BindingMap.orderedBuilder(groupItemSyncBox.getItemsToPush().size());
                     int itemOrder = 0;
                     for (Group group : groupItemSyncBox.getItemsToPush()) {
                         flatBatchAddGroupBag.add(new FlatBatchAddGroupBuilder(group)
-                                .setBatchOrder(itemOrder++).build());
+                                .setBatchOrder(Uint16.valueOf(itemOrder++)).build());
                     }
                     final Batch batch = new BatchBuilder()
                             .setBatchChoice(new FlatBatchAddGroupCaseBuilder()
-                                    .setFlatBatchAddGroup(flatBatchAddGroupBag)
+                                    .setFlatBatchAddGroup(flatBatchAddGroupBag.build())
                                     .build())
-                            .setBatchOrder(order)
+                            .setBatchOrder(Uint16.valueOf(order))
                             .build();
                     order += itemOrder;
                     batchBag.add(batch);
                 }
 
                 if (!groupItemSyncBox.getItemsToUpdate().isEmpty()) {
-                    final List<FlatBatchUpdateGroup> flatBatchUpdateGroupBag =
-                            new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
+                    final Builder<FlatBatchUpdateGroupKey, FlatBatchUpdateGroup> flatBatchUpdateGroupBag =
+                        BindingMap.orderedBuilder(groupItemSyncBox.getItemsToUpdate().size());
                     int itemOrder = 0;
                     for (ItemSyncBox.ItemUpdateTuple<Group> groupUpdate : groupItemSyncBox.getItemsToUpdate()) {
                         flatBatchUpdateGroupBag.add(new FlatBatchUpdateGroupBuilder()
-                            .setBatchOrder(itemOrder++)
+                            .setBatchOrder(Uint16.valueOf(itemOrder++))
                             .setOriginalBatchedGroup(new OriginalBatchedGroupBuilder(groupUpdate.getOriginal()).build())
                             .setUpdatedBatchedGroup(new UpdatedBatchedGroupBuilder(groupUpdate.getUpdated()).build())
                             .build());
                     }
                     final Batch batch = new BatchBuilder()
                             .setBatchChoice(new FlatBatchUpdateGroupCaseBuilder()
-                                    .setFlatBatchUpdateGroup(flatBatchUpdateGroupBag)
+                                    .setFlatBatchUpdateGroup(flatBatchUpdateGroupBag.build())
                                     .build())
-                            .setBatchOrder(order)
+                            .setBatchOrder(Uint16.valueOf(order))
                             .build();
                     order += itemOrder;
                     batchBag.add(batch);
@@ -306,25 +319,25 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
     }
 
     @VisibleForTesting
-    static int assembleRemoveGroups(final List<Batch> batchBag, int batchOrder,
+    static int assembleRemoveGroups(final List<Batch> batchBag, final int batchOrder,
             final List<ItemSyncBox<Group>> groupsToRemoveOrUpdate) {
         // process group add+update
         int order = batchOrder;
         if (groupsToRemoveOrUpdate != null) {
             for (ItemSyncBox<Group> groupItemSyncBox : groupsToRemoveOrUpdate) {
                 if (!groupItemSyncBox.getItemsToPush().isEmpty()) {
-                    final List<FlatBatchRemoveGroup> flatBatchRemoveGroupBag =
-                            new ArrayList<>(groupItemSyncBox.getItemsToUpdate().size());
+                    final Builder<FlatBatchRemoveGroupKey, FlatBatchRemoveGroup> flatBatchRemoveGroupBag =
+                        BindingMap.orderedBuilder(groupItemSyncBox.getItemsToUpdate().size());
                     int itemOrder = 0;
                     for (Group group : groupItemSyncBox.getItemsToPush()) {
                         flatBatchRemoveGroupBag.add(new FlatBatchRemoveGroupBuilder(group)
-                                .setBatchOrder(itemOrder++).build());
+                                .setBatchOrder(Uint16.valueOf(itemOrder++)).build());
                     }
                     final Batch batch = new BatchBuilder()
                             .setBatchChoice(new FlatBatchRemoveGroupCaseBuilder()
-                                    .setFlatBatchRemoveGroup(flatBatchRemoveGroupBag)
+                                    .setFlatBatchRemoveGroup(flatBatchRemoveGroupBag.build())
                                     .build())
-                            .setBatchOrder(order)
+                            .setBatchOrder(Uint16.valueOf(order))
                             .build();
                     order += itemOrder;
                     batchBag.add(batch);
@@ -335,44 +348,46 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
     }
 
     @VisibleForTesting
-    static int assembleAddOrUpdateMeters(final List<Batch> batchBag, int batchOrder,
+    static int assembleAddOrUpdateMeters(final List<Batch> batchBag, final int batchOrder,
             final ItemSyncBox<Meter> meterItemSyncBox) {
         // process meter add+update
         int order = batchOrder;
         if (meterItemSyncBox != null) {
             if (!meterItemSyncBox.getItemsToPush().isEmpty()) {
-                final List<FlatBatchAddMeter> flatBatchAddMeterBag =
-                        new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
+                final Builder<FlatBatchAddMeterKey, FlatBatchAddMeter> flatBatchAddMeterBag =
+                    BindingMap.orderedBuilder(meterItemSyncBox.getItemsToPush().size());
                 int itemOrder = 0;
                 for (Meter meter : meterItemSyncBox.getItemsToPush()) {
-                    flatBatchAddMeterBag.add(new FlatBatchAddMeterBuilder(meter).setBatchOrder(itemOrder++).build());
+                    flatBatchAddMeterBag.add(new FlatBatchAddMeterBuilder(meter)
+                        .setBatchOrder(Uint16.valueOf(itemOrder++))
+                        .build());
                 }
                 final Batch batch = new BatchBuilder()
                         .setBatchChoice(new FlatBatchAddMeterCaseBuilder()
-                                .setFlatBatchAddMeter(flatBatchAddMeterBag)
+                                .setFlatBatchAddMeter(flatBatchAddMeterBag.build())
                                 .build())
-                        .setBatchOrder(order)
+                        .setBatchOrder(Uint16.valueOf(order))
                         .build();
                 order += itemOrder;
                 batchBag.add(batch);
             }
 
             if (!meterItemSyncBox.getItemsToUpdate().isEmpty()) {
-                final List<FlatBatchUpdateMeter> flatBatchUpdateMeterBag =
-                        new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
+                final Builder<FlatBatchUpdateMeterKey, FlatBatchUpdateMeter> flatBatchUpdateMeterBag =
+                        BindingMap.orderedBuilder(meterItemSyncBox.getItemsToUpdate().size());
                 int itemOrder = 0;
                 for (ItemSyncBox.ItemUpdateTuple<Meter> meterUpdate : meterItemSyncBox.getItemsToUpdate()) {
                     flatBatchUpdateMeterBag.add(new FlatBatchUpdateMeterBuilder()
-                            .setBatchOrder(itemOrder++)
+                            .setBatchOrder(Uint16.valueOf(itemOrder++))
                             .setOriginalBatchedMeter(new OriginalBatchedMeterBuilder(meterUpdate.getOriginal()).build())
                             .setUpdatedBatchedMeter(new UpdatedBatchedMeterBuilder(meterUpdate.getUpdated()).build())
                             .build());
                 }
                 final Batch batch = new BatchBuilder()
                         .setBatchChoice(new FlatBatchUpdateMeterCaseBuilder()
-                                .setFlatBatchUpdateMeter(flatBatchUpdateMeterBag)
+                                .setFlatBatchUpdateMeter(flatBatchUpdateMeterBag.build())
                                 .build())
-                        .setBatchOrder(order)
+                        .setBatchOrder(Uint16.valueOf(order))
                         .build();
                 order += itemOrder;
                 batchBag.add(batch);
@@ -382,22 +397,24 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
     }
 
     @VisibleForTesting
-    static int assembleRemoveMeters(final List<Batch> batchBag, int batchOrder,
+    static int assembleRemoveMeters(final List<Batch> batchBag, final int batchOrder,
             final ItemSyncBox<Meter> meterItemSyncBox) {
         // process meter remove
         int order = batchOrder;
         if (meterItemSyncBox != null && !meterItemSyncBox.getItemsToPush().isEmpty()) {
-            final List<FlatBatchRemoveMeter> flatBatchRemoveMeterBag =
-                    new ArrayList<>(meterItemSyncBox.getItemsToUpdate().size());
+            final Builder<FlatBatchRemoveMeterKey, FlatBatchRemoveMeter> flatBatchRemoveMeterBag =
+                BindingMap.orderedBuilder(meterItemSyncBox.getItemsToPush().size());
             int itemOrder = 0;
             for (Meter meter : meterItemSyncBox.getItemsToPush()) {
-                flatBatchRemoveMeterBag.add(new FlatBatchRemoveMeterBuilder(meter).setBatchOrder(itemOrder++).build());
+                flatBatchRemoveMeterBag.add(new FlatBatchRemoveMeterBuilder(meter)
+                    .setBatchOrder(Uint16.valueOf(itemOrder++))
+                    .build());
             }
             final Batch batch = new BatchBuilder()
                     .setBatchChoice(new FlatBatchRemoveMeterCaseBuilder()
-                            .setFlatBatchRemoveMeter(flatBatchRemoveMeterBag)
+                            .setFlatBatchRemoveMeter(flatBatchRemoveMeterBag.build())
                             .build())
-                    .setBatchOrder(order)
+                    .setBatchOrder(Uint16.valueOf(order))
                     .build();
             order += itemOrder;
             batchBag.add(batch);
@@ -406,7 +423,7 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
     }
 
     @VisibleForTesting
-    static int assembleAddOrUpdateFlows(final List<Batch> batchBag, int batchOrder,
+    static int assembleAddOrUpdateFlows(final List<Batch> batchBag, final int batchOrder,
             final Map<TableKey, ItemSyncBox<Flow>> flowItemSyncTableMap) {
         // process flow add+update
         int order = batchOrder;
@@ -415,32 +432,32 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
                 final ItemSyncBox<Flow> flowItemSyncBox = syncBoxEntry.getValue();
 
                 if (!flowItemSyncBox.getItemsToPush().isEmpty()) {
-                    final List<FlatBatchAddFlow> flatBatchAddFlowBag =
-                            new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
+                    final Builder<FlatBatchAddFlowKey, FlatBatchAddFlow> flatBatchAddFlowBag =
+                        BindingMap.orderedBuilder(flowItemSyncBox.getItemsToPush().size());
                     int itemOrder = 0;
                     for (Flow flow : flowItemSyncBox.getItemsToPush()) {
                         flatBatchAddFlowBag.add(new FlatBatchAddFlowBuilder(flow)
-                                .setBatchOrder(itemOrder++)
+                                .setBatchOrder(Uint16.valueOf(itemOrder++))
                                 .setFlowId(flow.getId())
                                 .build());
                     }
                     final Batch batch = new BatchBuilder()
                             .setBatchChoice(new FlatBatchAddFlowCaseBuilder()
-                                    .setFlatBatchAddFlow(flatBatchAddFlowBag)
+                                    .setFlatBatchAddFlow(flatBatchAddFlowBag.build())
                                     .build())
-                            .setBatchOrder(order)
+                            .setBatchOrder(Uint16.valueOf(order))
                             .build();
                     order += itemOrder;
                     batchBag.add(batch);
                 }
 
                 if (!flowItemSyncBox.getItemsToUpdate().isEmpty()) {
-                    final List<FlatBatchUpdateFlow> flatBatchUpdateFlowBag =
-                            new ArrayList<>(flowItemSyncBox.getItemsToUpdate().size());
+                    final Builder<FlatBatchUpdateFlowKey, FlatBatchUpdateFlow> flatBatchUpdateFlowBag =
+                        BindingMap.orderedBuilder(flowItemSyncBox.getItemsToUpdate().size());
                     int itemOrder = 0;
                     for (ItemSyncBox.ItemUpdateTuple<Flow> flowUpdate : flowItemSyncBox.getItemsToUpdate()) {
                         flatBatchUpdateFlowBag.add(new FlatBatchUpdateFlowBuilder()
-                            .setBatchOrder(itemOrder++)
+                            .setBatchOrder(Uint16.valueOf(itemOrder++))
                             .setFlowId(flowUpdate.getUpdated().getId())
                             .setOriginalBatchedFlow(new OriginalBatchedFlowBuilder(flowUpdate.getOriginal()).build())
                             .setUpdatedBatchedFlow(new UpdatedBatchedFlowBuilder(flowUpdate.getUpdated()).build())
@@ -448,9 +465,9 @@ public class SyncPlanPushStrategyFlatBatchImpl implements SyncPlanPushStrategy {
                     }
                     final Batch batch = new BatchBuilder()
                             .setBatchChoice(new FlatBatchUpdateFlowCaseBuilder()
-                                    .setFlatBatchUpdateFlow(flatBatchUpdateFlowBag)
+                                    .setFlatBatchUpdateFlow(flatBatchUpdateFlowBag.build())
                                     .build())
-                            .setBatchOrder(order)
+                            .setBatchOrder(Uint16.valueOf(order))
                             .build();
                     order += itemOrder;
                     batchBag.add(batch);