Optimize message parsers 72/82872/7
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 1 Jul 2019 21:26:22 +0000 (23:26 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Jul 2019 09:50:47 +0000 (11:50 +0200)
This is a simple pass to optimize builder/ByteBuf usage.

Change-Id: Ic00f4e07ded8b9c5d46473de6a0972d04791f971
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
14 files changed:
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPNotificationMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPOpenMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/BGPRouteRefreshMessageParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/AddPathCapabilityHandler.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AggregatorAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AigpAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AsPathAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/AsPathSegmentParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/BgpPrefixSidAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/ClusterIdAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/MPReachAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/MPUnreachAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/NextHopAttributeParser.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/update/OriginAttributeParser.java

index 856d4ec6bf43de00f5bd6997c9b1b8dfa50e7bc5..6f792b26b62bb6b46c24c0d449d8b7c38f2fc2c1 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 import io.netty.buffer.Unpooled;
@@ -44,17 +45,19 @@ public final class BGPNotificationMessageParser implements MessageParser, Messag
      */
     @Override
     public void serializeMessage(final Notification msg, final ByteBuf bytes) {
-        Preconditions.checkArgument(msg instanceof Notify, "Message needs to be of type Notify");
+        checkArgument(msg instanceof Notify, "Message needs to be of type Notify");
         final Notify ntf = (Notify) msg;
 
-        final ByteBuf msgBody = Unpooled.buffer();
-        msgBody.writeByte(ntf.getErrorCode());
-        msgBody.writeByte(ntf.getErrorSubcode());
+        final ByteBuf msgBody = Unpooled.buffer()
+                .writeByte(ntf.getErrorCode())
+                .writeByte(ntf.getErrorSubcode());
         final byte[] data = ntf.getData();
         if (data != null) {
             msgBody.writeBytes(data);
         }
-        LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(msgBody));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Notification message serialized to: {}", ByteBufUtil.hexDump(msgBody));
+        }
         MessageUtil.formatMessage(TYPE, msgBody, bytes);
     }
 
@@ -69,7 +72,7 @@ public final class BGPNotificationMessageParser implements MessageParser, Messag
     @Override
     public Notify parseMessageBody(final ByteBuf body, final int messageLength,
             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
-        Preconditions.checkArgument(body != null, "Buffer cannot be null.");
+        checkArgument(body != null, "Buffer cannot be null.");
         if (body.readableBytes() < ERROR_SIZE) {
             throw BGPDocumentedException.badMessageLength("Notification message too small.", messageLength);
         }
@@ -80,8 +83,14 @@ public final class BGPNotificationMessageParser implements MessageParser, Messag
         if (body.isReadable()) {
             builder.setData(ByteArray.readAllBytes(body));
         }
-        LOG.debug("BGP Notification message was parsed: err = {}, data = {}.",
-                BGPError.forValue(errorCode, errorSubcode), Arrays.toString(builder.getData()));
-        return builder.build();
+
+        final Notify result = builder.build();
+        final BGPError err = BGPError.forValue(errorCode, errorSubcode);
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("BGP Notification message was parsed: err = {}, data = {}.", err,
+                Arrays.toString(result.getData()));
+        }
+        return result;
     }
 }
index b97dbf0aea16a2fd422454e573fc721617f09763..06a28a2c4ed98e5c58cbc407796034b8bf5ddc92 100644 (file)
@@ -84,18 +84,18 @@ public final class BGPOpenMessageParser implements MessageParser, MessageSeriali
     public void serializeMessage(final Notification msg, final ByteBuf bytes) {
         checkArgument(msg instanceof Open, "Message needs to be of type Open, not %s", msg);
         final Open open = (Open) msg;
-        final ByteBuf msgBody = Unpooled.buffer();
-
-        msgBody.writeByte(BGP_VERSION);
+        final ByteBuf msgBody = Unpooled.buffer()
+                .writeByte(BGP_VERSION);
 
         // When our AS number does not fit into two bytes, we report it as AS_TRANS
         int openAS = open.getMyAsNumber();
         if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) {
             openAS = AS_TRANS;
         }
-        msgBody.writeShort(openAS);
-        msgBody.writeShort(open.getHoldTimer());
-        msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
+        msgBody
+            .writeShort(openAS)
+            .writeShort(open.getHoldTimer())
+            .writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
 
         serializeParameters(open.getBgpParameters(), msgBody);
 
