Migrate users of Optional.get()
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / flow / FlowStatsResponseConvertor.java
index 84c8b69e2a51821c5e7d66f43b495d295563c250..192c83cfca49970a7931905afa86912c578ef77f 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow;
 
 import java.util.ArrayList;
@@ -17,12 +16,11 @@ import java.util.Set;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
-import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.FlowStatsResponseConvertorData;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
@@ -35,16 +33,17 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.M
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.duration.DurationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
-import org.opendaylight.yangtools.yang.binding.DataContainer;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 /**
  * Converts flow related statistics messages coming from openflow switch to MD-SAL messages.
  *
+ * <p>
  * Example usage:
  * <pre>
  * {@code
@@ -54,49 +53,51 @@ import org.opendaylight.yangtools.yang.binding.DataContainer;
  * }
  * </pre>
  */
-public class FlowStatsResponseConvertor extends Convertor<List<FlowStats>, List<FlowAndStatisticsMapList>, VersionDatapathIdConvertorData> {
+public class FlowStatsResponseConvertor extends Convertor<List<FlowStats>, List<FlowAndStatisticsMapList>,
+        FlowStatsResponseConvertorData> {
 
-    private static final Set<Class<? extends DataContainer>> TYPES = Collections.singleton(FlowStats.class);
+    private static final Set<Class<?>> TYPES = Collections.singleton(FlowStats.class);
 
     /**
-     * Method wraps openflow 1.0 actions list to Apply Action Instructions
+     * Method wraps openflow 1.0 actions list to Apply Action Instructions.
      *
      * @param actionsList list of action
+     * @param ipProtocol ip protocol
      * @return OF10 actions as an instructions
      */
-    private Instructions wrapOF10ActionsToInstruction(List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action> actionsList, final short version) {
+    private Instructions wrapOF10ActionsToInstruction(
+            final List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions
+                .grouping.Action> actionsList,
+            final Uint8 version, final Uint8 ipProtocol) {
         ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(version);
-        actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION);
-
-        InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
-        List<Instruction> salInstructionList = new ArrayList<>();
+        actionResponseConvertorData.setActionPath(ActionPath.FLOWS_STATISTICS_UPDATE_WRITE_ACTIONS);
+        actionResponseConvertorData.setIpProtocol(ipProtocol);
 
         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
         ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
 
-        final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>> actions = getConvertorExecutor().convert(
-                actionsList, actionResponseConvertorData);
+        final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>>
+            actions = getConvertorExecutor().convert(actionsList, actionResponseConvertorData);
 
         applyActionsBuilder.setAction(FlowConvertorUtil.wrapActionList(actions.orElse(Collections.emptyList())));
         applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
 
-        InstructionBuilder instBuilder = new InstructionBuilder();
-        instBuilder.setInstruction(applyActionsCaseBuilder.build());
-        instBuilder.setKey(new InstructionKey(0));
-        instBuilder.setOrder(0);
-        salInstructionList.add(instBuilder.build());
-
-        instructionsBuilder.setInstruction(salInstructionList);
-        return instructionsBuilder.build();
+        return new InstructionsBuilder()
+            .setInstruction(BindingMap.of(new InstructionBuilder()
+                .setInstruction(applyActionsCaseBuilder.build())
+                .setOrder(0)
+                .build()))
+            .build();
     }
 
     @Override
-    public Collection<Class<? extends DataContainer>> getTypes() {
+    public Collection<Class<?>> getTypes() {
         return TYPES;
     }
 
     @Override
-    public List<FlowAndStatisticsMapList> convert(List<FlowStats> source, VersionDatapathIdConvertorData data) {
+    public List<FlowAndStatisticsMapList> convert(final List<FlowStats> source,
+            final FlowStatsResponseConvertorData data) {
         final List<FlowAndStatisticsMapList> result = new ArrayList<>();
 
         for (FlowStats flowStats : source) {
@@ -120,31 +121,41 @@ public class FlowStatsResponseConvertor extends Convertor<List<FlowStats>, List<
             salFlowStatsBuilder.setTableId(flowStats.getTableId());
 
             if (flowStats.getMatchV10() != null) {
-                final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(flowStats.getMatchV10(), data);
+                final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(
+                        flowStats.getMatchV10(), data);
+                Uint8 ipProtocol = null;
 
                 if (matchBuilderOptional.isPresent()) {
-                    salFlowStatsBuilder.setMatch(matchBuilderOptional.get().build());
+                    final MatchBuilder matchBuilder = matchBuilderOptional.orElseThrow();
+                    final IpMatch ipMatch = matchBuilder.getIpMatch();
+                    if (ipMatch != null) {
+                        ipProtocol = ipMatch.getIpProtocol();
+                    }
+
+                    salFlowStatsBuilder.setMatch(matchBuilder.build());
                 }
 
                 if (flowStats.getAction() != null && flowStats.getAction().size() != 0) {
-                    salFlowStatsBuilder.setInstructions(wrapOF10ActionsToInstruction(flowStats.getAction(), data.getVersion()));
+                    salFlowStatsBuilder.setInstructions(wrapOF10ActionsToInstruction(flowStats.getAction(),
+                            data.getVersion(), ipProtocol));
                 }
             }
 
             if (flowStats.getMatch() != null) {
-                final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(flowStats.getMatch(), data);
+                final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(
+                        flowStats.getMatch(), data);
 
                 if (matchBuilderOptional.isPresent()) {
-                    final MatchBuilder matchBuilder = matchBuilderOptional.get();
+                    final MatchBuilder matchBuilder = matchBuilderOptional.orElseThrow();
 
                     final AugmentTuple<Match> matchExtensionWrap =
                             MatchExtensionHelper.processAllExtensions(
-                                    flowStats.getMatch().getMatchEntry(),
+                                    flowStats.getMatch().nonnullMatchEntry(),
                                     OpenflowVersion.get(data.getVersion()),
-                                    MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
+                                    data.getMatchPath());
 
                     if (matchExtensionWrap != null) {
-                        matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
+                        matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationObject());
                     }
 
                     salFlowStatsBuilder.setMatch(matchBuilder.build());
@@ -152,11 +163,11 @@ public class FlowStatsResponseConvertor extends Convertor<List<FlowStats>, List<
 
 
                 salFlowStatsBuilder.setFlags(
-                        new FlowModFlags(flowStats.getFlags().isOFPFFCHECKOVERLAP(),
-                                flowStats.getFlags().isOFPFFRESETCOUNTS(),
-                                flowStats.getFlags().isOFPFFNOPKTCOUNTS(),
-                                flowStats.getFlags().isOFPFFNOBYTCOUNTS(),
-                                flowStats.getFlags().isOFPFFSENDFLOWREM()));
+                        new FlowModFlags(flowStats.getFlags().getOFPFFCHECKOVERLAP(),
+                                flowStats.getFlags().getOFPFFRESETCOUNTS(),
+                                flowStats.getFlags().getOFPFFNOPKTCOUNTS(),
+                                flowStats.getFlags().getOFPFFNOBYTCOUNTS(),
+                                flowStats.getFlags().getOFPFFSENDFLOWREM()));
             }
 
             if (flowStats.getInstruction() != null) {
@@ -165,7 +176,7 @@ public class FlowStatsResponseConvertor extends Convertor<List<FlowStats>, List<
                         flowStats.getInstruction(), simpleConvertorData);
 
                 salFlowStatsBuilder.setInstructions(instructions.orElse(new InstructionsBuilder()
-                        .setInstruction(Collections.emptyList()).build()));
+                        .setInstruction(Collections.emptyMap()).build()));
             }
 
             result.add(salFlowStatsBuilder.build());