Search in sources :

Example 1 with SegmentType

use of org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType in project bgpcep by opendaylight.

the class AsPathAttributeParser method parseAsPath.

/**
 * Parses AS_PATH from bytes.
 *
 * @param refCache ReferenceCache shared reference of object
 * @param buffer bytes to be parsed
 * @return new ASPath object
 * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
 * @throws BGPParsingException
 */
private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
    if (!buffer.isReadable()) {
        return EMPTY;
    }
    final ArrayList<Segments> ases = new ArrayList<>();
    boolean isSequence = false;
    while (buffer.isReadable()) {
        final int type = buffer.readUnsignedByte();
        final SegmentType segmentType = AsPathSegmentParser.parseType(type);
        if (segmentType == null) {
            throw new BGPParsingException("AS Path segment type unknown : " + type);
        }
        final int count = buffer.readUnsignedByte();
        final List<AsNumber> asList = AsPathSegmentParser.parseAsSegment(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
        if (segmentType == SegmentType.AS_SEQUENCE) {
            ases.add(new SegmentsBuilder().setAsSequence(asList).build());
            isSequence = true;
        } else {
            ases.add(new SegmentsBuilder().setAsSet(asList).build());
        }
    }
    if (!isSequence) {
        throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
    }
    ases.trimToSize();
    return new AsPathBuilder().setSegments(ases).build();
}
Also used : SegmentType(org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType) AsPathBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder) SegmentsBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder) BGPDocumentedException(org.opendaylight.protocol.bgp.parser.BGPDocumentedException) ArrayList(java.util.ArrayList) Segments(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments) BGPParsingException(org.opendaylight.protocol.bgp.parser.BGPParsingException) AsNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber)

Aggregations

ArrayList (java.util.ArrayList)1 BGPDocumentedException (org.opendaylight.protocol.bgp.parser.BGPDocumentedException)1 BGPParsingException (org.opendaylight.protocol.bgp.parser.BGPParsingException)1 SegmentType (org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType)1 AsNumber (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber)1 AsPathBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder)1 Segments (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments)1 SegmentsBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder)1