Redice use of ByteBufWriteUtil in flowspec 05/86705/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 09:20:54 +0000 (10:20 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 09:20:54 +0000 (10:20 +0100)
yangtools is providing ByteBufUtils for dealing with uint types,
use those methods instead of our home-grown ones. In a few cases
going through uints is not necessary, in which case we end up
talking to ByteBuf directly.

Change-Id: If029b90c6773cae0b2428fa6949173c6cb62bca3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectAsFourOctetEcHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectAsTwoOctetEcHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectIpNextHopEcHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectIpv4EcHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/RedirectIpv6EcHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficMarkingEcHandler.java
bgp/extensions/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/extended/communities/TrafficRateEcHandler.java

index 4bbebd02f7b078f52e01002e04b5d9f345cf08f2..0976a455a0d3457bb42ac1f0aab172308a2f747f 100644 (file)
@@ -12,7 +12,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.as4.extended.community.RedirectAs4;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.as4.extended.community.RedirectAs4Builder;
@@ -31,16 +30,18 @@ public final class RedirectAsFourOctetEcHandler implements ExtendedCommunityPars
                 "The extended community %s is not RedirectAs4ExtendedCommunityCase type.", extendedCommunity);
         final RedirectAs4 redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec
                 .rev180329.RedirectAs4ExtendedCommunity) extendedCommunity).getRedirectAs4();
-        ByteBufWriteUtil.writeUnsignedInt(redirect.getGlobalAdministrator().getValue(), byteAggregator);
-        ByteBufWriteUtil.writeUnsignedShort(redirect.getLocalAdministrator(), byteAggregator);
+        ByteBufUtils.write(byteAggregator, redirect.getGlobalAdministrator().getValue());
+        ByteBufUtils.writeOrZero(byteAggregator, redirect.getLocalAdministrator());
     }
 
     @Override
     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
-        final RedirectAs4Builder builder = new RedirectAs4Builder();
-        builder.setGlobalAdministrator(new AsNumber(ByteBufUtils.readUint32(buffer)));
-        builder.setLocalAdministrator(ByteBufUtils.readUint16(buffer));
-        return new RedirectAs4ExtendedCommunityCaseBuilder().setRedirectAs4(builder.build()).build();
+        return new RedirectAs4ExtendedCommunityCaseBuilder()
+                .setRedirectAs4(new RedirectAs4Builder()
+                    .setGlobalAdministrator(new AsNumber(ByteBufUtils.readUint32(buffer)))
+                    .setLocalAdministrator(ByteBufUtils.readUint16(buffer))
+                    .build())
+                .build();
     }
 
     @Override
index a629e19b58773c90b9d3f4790671d359035037f7..8f6225a28edd6c3cf45f348f0b518def05bb06de 100644 (file)
@@ -13,7 +13,6 @@ import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
 import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.extended.community.RedirectExtendedCommunity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.extended.community.RedirectExtendedCommunityBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
@@ -34,7 +33,7 @@ public class RedirectAsTwoOctetEcHandler implements ExtendedCommunityParser, Ext
                 "The extended community %s is not RedirectExtendedCommunityCase type.", extendedCommunity);
         final RedirectExtendedCommunity redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
                 .bgp.flowspec.rev180329.RedirectExtendedCommunity) extendedCommunity).getRedirectExtendedCommunity();
-        ByteBufWriteUtil.writeUnsignedShort(redirect.getGlobalAdministrator().getValue().intValue(), byteAggregator);
+        byteAggregator.writeShort(redirect.getGlobalAdministrator().getValue().intValue());
         byteAggregator.writeBytes(redirect.getLocalAdministrator());
     }
 
index 982a6c95f974d0a7d016e1ba17ec286109fd3005..647e5353e70b01868f4927c6a6cfcfa87c746640 100644 (file)
@@ -22,12 +22,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flow
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
 
 public class RedirectIpNextHopEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
-
     //https://tools.ietf.org/html/draft-ietf-idr-flowspec-redirect-ip-00#section-7
     private static final int TYPE = 8;
-
     private static final int SUBTYPE = 0;
-
     private static final byte COPY = 0x1;
 
     @Override
@@ -44,7 +41,7 @@ public class RedirectIpNextHopEcHandler implements ExtendedCommunityParser, Exte
         } else {
             ByteBufWriteUtil.writeIpv6Address(nextHopAddress.getIpv6Address(), byteAggregator);
         }
-        ByteBufWriteUtil.writeUnsignedShort(redirect.isCopy() == null || !redirect.isCopy() ? 0 : 1, byteAggregator);
+        byteAggregator.writeShort(Boolean.TRUE.equals(redirect.isCopy()) ? 1 : 0);
     }
 
     @Override
index 7ea11c32d90361376710bcf80ecea69beb6350b0..d31b5eed5a78e80fc6bc3ed19dfcc25110eff25b 100644 (file)
@@ -32,15 +32,17 @@ public class RedirectIpv4EcHandler implements ExtendedCommunityParser, ExtendedC
         final RedirectIpv4 redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec
                 .rev180329.RedirectIpv4ExtendedCommunity) extendedCommunity).getRedirectIpv4();
         ByteBufWriteUtil.writeIpv4Address(redirect.getGlobalAdministrator(), byteAggregator);
-        ByteBufWriteUtil.writeUnsignedShort(redirect.getLocalAdministrator(), byteAggregator);
+        ByteBufUtils.writeOrZero(byteAggregator, redirect.getLocalAdministrator());
     }
 
     @Override
     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
