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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
23 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
24 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
25 import org.opendaylight.protocol.util.ReferenceCache;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPath;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.AsPathBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.SegmentsBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
39 public static final int TYPE = 2;
41 private final ReferenceCache refCache;
42 private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
44 private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.emptyList()).build();
46 public AsPathAttributeParser(final ReferenceCache refCache) {
47 this.refCache = requireNonNull(refCache);
51 * Parses AS_PATH from bytes.
53 * @param refCache ReferenceCache shared reference of object
54 * @param buffer bytes to be parsed
55 * @return new ASPath object
56 * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
57 * @throws BGPParsingException
59 private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
60 if (!buffer.isReadable()) {
63 final ArrayList<Segments> ases = new ArrayList<>();
64 boolean isSequence = false;
65 while (buffer.isReadable()) {
66 final int type = buffer.readUnsignedByte();
67 final SegmentType segmentType = AsPathSegmentParser.parseType(type);
68 if (segmentType == null) {
69 throw new BGPParsingException("AS Path segment type unknown : " + type);
71 final int count = buffer.readUnsignedByte();
73 final List<AsNumber> asList = AsPathSegmentParser.parseAsSegment(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
74 if (segmentType == SegmentType.AS_SEQUENCE) {
75 ases.add(new SegmentsBuilder().setAsSequence(asList).build());
78 ases.add(new SegmentsBuilder().setAsSet(asList).build());
82 throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
86 return new AsPathBuilder().setSegments(ases).build();
90 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
91 builder.setAsPath(parseAsPath(this.refCache, buffer));
95 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
96 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
97 final Attributes pathAttributes = (Attributes) attribute;
98 final AsPath asPath = pathAttributes.getAsPath();
102 final ByteBuf segmentsBuffer = Unpooled.buffer();
103 if (asPath.getSegments() != null) {
104 for (final Segments segments : asPath.getSegments()) {
105 if (segments.getAsSequence() != null) {
106 AsPathSegmentParser.serializeAsList(segments.getAsSequence(), SegmentType.AS_SEQUENCE, segmentsBuffer);
107 } else if (segments.getAsSet() != null) {
108 AsPathSegmentParser.serializeAsList(segments.getAsSet(), SegmentType.AS_SET, segmentsBuffer);
110 LOG.warn("Segment doesn't have AsSequence nor AsSet list.");
114 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, segmentsBuffer, byteAggregator);