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.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 = 4;
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++) {
73 refCache.getSharedReference(new AsSequenceBuilder().setAs(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()))).build()));
78 static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
79 final List<AsNumber> coll = new ArrayList<>();
80 for (int i = 0; i < count; i++) {
81 coll.add(refCache.getSharedReference(
82 new AsNumber(buffer.readUnsignedInt())));
87 static void serializeAsSet(ASetCase aSetCase, ByteBuf byteAggregator) {
88 ASet aset = aSetCase.getASet();
89 if (aset == null || aset.getAsSet() == null) {
92 byteAggregator.writeByte(serializeType(AS_SET));
93 byteAggregator.writeByte(aset.getAsSet().size());
94 for (AsNumber asNumber : aset.getAsSet()) {
95 byteAggregator.writeShort(asNumber.getValue().shortValue());
99 static void serializeAsSequence(AListCase aListCase, ByteBuf byteAggregator) {
100 AList alist = aListCase.getAList();
101 if (alist == null || alist.getAsSequence() == null) {
104 byteAggregator.writeByte(serializeType(AS_SEQUENCE));
105 byteAggregator.writeByte(alist.getAsSequence().size());
106 for (AsSequence value : alist.getAsSequence()) {
107 byteAggregator.writeShort(value.getAs().getValue().shortValue());