Migrate a few convertors 44/94344/6
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 2 Jan 2021 16:49:14 +0000 (17:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 2 Jan 2021 23:37:41 +0000 (00:37 +0100)
Be a bit smarter about dealing with units, fixing related warnings.

JIRA: OPNFLWPLUG-1099
Change-Id: I9ab33dc6634f37bb38735d948741424c9c2fa07f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/services/sal/SalPortServiceImpl.java
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/action/cases/SalToOfSetTpDstActionCase.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/action/cases/SalToOfSetTpDstActionV10Case.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/core/sal/convertor/match/MatchV10ResponseConvertor.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/PortConvertorTest.java

index d6e149ceb52bdf2d4749cb5abaa3aa19813510e7..1924c89ccf9700b69ef05c7d554031ccaf06fdd8 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalP
 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 public final class SalPortServiceImpl
         extends AbstractSimpleService<UpdatePortInput, UpdatePortOutput> implements SalPortService {
@@ -54,7 +55,7 @@ public final class SalPortServiceImpl
             .convert(getPortFromInput(input), data);
 
         final PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput
-                .orElse(PortConvertor.defaultResult(getVersion())))
+                .orElse(PortConvertor.defaultResult(Uint8.valueOf(getVersion()))))
                 .setXid(xid.getValue());
 
         return mdInput.build();
index 98c505c369216400cc5681bc487c98f4240d38bc..969529e260288eea13b8dbe689c8c1e422360d93 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.PortsBuilder;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 /**
  * Converts port mod, port status and port description MD-SAL messages to OF library data.
@@ -52,10 +53,8 @@ public class PortConvertor extends Convertor<Port, PortModInput, VersionConverto
      * @param version Openflow version
      * @return default empty port mod input
      */
