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
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
11 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SEQUENCE;
12 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SET;
13 import io.netty.buffer.ByteBuf;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.opendaylight.protocol.util.ReferenceCache;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AList;
24 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;
25 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.AsSequenceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASet;
29 * Representation of one AS Path Segment. It is, in fact, a TLV, but the length field is representing the count of AS
30 * Numbers in the collection (in its value). If the segment is of type AS_SEQUENCE, the collection is a List, if AS_SET,
31 * the collection is a Set.
33 public final class AsPathSegmentParser {
35 public static final int AS_NUMBER_LENGTH = 2;
38 * Possible types of AS Path segments.
40 public enum SegmentType {
44 private AsPathSegmentParser() {
48 static int serializeType(final SegmentType type) {
59 static SegmentType parseType(final int type) {
70 static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
71 final List<AsSequence> coll = new ArrayList<>();
72 for (int i = 0; i < count; i++) {
73 long asValue = buffer.readUnsignedShort();
75 refCache.getSharedReference(new AsSequenceBuilder().setAs(refCache.getSharedReference(new AsNumber(asValue))).build()));
80 static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
81 final List<AsNumber> coll = new ArrayList<>();
82 for (int i = 0; i < count; i++) {
83 long asValue = buffer.readUnsignedShort();
84 coll.add(refCache.getSharedReference(
85 new AsNumber(asValue)));
90 static void serializeAsSet(final ASetCase aSetCase, final ByteBuf byteAggregator) {
91 ASet aset = aSetCase.getASet();
92 if (aset == null || aset.getAsSet() == null) {
95 byteAggregator.writeByte(serializeType(AS_SET));
96 byteAggregator.writeByte(aset.getAsSet().size());
97 for (AsNumber asNumber : aset.getAsSet()) {
98 byteAggregator.writeShort(new ShortAsNumber(asNumber).getValue().shortValue());
102 static void serializeAsSequence(final AListCase aListCase, final ByteBuf byteAggregator) {
103 final AList alist = aListCase.getAList();
104 if (alist == null || alist.getAsSequence() == null) {
107 byteAggregator.writeByte(serializeType(AS_SEQUENCE));
108 byteAggregator.writeByte(alist.getAsSequence().size());
109 for (AsSequence value : alist.getAsSequence()) {
110 byteAggregator.writeShort(new ShortAsNumber(value.getAs()).getValue().shortValue());