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 static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SEQUENCE;
11 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SET;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableList.Builder;
15 import io.netty.buffer.ByteBuf;
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;
19 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
22 * Representation of one AS Path Segment. It is, in fact, a TLV, but the length field is representing the count of AS
23 * Numbers in the collection (in its value). If the segment is of type AS_SEQUENCE, the collection is a List, if AS_SET,
24 * the collection is a Set.
26 public final class AsPathSegmentParser {
28 public static final int AS_NUMBER_LENGTH = 4;
31 * Possible types of AS Path segments.
33 public enum SegmentType {
37 private AsPathSegmentParser() {
40 static int serializeType(final SegmentType type) {
51 static SegmentType parseType(final int type) {
62 static ImmutableList<AsNumber> parseAsSegment(final ReferenceCache refCache, final int count,
63 final ByteBuf buffer) {
65 return ImmutableList.of();
68 final Builder<AsNumber> coll = ImmutableList.builderWithExpectedSize(count);
69 for (int i = 0; i < count; i++) {
70 coll.add(refCache.getSharedReference(new AsNumber(ByteBufUtils.readUint32(buffer))));
75 static void serializeAsList(final List<AsNumber> asList, final SegmentType type, final ByteBuf byteAggregator) {
79 byteAggregator.writeByte(serializeType(type));
80 byteAggregator.writeByte(asList.size());
81 for (final AsNumber asNumber : asList) {
82 byteAggregator.writeInt(asNumber.getValue().intValue());