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;
14 import java.util.ArrayList;
15 import java.util.Collections;
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.rev130715.AsNumber;
21 * Representation of one AS Path Segment. It is, in fact, a TLV, but the length field is representing the count of AS
22 * Numbers in the collection (in its value). If the segment is of type AS_SEQUENCE, the collection is a List, if AS_SET,
23 * the collection is a Set.
25 public final class AsPathSegmentParser {
27 public static final int AS_NUMBER_LENGTH = 4;
30 * Possible types of AS Path segments.
32 public enum SegmentType {
36 private AsPathSegmentParser() {
37 throw new UnsupportedOperationException();
40 static int serializeType(final SegmentType type) {
51 static SegmentType parseType(final int type) {
62 static List<AsNumber> parseAsSegment(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
63 final List<AsNumber> coll = new ArrayList<>(count);
64 for (int i = 0; i < count; i++) {
65 coll.add(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt())));
67 return (coll.isEmpty()) ? Collections.emptyList() : coll;
70 static void serializeAsList(final List<AsNumber> asList, final SegmentType type, final ByteBuf byteAggregator) {
74 byteAggregator.writeByte(serializeType(type));
75 byteAggregator.writeByte(asList.size());
76 for (final AsNumber asNumber : asList) {
77 byteAggregator.writeInt( asNumber.getValue().intValue());