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 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.PathAttributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPathBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.Segments;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.as.path.SegmentsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
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;
42 public final class AsPathAttributeParser implements AttributeParser, AttributeSerializer {
44 public static final int TYPE = 2;
46 private final ReferenceCache refCache;
47 private static final Logger LOG = LoggerFactory.getLogger(AsPathAttributeParser.class);
49 private static final AsPath EMPTY = new AsPathBuilder().setSegments(Collections.<Segments> emptyList()).build();
51 public AsPathAttributeParser(final ReferenceCache refCache) {
52 this.refCache = Preconditions.checkNotNull(refCache);
56 * Parses AS_PATH from bytes.
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
64 private static AsPath parseAsPath(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
65 if (!buffer.isReadable()) {
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);
76 final int count = buffer.readUnsignedByte();
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());
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());
89 throw new BGPDocumentedException("AS_SEQUENCE must be present in AS_PATH attribute.", BGPError.AS_PATH_MALFORMED);
93 return new AsPathBuilder().setSegments(ases).build();
97 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
98 builder.setAsPath(parseAsPath(this.refCache, buffer));
102 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
103 Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
104 final PathAttributes pathAttributes = (PathAttributes) attribute;
105 final AsPath asPath = pathAttributes.getAsPath();
106 if (asPath == null) {
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);
119 LOG.warn("Segment class is neither AListCase nor ASetCase.");
123 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, segmentsBuffer, byteAggregator);