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 java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.protocol.util.ByteArray;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.c.a.list.AsSequence;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.c.a.list.AsSequenceBuilder;
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.
26 public class AsPathSegmentParser {
28 public static final int TYPE_LENGTH = 1; // bytes
30 public static final int LENGTH_SIZE = 1; // bytes
32 public static final int AS_NUMBER_LENGTH = 4; // bytes
35 * Possible types of AS Path segments.
37 public enum SegmentType {
41 private AsPathSegmentParser() {
45 static SegmentType parseType(final int type) {
48 return SegmentType.AS_SET;
50 return SegmentType.AS_SEQUENCE;
56 static List<AsSequence> parseAsSequence(final int count, final byte[] bytes) {
57 final List<AsSequence> coll = new ArrayList<>();
59 for (int i = 0; i < count; i++) {
60 coll.add(new AsSequenceBuilder().setAs(
61 new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH)))).build());
62 byteOffset += AS_NUMBER_LENGTH;
67 static List<AsNumber> parseAsSet(final int count, final byte[] bytes) {
68 final List<AsNumber> coll = new ArrayList<>();
70 for (int i = 0; i < count; i++) {
71 coll.add(new AsNumber(ByteArray.bytesToLong(ByteArray.subByte(bytes, byteOffset, AS_NUMBER_LENGTH))));
72 byteOffset += AS_NUMBER_LENGTH;