b8b6f55c29ed3d3eb71d8d94a4217c1e927b32ca
[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 io.netty.buffer.ByteBuf;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.opendaylight.protocol.util.ReferenceCache;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
18 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;
19 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;
20
21 /**
22  *
23  * Representation of one AS Path Segment. It is, in fact, a TLV, but the length field is representing the count of AS
24  * Numbers in the collection (in its value). If the segment is of type AS_SEQUENCE, the collection is a List, if AS_SET,
25  * the collection is a Set.
26  *
27  */
28 public final class AsPathSegmentParser {
29
30     public static final int TYPE_LENGTH = 1;
31
32     public static final int LENGTH_SIZE = 1;
33
34     public static final int AS_NUMBER_LENGTH = 4;
35
36     /**
37      * Possible types of AS Path segments.
38      */
39     public enum SegmentType {
40         AS_SEQUENCE, AS_SET
41     }
42
43     private AsPathSegmentParser() {
44
45     }
46
47     static SegmentType parseType(final int type) {
48         switch (type) {
49         case 1:
50             return SegmentType.AS_SET;
51         case 2:
52             return SegmentType.AS_SEQUENCE;
53         default:
54             return null;
55         }
56     }
57
58     static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
59         final List<AsSequence> coll = new ArrayList<>();
60         for (int i = 0; i < count; i++) {
61             coll.add(refCache.getSharedReference(new AsSequenceBuilder().setAs(
62                     refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()))).build()));
63         }
64         return coll;
65     }
66
67     static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
68         final List<AsNumber> coll = new ArrayList<>();
69         for (int i = 0; i < count; i++) {
70             coll.add(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt())));
71         }
72         return coll;
73     }
74 }