Revert "Bug 1205 - AS path segment value encoded by 2 bytes"
[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 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
14 import io.netty.buffer.ByteBuf;
15 import java.util.ArrayList;
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.rev100924.AsNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.ASetCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AList;
22 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;
23 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;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.set._case.ASet;
25
26 /**
27  * Representation of one AS Path Segment. It is, in fact, a TLV, but the length field is representing the count of AS
28  * Numbers in the collection (in its value). If the segment is of type AS_SEQUENCE, the collection is a List, if AS_SET,
29  * the collection is a Set.
30  */
31 public final class AsPathSegmentParser {
32
33     public static final int AS_NUMBER_LENGTH = 4;
34
35     /**
36      * Possible types of AS Path segments.
37      */
38     public enum SegmentType {
39         AS_SEQUENCE, AS_SET
40     }
41
42     private AsPathSegmentParser() {
43
44     }
45
46     static int serializeType(final SegmentType type) {
47         switch (type) {
48         case AS_SET:
49             return 1;
50         case AS_SEQUENCE:
51             return 2;
52         default:
53             return 0;
54         }
55     }
56
57     static SegmentType parseType(final int type) {
58         switch (type) {
59         case 1:
60             return AS_SET;
61         case 2:
62             return AS_SEQUENCE;
63         default:
64             return null;
65         }
66     }
67
68     static List<AsSequence> parseAsSequence(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
69         final List<AsSequence> coll = new ArrayList<>();
70         for (int i = 0; i < count; i++) {
71             coll.add(
72                     refCache.getSharedReference(new AsSequenceBuilder().setAs(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt()))).build()));
73         }
74         return coll;
75     }
76
77     static List<AsNumber> parseAsSet(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
78         final List<AsNumber> coll = new ArrayList<>();
79         for (int i = 0; i < count; i++) {
80             coll.add(refCache.getSharedReference(
81                     new AsNumber(buffer.readUnsignedInt())));
82         }
83         return coll;
84     }
85
86     static void serializeAsSet(ASetCase aSetCase, ByteBuf byteAggregator) {
87         ASet aset = aSetCase.getASet();
88         if (aset == null || aset.getAsSet() == null) {
89             return;
90         }
91         byteAggregator.writeByte(serializeType(AS_SET));
92         byteAggregator.writeByte(aset.getAsSet().size());
93         for (AsNumber asNumber : aset.getAsSet()) {
94             byteAggregator.writeInt(asNumber.getValue().intValue());
95         }
96     }
97
98     static void serializeAsSequence(AListCase aListCase, ByteBuf byteAggregator) {
99         AList alist = aListCase.getAList();
100         if (alist == null || alist.getAsSequence() == null) {
101             return;
102         }
103         byteAggregator.writeByte(serializeType(AS_SEQUENCE));
104         byteAggregator.writeByte(alist.getAsSequence().size());
105         for (AsSequence value : alist.getAsSequence()) {
106             byteAggregator.writeInt(value.getAs().getValue().intValue());
107         }
108     }
109 }