1bffae012f10b2eee415f599d4b8899431c89ef0
[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.linkstate.path.attribute.LinkStateAttribute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.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.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                 break;
182             case SR_LAN_ADJ_ID:
183                 final SrLanAdjId srLanAdjId = SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value);
184                 builder.setSrLanAdjId(srLanAdjId);
185                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjId);
186                 break;
187             default:
188                 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
189             }
190         }
191         LOG.trace("Finished parsing Link Attributes.");
192         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
193     }
194
195     static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
196         final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
197         LOG.trace("Started serializing Link Attributes");
198         if (linkAttributes.getLocalIpv4RouterId() != null) {
199             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
200         }
201         if (linkAttributes.getLocalIpv6RouterId() != null) {
202             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
203         }
204         if (linkAttributes.getRemoteIpv4RouterId() != null) {
205             TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
206         }
207         if (linkAttributes.getRemoteIpv6RouterId() != null) {
208             TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
209         }
210         if (linkAttributes.getAdminGroup() != null) {
211             TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
212         }
213         if (linkAttributes.getMaxLinkBandwidth() != null) {
214             TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
215         }
216         if (linkAttributes.getMaxReservableBandwidth() != null) {
217             TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
218         }
219         // this sub-TLV contains eight 32-bit IEEE floating point numbers
220         final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
221         if (ubList != null) {
222             final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
223             for (final UnreservedBandwidth unreservedBandwidth : ubList) {
224                 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
225             }
226             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
227         }
228         if (linkAttributes.getTeMetric() != null) {
229             TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
230         }
231         if (linkAttributes.getLinkProtection() != null) {
232             TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
233         }
234         serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
235         if (linkAttributes.getMetric() != null) {
236             // size of metric can be 1,2 or 3 depending on the protocol
237             TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
238         }
239         final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
240         if (srlgList != null) {
241             final ByteBuf sharedRLGBuf = Unpooled.buffer();
242             for (final SrlgId srlgId : srlgList) {
243                 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
244             }
245             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
246         }
247         if (linkAttributes.getLinkName() != null) {
248             TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
249         }
250         if (linkAttributes.getSrAdjId() != null) {
251             TlvUtil.writeTLV(SR_ADJ_ID, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getSrAdjId()), byteAggregator);
252         }
253         if (linkAttributes.getSrLanAdjId() != null) {
254             TlvUtil.writeTLV(SR_LAN_ADJ_ID, SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(linkAttributes.getSrLanAdjId()), byteAggregator);
255         }
256         LOG.trace("Finished serializing Link Attributes");
257     }
258
259     private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
260         if (mplsProtocolMask != null) {
261             final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
262             final BitArray mask = new BitArray(FLAGS_SIZE);
263             mask.set(LDP_BIT, mplsProtocolMask.isLdp());
264             mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
265             mask.toByteBuf(mplsProtocolMaskBuf);
266             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
267         }
268     }
269 }