2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.linkstate.attribute;
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.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.protocol.bgp.linkstate.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.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AdministrativeGroup;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6RouterIdentifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkProtectionType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.MplsProtocolMask;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerAdjSidBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerNodeSidBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSetSidBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIdBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
40 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;
41 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;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 public final class LinkAttributesParser {
52 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
54 private LinkAttributesParser() {
55 throw new UnsupportedOperationException();
58 private static final int UNRESERVED_BW_COUNT = 8;
60 private static final int BANDWIDTH_LENGTH = 4;
62 // MPLS protection mask bits
63 private static final int FLAGS_SIZE = 8;
65 private static final int LDP_BIT = 0;
66 private static final int RSVP_BIT = 1;
68 /* Link Attribute TLVs */
69 private static final int REMOTE_IPV4_ROUTER_ID = 1030;
70 private static final int REMOTE_IPV6_ROUTER_ID = 1031;
71 private static final int ADMIN_GROUP = 1088;
72 private static final int MAX_BANDWIDTH = 1089;
73 private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
74 private static final int UNRESERVED_BANDWIDTH = 1091;
75 private static final int TE_METRIC = 1092;
76 private static final int LINK_PROTECTION_TYPE = 1093;
77 private static final int MPLS_PROTOCOL = 1094;
78 private static final int METRIC = 1095;
79 private static final int SHARED_RISK_LINK_GROUP = 1096;
80 private static final int LINK_OPAQUE = 1097;
81 private static final int LINK_NAME = 1098;
82 private static final int SR_ADJ_ID = 1099;
83 private static final int SR_LAN_ADJ_ID = 1100;
84 private static final int PEER_NODE_SID_CODE = 1101;
85 private static final int PEER_ADJ_SID_CODE = 1102;
86 private static final int PEER_SET_SID_CODE = 1103;
89 * Parse Link Attributes.
91 * @param attributes key is the tlv type and value is the value of the tlv
92 * @param protocolId to differentiate parsing methods
93 * @return {@link LinkStateAttribute}
95 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
96 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
97 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
98 LOG.trace("Link attribute TLV {}", entry.getKey());
99 final int key = entry.getKey();
100 final ByteBuf value = entry.getValue();
102 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
103 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
104 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
106 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
107 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
108 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
110 case REMOTE_IPV4_ROUTER_ID:
111 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
112 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
114 case REMOTE_IPV6_ROUTER_ID:
115 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
116 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
119 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
120 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
123 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
124 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
126 case MAX_RESERVABLE_BANDWIDTH:
127 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
128 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
130 case UNRESERVED_BANDWIDTH:
131 parseUnreservedBandwidth(value, builder);
134 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
135 LOG.debug("Parsed Metric {}", builder.getTeMetric());
137 case LINK_PROTECTION_TYPE:
138 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
139 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
142 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
143 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
144 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
147 // length can 3, 2 or 1
148 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
149 LOG.debug("Parsed Metric {}", builder.getMetric());
151 case SHARED_RISK_LINK_GROUP:
152 parseSrlg(value, builder);
155 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
158 builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
159 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
162 builder.setSrAdjId(new SrAdjIdBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build());
163 LOG.debug("Parsed Adjacency Segment Identifier :{}", builder.getSrAdjId());
166 builder.setSrLanAdjId(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
167 LOG.debug("Parsed Adjacency Segment Identifier :{}", builder.getSrLanAdjId());
169 case PEER_NODE_SID_CODE:
170 builder.setPeerNodeSid(new PeerNodeSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
171 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid());
173 case PEER_ADJ_SID_CODE:
174 builder.setPeerAdjSid(new PeerAdjSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
175 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid());
177 case PEER_SET_SID_CODE:
178 builder.setPeerSetSid(new PeerSetSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
179 LOG.debug("Parsed Peer Set Sid :{}", builder.getPeerSetSid());
182 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
185 LOG.trace("Finished parsing Link Attributes.");
186 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
189 private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
190 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);
191 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
192 final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
193 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
195 builder.setUnreservedBandwidth(unreservedBandwidth);
196 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
199 private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
200 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
201 while (value.isReadable()) {
202 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
204 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
205 LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
208 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
209 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
210 LOG.trace("Started serializing Link Attributes");
211 if (linkAttributes.getLocalIpv4RouterId() != null) {
212 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
214 if (linkAttributes.getLocalIpv6RouterId() != null) {
215 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
217 if (linkAttributes.getRemoteIpv4RouterId() != null) {
218 TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
220 if (linkAttributes.getRemoteIpv6RouterId() != null) {
221 TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
223 if (linkAttributes.getAdminGroup() != null) {
224 TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
226 if (linkAttributes.getMaxLinkBandwidth() != null) {
227 TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
229 if (linkAttributes.getMaxReservableBandwidth() != null) {
230 TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
232 serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
233 if (linkAttributes.getTeMetric() != null) {
234 TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
236 if (linkAttributes.getLinkProtection() != null) {
237 TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
239 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
240 if (linkAttributes.getMetric() != null) {
241 // size of metric can be 1,2 or 3 depending on the protocol
242 TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
244 serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
245 if (linkAttributes.getLinkName() != null) {
246 TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
248 if (linkAttributes.getSrAdjId() != null) {
249 TlvUtil.writeTLV(SR_ADJ_ID, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getSrAdjId()), byteAggregator);
251 if (linkAttributes.getSrLanAdjId() != null) {
252 TlvUtil.writeTLV(SR_LAN_ADJ_ID, SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(linkAttributes.getSrLanAdjId()), byteAggregator);
254 if (linkAttributes.getPeerNodeSid() != null) {
255 TlvUtil.writeTLV(PEER_NODE_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerNodeSid()), byteAggregator);
257 if (linkAttributes.getPeerAdjSid() != null) {
258 TlvUtil.writeTLV(PEER_ADJ_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerAdjSid()), byteAggregator);
260 if (linkAttributes.getPeerSetSid() != null) {
261 TlvUtil.writeTLV(PEER_SET_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSetSid()), byteAggregator);
263 LOG.trace("Finished serializing Link Attributes");
266 private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
267 // this sub-TLV contains eight 32-bit IEEE floating point numbers
268 if (ubList != null) {
269 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
270 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
271 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
273 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
277 private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
278 if (srlgList != null) {
279 final ByteBuf sharedRLGBuf = Unpooled.buffer();
280 for (final SrlgId srlgId : srlgList) {
281 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
283 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
287 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
288 if (mplsProtocolMask != null) {
289 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
290 final BitArray mask = new BitArray(FLAGS_SIZE);
291 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
292 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
293 mask.toByteBuf(mplsProtocolMaskBuf);
294 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);