-    public static PortModInput defaultResult(final short version) {
-        return new PortModInputBuilder()
-                .setVersion(version)
-                .build();
+    public static PortModInput defaultResult(final Uint8 version) {
+        return new PortModInputBuilder().setVersion(version).build();
     }
 
     private static PortConfig maskPortConfigFields(
index e6b11a0ba1bc7f989cc296f3c15da88912b48273..c402f9c73fc453e085727d51f4664b7fcc2c1830 100644 (file)
@@ -5,20 +5,16 @@
  * 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.action.cases;
 
-import java.util.ArrayList;
 import java.util.List;
 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.ConvertorCase;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IPProtocols;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.dst.action._case.SetTpDstAction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetFieldCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.field._case.SetFieldActionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
@@ -28,7 +24,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Icmp
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.TcpDst;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.UdpDst;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4CodeCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6CodeCaseBuilder;
@@ -38,6 +33,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.matc
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.code._case.Icmpv6CodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.dst._case.TcpDstBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.udp.dst._case.UdpDstBuilder;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,41 +45,40 @@ public class SalToOfSetTpDstActionCase extends ConvertorCase<SetTpDstActionCase,
         super(SetTpDstActionCase.class, true, OFConstants.OFP_VERSION_1_3);
     }
 
-    @NonNull
     @Override
-    public Optional<Action> process(@NonNull final SetTpDstActionCase source, final ActionConvertorData data,
+    public Optional<Action> process(final SetTpDstActionCase source, final ActionConvertorData data,
             final ConvertorExecutor convertorExecutor) {
-        IPProtocols protocol = null;
-
-        if (data.getIpProtocol() != null) {
-            protocol = IPProtocols.fromProtocolNum(data.getIpProtocol());
-        }
-
-        SetTpDstAction settpdstaction = source.getSetTpDstAction();
-
-        MatchEntryBuilder matchBuilder = new MatchEntryBuilder()
+        final MatchEntryBuilder matchBuilder = new MatchEntryBuilder()
                 .setOxmClass(OpenflowBasicClass.class)
                 .setHasMask(Boolean.FALSE);
-        int port = settpdstaction.getPort().getValue().toJava();
-        int code = 0xff & port;
+
+        final Uint8 ipProtocol = data.getIpProtocol();
+        final IPProtocols protocol;
+        if (ipProtocol != null) {
+            protocol = IPProtocols.fromProtocolNum(ipProtocol);
+        } else {
+            protocol = null;
+        }
 
         if (protocol != null) {
+            final Uint16 port = source.getSetTpDstAction().getPort().getValue();
+
             switch (protocol) {
                 case ICMP:
                     matchBuilder.setOxmMatchField(Icmpv4Code.class);
-                    Icmpv4CodeCaseBuilder icmpv4CodeCaseBuilder = new Icmpv4CodeCaseBuilder();
-                    Icmpv4CodeBuilder icmpv4CodeBuilder = new Icmpv4CodeBuilder();
-                    icmpv4CodeBuilder.setIcmpv4Code((short) code);
-                    icmpv4CodeCaseBuilder.setIcmpv4Code(icmpv4CodeBuilder.build());
-                    matchBuilder.setMatchEntryValue(icmpv4CodeCaseBuilder.build());
+                    matchBuilder.setMatchEntryValue(new Icmpv4CodeCaseBuilder()
+                        .setIcmpv4Code(new Icmpv4CodeBuilder()
+                            .setIcmpv4Code(Uint8.valueOf(0xFF & port.toJava()))
+                            .build())
+                        .build());
                     break;
                 case ICMPV6:
                     matchBuilder.setOxmMatchField(Icmpv6Code.class);
-                    Icmpv6CodeCaseBuilder icmpv6CodeCaseBuilder = new Icmpv6CodeCaseBuilder();
-                    Icmpv6CodeBuilder icmpv6CodeBuilder = new Icmpv6CodeBuilder();
-                    icmpv6CodeBuilder.setIcmpv6Code((short) code);
-                    icmpv6CodeCaseBuilder.setIcmpv6Code(icmpv6CodeBuilder.build());
-                    matchBuilder.setMatchEntryValue(icmpv6CodeCaseBuilder.build());
+                    matchBuilder.setMatchEntryValue(new Icmpv6CodeCaseBuilder()
+                        .setIcmpv6Code(new Icmpv6CodeBuilder()
+                            .setIcmpv6Code(Uint8.valueOf(0xFF & port.toJava()))
+                            .build())
+                        .build());
                     break;
                 case TCP:
                     matchBuilder.setOxmMatchField(TcpDst.class);
@@ -110,17 +106,10 @@ public class SalToOfSetTpDstActionCase extends ConvertorCase<SetTpDstActionCase,
             LOG.warn("Missing protocol with combination of SetSourcePort");
         }
 
-        List<MatchEntry> entries = new ArrayList<>();
-        entries.add(matchBuilder.build());
-
-        SetFieldActionBuilder setFieldBuilder = new SetFieldActionBuilder();
-        setFieldBuilder.setMatchEntry(entries);
-
-        SetFieldCaseBuilder setFieldCaseBuilder = new SetFieldCaseBuilder();
-        setFieldCaseBuilder.setSetFieldAction(setFieldBuilder.build());
-
         return Optional.of(new ActionBuilder()
-                .setActionChoice(setFieldCaseBuilder.build())
-                .build());
+            .setActionChoice(new SetFieldCaseBuilder()
+                .setSetFieldAction(new SetFieldActionBuilder().setMatchEntry(List.of(matchBuilder.build())).build())
+                .build())
+            .build());
     }
 }
index 99836bad9571e69c7bc5ba5996f1123972dfd5fc..928ea991d7a0bb8f288cd2d2c200f2cd6fd69d4a 100644 (file)
@@ -5,17 +5,14 @@
  * 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.action.cases;
 
 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.ConvertorCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetTpDstActionCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.tp.dst.action._case.SetTpDstAction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpDstCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.dst._case.SetTpDstActionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
@@ -27,18 +24,15 @@ public class SalToOfSetTpDstActionV10Case extends ConvertorCase<SetTpDstActionCa
         super(SetTpDstActionCase.class, true, OFConstants.OFP_VERSION_1_0);
     }
 
