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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
23 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
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;
43 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
45 public static final int TYPE = 2;
47 private final ReferenceCache refCache;
48 private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
50 private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build();
52 public AsPathAttributeParser(final ReferenceCache refCache) {
53 this.refCache = Preconditions.checkNotNull(refCache);
57 * Parses AS_PATH from bytes.
59 * @param buffer bytes to be parsed @return new ASPath object
60 * @throws BGPDocumentedException if there is no AS_SEQUENCE present (mandatory)
61 * @throws BGPParsingException
63 private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
64 if (!buffer.isReadable()) {
67 final ArrayList<Segments> ases = new ArrayList<>();
68 boolean isSequence = false;
69 while (buffer.isReadable()) {
70 final int type = UnsignedBytes.toInt(buffer.readByte());
71 final SegmentType segmentType = AsPathSegmentParser.parseType(type);
72 if (segmentType == null) {
73 throw new BGPParsingException("AS Path segment type unknown : " + type);
75 final int count = UnsignedBytes.toInt(buffer.readByte());
77 if (segmentType == SegmentType.AS_SEQUENCE) {
78 final List<AsSequence> numbers = AsPathSegmentParser.parseAsSequence(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
79 ases.add(new SegmentsBuilder().setCSegment(
80 new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(numbers).build()).build()).build());
83 final List<AsNumber> list = AsPathSegmentParser.parseAsSet(refCache, count, buffer.readSlice(count * AsPathSegmentParser.AS_NUMBER_LENGTH));
84 ases.add(new SegmentsBuilder().setCSegment(new ASetCaseBuilder().setASet(new ASetBuilder().setAsSet(list).build()).build()).build());
88 throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
92 return new AsPathBuilder().setSegments(ases).build();
96 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
97 builder.setAsPath(parseAsPath(this.refCache, buffer));
101 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
102 Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
103 final PathAttributes pathAttributes = (PathAttributes) attribute;
104 final AsPath asPath = pathAttributes.getAsPath();
105 if (asPath == null) {
108 final ByteBuf segmentsBuffer = Unpooled.buffer();
109 if (asPath.getSegments() != null) {
110 for (final Segments segments : asPath.getSegments()) {
111 if (segments.getCSegment() instanceof AListCase) {
112 final AListCase listCase = (AListCase) segments.getCSegment();
113 AsPathSegmentParser.serializeAsSequence(listCase, segmentsBuffer);
114 } else if (segments.getCSegment() instanceof ASetCase) {
115 final ASetCase set = (ASetCase) segments.getCSegment();
116 AsPathSegmentParser.serializeAsSet(set, segmentsBuffer);
118 LOG.warn("Segment class is neither AListCase nor ASetCase.");
122 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, segmentsBuffer, byteAggregator);