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