Fix most bgp-parser-impl checkstyle
[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 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SEQUENCE;
11 import static org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType.AS_SET;
12
13 import io.netty.buffer.ByteBuf;
14 import java.util.ArrayList;
15 import java.util.Collections;
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.rev130715.AsNumber;
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 public final class AsPathSegmentParser {
26
27     public static final int AS_NUMBER_LENGTH = 4;
28
29     /**
30      * Possible types of AS Path segments.
31      */
32     public enum SegmentType {
33         AS_SEQUENCE, AS_SET
34     }
35
36     private AsPathSegmentParser() {
37     }
38
39     static int serializeType(final SegmentType type) {
40         switch (type) {
41             case AS_SET:
42                 return 1;
43             case AS_SEQUENCE:
44                 return 2;
45             default:
46                 return 0;
47         }
48     }
49
50     static SegmentType parseType(final int type) {
51         switch (type) {
52             case 1:
53                 return AS_SET;
54             case 2:
55                 return AS_SEQUENCE;
56             default:
57                 return null;
58         }
59     }
60
61     static List<AsNumber> parseAsSegment(final ReferenceCache refCache, final int count, final ByteBuf buffer) {
62         final List<AsNumber> coll = new ArrayList<>(count);
63         for (int i = 0; i < count; i++) {
64             coll.add(refCache.getSharedReference(new AsNumber(buffer.readUnsignedInt())));
65         }
66         return coll.isEmpty() ? Collections.emptyList() : coll;
67     }
68
69     static void serializeAsList(final List<AsNumber> asList, final SegmentType type, final ByteBuf byteAggregator) {
70         if (asList == null) {
71             return;
72         }
73         byteAggregator.writeByte(serializeType(type));
74         byteAggregator.writeByte(asList.size());
75         for (final AsNumber asNumber : asList) {
76             byteAggregator.writeInt(asNumber.getValue().intValue());
77         }
78     }
79 }