index e736e6797832323d81474c46e2ba41f3c733b4eb..b8775eae67617f9acfd52b2a7f9804d1f3ffe0e3 100644 (file)
@@ -7,13 +7,12 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
 import io.netty.buffer.Unpooled;
-import java.util.Optional;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
 import org.opendaylight.protocol.bgp.parser.BGPError;
 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
@@ -23,7 +22,6 @@ import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
 import org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefresh;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefreshBuilder;
 import org.opendaylight.yangtools.yang.binding.Notification;
@@ -56,14 +54,16 @@ public final class BGPRouteRefreshMessageParser implements MessageParser, Messag
      */
     @Override
     public void serializeMessage(final Notification message, final ByteBuf bytes) {
-        Preconditions.checkArgument(message instanceof RouteRefresh, ARGUMENT_ERROR);
+        checkArgument(message instanceof RouteRefresh, ARGUMENT_ERROR);
         final RouteRefresh msg = (RouteRefresh) message;
 
         final ByteBuf msgBuf = Unpooled.buffer(TRIPLET_BYTE_SIZE);
         MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(this.afiReg, this.safiReg,
                 msg.getAfi(), msg.getSafi(), msgBuf);
 
-        LOG.trace("RouteRefresh message serialized to: {}", ByteBufUtil.hexDump(msgBuf));
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("RouteRefresh message serialized to: {}", ByteBufUtil.hexDump(msgBuf));
+        }
         MessageUtil.formatMessage(TYPE, msgBuf, bytes);
     }
 
@@ -78,16 +78,13 @@ public final class BGPRouteRefreshMessageParser implements MessageParser, Messag
     @Override
     public RouteRefresh parseMessageBody(final ByteBuf body, final int messageLength,
             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
-        Preconditions.checkArgument(body != null, "Body buffer cannot be null.");
+        checkArgument(body != null, "Body buffer cannot be null.");
         if (body.readableBytes() < TRIPLET_BYTE_SIZE) {
             throw BGPDocumentedException.badMessageLength("RouteRefresh message is too small.", messageLength);
         }
-        final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, this.afiReg,
-                this.safiReg);
-        if (!parsedAfiSafi.isPresent()) {
-            throw new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.",
-                    BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
-        }
-        return new RouteRefreshBuilder(parsedAfiSafi.get()).build();
+        return new RouteRefreshBuilder(MultiprotocolCapabilitiesUtil.parseMPAfiSafi(body, this.afiReg, this.safiReg)
+            .orElseThrow(() -> new BGPDocumentedException("Unsupported afi/safi in Route Refresh message.",
+                BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED))
+            ).build();
     }
 }
index bc67c87e0276d936482edbade3b3b1eda6dd771f..e3989d841fad19c92b2a5e03496c4496a65dc1a1 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.protocol.bgp.parser.impl.message.open;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.ArrayList;
@@ -63,17 +63,14 @@ public class AddPathCapabilityHandler implements CapabilityParser, CapabilitySer
             for (final AddressFamilies addressFamily : families) {
                 final Class<? extends AddressFamily> afi = addressFamily.getAfi();
                 final Integer afival = this.afiReg.numberForClass(afi);
-                Preconditions.checkArgument(afival != null, "Unhandled address family " + afi);
-                capBuffer.writeShort(afival);
-
+                checkArgument(afival != null, "Unhandled address family " + afi);
                 final Class<? extends SubsequentAddressFamily> safi = addressFamily.getSafi();
                 final Integer safival = this.safiReg.numberForClass(safi);
-                Preconditions.checkArgument(safival != null, "Unhandled subsequent address family " + safi);
-                capBuffer.writeByte(safival);
-
+                checkArgument(safival != null, "Unhandled subsequent address family " + safi);
                 final SendReceive sendReceive = addressFamily.getSendReceive();
-                Preconditions.checkArgument(sendReceive != null, "Unhandled Send/Receive value");
-                capBuffer.writeByte(sendReceive.getIntValue());
+                checkArgument(sendReceive != null, "Unhandled Send/Receive value");
+
+                capBuffer.writeShort(afival).writeByte(safival).writeByte(sendReceive.getIntValue());
             }
 
             CapabilityUtil.formatCapability(CODE, capBuffer, byteAggregator);
index 2c9545033f16d84657df98dab1208c6aedd22d26..2e670fa4401e1c0b049e8739c6077f165fc29d54 100644 (file)
@@ -19,7 +19,6 @@ import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
 import org.opendaylight.protocol.util.Ipv4Util;
 import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aggregator;
