Partial cleanup of convertors 90/94390/5
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Jan 2021 02:12:51 +0000 (03:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Jan 2021 09:52:55 +0000 (10:52 +0100)
Migrate away from unit/lists and refactor the code for clarity and
speed.

Change-Id: I142b49de0908fef3c014a516058e9ffe13c998e0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/PortConvertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/flow/FlowConvertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchV10Convertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ActionUtil.java

index 969529e260288eea13b8dbe689c8c1e422360d93..1c597d449f7d40849bcdaecff42d693d2e02ca08 100644 (file)
@@ -7,8 +7,9 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
 
+import static java.util.Objects.requireNonNullElse;
+
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.MoreObjects;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Set;
@@ -43,8 +44,9 @@ import org.opendaylight.yangtools.yang.common.Uint8;
  * </pre>
  */
 public class PortConvertor extends Convertor<Port, PortModInput, VersionConvertorData> {
-
     private static final Set<Class<?>> TYPES = Collections.singleton(Port.class);
+    private static final PortConfig PORTCONFIG = new PortConfig(true, true, true, true);
+    private static final PortConfigV10 PORTCONFIG_V10 = new PortConfigV10(true, true, true, true, true, true, true);
 
     /**
      * Create default empty port mod input
@@ -106,30 +108,20 @@ public class PortConvertor extends Convertor<Port, PortModInput, VersionConverto
     static Ports toPortDesc(
             final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
             final short version) {
-
-        PortsBuilder ofPortDescDataBuilder = new PortsBuilder();
-
-        ofPortDescDataBuilder.setPortNo(OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version),
-                source.getPortNumber())); // portNO
-
-        ofPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
-        ofPortDescDataBuilder.setName(source.getName());
-
-        PortConfig config = maskPortConfigFields(source.getConfiguration());
-
-        ofPortDescDataBuilder.setConfig(config);
-
-        PortState portState = getPortState(source.getState());
-
-        ofPortDescDataBuilder.setState(portState);
-        ofPortDescDataBuilder.setCurrentFeatures(getPortFeatures(source.getCurrentFeature()));
-        ofPortDescDataBuilder.setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()));
-        ofPortDescDataBuilder.setSupportedFeatures(getPortFeatures(source.getSupported()));
-        ofPortDescDataBuilder.setPeerFeatures(getPortFeatures(source.getPeerFeatures()));
-        ofPortDescDataBuilder.setCurrSpeed(source.getCurrentSpeed());
-        ofPortDescDataBuilder.setMaxSpeed(source.getMaximumSpeed());
-
-        return ofPortDescDataBuilder.build();
+        return new PortsBuilder()
+            // portNO
+            .setPortNo(OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber()))
+            .setHwAddr(source.getHardwareAddress())
+            .setName(source.getName())
+            .setConfig(maskPortConfigFields(source.getConfiguration()))
+            .setState(getPortState(source.getState()))
+            .setCurrentFeatures(getPortFeatures(source.getCurrentFeature()))
+            .setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()))
+            .setSupportedFeatures(getPortFeatures(source.getSupported()))
+            .setPeerFeatures(getPortFeatures(source.getPeerFeatures()))
+            .setCurrSpeed(source.getCurrentSpeed())
+            .setMaxSpeed(source.getMaximumSpeed())
+            .build();
 
     }
 
@@ -145,26 +137,20 @@ public class PortConvertor extends Convertor<Port, PortModInput, VersionConverto
 
     @Override
     public PortModInput convert(final Port source, final VersionConvertorData data) {
-        PortConfig config = maskPortConfigFields(source.getConfiguration());
-        PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
-
-        PortModInputBuilder portModInputBuilder = new PortModInputBuilder();
-        portModInputBuilder.setAdvertise(getPortFeatures(source.getAdvertisedFeatures()));
-        portModInputBuilder.setPortNo(new PortNumber(OpenflowPortsUtil.getProtocolPortNumber(
-                OpenflowVersion.get(data.getVersion()), source.getPortNumber())));
-
-        portModInputBuilder.setConfig(config);
-        portModInputBuilder.setMask(MoreObjects.firstNonNull(maskPortConfigFields(source.getMask()),
-                new PortConfig(true, true, true, true)));
-
-        portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
-
-        portModInputBuilder.setVersion(data.getVersion());
-
-        portModInputBuilder.setConfigV10(configV10);
-        portModInputBuilder.setMaskV10(MoreObjects.firstNonNull(maskPortConfigV10Fields(source.getMask()),
-                new PortConfigV10(true, true, true, true, true, true, true)));
-        portModInputBuilder.setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()));
-        return portModInputBuilder.build();
+        final var config = maskPortConfigFields(source.getConfiguration());
+        final var configV10 = maskPortConfigV10Fields(source.getConfiguration());
+
+        return new PortModInputBuilder()
+            .setAdvertise(getPortFeatures(source.getAdvertisedFeatures()))
+            .setPortNo(new PortNumber(OpenflowPortsUtil.getProtocolPortNumber(
+                OpenflowVersion.get(data.getVersion()), source.getPortNumber())))
+            .setConfig(config)
+            .setMask(requireNonNullElse(maskPortConfigFields(source.getMask()), PORTCONFIG))
+            .setHwAddress(new MacAddress(source.getHardwareAddress()))
+            .setVersion(data.getVersion())
+            .setConfigV10(configV10)
+            .setMaskV10(requireNonNullElse(maskPortConfigV10Fields(source.getMask()), PORTCONFIG_V10))
+            .setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()))
+            .build();
     }
 }
index d721674d26d13a48470b244c61275a97193e317b..484625a22bae99c15761ee8583144ee1a144fd91 100644 (file)
@@ -7,8 +7,9 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow;
 
-import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+import static java.util.Objects.requireNonNullElse;
+
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Ordering;
 import java.util.ArrayList;
@@ -18,7 +19,9 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionConvertorData;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorProcessor;
@@ -32,7 +35,6 @@ import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.flow.cases.WriteMetadataCase;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchInjector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.VlanCfi;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
@@ -52,6 +54,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.M
 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.ApplyActions;
 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.InstructionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
@@ -65,6 +68,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev13
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
+import org.opendaylight.yangtools.yang.binding.util.BindingMap;
 import org.opendaylight.yangtools.yang.common.Uint16;
 import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.common.Uint64;
@@ -143,7 +147,7 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
     private static final TableId DEFAULT_TABLE_ID = new TableId(Uint32.ZERO);
     private static final Uint32 DEFAULT_BUFFER_ID = OFConstants.OFP_NO_BUFFER;
     private static final Uint32 OFPP_ANY = Uint32.MAX_VALUE;
-    private static final Uint32 DEFAULT_OUT_PORT = OFPP_ANY;
+    private static final PortNumber DEFAULT_OUT_PORT = new PortNumber(OFPP_ANY);
     private static final Uint32 OFPG_ANY = Uint32.MAX_VALUE;
     private static final Uint32 DEFAULT_OUT_GROUP = OFPG_ANY;
     private static final Uint16 PUSH_VLAN = Uint16.valueOf(0x8100);
@@ -169,37 +173,28 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
 
     static {
         final VlanId zeroVlan = new VlanId(Uint16.ZERO);
-        VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
-        VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
-        vlanIdBuilder.setVlanIdPresent(false);
-        vlanIdBuilder.setVlanId(zeroVlan);
-        vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
-
-        VLAN_MATCH_FALSE = vlanMatchBuilder.build();
-
-        VlanMatchBuilder vlanMatchBuilder2 = new VlanMatchBuilder();
-        VlanIdBuilder vlanIdBuilder2 = new VlanIdBuilder();
-        vlanIdBuilder2.setVlanIdPresent(true);
-        vlanIdBuilder2.setVlanId(zeroVlan);
-        vlanMatchBuilder2.setVlanId(vlanIdBuilder2.build());
-
-        VLAN_MATCH_TRUE = vlanMatchBuilder2.build();
+        VLAN_MATCH_FALSE = new VlanMatchBuilder()
+            .setVlanId(new VlanIdBuilder().setVlanIdPresent(false).setVlanId(zeroVlan).build())
+            .build();
+        VLAN_MATCH_TRUE = new VlanMatchBuilder()
+            .setVlanId(new VlanIdBuilder().setVlanIdPresent(true).setVlanId(zeroVlan).build())
+            .build();
     }
 
     private FlowModInputBuilder toFlowModInput(final Flow flow,
             final VersionDatapathIdConvertorData versionConverterData) {
 
-        FlowModInputBuilder flowMod = new FlowModInputBuilder();
+        FlowModInputBuilder flowMod = new FlowModInputBuilder()
+            .setIdleTimeout(requireNonNullElse(flow.getIdleTimeout(), DEFAULT_IDLE_TIMEOUT))
+            .setHardTimeout(requireNonNullElse(flow.getHardTimeout(), DEFAULT_HARD_TIMEOUT))
+            .setPriority(requireNonNullElse(flow.getPriority(), DEFAULT_PRIORITY))
+            .setBufferId(requireNonNullElse(flow.getBufferId(), DEFAULT_BUFFER_ID))
+            .setOutGroup(requireNonNullElse(flow.getOutGroup(), DEFAULT_OUT_GROUP));
         salToOFFlowCookie(flow, flowMod);
         salToOFFlowCookieMask(flow, flowMod);
         salToOFFlowTableId(flow, flowMod);
         salToOFFlowCommand(flow, flowMod);
-        salToOFFlowIdleTimeout(flow, flowMod);
-        salToOFFlowHardTimeout(flow, flowMod);
-        salToOFFlowPriority(flow, flowMod);
-        salToOFFlowBufferId(flow, flowMod);
         salToOFFlowOutPort(flow, flowMod);
-        salToOFFlowOutGroup(flow, flowMod);
 
         // convert and inject flowFlags
         final Optional<Object> conversion = getConvertorExecutor().convert(flow.getFlags(), versionConverterData);
@@ -210,98 +205,43 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
         MatchInjector.inject(conversionMatch, flowMod, versionConverterData.getVersion());
 
         if (flow.getInstructions() != null) {
-            flowMod.setInstruction(toInstructions(flow, versionConverterData.getVersion(),
-                    versionConverterData.getDatapathId()));
-            flowMod.setAction(getActions(versionConverterData.getVersion(),
-                    versionConverterData.getDatapathId(), flow));
+            flowMod
+                .setInstruction(toInstructions(flow, versionConverterData.getVersion(),
+                    versionConverterData.getDatapathId()))
+                .setAction(getActions(versionConverterData.getVersion(), versionConverterData.getDatapathId(), flow));
         }
 
-        flowMod.setVersion(versionConverterData.getVersion());
-        return flowMod;
-    }
-
-    private static void salToOFFlowOutGroup(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getOutGroup() != null) {
-            flowMod.setOutGroup(flow.getOutGroup());
-        } else {
-            flowMod.setOutGroup(DEFAULT_OUT_GROUP);
-        }
+        return flowMod
+            .setVersion(versionConverterData.getVersion());
     }
 
     private static void salToOFFlowOutPort(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getOutPort() != null) {
-            flowMod.setOutPort(new PortNumber(flow.getOutPort().longValue()));
-        } else {
-            flowMod.setOutPort(new PortNumber(DEFAULT_OUT_PORT));
-        }
-    }
-
-    private static void salToOFFlowBufferId(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getBufferId() != null) {
-            flowMod.setBufferId(flow.getBufferId());
-        } else {
-            flowMod.setBufferId(DEFAULT_BUFFER_ID);
-        }
-    }
-
-    private static void salToOFFlowPriority(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getPriority() != null) {
-            flowMod.setPriority(flow.getPriority());
-        } else {
-            flowMod.setPriority(DEFAULT_PRIORITY);
-        }
-    }
-
-    private static void salToOFFlowHardTimeout(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getHardTimeout() != null) {
-            flowMod.setHardTimeout(flow.getHardTimeout());
-        } else {
-            flowMod.setHardTimeout(DEFAULT_HARD_TIMEOUT);
-        }
-    }
-
-    private static void salToOFFlowIdleTimeout(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getIdleTimeout() != null) {
-            flowMod.setIdleTimeout(flow.getIdleTimeout());
-        } else {
-            flowMod.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
-        }
+        final var outPort = flow.getOutPort();
+        flowMod.setOutPort(outPort != null ? new PortNumber(outPort.longValue()) : DEFAULT_OUT_PORT);
     }
 
     private static void salToOFFlowCommand(final Flow flow, final FlowModInputBuilder flowMod) {
         if (flow instanceof AddFlowInput || flow instanceof UpdatedFlow) {
             flowMod.setCommand(FlowModCommand.OFPFCADD);
         } else if (flow instanceof RemoveFlowInput) {
-            if (MoreObjects.firstNonNull(flow.getStrict(), Boolean.FALSE)) {
-                flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT);
-            } else {
-                flowMod.setCommand(FlowModCommand.OFPFCDELETE);
-            }
+            flowMod.setCommand(Boolean.TRUE.equals(flow.getStrict())
+                ? FlowModCommand.OFPFCDELETESTRICT : FlowModCommand.OFPFCDELETE);
         }
     }
 
     private static void salToOFFlowTableId(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getTableId() != null) {
-            flowMod.setTableId(new TableId(flow.getTableId().toUint32()));
-        } else {
-            flowMod.setTableId(DEFAULT_TABLE_ID);
-        }
+        final var tableId = flow.getTableId();
+        flowMod.setTableId(tableId != null ? new TableId(flow.getTableId().toUint32()) : DEFAULT_TABLE_ID);
     }
 
     private static void salToOFFlowCookieMask(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getCookieMask() != null) {
-            flowMod.setCookieMask(flow.getCookieMask().getValue());
-        } else {
-            flowMod.setCookieMask(OFConstants.DEFAULT_COOKIE_MASK);
-        }
+        final var mask = flow.getCookieMask();
+        flowMod.setCookieMask(mask != null ? mask.getValue() : OFConstants.DEFAULT_COOKIE_MASK);
     }
 
     private static void salToOFFlowCookie(final Flow flow, final FlowModInputBuilder flowMod) {
-        if (flow.getCookie() != null) {
-            flowMod.setCookie(flow.getCookie().getValue());
-        } else {
-            flowMod.setCookie(OFConstants.DEFAULT_COOKIE);
-        }
+        final var omNomNom = flow.getCookie();
+        flowMod.setCookie(omNomNom != null ? omNomNom.getValue() : OFConstants.DEFAULT_COOKIE);
     }
 
     private List<Instruction> toInstructions(final Flow flow, final short version, final Uint64 datapathid) {
@@ -310,18 +250,10 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
         data.setDatapathId(datapathid);
         data.setIpProtocol(FlowConvertorUtil.getIpProtocolFromFlow(flow));
 
-        Instructions instructions = flow.getInstructions();
-
-        for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
-                instruction : instructions.nonnullInstruction().values()) {
-            org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
-                curInstruction = instruction.getInstruction();
-
-            Optional<Instruction> result = PROCESSOR.process(curInstruction, data, getConvertorExecutor());
-
-            if (result.isPresent()) {
-                instructionsList.add(result.get());
-            }
+        final ConvertorExecutor convertor = getConvertorExecutor();
+        for (var instruction : flow.getInstructions().nonnullInstruction().values()) {
+            Optional<Instruction> result = PROCESSOR.process(instruction.getInstruction(), data, convertor);
+            result.ifPresent(instructionsList::add);
         }
 
         return instructionsList;
@@ -332,23 +264,18 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
         List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
             sortedInstructions = INSTRUCTION_ORDERING.sortedCopy(instructions.nonnullInstruction().values());
 
-        for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
-                instruction : sortedInstructions) {
-            org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
-                curInstruction = instruction.getInstruction();
-
+        for (var instruction : sortedInstructions) {
+            final var curInstruction = instruction.getInstruction();
             if (curInstruction instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026
                     .instruction.instruction.ApplyActionsCase) {
-                org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction
-                    .ApplyActionsCase applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types
-                            .rev131026.instruction.instruction.ApplyActionsCase) curInstruction;
-                ApplyActions applyActions = applyActionscase.getApplyActions();
+                final var applyActions = ((org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026
+                    .instruction.instruction.ApplyActionsCase) curInstruction).getApplyActions();
 
                 final ActionConvertorData data = new ActionConvertorData(version);
                 data.setDatapathId(datapathid);
                 data.setIpProtocol(FlowConvertorUtil.getIpProtocolFromFlow(flow));
                 Optional<List<Action>> result = getConvertorExecutor().convert(applyActions.getAction(), data);
-                return result.orElse(Collections.emptyList());
+                return result.orElse(List.of());
             }
         }
 
@@ -359,20 +286,16 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
     private static boolean isSetVlanIdActionCasePresent(final Flow flow) {
         // we are trying to find if there is a set-vlan-id action (OF1.0) action present in the flow.
         // If yes,then we would need to two flows
-        if (flow.getInstructions() != null && flow.getInstructions().getInstruction() != null) {
-            for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
-                    instruction : flow.getInstructions().nonnullInstruction().values()) {
-                org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
-                    curInstruction = instruction.getInstruction();
+        final var insns = flow.getInstructions();
+        if (insns != null) {
+            for (var instruction : insns.nonnullInstruction().values()) {
+                final var curInstruction = instruction.getInstruction();
 
                 if (curInstruction instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026
                         .instruction.instruction.ApplyActionsCase) {
-                    org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction
-                        .ApplyActionsCase applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types
-                                .rev131026.instruction.instruction.ApplyActionsCase) curInstruction;
-                    ApplyActions applyActions = applyActionscase.getApplyActions();
-                    for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action
-                            action : applyActions.nonnullAction().values()) {
+                    ApplyActions applyActions = ((org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types
+                        .rev131026.instruction.instruction.ApplyActionsCase) curInstruction).getApplyActions();
+                    for (var action : applyActions.nonnullAction().values()) {
                         if (action.getAction() instanceof SetVlanIdActionCase) {
                             return true;
                         }
@@ -397,19 +320,19 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
             final VersionDatapathIdConvertorData versionDatapathIdConverterData) {
         List<FlowModInputBuilder> list = new ArrayList<>(2);
 
-        final Match srcMatch = Preconditions.checkNotNull(srcFlow.getMatch());
+        final Match srcMatch = requireNonNull(srcFlow.getMatch());
         final VlanMatch srcVlanMatch = srcMatch.getVlanMatch();
         if (srcVlanMatch != null) {
             //create flow with setfield and match
             // match on vlan tag or vlanid with no mask
-            VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder(srcVlanMatch);
-            VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
-            vlanIdBuilder.setVlanIdPresent(srcVlanMatch.getVlanId().getVlanIdPresent());
-            vlanIdBuilder.setVlanId(srcVlanMatch.getVlanId().getVlanId());
-            vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
-            Match match = new MatchBuilder(srcMatch).setVlanMatch(vlanMatchBuilder.build()).build();
-
-            Optional<? extends Flow> optional = injectMatchToFlow(srcFlow, match);
+            Optional<? extends Flow> optional = injectMatchToFlow(srcFlow, new MatchBuilder(srcMatch)
+                .setVlanMatch(new VlanMatchBuilder(srcVlanMatch)
+                    .setVlanId(new VlanIdBuilder()
+                        .setVlanIdPresent(srcVlanMatch.getVlanId().getVlanIdPresent())
+                        .setVlanId(srcVlanMatch.getVlanId().getVlanId())
+                        .build())
+                    .build())
+                .build());
             if (optional.isPresent()) {
                 list.add(toFlowModInput(optional.get(), versionDatapathIdConverterData));
             }
@@ -417,17 +340,16 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
             // create 2 flows
             //flow 1
             // match on no vlan tag with no mask
-            Match match1 = new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_FALSE).build();
-
-            Optional<? extends Flow> optional1 = injectMatchAndAction(srcFlow, match1);
+            Optional<? extends Flow> optional1 = injectMatchAndAction(srcFlow,
+                new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_FALSE).build());
             if (optional1.isPresent()) {
                 list.add(toFlowModInput(optional1.get(), versionDatapathIdConverterData));
             }
 
             //flow2
             // match on vlan tag with mask
-            Match match2 = new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_TRUE).build();
-            Optional<? extends Flow> optional2 = injectMatchToFlow(srcFlow, match2);
+            Optional<? extends Flow> optional2 = injectMatchToFlow(srcFlow,
+                new MatchBuilder(srcMatch).setVlanMatch(VLAN_MATCH_TRUE).build());
             if (optional2.isPresent()) {
                 list.add(toFlowModInput(optional2.get(), versionDatapathIdConverterData));
             }
@@ -468,94 +390,78 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
         }
     }
 
-    private static List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
-            injectPushActionToInstruction(final Flow sourceFlow) {
-
-        Map<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey,
+    private static @NonNull Map<InstructionKey,
             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
-            srcInstructionList = sourceFlow.getInstructions().nonnullInstruction();
-
-        List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
-            targetInstructionList = new ArrayList<>(srcInstructionList.size());
-        List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>
-            targetActionList = new ArrayList<>();
-
-        org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder
-            instructionBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction
-                .list.InstructionBuilder();
+                injectPushActionToInstruction(final Flow sourceFlow) {
+        final var srcInstructionList = sourceFlow.getInstructions().nonnullInstruction();
+        final var targetInstructionList = BindingMap.<
+            org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey,
+            org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction>
+                orderedBuilder(srcInstructionList.size());
+        final var targetActionList = BindingMap.<
+            org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey,
+            org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>orderedBuilder();
+        final var instructionBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026
+            .instruction.list.InstructionBuilder();
 
-        for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction
-                srcInstruction : srcInstructionList.values()) {
-            org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
-                curSrcInstruction = srcInstruction.getInstruction();
+        for (var srcInstruction : srcInstructionList.values()) {
+            final var curSrcInstruction = srcInstruction.getInstruction();
 
             if (curSrcInstruction instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026
                     .instruction.instruction.ApplyActionsCase) {
-                org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction
-                    .ApplyActionsCase applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types
+                final var applyActionscase = (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types
                             .rev131026.instruction.instruction.ApplyActionsCase) curSrcInstruction;
-                ApplyActions applyActions = applyActionscase.getApplyActions();
-                Map<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey,
-                    org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>
-                     srcActionList = applyActions.nonnullAction();
+                final var srcActionList = applyActionscase.getApplyActions().nonnullAction();
 
                 int offset = 0;
-                for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action
-                        actionItem : srcActionList.values()) {
+                for (var actionItem : srcActionList.values()) {
                     // check if its a set-vlan-action. If yes, then add the injected-action
 
                     if (actionItem.getAction() instanceof SetVlanIdActionCase) {
-                        SetVlanIdActionCase setVlanIdActionCase = (SetVlanIdActionCase) actionItem.getAction();
-
-                        PushVlanActionCaseBuilder pushVlanActionCaseBuilder = new PushVlanActionCaseBuilder();
-                        PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
-
-                        pushVlanActionBuilder.setCfi(PUSH_CFI)
-                                .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
-                                .setEthernetType(PUSH_VLAN)
-                                .setTag(PUSH_TAG);
-                        pushVlanActionCaseBuilder.setPushVlanAction(pushVlanActionBuilder.build());
-                        PushVlanActionCase injectedAction = pushVlanActionCaseBuilder.build();
-
-                        ActionBuilder actionBuilder = new ActionBuilder();
-                        actionBuilder.setAction(injectedAction)
-                                .withKey(actionItem.key())
-                                .setOrder(actionItem.getOrder() + offset);
-
-                        targetActionList.add(actionBuilder.build());
+                        final var setVlanIdActionCase = (SetVlanIdActionCase) actionItem.getAction();
+
+                        targetActionList.add(new ActionBuilder()
+                            .setAction(new PushVlanActionCaseBuilder()
+                                .setPushVlanAction(new PushVlanActionBuilder()
+                                    .setCfi(PUSH_CFI)
+                                    .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
+                                    .setEthernetType(PUSH_VLAN)
+                                    .setTag(PUSH_TAG)
+                                    .build())
+                                .build())
+                            // FIXME: this looks like a mismatch
+                            .withKey(actionItem.key())
+                            .setOrder(actionItem.getOrder() + offset)
+                            .build());
                         offset++;
                     }
 
                     if (offset > 0) {
                         // we need to increment the order for all the actions added after injection
-                        ActionBuilder actionBuilder =
-                                new ActionBuilder(actionItem);
-                        actionBuilder.setOrder(actionItem.getOrder() + offset)
-                                .withKey(new ActionKey(actionItem.key().getOrder() + offset));
-                        actionItem = actionBuilder.build();
+                        final Integer newOrder = actionItem.getOrder() + offset;
+                        actionItem = new ActionBuilder(actionItem)
+                            .setOrder(newOrder)
+                            .withKey(new ActionKey(newOrder))
+                            .build();
                     }
 
                     targetActionList.add(actionItem);
                 }
 
-                ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
-                ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
-                applyActionsBuilder.setAction(targetActionList);
-                applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
-
-                instructionBuilder.setInstruction(applyActionsCaseBuilder.build());
+                instructionBuilder.setInstruction(new ApplyActionsCaseBuilder()
+                    .setApplyActions(new ApplyActionsBuilder().setAction(targetActionList.build()).build())
+                    .build());
             } else {
                 instructionBuilder.setInstruction(curSrcInstruction);
             }
 
-            instructionBuilder
-                    .withKey(srcInstruction.key())
-                    .setOrder(srcInstruction.getOrder());
-            targetInstructionList.add(instructionBuilder.build());
-
+            targetInstructionList.add(instructionBuilder
+                .withKey(srcInstruction.key())
+                .setOrder(srcInstruction.getOrder())
+                .build());
         }
 
-        return targetInstructionList;
+        return targetInstructionList.build();
     }
 
     @Override
index 693d769efcc77a903c754c9ddadbe3560963b9e0..aa44309b283b6cd9f900fa0c9e7fc4a751570460 100644 (file)
@@ -5,10 +5,8 @@
  * 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.match;
 
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -23,7 +21,6 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4Match;
@@ -34,7 +31,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
@@ -46,7 +42,7 @@ import org.opendaylight.yangtools.yang.common.Uint8;
  */
 public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvertorData> {
 
-    private static final List<Class<?>> TYPES = Arrays.asList(
+    private static final List<Class<?>> TYPES = List.of(
             Match.class,
             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match.class,
             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.mod.removed.Match.class,
@@ -74,46 +70,46 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
      */
     private static final Uint16 OFP_VLAN_NONE = Uint16.MAX_VALUE;
 
-    private static boolean convertL4UdpDstMatch(final MatchV10Builder matchBuilder,
-                                                final UdpMatch udpMatch) {
-        if (udpMatch.getUdpDestinationPort() != null) {
-            matchBuilder.setTpDst(udpMatch.getUdpDestinationPort().getValue());
+    private static boolean convertL4UdpDstMatch(final MatchV10Builder matchBuilder, final UdpMatch udpMatch) {
+        final var port = udpMatch.getUdpDestinationPort();
+        if (port != null) {
+            matchBuilder.setTpDst(port.getValue());
             return false;
         }
         return true;
     }
 
-    private static boolean convertL4UdpSrcMatch(final MatchV10Builder matchBuilder,
-                                                final UdpMatch udpMatch) {
-        if (udpMatch.getUdpSourcePort() != null) {
-            matchBuilder.setTpSrc(udpMatch.getUdpSourcePort().getValue());
+    private static boolean convertL4UdpSrcMatch(final MatchV10Builder matchBuilder, final UdpMatch udpMatch) {
+        final var port = udpMatch.getUdpSourcePort();
+        if (port != null) {
+            matchBuilder.setTpSrc(port.getValue());
             return false;
         }
         return true;
     }
 
-    private static boolean convertL4TpDstMatch(final MatchV10Builder matchBuilder,
-                                               final TcpMatch tcpMatch) {
-        if (tcpMatch.getTcpDestinationPort() != null) {
+    private static boolean convertL4TpDstMatch(final MatchV10Builder matchBuilder, final TcpMatch tcpMatch) {
+        final var port = tcpMatch.getTcpDestinationPort();
+        if (port != null) {
             matchBuilder.setTpDst(tcpMatch.getTcpDestinationPort().getValue());
             return false;
         }
         return true;
     }
 
-    private static boolean convertL4TpSrcMatch(final MatchV10Builder matchBuilder,
-                                               final TcpMatch tcpMatch) {
-        if (tcpMatch.getTcpSourcePort() != null) {
-            matchBuilder.setTpSrc(tcpMatch.getTcpSourcePort().getValue());
+    private static boolean convertL4TpSrcMatch(final MatchV10Builder matchBuilder, final TcpMatch tcpMatch) {
+        final var port = tcpMatch.getTcpSourcePort();
+        if (port != null) {
+            matchBuilder.setTpSrc(port.getValue());
             return false;
         }
         return true;
     }
 
-    private static boolean convertNwTos(final MatchV10Builder matchBuilder,
-                                        final IpMatch ipMatch) {
-        if (ipMatch.getIpDscp() != null) {
-            matchBuilder.setNwTos(ActionUtil.dscpToTos(ipMatch.getIpDscp().getValue().toJava()));
+    private static boolean convertNwTos(final MatchV10Builder matchBuilder, final IpMatch ipMatch) {
+        final var ipDscp = ipMatch.getIpDscp();
+        if (ipDscp != null) {
+            matchBuilder.setNwTos(ActionUtil.dscpToTos(ipDscp.getValue().toJava()));
             return false;
         }
         return true;
@@ -166,7 +162,7 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
     }
 
     private static boolean convertDlVlanPcp(final MatchV10Builder matchBuilder, final VlanMatch vlanMatch) {
-        final VlanPcp vlanPcp = vlanMatch.getVlanPcp();
+        final var vlanPcp = vlanMatch.getVlanPcp();
         if (vlanPcp != null) {
             matchBuilder.setDlVlanPcp(vlanPcp.getValue());
             return false;
@@ -175,19 +171,20 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
     }
 
     private static boolean convertDlVlan(final MatchV10Builder matchBuilder, final VlanMatch vlanMatch) {
-        final VlanId id = vlanMatch.getVlanId();
+        final var id = vlanMatch.getVlanId();
         if (id != null) {
             final Uint16 vlanId = id.getVlanId().getValue();
-            matchBuilder.setDlVlan(vlanId.toJava() == 0 ? OFP_VLAN_NONE : vlanId);
+            matchBuilder.setDlVlan(Uint16.ZERO.equals(vlanId) ? OFP_VLAN_NONE : vlanId);
             return false;
         }
         return true;
     }
 
     private static boolean convertEthernetDlType(final MatchV10Builder matchBuilder,
-                                                 final EthernetMatch ethernetMatch) {
-        if (ethernetMatch.getEthernetType() != null) {
-            matchBuilder.setDlType(ethernetMatch.getEthernetType().getType().getValue().intValue());
+            final EthernetMatch ethernetMatch) {
+        final var ethType = ethernetMatch.getEthernetType();
+        if (ethType != null) {
+            matchBuilder.setDlType(Uint16.valueOf(ethType.getType().getValue().intValue()));
             return false;
         }
         return true;
@@ -195,8 +192,9 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
 
     private static boolean convertEthernetDlSrc(final MatchV10Builder matchBuilder,
                                                 final EthernetMatch ethernetMatch) {
-        if (ethernetMatch.getEthernetSource() != null) {
-            matchBuilder.setDlSrc(ethernetMatch.getEthernetSource().getAddress());
+        final var ethSource = ethernetMatch.getEthernetSource();
+        if (ethSource != null) {
+            matchBuilder.setDlSrc(ethSource.getAddress());
             return false;
         }
         return true;
@@ -213,8 +211,8 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
 
     private static boolean convertInPortMatch(final MatchV10Builder matchBuilder, final NodeConnectorId inPort) {
         if (inPort != null) {
-            matchBuilder.setInPort(InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF10,
-                    inPort).intValue());
+            matchBuilder.setInPort(Uint16.valueOf(
+                InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.OF10, inPort).intValue()));
             return false;
         }
         return true;
@@ -319,11 +317,9 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
             }
         }
 
-        FlowWildcardsV10 wildCards = new FlowWildcardsV10(
-                dlDst, dlSsc, dlType, dlVlan,
-                dlVlanPcp, ipPort, nwProto, nwTos, tpDst, tpSrc);
-        matchBuilder.setWildcards(wildCards);
-
-        return matchBuilder.build();
+        return matchBuilder
+            .setWildcards(new FlowWildcardsV10(
+                dlDst, dlSsc, dlType, dlVlan, dlVlanPcp, ipPort, nwProto, nwTos, tpDst, tpSrc))
+            .build();
     }
 }
index c04be1e3460d2514d0437f9ee3109262d378cef2..bcf2a7250f6e8f217f1b655ff5e694e84bf7cf49 100644 (file)
@@ -43,7 +43,7 @@ public final class ActionUtil {
      * @param dscpValue TypeOfService value
      * @return TOS value
      */
-    public static Short dscpToTos(final short dscpValue) {
-        return (short) (dscpValue << ActionUtil.ENC_FIELD_BIT_SIZE);
+    public static Uint8 dscpToTos(final short dscpValue) {
+        return Uint8.valueOf((short) (dscpValue << ActionUtil.ENC_FIELD_BIT_SIZE));
     }
 }