-        final RedirectIpv4Builder builder = new RedirectIpv4Builder();
-        builder.setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer));
-        builder.setLocalAdministrator(ByteBufUtils.readUint16(buffer));
-        return new RedirectIpv4ExtendedCommunityCaseBuilder().setRedirectIpv4(builder.build()).build();
+        return new RedirectIpv4ExtendedCommunityCaseBuilder()
+                .setRedirectIpv4(new RedirectIpv4Builder()
+                    .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
+                    .setLocalAdministrator(ByteBufUtils.readUint16(buffer))
+                    .build())
+                .build();
     }
 
     @Override
index 2a95dbd970c289eccb156202029cc8943e4a354f..6c43f0da1ea145c5a026e6ac74f3ff235926daed 100644 (file)
@@ -11,7 +11,6 @@ import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.protocol.util.Ipv6Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.RedirectIpv6ExtendedCommunity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ipv6.extended.community.RedirectIpv6;
@@ -30,7 +29,7 @@ public final class RedirectIpv6EcHandler implements ExtendedCommunityParser, Ext
             "The extended community %s is not RedirectIpv6ExtendedCommunity type.", extendedCommunity);
         final RedirectIpv6 redirectIpv6 = ((RedirectIpv6ExtendedCommunity) extendedCommunity).getRedirectIpv6();
         byteAggregator.writeBytes(Ipv6Util.byteBufForAddress(redirectIpv6.getGlobalAdministrator()));
-        ByteBufWriteUtil.writeUnsignedShort(redirectIpv6.getLocalAdministrator(), byteAggregator);
+        ByteBufUtils.writeOrZero(byteAggregator, redirectIpv6.getLocalAdministrator());
     }
 
     @Override
index 56f870f16421ab17fa7a48adc18932b15c25de58..c7e085238df130aa2e6941774d5c1770b0303cf0 100644 (file)
@@ -12,7 +12,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.Dscp;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.marking.extended.community.TrafficMarkingExtendedCommunity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.marking.extended.community.TrafficMarkingExtendedCommunityBuilder;
@@ -34,7 +33,7 @@ public class TrafficMarkingEcHandler implements ExtendedCommunityParser, Extende
                 .xml.ns.yang.bgp.flowspec.rev180329.TrafficMarkingExtendedCommunity) extendedCommunity)
                 .getTrafficMarkingExtendedCommunity();
         byteAggregator.writeZero(RESERVED);
-        ByteBufWriteUtil.writeUnsignedByte(trafficMarking.getGlobalAdministrator().getValue(), byteAggregator);
+        ByteBufUtils.write(byteAggregator, trafficMarking.getGlobalAdministrator().getValue());
     }
 
     @Override
@@ -51,10 +50,9 @@ public class TrafficMarkingEcHandler implements ExtendedCommunityParser, Extende
     @Override
     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
         buffer.skipBytes(RESERVED);
-        final Dscp dscp = new Dscp(ByteBufUtils.readUint8(buffer));
         return new TrafficMarkingExtendedCommunityCaseBuilder()
             .setTrafficMarkingExtendedCommunity(new TrafficMarkingExtendedCommunityBuilder()
-                .setGlobalAdministrator(dscp)
+                .setGlobalAdministrator(new Dscp(ByteBufUtils.readUint8(buffer)))
                 .build())
             .build();
     }
index 4f7ce52a281818707579168c7037258a408b52d4..808d4c812dbdbaf2016d70edf6e8201327c5c175 100644 (file)
@@ -13,7 +13,6 @@ import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
 import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.rate.extended.community.TrafficRateExtendedCommunity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.traffic.rate.extended.community.TrafficRateExtendedCommunityBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
@@ -38,7 +37,7 @@ public class TrafficRateEcHandler implements ExtendedCommunityParser, ExtendedCo
         final TrafficRateExtendedCommunity trafficRate = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
                 .yang.bgp.flowspec.rev180329.TrafficRateExtendedCommunity) extendedCommunity)
                 .getTrafficRateExtendedCommunity();
-        ByteBufWriteUtil.writeShort(trafficRate.getInformativeAs().getValue().shortValue(), byteAggregator);
+        byteAggregator.writeShort(trafficRate.getInformativeAs().getValue().intValue());
         byteAggregator.writeBytes(trafficRate.getLocalAdministrator().getValue());
     }
 
@@ -55,13 +54,12 @@ public class TrafficRateEcHandler implements ExtendedCommunityParser, ExtendedCo
 
     @Override
     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
-        final ShortAsNumber as = new ShortAsNumber(Uint32.valueOf(buffer.readUnsignedShort()));
-        final Bandwidth value = new Bandwidth(ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE));
-        return new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
-                new TrafficRateExtendedCommunityBuilder()
-                    .setInformativeAs(as)
-                    .setLocalAdministrator(value)
-                    .build()).build();
+        return new TrafficRateExtendedCommunityCaseBuilder()
+                .setTrafficRateExtendedCommunity(new TrafficRateExtendedCommunityBuilder()
+                    .setInformativeAs(new ShortAsNumber(Uint32.valueOf(buffer.readUnsignedShort())))
+                    .setLocalAdministrator(new Bandwidth(ByteArray.readBytes(buffer, TRAFFIC_RATE_SIZE)))
+                    .build())
+                .build();
     }
 
 }