@@ -51,10 +50,11 @@ public final class AggregatorAttributeParser extends AbstractAttributeParser imp
             return;
         }
 
-        // FIXME: above check should be expanded, so we report at least underflow errors
-        final AsNumber asNumber = this.refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()));
-        final Ipv4Address address = Ipv4Util.addressForByteBuf(buffer);
-        builder.setAggregator(new AggregatorBuilder().setAsNumber(asNumber).setNetworkAddress(address).build());
+        builder.setAggregator(new AggregatorBuilder()
+            // FIXME: above check should be expanded, so we report at least underflow errors
+            .setAsNumber(this.refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt())))
+            .setNetworkAddress(Ipv4Util.addressForByteBuf(buffer))
+            .build());
     }
 
     @Override
@@ -63,11 +63,11 @@ public final class AggregatorAttributeParser extends AbstractAttributeParser imp
         if (aggregator != null) {
             final AsNumber asNumber = aggregator.getAsNumber();
             if (asNumber != null) {
-                final ByteBuf buffer = Unpooled.buffer(AGGREGATOR_LENGTH);
-                buffer.writeInt(new ShortAsNumber(asNumber).getValue().intValue());
-                buffer.writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress()));
-                AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE,
-                        TYPE, buffer, byteAggregator);
+                AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE,
+                    Unpooled.buffer(AGGREGATOR_LENGTH)
+                        .writeInt(new ShortAsNumber(asNumber).getValue().intValue())
+                        .writeBytes(Ipv4Util.bytesForAddress(aggregator.getNetworkAddress())),
+                        byteAggregator);
             }
         }
     }
index 04ed7017cd70fa98b3988153046b064c2c71145a..bb2ee26fe43f81ba56abd7c58bd03fee72c557ab 100644 (file)
@@ -78,10 +78,9 @@ public class AigpAttributeParser implements AttributeParser, AttributeSerializer
         if (tlv == null) {
             return Unpooled.EMPTY_BUFFER;
         }
-        final ByteBuf value = Unpooled.buffer(AIGP_TLV_SIZE);
-        value.writeByte(AIGP_TLV_TYPE);
-        value.writeShort(AIGP_TLV_SIZE);
-        value.writeLong(tlv.getMetric().getValue().longValue());
-        return value;
+        return Unpooled.buffer(AIGP_TLV_SIZE)
+                .writeByte(AIGP_TLV_TYPE)
+                .writeShort(AIGP_TLV_SIZE)
+                .writeLong(tlv.getMetric().getValue().longValue());
     }
 }
index 44f34ab7696f5f637776fd71f178472d6ae8d529..d5d11090f274bd0fa3ddd72bd184631fd6c32706 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.collect.ImmutableList;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.ArrayList;
@@ -93,7 +94,7 @@ public final class AsPathAttributeParser extends AbstractAttributeParser impleme
             return EMPTY;
         }
 
-        final ArrayList<Segments> ases = new ArrayList<>();
+        final List<Segments> ases = new ArrayList<>();
         boolean isSequence = false;
         for (int readable = buffer.readableBytes(); readable != 0; readable = buffer.readableBytes()) {
             if (readable < 2) {
@@ -134,7 +135,6 @@ public final class AsPathAttributeParser extends AbstractAttributeParser impleme
                 "AS_SEQUENCE must be present in AS_PATH attribute.");
         }
 
-        ases.trimToSize();
-        return new AsPathBuilder().setSegments(ases).build();
+        return new AsPathBuilder().setSegments(ImmutableList.copyOf(ases)).build();
     }
 }
index 9b84a454bc169060c477ab19119dd9d98ed02f20..d613c48c4676216f707d4e06b70412aefb1cbaa8 100644 (file)
@@ -10,9 +10,9 @@ package org.opendaylight.protocol.bgp.parser.impl.message.update;
 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SEQUENCE;
 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SET;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableList.Builder;
 import io.netty.buffer.ByteBuf;
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import org.opendaylight.protocol.util.ReferenceCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
@@ -58,12 +58,17 @@ public final class AsPathSegmentParser {
         }
     }
 
