This is a simple pass to optimize builder/ByteBuf usage.
Change-Id: Ic00f4e07ded8b9c5d46473de6a0972d04791f971
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
*/
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;
*/
@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);
}
@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);
}
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;
}
}
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);
*/
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;
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;
*/
@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);
}
@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();
}
}
*/
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;
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);
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;
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
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);
}
}
}
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());
}
}
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;
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) {
"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();
}
}
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;
}
}
- 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) {
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 {
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());
}
@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);
}
}
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;
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);
}
@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);
}
}
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;
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);
}
@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);
}
}
@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);
}
}
@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);
+ }
}
}