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