Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / FlatBatchUtil.java
index 4e0c14529f2aaf2dacf0cd68d02a25aee22ee376..25f1179612805e05f5b5a69e90e2eee6ea89db89 100644 (file)
@@ -14,26 +14,30 @@ 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.EnumSet;
 import java.util.List;
+import java.util.Map;
 import org.opendaylight.openflowplugin.impl.services.batch.BatchPlanStep;
 import org.opendaylight.openflowplugin.impl.services.batch.BatchStepType;
 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.ProcessFlatBatchOutputBuilder;
+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.FlatBatchRemoveFlowCase;
+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.group.crud._case.aug.FlatBatchAddGroupCase;
+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.FlatBatchUpdateGroupCase;
+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.FlatBatchRemoveMeterCase;
+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.process.flat.batch.input.Batch;
 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.FlatBatchAddGroupCase;
-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.FlatBatchRemoveFlowCase;
-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.FlatBatchRemoveMeterCase;
-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.FlatBatchUpdateGroupCase;
-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.output.BatchFailure;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.service.batch.common.rev160322.BatchOrderGrouping;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -42,9 +46,8 @@ import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
  * Provides flat batch util methods.
  */
 public final class FlatBatchUtil {
-
     private FlatBatchUtil() {
-        throw new IllegalStateException("This class should not be instantiated.");
+        // Hidden on purpose
     }
 
     public static void markBarriersWhereNeeded(final List<BatchPlanStep> batchPlan) {
@@ -77,14 +80,14 @@ public final class FlatBatchUtil {
     }
 
     private static boolean isGroupBarrierNeeded(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
-        return (type == BatchStepType.GROUP_ADD
+        return type == BatchStepType.GROUP_ADD
                 && (previousTypes.contains(BatchStepType.GROUP_ADD)
-                || previousTypes.contains(BatchStepType.GROUP_UPDATE)))
-                || (type == BatchStepType.GROUP_REMOVE
+                || previousTypes.contains(BatchStepType.GROUP_UPDATE))
+                || type == BatchStepType.GROUP_REMOVE
                 && (previousTypes.contains(BatchStepType.FLOW_REMOVE)
                 || previousTypes.contains(BatchStepType.FLOW_UPDATE)
                 || previousTypes.contains(BatchStepType.GROUP_REMOVE)
-                || previousTypes.contains(BatchStepType.GROUP_UPDATE)));
+                || previousTypes.contains(BatchStepType.GROUP_UPDATE));
     }
 
     private static boolean isMeterBarrierNeeded(final EnumSet<BatchStepType> previousTypes, final BatchStepType type) {
@@ -93,7 +96,7 @@ public final class FlatBatchUtil {
                 || previousTypes.contains(BatchStepType.FLOW_UPDATE));
     }
 
