BUG-2982 : moved path-attributes container to grouping
[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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
23 import org.opendaylight.protocol.util.ReferenceCache;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPath;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AListBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASetBuilder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
43
44     public static final int TYPE = 2;
45
46     private final ReferenceCache refCache;
47     private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
48
49     private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build();
50
51     public AsPathAttributeParser(final ReferenceCache refCache) {
52         this.refCache = Preconditions.checkNotNull(refCache);
53     }
54
55     /**
56      * Parses AS_PATH from bytes.
57      *
58      * @param refCache ReferenceCache shared reference of object
59      * @param buffer bytes to be parsed
60      * @return new ASPath object
61      * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
62      * @throws BGPParsingException
63      */
64     private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
65         if (!buffer.isReadable()) {
66             return EMPTY;
67         }
68         final ArrayList<Segments> ases = new ArrayList<>();
69         boolean isSequence = false;
70         while (buffer.isReadable()) {
71             final int type = buffer.readUnsignedByte();
72             final SegmentType segmentType = AsPathSegmentParser.parseType(type);
73             if (segmentType == null) {
74                 throw new BGPParsingException("AS Path segment type unknown : " + type);
75             }
76             final int count = buffer.readUnsignedByte();
77
78             if (segmentType == SegmentType.AS_SEQUENCE) {
79                 final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
80                 ases.add(new SegmentsBuilder().setCSegment(
81                     new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(numbers).build()).build()).build());
82                 isSequence = true;
83             } else {
84                 final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
85                 ases.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(list).build()).build()).build());
86             }
87         }
88         if (!isSequence) {
89             throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
90         }
91
92         ases.trimToSize();
93         return new AsPathBuilder().setSegments(ases).build();
94     }
95
96     @Override
97     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
98         builder.setAsPath(parseAsPath(this.refCache, buffer));
99     }
100
101     @Override
102     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
103         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
104         final Attributes pathAttributes = (Attributes) attribute;
105         final AsPath asPath = pathAttributes.getAsPath();
106         if (asPath == null) {
107             return;
108         }
109         final ByteBuf segmentsBuffer = Unpooled.buffer();
110         if (asPath.getSegments() != null) {
111             for (final Segments segments : asPath.getSegments()) {
112                 if (segments.getCSegment() instanceof AListCase) {
113                     final AListCase listCase = (AListCase) segments.getCSegment();
114                     AsPathSegmentParser.serializeAsSequence(listCase, segmentsBuffer);
115                 } else if (segments.getCSegment() instanceof ASetCase) {
116                     final ASetCase set = (ASetCase) segments.getCSegment();
117                     AsPathSegmentParser.serializeAsSet(set, segmentsBuffer);
118                 } else {
119                     LOG.warn("Segment class is neither AListCase nor ASetCase.");
120                 }
121             }
122         }
123         AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, segmentsBuffer, byteAggregator);
124     }
125 }