package org.opendaylight.protocol.bgp.parser.impl.message.update;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import com.google.common.primitives.UnsignedBytes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPError;
private final ReferenceCache refCache;
private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
+ private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build();
+
public AsPathAttributeParser(final ReferenceCache refCache) {
this.refCache = Preconditions.checkNotNull(refCache);
}
* @throws BGPParsingException
*/
private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
- final List<Segments> ases = Lists.newArrayList();
+ if (!buffer.isReadable()) {
+ return EMPTY;
+ }
+ final List<Segments> ases = new ArrayList<>();
boolean isSequence = false;
while (buffer.isReadable()) {
final int type = UnsignedBytes.toInt(buffer.readByte());
}
buffer.skipBytes(count * AsPathSegmentParser.AS_NUMBER_LENGTH);
}
-
- if (!isSequence && buffer.readableBytes() != 0) {
+ if (!isSequence) {
throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
}
return new AsPathBuilder().setSegments(ases).build();
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.rev100924.AsNumber;
coll.add(
refCache.getSharedReference(new AsSequenceBuilder().setAs(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()))).build()));
}
- return coll;
+ return (coll.isEmpty()) ? Collections.<AsSequence>emptyList() : coll;
}
static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
coll.add(refCache.getSharedReference(
new AsNumber(buffer.readUnsignedInt())));
}
- return coll;
+ return (coll.isEmpty()) ? Collections.<AsNumber>emptyList() : coll;
}
- static void serializeAsSet(ASetCase aSetCase, ByteBuf byteAggregator) {
- ASet aset = aSetCase.getASet();
+ static void serializeAsSet(final ASetCase aSetCase, final ByteBuf byteAggregator) {
+ final ASet aset = aSetCase.getASet();
if (aset == null || aset.getAsSet() == null) {
return;
}
byteAggregator.writeByte(serializeType(AS_SET));
byteAggregator.writeByte(aset.getAsSet().size());
- for (AsNumber asNumber : aset.getAsSet()) {
+ for (final AsNumber asNumber : aset.getAsSet()) {
byteAggregator.writeInt(asNumber.getValue().intValue());
}
}
- static void serializeAsSequence(AListCase aListCase, ByteBuf byteAggregator) {
- AList alist = aListCase.getAList();
+ static void serializeAsSequence(final AListCase aListCase, final ByteBuf byteAggregator) {
+ final AList alist = aListCase.getAList();
if (alist == null || alist.getAsSequence() == null) {
return;
}
byteAggregator.writeByte(serializeType(AS_SEQUENCE));
byteAggregator.writeByte(alist.getAsSequence().size());
- for (AsSequence value : alist.getAsSequence()) {
+ for (final AsSequence value : alist.getAsSequence()) {
byteAggregator.writeInt(value.getAs().getValue().intValue());
}
}
public static final int TYPE = 1;
+ private static final Origin IGP = new OriginBuilder().setValue(BgpOrigin.Igp).build();
+ private static final Origin EGP = new OriginBuilder().setValue(BgpOrigin.Egp).build();
+ private static final Origin INC = new OriginBuilder().setValue(BgpOrigin.Incomplete).build();
+
@Override
public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
final byte rawOrigin = buffer.readByte();
if (borigin == null) {
throw new BGPDocumentedException("Unknown Origin type.", BGPError.ORIGIN_ATTR_NOT_VALID, new byte[] { (byte) 0x01, (byte) 0x01, rawOrigin} );
}
- builder.setOrigin(new OriginBuilder().setValue(borigin).build());
+ switch (borigin) {
+ case Egp:
+ builder.setOrigin(EGP);
+ return;
+ case Igp:
+ builder.setOrigin(IGP);
+ return;
+ case Incomplete:
+ builder.setOrigin(INC);
+ return;
+ }
}
@Override
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.junit.BeforeClass;
new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
new Ipv4Address("192.168.1.0")).setLocalAdministrator(new byte[] { 0x12, 0x34 }).build()).build()).build());
- final List<Segments> asPath = Lists.newArrayList();
-
// check path attributes
final PathAttributes attrs = message.getPathAttributes();
paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Egp).build());
assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
- paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
+ paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build());
assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
paBuilder.setCNextHop(nextHop);
final Ipv4NextHopCase nextHop = new Ipv4NextHopCaseBuilder().setIpv4NextHop(
new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("25.25.25.1")).build()).build();
- final List<Segments> asPath = Lists.newArrayList();
-
final LocalNodeDescriptorsBuilder lndBuilder = new LocalNodeDescriptorsBuilder().setAsNumber(new AsNumber((long) 100)).setDomainId(
new DomainIdentifier(0x19191901L)).setAreaId(new AreaIdentifier(0L));
paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
- paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
+ paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build());
assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());
new DestinationLinkstateCaseBuilder().setDestinationLinkstate(dBuilder.build()).build()).build());
lsBuilder.setMpReachNlri(mpBuilder.build());
- final List<Segments> asPath = Lists.newArrayList();
-
// check path attributes
final PathAttributes attrs = message.getPathAttributes();
paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
assertEquals(paBuilder.getOrigin(), attrs.getOrigin());
- paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
+ paBuilder.setAsPath(new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build());
assertEquals(paBuilder.getAsPath(), attrs.getAsPath());
paBuilder.setLocalPref(new LocalPrefBuilder().setPref(100L).build());