Bump MRI upstreams
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / 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.impl.attribute;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Multimap;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.buffer.Unpooled;
15 import java.nio.charset.StandardCharsets;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import org.opendaylight.protocol.bgp.linkstate.impl.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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.AdministrativeGroup;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Delay;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Ipv4RouterIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Ipv6RouterIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkProtectionType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Loss;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.MplsProtocolMask;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.ProtocolId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.LinkMinMaxDelay;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.LinkMinMaxDelayBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.PeerAdjSid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.PeerAdjSidBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.PeerNodeSid;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.PeerNodeSidBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.PeerSetSids;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.PeerSetSidsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.SrAdjIds;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.SrLanAdjIds;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.UnreservedBandwidth;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.UnreservedBandwidthBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.UnreservedBandwidthKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.LinkStateAttribute;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
58 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
59 import org.opendaylight.yangtools.yang.common.Uint32;
60 import org.opendaylight.yangtools.yang.common.Uint8;
61 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 @VisibleForTesting
66 public final class LinkAttributesParser {
67     public static final int SR_LAN_ADJ_ID = 1100;
68
69     private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
70     private static final int UNRESERVED_BW_COUNT = 8;
71
72     private static final int BANDWIDTH_LENGTH = 4;
73
74     // MPLS protection mask bits
75     private static final int FLAGS_SIZE = 8;
76
77     private static final int LDP_BIT = 0;
78     private static final int RSVP_BIT = 1;
79
80     /* Link Attribute TLVs */
81     private static final int REMOTE_IPV4_ROUTER_ID = 1030;
82     private static final int REMOTE_IPV6_ROUTER_ID = 1031;
83     private static final int ADMIN_GROUP = 1088;
84     private static final int MAX_BANDWIDTH = 1089;
85     private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
86     private static final int UNRESERVED_BANDWIDTH = 1091;
87     private static final int TE_METRIC = 1092;
88     private static final int LINK_PROTECTION_TYPE = 1093;
89     private static final int MPLS_PROTOCOL = 1094;
90     private static final int METRIC = 1095;
91     private static final int SHARED_RISK_LINK_GROUP = 1096;
92     private static final int LINK_OPAQUE = 1097;
93     private static final int LINK_NAME = 1098;
94     private static final int SR_ADJ_ID = 1099;
95     private static final int PEER_NODE_SID_CODE = 1101;
96     private static final int PEER_ADJ_SID_CODE = 1102;
97     private static final int PEER_SET_SID_CODE = 1103;
98     // Performance Metrics
99     private static final int LINK_DELAY = 1114;
100     private static final int LINK_MIN_MAX_DELAY = 1115;
101     private static final int DELAY_VARIATION = 1116;
102     private static final int LINK_LOSS = 1117;
103     private static final int RESIDUAL_BANDWIDTH = 1118;
104     private static final int AVAILABLE_BANDWIDTH = 1119;
105     private static final int UTILIZED_BANDWIDTH = 1120;
106
107     private LinkAttributesParser() {
108
109     }
110
111     @FunctionalInterface
112     private interface SerializerInterface {
113         void check(Object cont);
114     }
115
116     /**
117      * Parse Link Attributes.
118      *
119      * @param attributes key is the tlv type and value is the value of the tlv
120      * @param protocolId to differentiate parsing methods
121      * @return {@link LinkStateAttribute}
122      */
123     static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes,
124             final ProtocolId protocolId) {
125         final LinkAttributesBuilder builder = new LinkAttributesBuilder();
126         final List<SrAdjIds> srAdjIds = new ArrayList<>();
127         final List<SrLanAdjIds> srLanAdjIds = new ArrayList<>();
128         final List<PeerSetSids> peerSetSids = new ArrayList<>();
129         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
130             LOG.trace("Link attribute TLV {}", entry.getKey());
131             final int key = entry.getKey();
132             final ByteBuf value = entry.getValue();
133             switch (key) {
134                 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
135                     builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
136                     LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
137                     break;
138                 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
139                     builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
140                     LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
141                     break;
142                 case REMOTE_IPV4_ROUTER_ID:
143                     builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
144                     LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
145                     break;
146                 case REMOTE_IPV6_ROUTER_ID:
147                     builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
148                     LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
149                     break;
150                 case ADMIN_GROUP:
151                     builder.setAdminGroup(new AdministrativeGroup(ByteBufUtils.readUint32(value)));
152                     LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
153                     break;
154                 case MAX_BANDWIDTH:
155                     builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
156                     LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
157                     break;
158                 case MAX_RESERVABLE_BANDWIDTH:
159                     builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
160                     LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
161                     break;
162                 case UNRESERVED_BANDWIDTH:
163                     parseUnreservedBandwidth(value, builder);
164                     break;
165                 case TE_METRIC:
166                     builder.setTeMetric(new TeMetric(ByteBufUtils.readUint32(value)));
167                     LOG.debug("Parsed Metric {}", builder.getTeMetric());
168                     break;
169                 case LINK_PROTECTION_TYPE:
170                     builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
171                     LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
172                     break;
173                 case MPLS_PROTOCOL:
174                     final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
175                     builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
176                     LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
177                     break;
178                 case METRIC:
179                     // length can 3, 2 or 1
180                     builder.setMetric(new Metric(Uint32.valueOf(ByteArray.bytesToLong(ByteArray.readAllBytes(value)))));
181                     LOG.debug("Parsed Metric {}", builder.getMetric());
182                     break;
183                 case SHARED_RISK_LINK_GROUP:
184                     parseSrlg(value, builder);
185                     break;
186                 case LINK_OPAQUE:
187                     LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
188                     break;
189                 case LINK_NAME:
190                     builder.setLinkName(new String(ByteArray.readAllBytes(value), StandardCharsets.US_ASCII));
191                     LOG.debug("Parsed Link Name : {}", builder.getLinkName());
192                     break;
193                 case SR_ADJ_ID:
194                     srAdjIds.add(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId));
195                     LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size() - 1));
196                     break;
197                 case SR_LAN_ADJ_ID:
198                     srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
199                     LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size() - 1));
200                     break;
201                 case PEER_NODE_SID_CODE:
202                     builder.setPeerNodeSid(new PeerNodeSidBuilder(
203                         SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
204                     LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid());
205                     break;
206                 case PEER_ADJ_SID_CODE:
207                     builder.setPeerAdjSid(new PeerAdjSidBuilder(
208                         SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
209                     LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid());
210                     break;
211                 case PEER_SET_SID_CODE:
212                     peerSetSids.add(new PeerSetSidsBuilder(
213                         SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
214                     LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size() - 1));
215                     break;
216                     // Performance Metrics
217                 case LINK_DELAY:
218                     builder.setLinkDelay(new Delay(ByteBufUtils.readUint32(value)));
219                     LOG.debug("Parsed Link Delay {}", builder.getLinkDelay());
220                     break;
221                 case LINK_MIN_MAX_DELAY:
222                     builder.setLinkMinMaxDelay(new LinkMinMaxDelayBuilder()
223                         .setMinDelay(new Delay(ByteBufUtils.readUint32(value)))
224                         .setMaxDelay(new Delay(ByteBufUtils.readUint32(value)))
225                         .build());
226                     LOG.debug("Parsed Link Min/Max Delay {}", builder.getLinkMinMaxDelay());
227                     break;
228                 case DELAY_VARIATION:
229                     builder.setDelayVariation(new Delay(ByteBufUtils.readUint32(value)));
230                     LOG.debug("Parsed Delay Variation {}", builder.getDelayVariation());
231                     break;
232                 case LINK_LOSS:
233                     builder.setLinkLoss(new Loss(ByteBufUtils.readUint32(value)));
234                     LOG.debug("Parsed Link Loss {}", builder.getLinkLoss());
235                     break;
236                 case RESIDUAL_BANDWIDTH:
237                     builder.setResidualBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
238                     LOG.debug("Parsed Residual Bandwidth {}", builder.getResidualBandwidth());
239                     break;
240                 case AVAILABLE_BANDWIDTH:
241                     builder.setAvailableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
242                     LOG.debug("Parsed Available Bandwidth {}", builder.getAvailableBandwidth());
243                     break;
244                 case UTILIZED_BANDWIDTH:
245                     builder.setUtilizedBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
246                     LOG.debug("Parsed Utilized Bandwidth {}", builder.getUtilizedBandwidth());
247                     break;
248                 default:
249                     LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
250             }
251         }
252         if (!srAdjIds.isEmpty()) {
253             builder.setSrAdjIds(srAdjIds);
254         }
255         if (!srLanAdjIds.isEmpty()) {
256             builder.setSrLanAdjIds(srLanAdjIds);
257         }
258         if (!peerSetSids.isEmpty()) {
259             builder.setPeerSetSids(peerSetSids);
260         }
261         LOG.trace("Finished parsing Link Attributes.");
262         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
263     }
264
265     private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
266         final var unreservedBandwidth =
267             BindingMap.<UnreservedBandwidthKey, UnreservedBandwidth>orderedBuilder(UNRESERVED_BW_COUNT);
268         for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
269             final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
270             unreservedBandwidth.add(new UnreservedBandwidthBuilder()
271                 .setBandwidth(new Bandwidth(ByteArray.readAllBytes(v)))
272                 .setPriority(Uint8.valueOf(i))
273                 .build());
274         }
275         builder.setUnreservedBandwidth(unreservedBandwidth.build());
276         LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
277     }
278
279     private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
280         final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
281         while (value.isReadable()) {
282             sharedRiskLinkGroups.add(new SrlgId(ByteBufUtils.readUint32(value)));
283         }
284         builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
285         LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
286     }
287
288     private static void ifPresentApply(final Object tlv, final SerializerInterface serializer) {
289         if (tlv != null) {
290             serializer.check(tlv);
291         }
292     }
293
294
295     static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf output) {
296         final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
297         LOG.trace("Started serializing Link Attributes");
298         ifPresentApply(linkAttributes.getLocalIpv4RouterId(), value -> TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID,
299             Ipv4Util.byteBufForAddress((Ipv4AddressNoZone) value), output));
300         ifPresentApply(linkAttributes.getLocalIpv6RouterId(), value -> TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID,
301             Ipv6Util.byteBufForAddress((Ipv6AddressNoZone) value), output));
302         ifPresentApply(linkAttributes.getRemoteIpv4RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID,
303             Ipv4Util.byteBufForAddress((Ipv4AddressNoZone) value), output));
304         ifPresentApply(linkAttributes.getRemoteIpv6RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID,
305             Ipv6Util.byteBufForAddress((Ipv6AddressNoZone) value), output));
306         ifPresentApply(linkAttributes.getAdminGroup(), value -> TlvUtil.writeTLV(ADMIN_GROUP,
307             Unpooled.copyInt(((AdministrativeGroup) value).getValue().intValue()), output));
308         ifPresentApply(linkAttributes.getMaxLinkBandwidth(), value -> TlvUtil.writeTLV(MAX_BANDWIDTH,
309             Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
310         ifPresentApply(linkAttributes.getMaxReservableBandwidth(), value -> TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH,
311             Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
312         serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), output);
313         ifPresentApply(linkAttributes.getTeMetric(), value -> TlvUtil.writeTLV(TE_METRIC,
314             Unpooled.copyLong(((TeMetric) value).getValue().toJava()), output));
315         ifPresentApply(linkAttributes.getLinkProtection(), value -> TlvUtil.writeTLV(LINK_PROTECTION_TYPE,
316             Unpooled.copyShort(((LinkProtectionType) value).getIntValue()), output));
317         serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), output);
318         ifPresentApply(linkAttributes.getMetric(), value -> TlvUtil.writeTLV(METRIC,
319             Unpooled.copyMedium(((Metric) value).getValue().intValue()), output));
320         serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), output);
321         ifPresentApply(linkAttributes.getLinkName(), value -> TlvUtil.writeTLV(LINK_NAME,
322             Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode((String) value)), output));
323         ifPresentApply(linkAttributes.getSrAdjIds(),
324             value -> SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers((List<SrAdjIds>) value, SR_ADJ_ID,
325                 output));
326         ifPresentApply(linkAttributes.getSrLanAdjIds(),
327             value -> SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers((List<SrLanAdjIds>) value, output));
328         ifPresentApply(linkAttributes.getPeerNodeSid(), value -> TlvUtil.writeTLV(PEER_NODE_SID_CODE,
329             SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerNodeSid) value), output));
330         ifPresentApply(linkAttributes.getPeerAdjSid(), value -> TlvUtil.writeTLV(PEER_ADJ_SID_CODE,
331             SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerAdjSid) value), output));
332         ifPresentApply(linkAttributes.getPeerSetSids(),
333             value -> SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers((List<PeerSetSids>) value,
334                 PEER_SET_SID_CODE, output));
335         // Performance Metrics
336         ifPresentApply(linkAttributes.getLinkDelay(), value -> TlvUtil.writeTLV(LINK_DELAY,
337             Unpooled.copyInt(((Delay) value).getValue().intValue()), output));
338         serializeLinkMinMaxDelay(linkAttributes.getLinkMinMaxDelay(), output);
339         ifPresentApply(linkAttributes.getDelayVariation(), value -> TlvUtil.writeTLV(DELAY_VARIATION,
340             Unpooled.copyInt(((Delay) value).getValue().intValue()), output));
341         ifPresentApply(linkAttributes.getLinkLoss(), value -> TlvUtil.writeTLV(LINK_LOSS,
342             Unpooled.copyInt(((Loss) value).getValue().intValue()), output));
343         ifPresentApply(linkAttributes.getResidualBandwidth(), value -> TlvUtil.writeTLV(RESIDUAL_BANDWIDTH,
344             Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
345         ifPresentApply(linkAttributes.getAvailableBandwidth(), value -> TlvUtil.writeTLV(AVAILABLE_BANDWIDTH,
346             Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
347         ifPresentApply(linkAttributes.getUtilizedBandwidth(), value -> TlvUtil.writeTLV(UTILIZED_BANDWIDTH,
348             Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
349         LOG.trace("Finished serializing Link Attributes");
350     }
351
352     private static void serializeUnreservedBw(final Map<UnreservedBandwidthKey, UnreservedBandwidth> ubList,
353             final ByteBuf byteAggregator) {
354         // this sub-TLV contains eight 32-bit IEEE floating point numbers
355         if (ubList != null) {
356             final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
357             for (final UnreservedBandwidth unreservedBandwidth : ubList.values()) {
358                 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
359             }
360             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
361         }
362     }
363
364     private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
365         if (srlgList != null) {
366             final ByteBuf sharedRLGBuf = Unpooled.buffer();
367             for (final SrlgId srlgId : srlgList) {
368                 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
369             }
370             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
371         }
372     }
373
374     private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask,
375             final ByteBuf byteAggregator) {
376         if (mplsProtocolMask != null) {
377             final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
378             final BitArray mask = new BitArray(FLAGS_SIZE);
379             mask.set(LDP_BIT, mplsProtocolMask.getLdp());
380             mask.set(RSVP_BIT, mplsProtocolMask.getRsvpte());
381             mask.toByteBuf(mplsProtocolMaskBuf);
382             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
383         }
384     }
385
386     private static void serializeLinkMinMaxDelay(final LinkMinMaxDelay linkMinMaxDelay, final ByteBuf byteAggregator) {
387         if (linkMinMaxDelay != null) {
388             final ByteBuf linkMinMaxDelayBuf = Unpooled.buffer();
389             linkMinMaxDelayBuf.writeInt(linkMinMaxDelay.getMinDelay().getValue().intValue());
390             linkMinMaxDelayBuf.writeInt(linkMinMaxDelay.getMaxDelay().getValue().intValue());
391             TlvUtil.writeTLV(LINK_MIN_MAX_DELAY, linkMinMaxDelayBuf, byteAggregator);
392         }
393     }
394 }