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.PeerSetSids;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSetSidsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIdsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrLanAdjIds;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
43 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;
44 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;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 public final class LinkAttributesParser {
55 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
57 private LinkAttributesParser() {
58 throw new UnsupportedOperationException();
61 private static final int UNRESERVED_BW_COUNT = 8;
63 private static final int BANDWIDTH_LENGTH = 4;
65 // MPLS protection mask bits
66 private static final int FLAGS_SIZE = 8;
68 private static final int LDP_BIT = 0;
69 private static final int RSVP_BIT = 1;
71 /* Link Attribute TLVs */
72 private static final int REMOTE_IPV4_ROUTER_ID = 1030;
73 private static final int REMOTE_IPV6_ROUTER_ID = 1031;
74 private static final int ADMIN_GROUP = 1088;
75 private static final int MAX_BANDWIDTH = 1089;
76 private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
77 private static final int UNRESERVED_BANDWIDTH = 1091;
78 private static final int TE_METRIC = 1092;
79 private static final int LINK_PROTECTION_TYPE = 1093;
80 private static final int MPLS_PROTOCOL = 1094;
81 private static final int METRIC = 1095;
82 private static final int SHARED_RISK_LINK_GROUP = 1096;
83 private static final int LINK_OPAQUE = 1097;
84 private static final int LINK_NAME = 1098;
85 private static final int SR_ADJ_ID = 1099;
86 private static final int SR_LAN_ADJ_ID = 1100;
87 private static final int PEER_NODE_SID_CODE = 1101;
88 private static final int PEER_ADJ_SID_CODE = 1102;
89 private static final int PEER_SET_SID_CODE = 1103;
92 * Parse Link Attributes.
94 * @param attributes key is the tlv type and value is the value of the tlv
95 * @param protocolId to differentiate parsing methods
96 * @return {@link LinkStateAttribute}
98 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
99 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
100 final List<SrAdjIds> srAdjIds = new ArrayList<SrAdjIds>();
101 final List<SrLanAdjIds> srLanAdjIds = new ArrayList<SrLanAdjIds>();
102 final List<PeerSetSids> peerSetSids = new ArrayList<PeerSetSids>();
103 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
104 LOG.trace("Link attribute TLV {}", entry.getKey());
105 final int key = entry.getKey();
106 final ByteBuf value = entry.getValue();
108 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
109 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
110 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
112 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
113 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
114 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
116 case REMOTE_IPV4_ROUTER_ID:
117 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
118 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
120 case REMOTE_IPV6_ROUTER_ID:
121 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
122 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
125 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
126 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
129 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
130 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
132 case MAX_RESERVABLE_BANDWIDTH:
133 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
134 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
136 case UNRESERVED_BANDWIDTH:
137 parseUnreservedBandwidth(value, builder);
140 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
141 LOG.debug("Parsed Metric {}", builder.getTeMetric());
143 case LINK_PROTECTION_TYPE:
144 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
145 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
148 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
149 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
150 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
153 // length can 3, 2 or 1
154 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
155 LOG.debug("Parsed Metric {}", builder.getMetric());
157 case SHARED_RISK_LINK_GROUP:
158 parseSrlg(value, builder);
161 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
164 builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
165 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
168 srAdjIds.add(new SrAdjIdsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build());
169 LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size()-1));
172 srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
173 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size()-1));
175 case PEER_NODE_SID_CODE:
176 builder.setPeerNodeSid(new PeerNodeSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
177 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid());
179 case PEER_ADJ_SID_CODE:
180 builder.setPeerAdjSid(new PeerAdjSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
181 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid());
183 case PEER_SET_SID_CODE:
184 peerSetSids.add(new PeerSetSidsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
185 LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size()-1));
188 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
191 if (!srAdjIds.isEmpty()) {
192 builder.setSrAdjIds(srAdjIds);
194 if (!srLanAdjIds.isEmpty()) {
195 builder.setSrLanAdjIds(srLanAdjIds);
197 if (!peerSetSids.isEmpty()) {
198 builder.setPeerSetSids(peerSetSids);
200 LOG.trace("Finished parsing Link Attributes.");
201 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
204 private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
205 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);
206 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
207 final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
208 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
210 builder.setUnreservedBandwidth(unreservedBandwidth);
211 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
214 private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
215 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
216 while (value.isReadable()) {
217 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
219 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
220 LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
223 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
224 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
225 LOG.trace("Started serializing Link Attributes");
226 if (linkAttributes.getLocalIpv4RouterId() != null) {
227 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
229 if (linkAttributes.getLocalIpv6RouterId() != null) {
230 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
232 if (linkAttributes.getRemoteIpv4RouterId() != null) {
233 TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
235 if (linkAttributes.getRemoteIpv6RouterId() != null) {
236 TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
238 if (linkAttributes.getAdminGroup() != null) {
239 TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
241 if (linkAttributes.getMaxLinkBandwidth() != null) {
242 TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
244 if (linkAttributes.getMaxReservableBandwidth() != null) {
245 TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
247 serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
248 if (linkAttributes.getTeMetric() != null) {
249 TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
251 if (linkAttributes.getLinkProtection() != null) {
252 TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
254 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
255 if (linkAttributes.getMetric() != null) {
256 // size of metric can be 1,2 or 3 depending on the protocol
257 TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
259 serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
260 if (linkAttributes.getLinkName() != null) {
261 TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
263 if (linkAttributes.getSrAdjIds() != null) {
264 SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getSrAdjIds(), SR_ADJ_ID, byteAggregator);
266 if (linkAttributes.getSrLanAdjIds() != null) {
267 SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers(linkAttributes.getSrLanAdjIds(), SR_LAN_ADJ_ID, byteAggregator);
269 if (linkAttributes.getPeerNodeSid() != null) {
270 TlvUtil.writeTLV(PEER_NODE_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerNodeSid()), byteAggregator);
272 if (linkAttributes.getPeerAdjSid() != null) {
273 TlvUtil.writeTLV(PEER_ADJ_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerAdjSid()), byteAggregator);
275 if (linkAttributes.getPeerSetSids() != null) {
276 SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getPeerSetSids(), PEER_SET_SID_CODE, byteAggregator);
278 LOG.trace("Finished serializing Link Attributes");
281 private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
282 // this sub-TLV contains eight 32-bit IEEE floating point numbers
283 if (ubList != null) {
284 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
285 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
286 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
288 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
292 private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
293 if (srlgList != null) {
294 final ByteBuf sharedRLGBuf = Unpooled.buffer();
295 for (final SrlgId srlgId : srlgList) {
296 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
298 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
302 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
303 if (mplsProtocolMask != null) {
304 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
305 final BitArray mask = new BitArray(FLAGS_SIZE);
306 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
307 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
308 mask.toByteBuf(mplsProtocolMaskBuf);
309 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);