BUG-2794 : refactored code to use BitArray
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / attribute / LinkAttributesParser.java
1 /*
2  * Copyright (c) 2014 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.linkstate.attribute;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Charsets;
12 import com.google.common.collect.Multimap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.Map.Entry;
20 import org.opendaylight.protocol.bgp.linkstate.attribute.sr.SrLinkAttributesParser;
21 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
22 import org.opendaylight.protocol.util.BitArray;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.Ipv4Util;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AdministrativeGroup;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6RouterIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkProtectionType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.MplsProtocolMask;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrLanAdjId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @VisibleForTesting
48 public final class LinkAttributesParser {
49
50     private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
51
52     private LinkAttributesParser() {
53         throw new UnsupportedOperationException();
54     }
55
56     private static final int UNRESERVED_BW_COUNT = 8;
57
58     private static final int BANDWIDTH_LENGTH = 4;
59
60     // MPLS protection mask bits
61     private static final int FLAGS_SIZE = 8;
62
63     private static final int LDP_BIT = 0;
64     private static final int RSVP_BIT = 1;
65
66     /* Link Attribute TLVs */
67     private static final int REMOTE_IPV4_ROUTER_ID = 1030;
68     private static final int REMOTE_IPV6_ROUTER_ID = 1031;
69     private static final int ADMIN_GROUP = 1088;
70     private static final int MAX_BANDWIDTH = 1089;
71     private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
72     private static final int UNRESERVED_BANDWIDTH = 1091;
73     private static final int TE_METRIC = 1092;
74     private static final int LINK_PROTECTION_TYPE = 1093;
75     private static final int MPLS_PROTOCOL = 1094;
76     private static final int METRIC = 1095;
77     private static final int SHARED_RISK_LINK_GROUP = 1096;
78     private static final int LINK_OPAQUE = 1097;
79     private static final int LINK_NAME = 1098;
80     private static final int SR_ADJ_ID = 1099;
81     private static final int SR_LAN_ADJ_ID = 1100;
82
83     /**
84      * Parse Link Attributes.
85      *
86      * @param attributes key is the tlv type and value is the value of the tlv
87      * @return {@link LinkStateAttribute}
88      */
89     static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes) {
90         final LinkAttributesBuilder builder = new LinkAttributesBuilder();
91         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
92             LOG.trace("Link attribute TLV {}", entry.getKey());
93             final int key = entry.getKey();
94             final ByteBuf value = entry.getValue();
95             switch (key) {
96             case TlvUtil.LOCAL_IPV4_ROUTER_ID:
97                 final Ipv4RouterIdentifier lipv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
98                 builder.setLocalIpv4RouterId(lipv4);
99                 LOG.debug("Parsed IPv4 Router-ID of local node: {}", lipv4);
100                 break;
101             case TlvUtil.LOCAL_IPV6_ROUTER_ID:
102                 final Ipv6RouterIdentifier lipv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
103                 builder.setLocalIpv6RouterId(lipv6);
104                 LOG.debug("Parsed IPv6 Router-ID of local node: {}", lipv6);
105                 break;
106             case REMOTE_IPV4_ROUTER_ID:
107                 final Ipv4RouterIdentifier ripv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
108                 builder.setRemoteIpv4RouterId(ripv4);
109                 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", ripv4);
110                 break;
111             case REMOTE_IPV6_ROUTER_ID:
112                 final Ipv6RouterIdentifier ripv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
113                 builder.setRemoteIpv6RouterId(ripv6);
114                 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", ripv6);
115                 break;
116             case ADMIN_GROUP:
117                 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
118                 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
119                 break;
120             case MAX_BANDWIDTH:
121                 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
122                 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
123                 break;
124             case MAX_RESERVABLE_BANDWIDTH:
125                 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
126                 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
127                 break;
128             case UNRESERVED_BANDWIDTH:
129                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
130                 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
131                     final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
132                     unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
133                 }
134                 builder.setUnreservedBandwidth(unreservedBandwidth);
135                 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
136                 break;
137             case TE_METRIC:
138                 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
139                 LOG.debug("Parsed Metric {}", builder.getTeMetric());
140                 break;
141             case LINK_PROTECTION_TYPE:
142                 final int l = value.readShort();
143                 final LinkProtectionType lpt = LinkProtectionType.forValue(l);
144                 if (lpt == null) {
145                     LOG.warn("Link Protection Type not recognized: {}", l);
146                     break;
147                 }
148                 builder.setLinkProtection(lpt);
149                 LOG.debug("Parsed Link Protection Type {}", lpt);
150                 break;
151             case MPLS_PROTOCOL:
152                 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
153                 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
154                 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
155                 break;
156             case METRIC:
157                 // length can 3, 2 or 1
158                 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
159                 LOG.debug("Parsed Metric {}", builder.getMetric());
160                 break;
161             case SHARED_RISK_LINK_GROUP:
162                 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
163                 while (value.isReadable()) {
164                     sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
165                 }
166                 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
167                 LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
168                 break;
169             case LINK_OPAQUE:
170                 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
171                 break;
172             case LINK_NAME:
173                 final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
174                 builder.setLinkName(name);
175                 LOG.debug("Parsed Link Name : {}", name);
176                 break;
177             case SR_ADJ_ID:
178                 final SrAdjId srAdjId = SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value);
179                 builder.setSrAdjId(srAdjId);
180                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjId);
181             case SR_LAN_ADJ_ID:
182                 final SrLanAdjId srLanAdjId = SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value);
183                 builder.setSrLanAdjId(srLanAdjId);
184                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjId);
185             default:
186                 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
187             }
188         }
189         LOG.trace("Finished parsing Link Attributes.");
190         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
191     }
192
193     static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
194         final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
195         LOG.trace("Started serializing Link Attributes");
196         if (linkAttributes.getLocalIpv4RouterId() != null) {
197             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
198         }
199         if (linkAttributes.getLocalIpv6RouterId() != null) {
200             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
201         }
202         if (linkAttributes.getRemoteIpv4RouterId() != null) {
203             TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
204         }
205         if (linkAttributes.getRemoteIpv6RouterId() != null) {
206             TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
207         }
208         if (linkAttributes.getAdminGroup() != null) {
209             TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
210         }
211         if (linkAttributes.getMaxLinkBandwidth() != null) {
212             TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
213         }
214         if (linkAttributes.getMaxReservableBandwidth() != null) {
215             TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
216         }
217         // this sub-TLV contains eight 32-bit IEEE floating point numbers
218         final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
219         if (ubList != null) {
220             final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
221             for (final UnreservedBandwidth unreservedBandwidth : ubList) {
222                 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
223             }
224             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
225         }
226         if (linkAttributes.getTeMetric() != null) {
227             TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
228         }
229         if (linkAttributes.getLinkProtection() != null) {
230             TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
231         }
232         serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
233         if (linkAttributes.getMetric() != null) {
234             // size of metric can be 1,2 or 3 depending on the protocol
235             TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
236         }
237         final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
238         if (srlgList != null) {
239             final ByteBuf sharedRLGBuf = Unpooled.buffer();
240             for (final SrlgId srlgId : srlgList) {
241                 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
242             }
243             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
244         }
245         if (linkAttributes.getLinkName() != null) {
246             TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
247         }
248         if (linkAttributes.getSrAdjId() != null) {
249             TlvUtil.writeTLV(SR_ADJ_ID, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getSrAdjId()), byteAggregator);
250         }
251         if (linkAttributes.getSrLanAdjId() != null) {
252             TlvUtil.writeTLV(SR_LAN_ADJ_ID, SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(linkAttributes.getSrLanAdjId()), byteAggregator);
253         }
254         LOG.trace("Finished serializing Link Attributes");
255     }
256
257     private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
258         if (mplsProtocolMask != null) {
259             final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
260             final BitArray mask = new BitArray(FLAGS_SIZE);
261             mask.set(LDP_BIT, mplsProtocolMask.isLdp());
262             mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
263             mask.toByteBuf(mplsProtocolMaskBuf);
264             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
265         }
266     }
267 }