Minimize linkstate use of ByteBufWriteUtils 08/86708/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 10:12:24 +0000 (11:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 5 Jan 2020 13:46:54 +0000 (14:46 +0100)
Most of the usage of ByteBufWriteUtils can be replaced by
ByteBufUtils or direct interaction with ByteBuf.

Change-Id: I6d381f30c99b4a6f5d6ff5723385df6aa7474cc2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/AreaIdTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/AsNumTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/DomainIdTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/LinkIdTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/MemAsNumTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/MultiTopoIdTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/OspfRouteTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/impl/tlvs/RouterIdTlvParser.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/spi/AbstractNlriTypeCodec.java
bgp/extensions/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/spi/AbstractTeLspNlriCodec.java

index d42e3e2707cc8d1acae5e895b58c7e9f37402460..f1fd8b452e9177c0a6d92e9d2da925ab1b0b8fe6 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.AreaIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -23,7 +22,7 @@ public final class AreaIdTlvParser implements LinkstateTlvParser<AreaIdentifier>
 
     @Override
     public void serializeTlvBody(final AreaIdentifier tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedInt(tlv.getValue(), body);
+        ByteBufUtils.write(body, tlv.getValue());
     }
 
     @Override
index b323d2fd7fb362086153cda45563590b02bb5126..a013e765afaec49516b39288d8480f32172172e1 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
-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.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -23,7 +22,7 @@ public final class AsNumTlvParser implements LinkstateTlvParser<AsNumber>,
 
     @Override
     public void serializeTlvBody(final AsNumber tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedInt(tlv.getValue(), body);
+        ByteBufUtils.write(body, tlv.getValue());
     }
 
     @Override
index 8b22184ef0f2eda6504a956a297d46b230d29682..248da4231eb3029c4d4f0d8970838145c9125a02 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.DomainIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.node._case.NodeDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -23,7 +22,7 @@ public final class DomainIdTlvParser implements LinkstateTlvParser<DomainIdentif
 
     @Override
     public void serializeTlvBody(final DomainIdentifier tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedInt(tlv.getValue(), body);
+        ByteBufUtils.write(body, tlv.getValue());
     }
 
     @Override
index 190ff0abf361946b56b8c1f7fd2186ec77f4d286..6a7cdb655da262230059e4c888d6fb6d61781f91 100644 (file)
@@ -7,44 +7,30 @@
  */
 package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 
+import static java.util.Objects.requireNonNull;
+
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkLrIdentifiers;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 public final class LinkIdTlvParser implements LinkstateTlvParser.LinkstateTlvSerializer<LinkLrIdentifiers>,
         LinkstateTlvParser<LinkLrIdentifiers> {
-
     private static final int LINK_LR_IDENTIFIERS = 258;
 
     @Override
     public void serializeTlvBody(final LinkLrIdentifiers tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedInt(tlv.getLinkLocalIdentifier(), body);
-        ByteBufWriteUtil.writeUnsignedInt(tlv.getLinkRemoteIdentifier(), body);
+        ByteBufUtils.writeOrZero(body, tlv.getLinkLocalIdentifier());
+        ByteBufUtils.writeOrZero(body, tlv.getLinkRemoteIdentifier());
     }
 
     @Override
     public LinkLrIdentifiers parseTlvBody(final ByteBuf value) {
-        final Uint32 localId = Uint32.valueOf(value.readUnsignedInt());
-        final Uint32 remoteId = Uint32.valueOf(value.readUnsignedInt());
-        return new LinkLrIdentifiers() {
-            @Override
-            public Class<LinkLrIdentifiers> implementedInterface() {
-                return LinkLrIdentifiers.class;
-            }
-
-            @Override
-            public Uint32 getLinkRemoteIdentifier() {
-                return remoteId;
-            }
-
-            @Override
-            public Uint32 getLinkLocalIdentifier() {
-                return localId;
-            }
-        };
+        final Uint32 localId = ByteBufUtils.readUint32(value);
+        final Uint32 remoteId = ByteBufUtils.readUint32(value);
+        return new AnonymousLinkLrIdentifiers(localId, remoteId);
     }
 
     @Override
@@ -56,4 +42,29 @@ public final class LinkIdTlvParser implements LinkstateTlvParser.LinkstateTlvSer
     public int getType() {
         return LINK_LR_IDENTIFIERS;
     }
+
+    private static final class AnonymousLinkLrIdentifiers implements LinkLrIdentifiers {
+        private final Uint32 localId;
+        private final Uint32 remoteId;
+
+        AnonymousLinkLrIdentifiers(final Uint32 localId, final Uint32 remoteId) {
+            this.localId = requireNonNull(localId);
+            this.remoteId = requireNonNull(remoteId);
+        }
+
+        @Override
+        public Class<LinkLrIdentifiers> implementedInterface() {
+            return LinkLrIdentifiers.class;
+        }
+
+        @Override
+        public Uint32 getLinkRemoteIdentifier() {
+            return remoteId;
+        }
+
+        @Override
+        public Uint32 getLinkLocalIdentifier() {
+            return localId;
+        }
+    }
 }
index 38437b142a588c8552f62bbfbaf1a8f6d1a00fd4..3c377b8851fe772ce875a429cf17eb0301986cda 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
-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.epe.rev150622.EpeNodeDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -23,7 +22,7 @@ public final class MemAsNumTlvParser implements LinkstateTlvParser<AsNumber>,
 
     @Override
     public void serializeTlvBody(final AsNumber tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedInt(tlv.getValue(), body);
+        ByteBufUtils.write(body, tlv.getValue());
     }
 
     @Override
index 3e29dced8b7205285b193fb0da45072d734c49a1..56b1e4899c3de015c68aa8bfe75dbac6bd1406d0 100644 (file)
@@ -10,22 +10,21 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.TopologyIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LinkDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 
 public final class MultiTopoIdTlvParser implements LinkstateTlvParser<TopologyIdentifier>,
         LinkstateTlvParser.LinkstateTlvSerializer<TopologyIdentifier> {
-
     public static final QName MULTI_TOPOLOGY_ID_QNAME = QName.create(LinkDescriptors.QNAME, "multi-topology-id")
             .intern();
 
     @Override
     public void serializeTlvBody(final TopologyIdentifier tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedShort(tlv.getValue(), body);
+        ByteBufUtils.write(body, tlv.getValue());
     }
 
     @Override
@@ -44,10 +43,8 @@ public final class MultiTopoIdTlvParser implements LinkstateTlvParser<TopologyId
     }
 
     public static TopologyIdentifier serializeModel(final ContainerNode prefixDesc) {
-        if (prefixDesc.getChild(TlvUtil.MULTI_TOPOLOGY_NID).isPresent()) {
-            return new TopologyIdentifier((Uint16) prefixDesc.getChild(TlvUtil.MULTI_TOPOLOGY_NID).get().getValue());
-        }
-        return null;
+        return prefixDesc.getChild(TlvUtil.MULTI_TOPOLOGY_NID)
+                .map(child -> new TopologyIdentifier((Uint16) child.getValue()))
+                .orElse(null);
     }
 }
