Migrate most of openflowplugin to use Uint types 77/92377/6
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 2 Sep 2020 12:46:44 +0000 (14:46 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Sep 2020 08:12:52 +0000 (10:12 +0200)
Migrate production code to use non-widened values. There are a few
methods left out, as they seem to be performing lossy conversion,
which needs to be investigated separately.

JIRA: OPNFLWPLUG-1099
Change-Id: I7bd10498557396f96a6ec0baf9dccc7b8186b28f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/GroupConvertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/GroupStatsResponseConvertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/MeterConfigStatsResponseConvertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/MeterStatsResponseConvertor.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/action/cases/SalToOfSetTpSrcActionV10Case.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/action/cases/SalToOfStripVlanActionCase.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/cases/OfToSalMplsBosCase.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtilTest.java

index 3432d6964dbc2e39f974ebb9106a1f4cbe1766bc..8ce3afb263a053b93ce118a2b7e8e2cdc03a1d45 100644 (file)
@@ -16,7 +16,6 @@ import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import org.opendaylight.openflowjava.protocol.api.util.BinContent;
 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.data.VersionDatapathIdConvertorData;
@@ -36,7 +35,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev13
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.common.Uint64;
+import org.opendaylight.yangtools.yang.common.Uint8;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,18 +66,30 @@ public class GroupConvertor extends Convertor<Group, GroupModInputBuilder, Versi
      *
      * @param version Openflow version
      * @return default empty group mod input builder
+     * @deprecated Use {@link #defaultResult(Uint8)} instead.
      */
+    @Deprecated
     public static GroupModInputBuilder defaultResult(final short version) {
-        return new GroupModInputBuilder()
-                .setVersion(version);
+        return defaultResult(Uint8.valueOf(version));
     }
 
+    /**
+     * Create default empty group mod input builder
+     * Use this method, if result from convertor is empty.
+     *
+     * @param version Openflow version
+     * @return default empty group mod input builder
+     * @throws NullPointerException if version is null
+     */
+    public static GroupModInputBuilder defaultResult(final Uint8 version) {
+        return new GroupModInputBuilder().setVersion(version);
+    }
+
+
     private static final Logger LOG = LoggerFactory.getLogger(GroupConvertor.class);
-    private static final Integer DEFAULT_WEIGHT = 0;
-    private static final Long OFPP_ANY = Long.parseLong("ffffffff", 16);
-    private static final Long DEFAULT_WATCH_PORT = OFPP_ANY;
-    private static final Long OFPG_ANY = Long.parseLong("ffffffff", 16);
-    private static final Long DEFAULT_WATCH_GROUP = OFPG_ANY;
+    private static final Uint16 DEFAULT_WEIGHT = Uint16.ZERO;
+    private static final PortNumber DEFAULT_WATCH_PORT = new PortNumber(Uint32.MAX_VALUE);
+    private static final Uint32 DEFAULT_WATCH_GROUP = Uint32.MAX_VALUE;
     private static final Comparator<Bucket> COMPARATOR = (bucket1, bucket2) -> {
         if (bucket1.getBucketId() == null || bucket2.getBucketId() == null) {
             return 0;
@@ -114,7 +128,7 @@ public class GroupConvertor extends Convertor<Group, GroupModInputBuilder, Versi
         if (null != groupBucket.getWatchPort()) {
             bucketBuilder.setWatchPort(new PortNumber(groupBucket.getWatchPort()));
         } else {
-            bucketBuilder.setWatchPort(new PortNumber(BinContent.intToUnsignedLong(DEFAULT_WATCH_PORT.intValue())));
+            bucketBuilder.setWatchPort(DEFAULT_WATCH_PORT);
             if (groupType == GroupType.OFPGTFF.getIntValue()) {
                 LOG.error("WatchPort required for this OFPGT_FF");
             }
@@ -126,7 +140,7 @@ public class GroupConvertor extends Convertor<Group, GroupModInputBuilder, Versi
         if (null != groupBucket.getWatchGroup()) {
             bucketBuilder.setWatchGroup(groupBucket.getWatchGroup());
         } else {
-            bucketBuilder.setWatchGroup(BinContent.intToUnsignedLong(DEFAULT_WATCH_GROUP.intValue()));
+            bucketBuilder.setWatchGroup(DEFAULT_WATCH_GROUP);
             if (groupType == GroupType.OFPGTFF.getIntValue()) {
                 LOG.error("WatchGroup required for this OFPGT_FF");
             }
index 591ffa06bd17d72952479d0b24176d1b1065dafb..b86d45aca8e26e90532079533d49bf3277a9420d 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
 
+import com.google.common.collect.ImmutableMap;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -29,6 +30,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStatsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStatsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group._case.multipart.reply.group.group.stats.BucketStats;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 /**
  * Converts group related statistics messages coming from switch to MD-SAL messages.
@@ -53,24 +55,20 @@ public class GroupStatsResponseConvertor extends Convertor<
                 .reply.group.GroupStats.class);
 
     private static Buckets toSALBuckets(List<BucketStats> bucketStats) {
-        BucketsBuilder salBuckets = new BucketsBuilder();
+        final ImmutableMap.Builder<BucketCounterKey, BucketCounter> allBucketStats =
+                ImmutableMap.builderWithExpectedSize(bucketStats.size());
 
-        List<BucketCounter> allBucketStats = new ArrayList<>();
         int bucketKey = 0;
-
         for (BucketStats bucketStat : bucketStats) {
-            BucketCounterBuilder bucketCounter = new BucketCounterBuilder();
-            bucketCounter.setByteCount(new Counter64(bucketStat.getByteCount()));
-            bucketCounter.setPacketCount(new Counter64(bucketStat.getPacketCount()));
-            BucketId bucketId = new BucketId((long) bucketKey);
-            bucketCounter.withKey(new BucketCounterKey(bucketId));
-            bucketCounter.setBucketId(bucketId);
-            bucketKey++;
-            allBucketStats.add(bucketCounter.build());
+            final BucketCounterKey key = new BucketCounterKey(new BucketId(Uint32.valueOf(bucketKey++)));
+            allBucketStats.put(key, new BucketCounterBuilder()
+                .withKey(key)
+                .setByteCount(new Counter64(bucketStat.getByteCount()))
+                .setPacketCount(new Counter64(bucketStat.getPacketCount()))
+                .build());
         }
 
-        salBuckets.setBucketCounter(allBucketStats);
-        return salBuckets.build();
+        return new BucketsBuilder().setBucketCounter(allBucketStats.build()).build();
     }
 
     @Override
@@ -86,21 +84,17 @@ public class GroupStatsResponseConvertor extends Convertor<
 
         for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply
                 .body.multipart.reply.group._case.multipart.reply.group.GroupStats groupStats : source) {
-            GroupStatsBuilder salGroupStats = new GroupStatsBuilder();
-
-            salGroupStats.setBuckets(toSALBuckets(groupStats.nonnullBucketStats()));
-            salGroupStats.setByteCount(new Counter64(groupStats.getByteCount()));
-
-            DurationBuilder time = new DurationBuilder();
-            time.setSecond(new Counter32(groupStats.getDurationSec()));
-            time.setNanosecond(new Counter32(groupStats.getDurationNsec()));
-
-            salGroupStats.setDuration(time.build());
-            salGroupStats.setGroupId(new GroupId(groupStats.getGroupId().getValue()));
-            salGroupStats.setPacketCount(new Counter64(groupStats.getPacketCount()));
-            salGroupStats.setRefCount(new Counter32(groupStats.getRefCount()));
-            salGroupStats.withKey(new GroupStatsKey(salGroupStats.getGroupId()));
-            convertedSALGroups.add(salGroupStats.build());
+            convertedSALGroups.add(new GroupStatsBuilder()
+                .withKey(new GroupStatsKey(new GroupId(groupStats.getGroupId().getValue())))
+                .setBuckets(toSALBuckets(groupStats.nonnullBucketStats()))
+                .setByteCount(new Counter64(groupStats.getByteCount()))
+                .setDuration(new DurationBuilder()
+                    .setSecond(new Counter32(groupStats.getDurationSec()))
+                    .setNanosecond(new Counter32(groupStats.getDurationNsec()))
+                    .build())
+                .setPacketCount(new Counter64(groupStats.getPacketCount()))
+                .setRefCount(new Counter32(groupStats.getRefCount()))
+                .build());
         }
 
         return convertedSALGroups;
index dd12e99f043dd2ef9f4379cc805e3f34012d451d..a78c6a573a14c011345b789bf51715505a7bbe22 100644 (file)
@@ -38,6 +38,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.MeterConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.meter.config.Bands;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 /**
  * Converts list of OF library config meter stats to MD-SAL config meter stats.
@@ -92,7 +93,7 @@ public class MeterConfigStatsResponseConvertor extends Convertor<List<MeterConfi
 
                     meterBandHeaderBuilder.setBandBurstSize(dropBand.getBurstSize());
                     meterBandHeaderBuilder.setBandRate(dropBand.getRate());
-                    BandId bandId = new BandId((long) bandKey);
+                    BandId bandId = new BandId(Uint32.valueOf(bandKey));
                     meterBandHeaderBuilder.withKey(new MeterBandHeaderKey(bandId));
                     meterBandHeaderBuilder.setBandId(bandId);
 
@@ -112,7 +113,7 @@ public class MeterConfigStatsResponseConvertor extends Convertor<List<MeterConfi
 
                     meterBandHeaderBuilder.setBandBurstSize(dscpRemarkBand.getBurstSize());
                     meterBandHeaderBuilder.setBandRate(dscpRemarkBand.getRate());
-                    BandId bandId = new BandId((long) bandKey);
+                    BandId bandId = new BandId(Uint32.valueOf(bandKey));
                     meterBandHeaderBuilder.withKey(new MeterBandHeaderKey(bandId));
                     meterBandHeaderBuilder.setBandId(bandId);
 
@@ -132,7 +133,7 @@ public class MeterConfigStatsResponseConvertor extends Convertor<List<MeterConfi
 
                     meterBandHeaderBuilder.setBandBurstSize(experimenterBand.getBurstSize());
                     meterBandHeaderBuilder.setBandRate(experimenterBand.getRate());
-                    BandId bandId = new BandId((long) bandKey);
+                    BandId bandId = new BandId(Uint32.valueOf(bandKey));
                     meterBandHeaderBuilder.withKey(new MeterBandHeaderKey(bandId));
                     meterBandHeaderBuilder.setBandId(bandId);
 
index 5fb610cfcbff98464b3f7722dcf06e69d8ce66ae..3bfb944f0ba32810ca48ce88b26a9bdeff168ac5 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.meter.stats.MeterBandStats;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
 /**
  * Converts list of OF library meter stats to MD-SAL meter stats.
@@ -88,7 +89,7 @@ public class MeterStatsResponseConvertor extends Convertor<
                 BandStatBuilder bandStatBuilder = new BandStatBuilder();
                 bandStatBuilder.setByteBandCount(new Counter64(meterBandStats.getByteBandCount()));
                 bandStatBuilder.setPacketBandCount(new Counter64(meterBandStats.getPacketBandCount()));
-                BandId bandId = new BandId((long) bandKey);
+                BandId bandId = new BandId(Uint32.valueOf(bandKey));
                 bandStatBuilder.withKey(new BandStatKey(bandId));
                 bandStatBuilder.setBandId(bandId);
                 bandKey++;
index ad8b79422a579bb5fff89491b82c603f6a619df9..581abde212439e9cdae13f7ccfcdb2c841660644 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.action.cases;
 
 import java.util.Optional;
@@ -38,9 +37,7 @@ public class SalToOfSetTpSrcActionV10Case extends ConvertorCase<SetTpSrcActionCa
         SetTpSrcAction settpsrcaction = source.getSetTpSrcAction();
         SetTpSrcCaseBuilder setTpSrcCaseBuilder = new SetTpSrcCaseBuilder();
         SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
-        setTpSrcActionBuilder.setPort(new PortNumber(settpsrcaction.getPort()
-                .getValue()
-                .longValue()));
+        setTpSrcActionBuilder.setPort(new PortNumber(settpsrcaction.getPort().getValue().toUint32()));
         setTpSrcCaseBuilder.setSetTpSrcAction(setTpSrcActionBuilder.build());
 
         return Optional.of(new ActionBuilder()
index 29ff95c721a9f971e561287bba45e6451a783fb5..a24b12a189e9228f0b276a5c85dab1ca55935ea2 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.action.cases;
 
 import java.util.ArrayList;
@@ -27,6 +26,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.matc
 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.VlanVidCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.vid._case.VlanVidBuilder;
+import org.opendaylight.yangtools.yang.common.Uint16;
 
 public class SalToOfStripVlanActionCase extends ConvertorCase<StripVlanActionCase, Action, ActionConvertorData> {
     public SalToOfStripVlanActionCase() {
@@ -44,7 +44,7 @@ public class SalToOfStripVlanActionCase extends ConvertorCase<StripVlanActionCas
         VlanVidCaseBuilder vlanVidCaseBuilder = new VlanVidCaseBuilder();
         VlanVidBuilder vlanVidBuilder = new VlanVidBuilder();
         vlanVidBuilder.setCfiBit(true);
-        vlanVidBuilder.setVlanVid(0x0000);
+        vlanVidBuilder.setVlanVid(Uint16.ZERO);
         vlanVidCaseBuilder.setVlanVid(vlanVidBuilder.build());
         matchBuilder.setMatchEntryValue(vlanVidCaseBuilder.build());
         matchBuilder.setHasMask(false);
index d43253d0872a66d690b951c30b8a1e20dee3a599..8a0bffd2221acdccb2f8d3dd6abcc9e18f85b6c3 100644 (file)
@@ -283,7 +283,7 @@ public class FlowConvertor extends Convertor<Flow, List<FlowModInputBuilder>, Ve
 
     private static void salToOFFlowTableId(final Flow flow, final FlowModInputBuilder flowMod) {
         if (flow.getTableId() != null) {
-            flowMod.setTableId(new TableId(flow.getTableId().longValue()));
+            flowMod.setTableId(new TableId(flow.getTableId().toUint32()));
         } else {
             flowMod.setTableId(DEFAULT_TABLE_ID);
         }
index 49b48be89f6631e7bd379241da8515a0b0a4e51e..0b5a134ba5120272f951e4faf13f49bd6511d2ed 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.match.cases;
 
 import java.util.Optional;
@@ -18,6 +17,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.M
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.MplsBosCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.mpls.bos._case.MplsBos;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 public class OfToSalMplsBosCase extends ConvertorCase<MplsBosCase, MatchBuilder, MatchResponseConvertorData> {
     public OfToSalMplsBosCase() {
@@ -33,7 +33,7 @@ public class OfToSalMplsBosCase extends ConvertorCase<MplsBosCase, MatchBuilder,
         MplsBos mplsBos = source.getMplsBos();
 
         if (mplsBos != null) {
-            protocolMatchFieldsBuilder.setMplsBos(mplsBos.isBos() ? (short) 1 : (short) 0);
+            protocolMatchFieldsBuilder.setMplsBos(mplsBos.isBos() ? Uint8.ONE : Uint8.ZERO);
             matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
         }
 
index 1cd7370244b7adcebf8920041e5b50b4e3a2fe13..3f83fbb0270354b3180bc7ef8b213452ce188b5b 100644 (file)
@@ -91,9 +91,9 @@ public final class ByteUtil {
      * @param bytes an array of 4 unsigned bytes
      * @return a long representing the unsigned int
      */
-    public static long bytesToUnsignedInt(final byte[] bytes) {
+    public static Uint32 bytesToUnsignedInt(final byte[] bytes) {
         Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
-        long unsignedInt = 0;
+        int unsignedInt = 0;
         unsignedInt |= bytes[0] & 0xFF;
         unsignedInt <<= 8;
         unsignedInt |= bytes[1] & 0xFF;
@@ -101,7 +101,7 @@ public final class ByteUtil {
         unsignedInt |= bytes[2] & 0xFF;
         unsignedInt <<= 8;
         unsignedInt |= bytes[3] & 0xFF;
-        return unsignedInt;
+        return Uint32.fromIntBits(unsignedInt);
     }
 
     /**
index 9e9a3373f1fda0678c3b8cc3a2785d7de0895f46..871e46bee748b18474486d2862a51888a88979bb 100644 (file)
@@ -14,6 +14,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.math.BigInteger;
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.common.Uint64;
 
 /**
@@ -74,14 +75,14 @@ public class ByteUtilTest {
 
     @Test
     public void testBytesToUnsignedInt() {
-        long unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES);
-        assertEquals(BIG_INTEGER.longValue(), unsigned);
+        Uint32 unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES);
+        assertEquals(BIG_INTEGER.longValue(), unsigned.toJava());
 
         unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTES00);
-        assertEquals(0, unsigned);
+        assertEquals(Uint32.ZERO, unsigned);
 
         unsigned = ByteUtil.bytesToUnsignedInt(TEST_BYTESFF);
-        assertEquals(BIG_INTFF.longValue(), unsigned);
+        assertEquals(BIG_INTFF.longValue(), unsigned.toJava());
     }
 
     @Test