MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / AsPathAttributeParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import static java.util.Objects.requireNonNull;
11
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.rev180329.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPath;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
38
39     public static final int TYPE = 2;
40
41     private final ReferenceCache refCache;
42     private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
43
44     private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.emptyList()).build();
45
46     public AsPathAttributeParser(final ReferenceCache refCache) {
47         this.refCache = requireNonNull(refCache);
48     }
49
50     /**
51      * Parses AS_PATH from bytes.
52      *
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
58      */
59     private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
60         if (!buffer.isReadable()) {
61             return EMPTY;
62         }
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);
70             }
71             final int count = buffer.readUnsignedByte();
72
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());
76                 isSequence = true;
77             } else {
78                 ases.add(new SegmentsBuilder().setAsSet(asList).build());
79             }
80         }
81         if (!isSequence) {
82             throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
83         }
84
85         ases.trimToSize();
86         return new AsPathBuilder().setSegments(ases).build();
87     }
88
89     @Override
90     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
91         builder.setAsPath(parseAsPath(this.refCache, buffer));
92     }
93
94     @Override
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();
99         if (asPath == null) {
100             return;
101         }
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);
109                 } else {
110                     LOG.warn("Segment doesn't have AsSequence nor AsSet list.");
111                 }
112             }
113         }
114         AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, segmentsBuffer, byteAggregator);
115     }
116 }