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.link.state.PeerSetSidBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSidBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIdBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
38 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;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 public final class LinkAttributesParser {
50 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
52 private LinkAttributesParser() {
53 throw new UnsupportedOperationException();
56 private static final int UNRESERVED_BW_COUNT = 8;
58 private static final int BANDWIDTH_LENGTH = 4;
60 // MPLS protection mask bits
61 private static final int FLAGS_SIZE = 8;
63 private static final int LDP_BIT = 0;
64 private static final int RSVP_BIT = 1;
66 /* Link Attribute TLVs */
67 private static final int REMOTE_IPV4_ROUTER_ID = 1030;
68 private static final int REMOTE_IPV6_ROUTER_ID = 1031;
69 private static final int ADMIN_GROUP = 1088;
70 private static final int MAX_BANDWIDTH = 1089;
71 private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
72 private static final int UNRESERVED_BANDWIDTH = 1091;
73 private static final int TE_METRIC = 1092;
74 private static final int LINK_PROTECTION_TYPE = 1093;
75 private static final int MPLS_PROTOCOL = 1094;
76 private static final int METRIC = 1095;
77 private static final int SHARED_RISK_LINK_GROUP = 1096;
78 private static final int LINK_OPAQUE = 1097;
79 private static final int LINK_NAME = 1098;
80 private static final int SR_ADJ_ID = 1099;
81 private static final int SR_LAN_ADJ_ID = 1100;
82 private static final int PEER_SID_CODE = 1036;
83 private static final int PEER_SET_SID_CODE = 1037;
86 * Parse Link Attributes.
88 * @param attributes key is the tlv type and value is the value of the tlv
89 * @return {@link LinkStateAttribute}
91 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes) {
92 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
93 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
94 LOG.trace("Link attribute TLV {}", entry.getKey());
95 final int key = entry.getKey();
96 final ByteBuf value = entry.getValue();
98 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
99 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
100 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
102 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
103 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
104 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
106 case REMOTE_IPV4_ROUTER_ID:
107 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
108 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
110 case REMOTE_IPV6_ROUTER_ID:
111 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
112 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
115 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
116 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
119 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
120 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
122 case MAX_RESERVABLE_BANDWIDTH:
123 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
124 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
126 case UNRESERVED_BANDWIDTH:
127 parseUnreservedBandwidth(value, builder);
130 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
131 LOG.debug("Parsed Metric {}", builder.getTeMetric());
133 case LINK_PROTECTION_TYPE:
134 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
135 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
138 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
139 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
140 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
143 // length can 3, 2 or 1
144 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
145 LOG.debug("Parsed Metric {}", builder.getMetric());
147 case SHARED_RISK_LINK_GROUP:
148 parseSrlg(value, builder);
151 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
154 builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
155 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
158 builder.setSrAdjId(new SrAdjIdBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value)).build());
159 LOG.debug("Parsed Adjacency Segment Identifier :{}", builder.getSrAdjId());
162 builder.setSrLanAdjId(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value));
163 LOG.debug("Parsed Adjacency Segment Identifier :{}", builder.getSrLanAdjId());
166 builder.setPeerSid(new PeerSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value)).build());
167 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerSid());
169 case PEER_SET_SID_CODE:
170 builder.setPeerSetSid(new PeerSetSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value)).build());
171 LOG.debug("Parsed Peer Set Sid :{}", builder.getPeerSetSid());
174 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
177 LOG.trace("Finished parsing Link Attributes.");
178 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
181 private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
182 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);
183 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
184 final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
185 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
187 builder.setUnreservedBandwidth(unreservedBandwidth);
188 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
191 private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
192 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
193 while (value.isReadable()) {
194 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
196 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
197 LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
200 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
201 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
202 LOG.trace("Started serializing Link Attributes");
203 if (linkAttributes.getLocalIpv4RouterId() != null) {
204 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
206 if (linkAttributes.getLocalIpv6RouterId() != null) {
207 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
209 if (linkAttributes.getRemoteIpv4RouterId() != null) {
210 TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
212 if (linkAttributes.getRemoteIpv6RouterId() != null) {
213 TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
215 if (linkAttributes.getAdminGroup() != null) {
216 TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
218 if (linkAttributes.getMaxLinkBandwidth() != null) {
219 TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
221 if (linkAttributes.getMaxReservableBandwidth() != null) {
222 TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
224 serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
225 if (linkAttributes.getTeMetric() != null) {
226 TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
228 if (linkAttributes.getLinkProtection() != null) {
229 TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
231 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
232 if (linkAttributes.getMetric() != null) {
233 // size of metric can be 1,2 or 3 depending on the protocol
234 TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
236 serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
237 if (linkAttributes.getLinkName() != null) {
238 TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
240 if (linkAttributes.getSrAdjId() != null) {
241 TlvUtil.writeTLV(SR_ADJ_ID, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getSrAdjId()), byteAggregator);
243 if (linkAttributes.getSrLanAdjId() != null) {
244 TlvUtil.writeTLV(SR_LAN_ADJ_ID, SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(linkAttributes.getSrLanAdjId()), byteAggregator);
246 if (linkAttributes.getPeerSid() != null) {
247 TlvUtil.writeTLV(PEER_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSid()), byteAggregator);
249 if (linkAttributes.getPeerSetSid() != null) {
250 TlvUtil.writeTLV(PEER_SET_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSetSid()), byteAggregator);
252 LOG.trace("Finished serializing Link Attributes");
255 private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
256 // this sub-TLV contains eight 32-bit IEEE floating point numbers
257 if (ubList != null) {
258 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
259 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
260 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
262 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
266 private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
267 if (srlgList != null) {
268 final ByteBuf sharedRLGBuf = Unpooled.buffer();
269 for (final SrlgId srlgId : srlgList) {
270 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
272 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
276 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
277 if (mplsProtocolMask != null) {
278 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
279 final BitArray mask = new BitArray(FLAGS_SIZE);
280 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
281 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
282 mask.toByteBuf(mplsProtocolMaskBuf);
283 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);