-    @NonNull
     @Override
-    public Optional<Action> process(@NonNull final SetTpDstActionCase source, final ActionConvertorData data,
-            ConvertorExecutor convertorExecutor) {
-        SetTpDstAction settpdstaction = source.getSetTpDstAction();
-        SetTpDstCaseBuilder setTpDstCaseBuilder = new SetTpDstCaseBuilder();
-        SetTpDstActionBuilder setTpDstActionBuilder = new SetTpDstActionBuilder();
-        setTpDstActionBuilder.setPort(new PortNumber(settpdstaction.getPort().getValue().longValue()));
-        setTpDstCaseBuilder.setSetTpDstAction(setTpDstActionBuilder.build());
-
+    public Optional<Action> process(final SetTpDstActionCase source, final ActionConvertorData data,
+            final ConvertorExecutor convertorExecutor) {
         return Optional.of(new ActionBuilder()
-                .setActionChoice(setTpDstCaseBuilder.build())
-                .build());
+            .setActionChoice(new SetTpDstCaseBuilder()
+                .setSetTpDstAction(new SetTpDstActionBuilder()
+                    .setPort(new PortNumber(source.getSetTpDstAction().getPort().getValue().toUint32()))
+                    .build())
+                .build())
+            .build());
     }
 }
index ac3520a40fdcb8e665e1957aedc9d19817ed4916..693d769efcc77a903c754c9ddadbe3560963b9e0 100644 (file)
@@ -19,9 +19,11 @@ import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.Versi
 import org.opendaylight.openflowplugin.openflow.md.util.ActionUtil;
 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
 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;
@@ -64,6 +66,8 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
      */
     private static final Ipv4Address ZERO_IPV4 = new Ipv4Address("0.0.0.0");
 
+    private static final Uint8 DEFAULT_PREFIX = Uint8.valueOf(32);
+
     /*
      * The value 0xffff (OFP_VLAN_NONE) is used to indicate
      * that no VLAN ID is set for OF Flow.
@@ -130,15 +134,13 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
      * @param matchBuilder match builder
      * @param ipv4         ip v4 match
      */
