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