2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10 import java.util.List;
12 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
13 import org.opendaylight.protocol.bgp.parser.BGPError;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.SegmentsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.CAListBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.CASetBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.c.a.list.AsSequence;
28 import com.google.common.collect.Lists;
29 import com.google.common.primitives.UnsignedBytes;
31 public final class AsPathAttributeParser implements AttributeParser {
32 public static final int TYPE = 2;
35 * Parses AS_PATH from bytes.
37 * @param bytes byte array to be parsed
38 * @return new ASPath object
39 * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
40 * @throws BGPParsingException
42 private static AsPath parseAsPath(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
44 final List<Segments> ases = Lists.newArrayList();
45 boolean isSequence = false;
46 while (byteOffset < bytes.length) {
47 final int type = UnsignedBytes.toInt(bytes[byteOffset]);
48 final SegmentType segmentType = AsPathSegmentParser.parseType(type);
49 if (segmentType == null) {
50 throw new BGPParsingException("AS Path segment type unknown : " + type);
52 byteOffset += AsPathSegmentParser.TYPE_LENGTH;
54 final int count = UnsignedBytes.toInt(bytes[byteOffset]);
55 byteOffset += AsPathSegmentParser.LENGTH_SIZE;
57 if (segmentType == SegmentType.AS_SEQUENCE) {
58 final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(count,
59 ByteArray.subByte(bytes, byteOffset, count * AsPathSegmentParser.AS_NUMBER_LENGTH));
60 ases.add(new SegmentsBuilder().setCSegment(new CAListBuilder().setAsSequence(numbers).build()).build());
63 final List<AsNumber> list = AsPathSegmentParser.parseAsSet(count,
64 ByteArray.subByte(bytes, byteOffset, count * AsPathSegmentParser.AS_NUMBER_LENGTH));
65 ases.add(new SegmentsBuilder().setCSegment(new CASetBuilder().setAsSet(list).build()).build());
68 byteOffset += count * AsPathSegmentParser.AS_NUMBER_LENGTH;
71 if (!isSequence && bytes.length != 0) {
72 throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
74 return new AsPathBuilder().setSegments(ases).build();
78 public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
79 builder.setAsPath(parseAsPath(bytes));