Code clean up
[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.rev130715.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.yangtools.yang.binding.DataObject;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
36
37     public static final int TYPE = 2;
38
39     private final ReferenceCache refCache;
40     private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
41
42     private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.emptyList()).build();
43
44     public AsPathAttributeParser(final ReferenceCache refCache) {
45         this.refCache = Preconditions.checkNotNull(refCache);
46     }
47
48     /**
49      * Parses AS_PATH from bytes.
50      *
51      * @param refCache ReferenceCache shared reference of object
52      * @param buffer bytes to be parsed
53      * @return new ASPath object
54      * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
55      * @throws BGPParsingException
56      */
57     private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
58         if (!buffer.isReadable()) {
59             return EMPTY;
60         }
61         final ArrayList<Segments> ases = new ArrayList<>();
62         boolean isSequence = false;
63         while (buffer.isReadable()) {
64             final int type = buffer.readUnsignedByte();
65             final SegmentType segmentType = AsPathSegmentParser.parseType(type);
66             if (segmentType == null) {
67                 throw new BGPParsingException("AS Path segment type unknown : " + type);
68             }
69             final int count = buffer.readUnsignedByte();
70
71             final List<AsNumber> asList = AsPathSegmentParser.parseAsSegment(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
72             if (segmentType == SegmentType.AS_SEQUENCE) {
73                 ases.add(new SegmentsBuilder().setAsSequence(asList).build());
74                 isSequence = true;
75             } else {
76                 ases.add(new SegmentsBuilder().setAsSet(asList).build());
77             }
78         }
79         if (!isSequence) {
80             throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
81         }
82
83         ases.trimToSize();
84         return new AsPathBuilder().setSegments(ases).build();
85     }
86
87     @Override
88     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
89         builder.setAsPath(parseAsPath(this.refCache, buffer));
90     }
91
92     @Override
93     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
94         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
95         final Attributes pathAttributes = (Attributes) attribute;
96         final AsPath asPath = pathAttributes.getAsPath();
97         if (asPath == null) {
98             return;
99         }
100         final ByteBuf segmentsBuffer = Unpooled.buffer();
101         if (asPath.getSegments() != null) {
102             for (final Segments segments : asPath.getSegments()) {
103                 if (segments.getAsSequence() != null) {
104                     AsPathSegmentParser.serializeAsList(segments.getAsSequence(), SegmentType.AS_SEQUENCE, segmentsBuffer);
105                 } else if (segments.getAsSet() != null) {
106                     AsPathSegmentParser.serializeAsList(segments.getAsSet(), SegmentType.AS_SET, segmentsBuffer);
107                 } else {
108                     LOG.warn("Segment doesn't have AsSequence nor AsSet list.");
109                 }
110             }
111         }
112         AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, segmentsBuffer, byteAggregator);
113     }
114 }