Merge "Add linkstate information to RIB"
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / AsPathSegmentParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
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;
18
19 /**
20  * 
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.
24  * 
25  */
26 public final class AsPathSegmentParser {
27
28         public static final int TYPE_LENGTH = 1;
29
30         public static final int LENGTH_SIZE = 1;
31
32         public static final int AS_NUMBER_LENGTH = 4;
33
34         /**
35          * Possible types of AS Path segments.
36          */
37         public enum SegmentType {
38                 AS_SEQUENCE, AS_SET
39         }
40
41         private AsPathSegmentParser() {
42
43         }
44
45         static SegmentType parseType(final int type) {
46                 switch (type) {
47                 case 1:
48                         return SegmentType.AS_SET;
49                 case 2:
50                         return SegmentType.AS_SEQUENCE;
51                 default:
52                         return null;
53                 }
54         }
55
56         static List<AsSequence> parseAsSequence(final int count, final byte[] bytes) {
57                 final List<AsSequence> coll = new ArrayList<>();
58                 int byteOffset = 0;
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;
63                 }
64                 return coll;
65         }
66
67         static List<AsNumber> parseAsSet(final int count, final byte[] bytes) {
68                 final List<AsNumber> coll = new ArrayList<>();
69                 int byteOffset = 0;
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;
73                 }
74                 return coll;
75         }
76 }