-
index 2feab94bf7bb18b8a8d7520a2e356c5bd3f2c27b..087aaa0de907b60815126a819d596292800c09a8 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.OspfRouteType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.prefix._case.PrefixDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -18,15 +17,14 @@ import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 
 public final class OspfRouteTlvParser implements LinkstateTlvParser<OspfRouteType>,
         LinkstateTlvParser.LinkstateTlvSerializer<OspfRouteType> {
+    public static final QName OSPF_ROUTE_TYPE_QNAME = QName.create(PrefixDescriptors.QNAME, "ospf-route-type").intern();
+    public static final NodeIdentifier OSPF_ROUTE_NID = NodeIdentifier.create(OspfRouteTlvParser.OSPF_ROUTE_TYPE_QNAME);
 
-    public static final QName OSPF_ROUTE_TYPE_QNAME =
-            QName.create(PrefixDescriptors.QNAME.getModule(), "ospf-route-type");
-    public static final NodeIdentifier OSPF_ROUTE_NID = new NodeIdentifier(OspfRouteTlvParser.OSPF_ROUTE_TYPE_QNAME);
     private static final int OSPF_ROUTE_TYPE = 264;
 
     @Override
     public void serializeTlvBody(final OspfRouteType tlv, final ByteBuf body) {
-        ByteBufWriteUtil.writeUnsignedByte((short) tlv.getIntValue(), body);
+        body.writeByte(tlv.getIntValue());
     }
 
     @Override
index 543443c7bce5f1f6fe870d94adee902a7ea8ac80..2b854e48496630dbc0aac608eb2b5041949f6ea1 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.protocol.bgp.linkstate.impl.tlvs;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.bgp.linkstate.spi.LinkstateTlvParser;
 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.linkstate.rev180329.OspfInterfaceIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.isis.lan.identifier.IsIsRouterIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.isis.lan.identifier.IsIsRouterIdentifierBuilder;
@@ -36,7 +35,6 @@ import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 
 public final class RouterIdTlvParser implements LinkstateTlvParser<CRouterIdentifier>,
         LinkstateTlvParser.LinkstateTlvSerializer<CRouterIdentifier> {
-
     private static final int OSPF_PSEUDONODE_ROUTER_ID_LENGTH = 8;
     private static final int OSPF_ROUTER_ID_LENGTH = 4;
     private static final int ISO_SYSTEM_ID_LENGTH = 6;
@@ -51,13 +49,13 @@ public final class RouterIdTlvParser implements LinkstateTlvParser<CRouterIdenti
         } else if (tlv instanceof IsisPseudonodeCase) {
             final IsisPseudonode isis = ((IsisPseudonodeCase) tlv).getIsisPseudonode();
             body.writeBytes(isis.getIsIsRouterIdentifier().getIsoSystemId().getValue());
-            ByteBufWriteUtil.writeUnsignedByte(isis.getPsn(), body);
+            ByteBufUtils.writeOrZero(body, isis.getPsn());
         } else if (tlv instanceof OspfNodeCase) {
-            ByteBufWriteUtil.writeUnsignedInt(((OspfNodeCase) tlv).getOspfNode().getOspfRouterId(), body);
+            ByteBufUtils.writeOrZero(body, ((OspfNodeCase) tlv).getOspfNode().getOspfRouterId());
         } else if (tlv instanceof OspfPseudonodeCase) {
             final OspfPseudonode node = ((OspfPseudonodeCase) tlv).getOspfPseudonode();
-            ByteBufWriteUtil.writeUnsignedInt(node.getOspfRouterId(), body);
-            ByteBufWriteUtil.writeUnsignedInt(node.getLanInterface().getValue(), body);
+            ByteBufUtils.writeOrZero(body, node.getOspfRouterId());
+            ByteBufUtils.write(body, node.getLanInterface().getValue());
         }
     }
 