-    static List<AsNumber> parseAsSegment(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
-        final List<AsNumber> coll = new ArrayList<>(count);
+    static ImmutableList<AsNumber> parseAsSegment(final ReferenceCache refCache, final int count,
+            final ByteBuf buffer) {
+        if (count == 0) {
+            return ImmutableList.of();
+        }
+
+        final Builder<AsNumber> coll = ImmutableList.builderWithExpectedSize(count);
         for (int i = 0; i < count; i++) {
             coll.add(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt())));
         }
-        return coll.isEmpty() ? Collections.emptyList() : coll;
+        return coll.build();
     }
 
     static void serializeAsList(final List<AsNumber> asList, final SegmentType type, final ByteBuf byteAggregator) {
index c53f91d226179faee5e776d6a381eebcc0fc8dee..1e5f5b9512d8e5d7c5791a58f130975aa771d88c 100644 (file)
@@ -24,7 +24,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.BgpPrefixSidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
 
 public final class BgpPrefixSidAttributeParser implements AttributeParser, AttributeSerializer {
 
@@ -53,8 +52,9 @@ public final class BgpPrefixSidAttributeParser implements AttributeParser, Attri
         final BgpPrefixSidBuilder sid = new BgpPrefixSidBuilder();
         final List<BgpPrefixSidTlvs> tlvList = new ArrayList<>();
         while (buffer.isReadable()) {
-            final BgpPrefixSidTlv tlv = this.reg.parseBgpPrefixSidTlv(buffer.readUnsignedByte(), buffer);
-            tlvList.add(new BgpPrefixSidTlvsBuilder().setBgpPrefixSidTlv(tlv).build());
+            tlvList.add(new BgpPrefixSidTlvsBuilder()
+                .setBgpPrefixSidTlv(this.reg.parseBgpPrefixSidTlv(buffer.readUnsignedByte(), buffer))
+                .build());
         }
         builder.setBgpPrefixSid(sid.setBgpPrefixSidTlvs(tlvList).build());
     }
index b3daa9da97590d804a0bb2d03414bfbfa7fa3d1d..1632d850dabdf292ca3e4f6f110f27a9081db432 100644 (file)
@@ -64,17 +64,15 @@ public final class ClusterIdAttributeParser extends AbstractAttributeParser impl
     @Override
     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
         final ClusterId cid = pathAttributes.getClusterId();
-        if (cid == null) {
-            return;
-        }
-        final List<ClusterIdentifier> cluster = cid.getCluster();
-        if (cluster == null || cluster.isEmpty()) {
-            return;
-        }
-        final ByteBuf clusterIdBuffer = Unpooled.buffer();
-        for (final ClusterIdentifier clusterIdentifier : cid.getCluster()) {
-            clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier));
+        if (cid != null) {
+            final List<ClusterIdentifier> cluster = cid.getCluster();
+            if (cluster != null && !cluster.isEmpty()) {
+                final ByteBuf clusterIdBuffer = Unpooled.buffer();
+                for (final ClusterIdentifier clusterIdentifier : cluster) {
+                    clusterIdBuffer.writeBytes(Ipv4Util.bytesForAddress(clusterIdentifier));
+                }
+                AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator);
+            }
         }
-        AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, clusterIdBuffer, byteAggregator);
     }
 }