-    private static void convertL3Ipv4DstMatch(final MatchV10Builder matchBuilder,
-                                              final Ipv4Match ipv4) {
-        if (ipv4.getIpv4Destination() != null) {
-            Iterator<String> addressParts = IpConversionUtil.PREFIX_SPLITTER.split(
-                    ipv4.getIpv4Destination().getValue()).iterator();
-            Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
-            Integer prefix = buildPrefix(addressParts);
-            matchBuilder.setNwDst(ipv4Address);
-            matchBuilder.setNwDstMask(prefix.shortValue());
+    private static void convertL3Ipv4DstMatch(final MatchV10Builder matchBuilder, final Ipv4Match ipv4) {
+        final Ipv4Prefix destination = ipv4.getIpv4Destination();
+        if (destination != null) {
+            // TODO: consider using IetfInetUtil
+            Iterator<String> addressParts = IpConversionUtil.PREFIX_SPLITTER.split(destination.getValue()).iterator();
+            matchBuilder.setNwDst(new Ipv4Address(addressParts.next()));
+            matchBuilder.setNwDstMask(buildPrefix(addressParts));
         }
     }
 
@@ -149,31 +151,24 @@ public class MatchV10Convertor extends Convertor<Match, MatchV10, VersionConvert
      * @param matchBuilder match builder
      * @param ipv4         ip v4 match
      */
-    private static void convertL3Ipv4SrcMatch(final MatchV10Builder matchBuilder,
-                                              final Ipv4Match ipv4) {
-        if (ipv4.getIpv4Source() != null) {
-            Iterator<String> addressParts = IpConversionUtil.PREFIX_SPLITTER.split(
-                    ipv4.getIpv4Source().getValue()).iterator();
-            Ipv4Address ipv4Address = new Ipv4Address(addressParts.next());
-            int prefix = buildPrefix(addressParts);
-
-            matchBuilder.setNwSrc(ipv4Address);
-            matchBuilder.setNwSrcMask((short) prefix);
+    private static void convertL3Ipv4SrcMatch(final MatchV10Builder matchBuilder, final Ipv4Match ipv4) {
+        final Ipv4Prefix source = ipv4.getIpv4Source();
+        if (source != null) {
+            // TODO: consider IetfInetUtil
+            Iterator<String> addressParts = IpConversionUtil.PREFIX_SPLITTER.split(source.getValue()).iterator();
+            matchBuilder.setNwSrc(new Ipv4Address(addressParts.next()));
+            matchBuilder.setNwSrcMask(buildPrefix(addressParts));
         }
     }
 
-    private static int buildPrefix(final Iterator<String> addressParts) {
-        int prefix = 32;
-        if (addressParts.hasNext()) {
-            prefix = Integer.parseInt(addressParts.next());
-        }
-        return prefix;
+    private static Uint8 buildPrefix(final Iterator<String> addressParts) {
+        return addressParts.hasNext() ? Uint8.valueOf(addressParts.next()) : DEFAULT_PREFIX;
     }
 
-    private static boolean convertDlVlanPcp(final MatchV10Builder matchBuilder,
-                                            final VlanMatch vlanMatch) {
-        if (vlanMatch.getVlanPcp() != null) {
-            matchBuilder.setDlVlanPcp(vlanMatch.getVlanPcp().getValue());
+    private static boolean convertDlVlanPcp(final MatchV10Builder matchBuilder, final VlanMatch vlanMatch) {
+        final VlanPcp vlanPcp = vlanMatch.getVlanPcp();
+        if (vlanPcp != null) {
+            matchBuilder.setDlVlanPcp(vlanPcp.getValue());
             return false;
         }
         return true;
index 8695bb8b29b083f92fa7466fba41762e5d2931b7..bd1eacc3b1b14433cc4732a8607cb0b3a72aed64 100644 (file)
@@ -96,12 +96,15 @@ public class MatchV10ResponseConvertor extends Convertor<MatchV10, MatchBuilder,
             matchBuilder.setEthernetMatch(ethMatchBuilder.build());
         }
         if (!source.getWildcards().getDLVLAN() && source.getDlVlan() != null) {
-            VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
-            int vlanId = source.getDlVlan().toJava() == 0xffff ? 0 : source.getDlVlan().toJava();
-            vlanIdBuilder.setVlanId(
-                    new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId));
-            vlanIdBuilder.setVlanIdPresent(vlanId != 0);
-            vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
+            Uint16 vlanId = source.getDlVlan();
+            if (vlanId.toJava() == 0xffff) {
+                vlanId = Uint16.ZERO;
+            }
+
+            vlanMatchBuilder.setVlanId(new VlanIdBuilder()
+                .setVlanId(new org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId(vlanId))
+                .setVlanIdPresent(!Uint16.ZERO.equals(vlanId))
+                .build());
             matchBuilder.setVlanMatch(vlanMatchBuilder.build());
         }
         if (!source.getWildcards().getDLVLANPCP() && source.getDlVlanPcp() != null) {
index 3acb659a4e93a74bde1fda7325eb963431affc63..26d8d0fcaed0411d4ef76e3cb2cb91bef387349d 100644 (file)
@@ -68,8 +68,9 @@ public class PortConvertorTest {
 
         VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
-        Optional<PortModInput> portOutOptional = convertorManager.convert(portBld.build(), data);
-        final PortModInput portOut = portOutOptional.orElse(PortConvertor.defaultResult(OFConstants.OFP_VERSION_1_3));
+        final Optional<PortModInput> portOutOptional = convertorManager.convert(portBld.build(), data);
+        final PortModInput portOut = portOutOptional
+            .orElse(PortConvertor.defaultResult(EncodeConstants.OF_VERSION_1_3));
 
         PortConfigV10 portConfV10 = new PortConfigV10(false, false, false, false, true, true, false);