Bug 611 - BGP Update message serialization
[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 com.google.common.collect.Lists;
12 import com.google.common.primitives.UnsignedBytes;
13
14 import io.netty.buffer.ByteBuf;
15
16 import java.util.List;
17
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.util.ReferenceCache;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.SegmentsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AListBuilder;
37 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;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASetBuilder;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
45
46     public static final int TYPE = 2;
47
48     private final ReferenceCache refCache;
49     private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
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 buffer bytes to be parsed
59      * @return new ASPath object
60      * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
61      * @throws BGPParsingException
62      */
63     private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
64         final List<Segments> ases = Lists.newArrayList();
65         boolean isSequence = false;
66         while (buffer.isReadable()) {
67             final int type = UnsignedBytes.toInt(buffer.readByte());
68             final SegmentType segmentType = AsPathSegmentParser.parseType(type);
69             if (segmentType == null) {
70                 throw new BGPParsingException("AS Path segment type unknown : " + type);
71             }
72             final int count = UnsignedBytes.toInt(buffer.readByte());
73
74             if (segmentType == SegmentType.AS_SEQUENCE) {
75                 final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count, buffer.slice(buffer.readerIndex(),
76                         count * AsPathSegmentParser.AS_NUMBER_LENGTH));
77                 ases.add(new SegmentsBuilder().setCSegment(
78                         new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(numbers).build()).build()).build());
79                 isSequence = true;
80             } else {
81                 final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count, buffer.slice(buffer.readerIndex(), count
82                         * AsPathSegmentParser.AS_NUMBER_LENGTH));
83                 ases.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(list).build()).build()).build());
84
85             }
86             buffer.skipBytes(count * AsPathSegmentParser.AS_NUMBER_LENGTH);
87         }
88
89         if (!isSequence && buffer.readableBytes() != 0) {
90             throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
91         }
92         return new AsPathBuilder().setSegments(ases).build();
93     }
94
95     @Override
96     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
97         builder.setAsPath(parseAsPath(this.refCache, buffer));
98     }
99
100     @Override
101     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
102         PathAttributes pathAttributes = (PathAttributes) attribute;
103         if (pathAttributes.getAsPath() == null) {
104             return;
105         }
106         AsPath asPath = pathAttributes.getAsPath();
107         for (Segments segments : asPath.getSegments()) {
108             if (segments.getCSegment() instanceof AListCase) {
109                 AListCase listCase = (AListCase) segments.getCSegment();
110                 AsPathSegmentParser.serializeAsSequence(listCase, byteAggregator);
111             } else if (segments.getCSegment() instanceof ASetCase) {
112                 ASetCase set = (ASetCase) segments.getCSegment();
113                 AsPathSegmentParser.serializeAsSet(set, byteAggregator);
114             } else {
115                 LOG.warn("CSegment class is neither AListCase nor ASetCase.");
116             }
117         }
118     }
119 }