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.SrLanAdjIds;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
42 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;
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.LinkAttributesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 public final class LinkAttributesParser {
54 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
56 private LinkAttributesParser() {
57 throw new UnsupportedOperationException();
60 private static final int UNRESERVED_BW_COUNT = 8;
62 private static final int BANDWIDTH_LENGTH = 4;
64 // MPLS protection mask bits
65 private static final int FLAGS_SIZE = 8;
67 private static final int LDP_BIT = 0;
68 private static final int RSVP_BIT = 1;
70 /* Link Attribute TLVs */
71 private static final int REMOTE_IPV4_ROUTER_ID = 1030;
72 private static final int REMOTE_IPV6_ROUTER_ID = 1031;
73 private static final int ADMIN_GROUP = 1088;
74 private static final int MAX_BANDWIDTH = 1089;
75 private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
76 private static final int UNRESERVED_BANDWIDTH = 1091;
77 private static final int TE_METRIC = 1092;
78 private static final int LINK_PROTECTION_TYPE = 1093;
79 private static final int MPLS_PROTOCOL = 1094;
80 private static final int METRIC = 1095;
81 private static final int SHARED_RISK_LINK_GROUP = 1096;
82 private static final int LINK_OPAQUE = 1097;
83 private static final int LINK_NAME = 1098;
84 private static final int SR_ADJ_ID = 1099;
85 private static final int SR_LAN_ADJ_ID = 1100;
86 private static final int PEER_NODE_SID_CODE = 1101;
87 private static final int PEER_ADJ_SID_CODE = 1102;
88 private static final int PEER_SET_SID_CODE = 1103;
91 * Parse Link Attributes.
93 * @param attributes key is the tlv type and value is the value of the tlv
94 * @param protocolId to differentiate parsing methods
95 * @return {@link LinkStateAttribute}
97 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
98 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
99 final List<SrAdjIds> srAdjIds = new ArrayList<SrAdjIds>();
100 final List<SrLanAdjIds> srLanAdjIds = new ArrayList<SrLanAdjIds>();
101 final List<PeerSetSids> peerSetSids = new ArrayList<PeerSetSids>();
102 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
103 LOG.trace("Link attribute TLV {}", entry.getKey());
104 final int key = entry.getKey();
105 final ByteBuf value = entry.getValue();
107 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
108 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
109 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
111 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
112 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
113 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
115 case REMOTE_IPV4_ROUTER_ID:
116 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
117 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
119 case REMOTE_IPV6_ROUTER_ID:
120 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
121 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
124 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
125 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
128 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
129 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
131 case MAX_RESERVABLE_BANDWIDTH:
132 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
133 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
135 case UNRESERVED_BANDWIDTH:
136 parseUnreservedBandwidth(value, builder);
139 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
140 LOG.debug("Parsed Metric {}", builder.getTeMetric());
142 case LINK_PROTECTION_TYPE:
143 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
144 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
147 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
148 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
149 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
152 // length can 3, 2 or 1
153 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
154 LOG.debug("Parsed Metric {}", builder.getMetric());
156 case SHARED_RISK_LINK_GROUP:
157 parseSrlg(value, builder);
160 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
163 builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
164 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
167 srAdjIds.add(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId));
168 LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size()-1));
171 srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
172 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size()-1));
174 case PEER_NODE_SID_CODE:
175 builder.setPeerNodeSid(new PeerNodeSidBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
176 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid());
178 case PEER_ADJ_SID_CODE:
179 builder.setPeerAdjSid(new PeerAdjSidBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
180 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid());
182 case PEER_SET_SID_CODE:
183 peerSetSids.add(new PeerSetSidsBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
184 LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size()-1));
187 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
190 if (!srAdjIds.isEmpty()) {
191 builder.setSrAdjIds(srAdjIds);
193 if (!srLanAdjIds.isEmpty()) {
194 builder.setSrLanAdjIds(srLanAdjIds);
196 if (!peerSetSids.isEmpty()) {
197 builder.setPeerSetSids(peerSetSids);
199 LOG.trace("Finished parsing Link Attributes.");
200 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
203 private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
204 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);
205 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
206 final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
207 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
209 builder.setUnreservedBandwidth(unreservedBandwidth);
210 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
213 private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
214 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
215 while (value.isReadable()) {
216 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
218 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
219 LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
222 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
223 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
224 LOG.trace("Started serializing Link Attributes");
225 if (linkAttributes.getLocalIpv4RouterId() != null) {
226 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
228 if (linkAttributes.getLocalIpv6RouterId() != null) {
229 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
231 if (linkAttributes.getRemoteIpv4RouterId() != null) {
232 TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
234 if (linkAttributes.getRemoteIpv6RouterId() != null) {
235 TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
237 if (linkAttributes.getAdminGroup() != null) {
238 TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
240 if (linkAttributes.getMaxLinkBandwidth() != null) {
241 TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
243 if (linkAttributes.getMaxReservableBandwidth() != null) {
244 TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
246 serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
247 if (linkAttributes.getTeMetric() != null) {
248 TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
250 if (linkAttributes.getLinkProtection() != null) {
251 TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
253 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
254 if (linkAttributes.getMetric() != null) {
255 // size of metric can be 1,2 or 3 depending on the protocol
256 TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
258 serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
259 if (linkAttributes.getLinkName() != null) {
260 TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
262 if (linkAttributes.getSrAdjIds() != null) {
263 SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getSrAdjIds(), SR_ADJ_ID, byteAggregator);
265 if (linkAttributes.getSrLanAdjIds() != null) {
266 SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers(linkAttributes.getSrLanAdjIds(), SR_LAN_ADJ_ID, byteAggregator);
268 if (linkAttributes.getPeerNodeSid() != null) {
269 TlvUtil.writeTLV(PEER_NODE_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerNodeSid()), byteAggregator);
271 if (linkAttributes.getPeerAdjSid() != null) {
272 TlvUtil.writeTLV(PEER_ADJ_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerAdjSid()), byteAggregator);
274 if (linkAttributes.getPeerSetSids() != null) {
275 SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getPeerSetSids(), PEER_SET_SID_CODE, byteAggregator);
277 LOG.trace("Finished serializing Link Attributes");
280 private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
281 // this sub-TLV contains eight 32-bit IEEE floating point numbers
282 if (ubList != null) {
283 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
284 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
285 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
287 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
291 private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
292 if (srlgList != null) {
293 final ByteBuf sharedRLGBuf = Unpooled.buffer();
294 for (final SrlgId srlgId : srlgList) {
295 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
297 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
301 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
302 if (mplsProtocolMask != null) {
303 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
304 final BitArray mask = new BitArray(FLAGS_SIZE);
305 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
306 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
307 mask.toByteBuf(mplsProtocolMaskBuf);
308 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);