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;
14 import io.netty.buffer.ByteBuf;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.util.ReferenceCache;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AList;
23 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;
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.AsSequenceBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASet;
28 * Representation of one AS Path Segment. It is, in fact, a TLV, but the length field is representing the count of AS
29 * Numbers in the collection (in its value). If the segment is of type AS_SEQUENCE, the collection is a List, if AS_SET,
30 * the collection is a Set.
32 public final class AsPathSegmentParser {
34 public static final int AS_NUMBER_LENGTH = 2;
37 * Possible types of AS Path segments.
39 public enum SegmentType {
43 private AsPathSegmentParser() {
47 static int serializeType(final SegmentType type) {
58 static SegmentType parseType(final int type) {
69 static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
70 final List<AsSequence> coll = new ArrayList<>();
71 for (int i = 0; i < count; i++) {
72 long asValue = buffer.readUnsignedShort();
74 refCache.getSharedReference(new AsSequenceBuilder().setAs(refCache.getSharedReference(new AsNumber(asValue))).build()));
79 static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
80 final List<AsNumber> coll = new ArrayList<>();
81 for (int i = 0; i < count; i++) {
82 long asValue = buffer.readUnsignedShort();
83 coll.add(refCache.getSharedReference(
84 new AsNumber(asValue)));
89 static void serializeAsSet(ASetCase aSetCase, ByteBuf byteAggregator) {
90 ASet aset = aSetCase.getASet();
91 if (aset == null || aset.getAsSet() == null) {
94 byteAggregator.writeByte(serializeType(AS_SET));
95 byteAggregator.writeByte(aset.getAsSet().size());
96 for (AsNumber asNumber : aset.getAsSet()) {
97 ShortAsNumber shortAsNumber = new ShortAsNumber(asNumber);
98 byteAggregator.writeShort(shortAsNumber.getValue().shortValue());
102 static void serializeAsSequence(AListCase aListCase, ByteBuf byteAggregator) {
103 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 ShortAsNumber shortAsNumber = new ShortAsNumber(value.getAs());
111 byteAggregator.writeShort(shortAsNumber.getValue().shortValue());