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.PeerSetSidBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSidBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIdBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
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.LinkAttributes;
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.LinkAttributesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 public final class LinkAttributesParser {
51 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
53 private LinkAttributesParser() {
54 throw new UnsupportedOperationException();
57 private static final int UNRESERVED_BW_COUNT = 8;
59 private static final int BANDWIDTH_LENGTH = 4;
61 // MPLS protection mask bits
62 private static final int FLAGS_SIZE = 8;
64 private static final int LDP_BIT = 0;
65 private static final int RSVP_BIT = 1;
67 /* Link Attribute TLVs */
68 private static final int REMOTE_IPV4_ROUTER_ID = 1030;
69 private static final int REMOTE_IPV6_ROUTER_ID = 1031;
70 private static final int ADMIN_GROUP = 1088;
71 private static final int MAX_BANDWIDTH = 1089;
72 private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
73 private static final int UNRESERVED_BANDWIDTH = 1091;
74 private static final int TE_METRIC = 1092;
75 private static final int LINK_PROTECTION_TYPE = 1093;
76 private static final int MPLS_PROTOCOL = 1094;
77 private static final int METRIC = 1095;
78 private static final int SHARED_RISK_LINK_GROUP = 1096;
79 private static final int LINK_OPAQUE = 1097;
80 private static final int LINK_NAME = 1098;
81 private static final int SR_ADJ_ID = 1099;
82 private static final int SR_LAN_ADJ_ID = 1100;
83 private static final int PEER_SID_CODE = 1036;
84 private static final int PEER_SET_SID_CODE = 1037;
87 * Parse Link Attributes.
89 * @param attributes key is the tlv type and value is the value of the tlv
90 * @param protocolId to differentiate parsing methods
91 * @return {@link LinkStateAttribute}
93 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
94 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
95 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
96 LOG.trace("Link attribute TLV {}", entry.getKey());
97 final int key = entry.getKey();
98 final ByteBuf value = entry.getValue();
100 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
101 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
102 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
104 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
105 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
106 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
108 case REMOTE_IPV4_ROUTER_ID:
109 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
110 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
112 case REMOTE_IPV6_ROUTER_ID:
113 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
114 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
117 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
118 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
121 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
122 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
124 case MAX_RESERVABLE_BANDWIDTH:
125 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
126 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
128 case UNRESERVED_BANDWIDTH:
129 parseUnreservedBandwidth(value, builder);
132 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
133 LOG.debug("Parsed Metric {}", builder.getTeMetric());
135 case LINK_PROTECTION_TYPE:
136 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
137 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
140 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
141 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
142 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
145 // length can 3, 2 or 1
146 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
147 LOG.debug("Parsed Metric {}", builder.getMetric());
149 case SHARED_RISK_LINK_GROUP:
150 parseSrlg(value, builder);
153 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
156 builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
157 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
160 builder.setSrAdjId(new SrAdjIdBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build());
161 LOG.debug("Parsed Adjacency Segment Identifier :{}", builder.getSrAdjId());
164 builder.setSrLanAdjId(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
165 LOG.debug("Parsed Adjacency Segment Identifier :{}", builder.getSrLanAdjId());
168 builder.setPeerSid(new PeerSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build());
169 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerSid());
171 case PEER_SET_SID_CODE:
172 builder.setPeerSetSid(new PeerSetSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build());
173 LOG.debug("Parsed Peer Set Sid :{}", builder.getPeerSetSid());
176 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
179 LOG.trace("Finished parsing Link Attributes.");
180 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
183 private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
184 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);
185 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
186 final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
187 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
189 builder.setUnreservedBandwidth(unreservedBandwidth);
190 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
193 private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
194 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
195 while (value.isReadable()) {
196 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
198 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
199 LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
202 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
203 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
204 LOG.trace("Started serializing Link Attributes");
205 if (linkAttributes.getLocalIpv4RouterId() != null) {
206 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
208 if (linkAttributes.getLocalIpv6RouterId() != null) {
209 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
211 if (linkAttributes.getRemoteIpv4RouterId() != null) {
212 TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
214 if (linkAttributes.getRemoteIpv6RouterId() != null) {
215 TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
217 if (linkAttributes.getAdminGroup() != null) {
218 TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
220 if (linkAttributes.getMaxLinkBandwidth() != null) {
221 TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
223 if (linkAttributes.getMaxReservableBandwidth() != null) {
224 TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
226 serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
227 if (linkAttributes.getTeMetric() != null) {
228 TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
230 if (linkAttributes.getLinkProtection() != null) {
231 TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
233 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
234 if (linkAttributes.getMetric() != null) {
235 // size of metric can be 1,2 or 3 depending on the protocol
236 TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
238 serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
239 if (linkAttributes.getLinkName() != null) {
240 TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
242 if (linkAttributes.getSrAdjId() != null) {
243 TlvUtil.writeTLV(SR_ADJ_ID, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getSrAdjId()), byteAggregator);
245 if (linkAttributes.getSrLanAdjId() != null) {
246 TlvUtil.writeTLV(SR_LAN_ADJ_ID, SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(linkAttributes.getSrLanAdjId()), byteAggregator);
248 if (linkAttributes.getPeerSid() != null) {
249 TlvUtil.writeTLV(PEER_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSid()), byteAggregator);
251 if (linkAttributes.getPeerSetSid() != null) {
252 TlvUtil.writeTLV(PEER_SET_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSetSid()), byteAggregator);
254 LOG.trace("Finished serializing Link Attributes");
257 private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
258 // this sub-TLV contains eight 32-bit IEEE floating point numbers
259 if (ubList != null) {
260 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
261 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
262 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
264 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
268 private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
269 if (srlgList != null) {
270 final ByteBuf sharedRLGBuf = Unpooled.buffer();
271 for (final SrlgId srlgId : srlgList) {
272 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
274 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
278 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
279 if (mplsProtocolMask != null) {
280 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
281 final BitArray mask = new BitArray(FLAGS_SIZE);
282 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
283 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
284 mask.toByteBuf(mplsProtocolMaskBuf);
285 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);