index 81b1a94de7e3f61ad2331a03184343170c3cb47d..59cde5330b9033c9d96699553bc70291ded924f8 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.bgp.linkstate.spi;
 
 import io.netty.buffer.ByteBuf;
-import org.opendaylight.protocol.util.ByteBufWriteUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Identifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.ObjectType;
@@ -19,17 +18,17 @@ import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 public abstract class AbstractNlriTypeCodec implements NlriTypeCaseParser, NlriTypeCaseSerializer {
     @Override
     public final CLinkstateDestination parseTypeNlri(final ByteBuf nlri) {
-        final CLinkstateDestinationBuilder builder = new CLinkstateDestinationBuilder();
-        builder.setProtocolId(ProtocolId.forValue(nlri.readUnsignedByte()));
-        builder.setIdentifier(new Identifier(ByteBufUtils.readUint64(nlri)));
-        builder.setObjectType(parseObjectType(nlri));
-        return builder.build();
+        return new CLinkstateDestinationBuilder()
+                .setProtocolId(ProtocolId.forValue(nlri.readUnsignedByte()))
+                .setIdentifier(new Identifier(ByteBufUtils.readUint64(nlri)))
+                .setObjectType(parseObjectType(nlri))
+                .build();
     }
 
     @Override
     public final void serializeTypeNlri(final CLinkstateDestination nlriType, final ByteBuf byteAggregator) {
-        ByteBufWriteUtil.writeUnsignedByte((short) nlriType.getProtocolId().getIntValue(), byteAggregator);
-        ByteBufWriteUtil.writeUnsignedLong(nlriType.getIdentifier().getValue(), byteAggregator);
+        byteAggregator.writeByte(nlriType.getProtocolId().getIntValue());
+        ByteBufUtils.write(byteAggregator, nlriType.getIdentifier().getValue());
         serializeObjectType(nlriType.getObjectType(), byteAggregator);
     }
 
index 4a7c4c2c490cf7e729676f5cdc21ce20fa7a8681..7813f2d9f2ae78924a234511dd02120c9c0f5873 100644 (file)
@@ -27,6 +27,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Uint16;
 import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 
@@ -59,14 +60,13 @@ public abstract class AbstractTeLspNlriCodec extends AbstractNlriTypeCodec {
     }
 
     public static TeLspCase serializeTeLsp(final ChoiceNode objectType) {
-        final TeLspCaseBuilder teLsp = new TeLspCaseBuilder();
-        teLsp.setLspId(new LspId((Uint32) objectType.getChild(LSP_ID).get().getValue()));
-        teLsp.setTunnelId(new TunnelId((Uint16) objectType.getChild(TUNNEL_ID).get().getValue()));
         final ChoiceNode addressFamily = (ChoiceNode) objectType.getChild(ADDRESS_FAMILY).get();
-        teLsp.setAddressFamily(serializeAddressFamily(addressFamily,
-            addressFamily.getChild(IPV4_TUNNEL_SENDER_ADDRESS).isPresent()));
-
-        return teLsp.build();
+        return new TeLspCaseBuilder()
+                .setLspId(new LspId((Uint32) objectType.getChild(LSP_ID).get().getValue()))
+                .setTunnelId(new TunnelId((Uint16) objectType.getChild(TUNNEL_ID).get().getValue()))
+                .setAddressFamily(serializeAddressFamily(addressFamily,
+                    addressFamily.getChild(IPV4_TUNNEL_SENDER_ADDRESS).isPresent()))
+                .build();
     }
 
     private static AddressFamily serializeAddressFamily(final ChoiceNode addressFamily, final boolean ipv4Case) {
@@ -112,7 +112,7 @@ public abstract class AbstractTeLspNlriCodec extends AbstractNlriTypeCodec {
     }
 
     private static void serializeTunnelIdAndLspId(final ByteBuf buffer, final TeLspCase teLSP) {
-        ByteBufWriteUtil.writeUnsignedShort(teLSP.getTunnelId().getValue(), buffer);
-        ByteBufWriteUtil.writeUnsignedShort(teLSP.getLspId().getValue().intValue(), buffer);
+        ByteBufUtils.write(buffer, teLSP.getTunnelId().getValue());
+        buffer.writeShort(teLSP.getLspId().getValue().intValue());
     }
 }