-    public static List<BatchPlanStep> assembleBatchPlan(List<Batch> batches) {
+    public static List<BatchPlanStep> assembleBatchPlan(final Collection<Batch> batches) {
         final List<BatchPlanStep> plan = new ArrayList<>();
 
         BatchPlanStep planStep;
@@ -110,36 +113,36 @@ public final class FlatBatchUtil {
         return plan;
     }
 
-    private static List<? extends BatchOrderGrouping> extractBatchData(final BatchStepType batchStepType,
+    private static Collection<? extends BatchOrderGrouping> extractBatchData(final BatchStepType batchStepType,
                                                                        final BatchChoice batchChoice) {
-        final List<? extends BatchOrderGrouping> batchData;
+        final Collection<? extends BatchOrderGrouping> batchData;
         switch (batchStepType) {
             case FLOW_ADD:
-                batchData = ((FlatBatchAddFlowCase) batchChoice).getFlatBatchAddFlow();
+                batchData = ((FlatBatchAddFlowCase) batchChoice).nonnullFlatBatchAddFlow().values();
                 break;
             case FLOW_REMOVE:
-                batchData = ((FlatBatchRemoveFlowCase) batchChoice).getFlatBatchRemoveFlow();
+                batchData = ((FlatBatchRemoveFlowCase) batchChoice).nonnullFlatBatchRemoveFlow().values();
                 break;
             case FLOW_UPDATE:
-                batchData = ((FlatBatchUpdateFlowCase) batchChoice).getFlatBatchUpdateFlow();
+                batchData = ((FlatBatchUpdateFlowCase) batchChoice).nonnullFlatBatchUpdateFlow().values();
                 break;
             case GROUP_ADD:
-                batchData = ((FlatBatchAddGroupCase) batchChoice).getFlatBatchAddGroup();
+                batchData = ((FlatBatchAddGroupCase) batchChoice).nonnullFlatBatchAddGroup().values();
                 break;
             case GROUP_REMOVE:
-                batchData = ((FlatBatchRemoveGroupCase) batchChoice).getFlatBatchRemoveGroup();
+                batchData = ((FlatBatchRemoveGroupCase) batchChoice).nonnullFlatBatchRemoveGroup().values();
                 break;
             case GROUP_UPDATE:
-                batchData = ((FlatBatchUpdateGroupCase) batchChoice).getFlatBatchUpdateGroup();
+                batchData = ((FlatBatchUpdateGroupCase) batchChoice).nonnullFlatBatchUpdateGroup().values();
                 break;
             case METER_ADD:
-                batchData = ((FlatBatchAddMeterCase) batchChoice).getFlatBatchAddMeter();
+                batchData = ((FlatBatchAddMeterCase) batchChoice).nonnullFlatBatchAddMeter().values();
                 break;
             case METER_REMOVE:
-                batchData = ((FlatBatchRemoveMeterCase) batchChoice).getFlatBatchRemoveMeter();
+                batchData = ((FlatBatchRemoveMeterCase) batchChoice).nonnullFlatBatchRemoveMeter().values();
                 break;
             case METER_UPDATE:
-                batchData = ((FlatBatchUpdateMeterCase) batchChoice).getFlatBatchUpdateMeter();
+                batchData = ((FlatBatchUpdateMeterCase) batchChoice).nonnullFlatBatchUpdateMeter().values();
                 break;
             default:
                 throw new IllegalArgumentException("Unsupported batch step type obtained: " + batchStepType);
@@ -150,8 +153,9 @@ public final class FlatBatchUtil {
     @VisibleForTesting
     static <T extends BatchChoice> BatchStepType detectBatchStepType(final T batchCase) {
         final BatchStepType type;
-        final Class<? extends DataContainer> implementedInterface = batchCase.getImplementedInterface();
+        final Class<? extends DataContainer> implementedInterface = batchCase.implementedInterface();
 
+        // FIXME: use a lookup table instead of this cascade
         if (FlatBatchAddFlowCase.class.equals(implementedInterface)) {
             type = BatchStepType.FLOW_ADD;
         } else if (FlatBatchRemoveFlowCase.class.equals(implementedInterface)) {
@@ -181,19 +185,19 @@ public final class FlatBatchUtil {
         return jobsResults -> {
             boolean isSuccessful = true;
             List<RpcError> rpcErrors = new ArrayList<>();
-            List<BatchFailure> batchFailures = new ArrayList<>();
+            BindingMap.Builder<BatchFailureKey, BatchFailure> batchFailures = BindingMap.orderedBuilder();
 
             for (RpcResult<ProcessFlatBatchOutput> jobResult : jobsResults) {
                 if (jobResult != null) {
-                    isSuccessful = (isSuccessful && jobResult.isSuccessful());
+                    isSuccessful = isSuccessful && jobResult.isSuccessful();
                     rpcErrors.addAll(jobResult.getErrors());
-                    batchFailures.addAll(jobResult.getResult().getBatchFailure());
+                    batchFailures.addAll(jobResult.getResult().nonnullBatchFailure().values());
                 }
             }
 
             return RpcResultBuilder.<ProcessFlatBatchOutput>status(isSuccessful)
                     .withRpcErrors(rpcErrors)
-                    .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures).build())
+                    .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(batchFailures.build()).build())
                     .build();
         };
     }
@@ -219,9 +223,7 @@ public final class FlatBatchUtil {
             final boolean status) {
         return RpcResultBuilder.<ProcessFlatBatchOutput>status(status)
                                .withRpcErrors(new ArrayList<>())
-                               .withResult(new ProcessFlatBatchOutputBuilder()
-                                       .setBatchFailure(new ArrayList<>())
-                                       .build())
+                               .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(Map.of()).build())
                                .buildFuture();
     }
 }