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