index 4f8a3a2891304b84ff40a27195a3743f24108f1c..d528523893a834c3407451ad180c198450b95db6 100644 (file)
@@ -23,10 +23,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
 
 public final class MPReachAttributeParser extends ReachAttributeParser {
-
     public static final int TYPE = 14;
 
     private final NlriRegistry reg;
@@ -40,9 +38,9 @@ public final class MPReachAttributeParser extends ReachAttributeParser {
             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
                     throws BGPDocumentedException {
         try {
-            final MpReachNlri mpReachNlri = this.reg.parseMpReach(buffer, constraint);
-            final Attributes1 a = new Attributes1Builder().setMpReachNlri(mpReachNlri).build();
-            builder.addAugmentation(Attributes1.class, a);
+            builder.addAugmentation(Attributes1.class, new Attributes1Builder()
+                .setMpReachNlri(reg.parseMpReach(buffer, constraint))
+                .build());
         } catch (final BGPParsingException e) {
             throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
         }
@@ -51,16 +49,14 @@ public final class MPReachAttributeParser extends ReachAttributeParser {
     @Override
     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
         final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
-        if (pathAttributes1 == null) {
-            return;
-        }
-        final MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
-        final ByteBuf reachBuffer = Unpooled.buffer();
-        this.reg.serializeMpReach(mpReachNlri, reachBuffer);
-
-        for (final NlriSerializer nlriSerializer : this.reg.getSerializers()) {
-            nlriSerializer.serializeAttribute(pathAttributes, reachBuffer);
+        if (pathAttributes1 != null) {
+            final ByteBuf reachBuffer = Unpooled.buffer();
+            reg.serializeMpReach(pathAttributes1.getMpReachNlri(), reachBuffer);
+
+            for (final NlriSerializer nlriSerializer : reg.getSerializers()) {
+                nlriSerializer.serializeAttribute(pathAttributes, reachBuffer);
+            }
+            AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, reachBuffer, byteAggregator);
         }
-        AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, reachBuffer, byteAggregator);
     }
 }
index a055386a37f847e96aa489107ecabeca24781cb6..ba18f0558f278451f43346200d58a7d38094dbc5 100644 (file)
@@ -23,7 +23,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
 
 public final class MPUnreachAttributeParser extends ReachAttributeParser {
     public static final int TYPE = 15;
@@ -39,9 +38,9 @@ public final class MPUnreachAttributeParser extends ReachAttributeParser {
             final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
                     throws BGPDocumentedException {
         try {
-            final MpUnreachNlri mpUnreachNlri = this.reg.parseMpUnreach(buffer, constraint);
-            final Attributes2 a = new Attributes2Builder().setMpUnreachNlri(mpUnreachNlri).build();
-            builder.addAugmentation(Attributes2.class, a);
+            builder.addAugmentation(Attributes2.class, new Attributes2Builder()
+                .setMpUnreachNlri(reg.parseMpUnreach(buffer, constraint))
+                .build());
         } catch (final BGPParsingException e) {
             throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
         }
@@ -49,17 +48,14 @@ public final class MPUnreachAttributeParser extends ReachAttributeParser {
 
     @Override
     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
-        final Attributes pathAttributes = attribute;
-        final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
-        if (pathAttributes2 == null) {
-            return;
+        final Attributes2 pathAttributes2 = attribute.augmentation(Attributes2.class);
+        if (pathAttributes2 != null) {
+            final ByteBuf unreachBuffer = Unpooled.buffer();
+            reg.serializeMpUnReach(pathAttributes2.getMpUnreachNlri(), unreachBuffer);
+            for (final NlriSerializer nlriSerializer : this.reg.getSerializers()) {
+                nlriSerializer.serializeAttribute(attribute, unreachBuffer);
+            }
+            AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);
         }
-        final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
-        final ByteBuf unreachBuffer = Unpooled.buffer();
-        this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
-        for (final NlriSerializer nlriSerializer : this.reg.getSerializers()) {
-            nlriSerializer.serializeAttribute(attribute, unreachBuffer);
-        }
-        AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);
     }
 }
index 8382dcf3e4902101539766ada19d0b0e4ca5751e..d4b0cddd1666b70961622dbcfda391da17e069a7 100644 (file)
@@ -56,11 +56,10 @@ public final class NextHopAttributeParser extends AbstractAttributeParser implem
     @Override
     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
         final CNextHop cNextHop = attribute.getCNextHop();
-        if (cNextHop == null) {
-            return;
+        if (cNextHop != null) {
+            final ByteBuf nextHopBuffer = Unpooled.buffer();
+            NextHopUtil.serializeNextHop(cNextHop, nextHopBuffer);
+            AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, nextHopBuffer, byteAggregator);
         }
-        final ByteBuf nextHopBuffer = Unpooled.buffer();
-        NextHopUtil.serializeNextHop(cNextHop, nextHopBuffer);
-        AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, nextHopBuffer, byteAggregator);
     }
 }
index 8947aa17c47d896b79aeaf3ad63ec26682616810..803ca61b22b169481ddf92d3a50a6865d6661b69 100644 (file)
@@ -64,11 +64,10 @@ public final class OriginAttributeParser extends AbstractAttributeParser impleme
     @Override
     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
         final Origin origin = attribute.getOrigin();
-        if (origin == null) {
-            return;
-        }
-        AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE,
-                Unpooled.wrappedBuffer(new byte[]{UnsignedBytes.checkedCast(origin.getValue().getIntValue())}),
+        if (origin != null) {
+            AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE,
+                Unpooled.wrappedBuffer(new byte[]{ UnsignedBytes.checkedCast(origin.getValue().getIntValue()) }),
                 byteAggregator);
+        }
     }
 }