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.impl.attribute;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Multimap;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufUtil;
14 import io.netty.buffer.Unpooled;
15 import java.nio.charset.StandardCharsets;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.protocol.bgp.linkstate.impl.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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.AdministrativeGroup;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv4RouterIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv6RouterIdentifier;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkProtectionType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.MplsProtocolMask;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.ProtocolId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.PeerAdjSid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.PeerAdjSidBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.PeerNodeSid;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.PeerNodeSidBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.PeerSetSids;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.PeerSetSidsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.SrAdjIds;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.SrLanAdjIds;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.UnreservedBandwidth;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.UnreservedBandwidthBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.LinkStateAttribute;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 public final class LinkAttributesParser {
58 public static final int SR_LAN_ADJ_ID = 1100;
59 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
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 PEER_NODE_SID_CODE = 1101;
86 private static final int PEER_ADJ_SID_CODE = 1102;
87 private static final int PEER_SET_SID_CODE = 1103;
89 private LinkAttributesParser() {
90 throw new UnsupportedOperationException();
94 private interface SerializerInterface {
95 void check(Object cont);
99 * Parse Link Attributes.
101 * @param attributes key is the tlv type and value is the value of the tlv
102 * @param protocolId to differentiate parsing methods
103 * @return {@link LinkStateAttribute}
105 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
106 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
107 final List<SrAdjIds> srAdjIds = new ArrayList<>();
108 final List<SrLanAdjIds> srLanAdjIds = new ArrayList<>();
109 final List<PeerSetSids> peerSetSids = new ArrayList<>();
110 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
111 LOG.trace("Link attribute TLV {}", entry.getKey());
112 final int key = entry.getKey();
113 final ByteBuf value = entry.getValue();
115 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
116 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
117 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
119 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
120 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
121 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
123 case REMOTE_IPV4_ROUTER_ID:
124 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
125 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
127 case REMOTE_IPV6_ROUTER_ID:
128 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
129 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
132 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
133 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
136 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
137 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
139 case MAX_RESERVABLE_BANDWIDTH:
140 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
141 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
143 case UNRESERVED_BANDWIDTH:
144 parseUnreservedBandwidth(value, builder);
147 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
148 LOG.debug("Parsed Metric {}", builder.getTeMetric());
150 case LINK_PROTECTION_TYPE:
151 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
152 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
155 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
156 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
157 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
160 // length can 3, 2 or 1
161 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
162 LOG.debug("Parsed Metric {}", builder.getMetric());
164 case SHARED_RISK_LINK_GROUP:
165 parseSrlg(value, builder);
168 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
171 builder.setLinkName(new String(ByteArray.readAllBytes(value), StandardCharsets.US_ASCII));
172 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
175 srAdjIds.add(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId));
176 LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size() - 1));
179 srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
180 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size() - 1));
182 case PEER_NODE_SID_CODE:
183 builder.setPeerNodeSid(new PeerNodeSidBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
184 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid());
186 case PEER_ADJ_SID_CODE:
187 builder.setPeerAdjSid(new PeerAdjSidBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
188 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid());
190 case PEER_SET_SID_CODE:
191 peerSetSids.add(new PeerSetSidsBuilder(SrLinkAttributesParser.parseEpeAdjacencySegmentIdentifier(value)).build());
192 LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size() - 1));
195 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
198 if (!srAdjIds.isEmpty()) {
199 builder.setSrAdjIds(srAdjIds);
201 if (!srLanAdjIds.isEmpty()) {
202 builder.setSrLanAdjIds(srLanAdjIds);
204 if (!peerSetSids.isEmpty()) {
205 builder.setPeerSetSids(peerSetSids);
207 LOG.trace("Finished parsing Link Attributes.");
208 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
211 private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
212 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.attribute.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
213 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
214 final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
215 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
217 builder.setUnreservedBandwidth(unreservedBandwidth);
218 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
221 private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
222 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
223 while (value.isReadable()) {
224 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
226 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
227 LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
230 private static void ifPresentApply(final Object tlv, final SerializerInterface serializer) {
232 serializer.check(tlv);
237 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf output) {
238 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
239 LOG.trace("Started serializing Link Attributes");
240 ifPresentApply(linkAttributes.getLocalIpv4RouterId(), value -> TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress((Ipv4Address) value), output));
241 ifPresentApply(linkAttributes.getLocalIpv6RouterId(), value -> TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress((Ipv6Address) value), output));
242 ifPresentApply(linkAttributes.getRemoteIpv4RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress((Ipv4Address) value), output));
243 ifPresentApply(linkAttributes.getRemoteIpv6RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress((Ipv6Address) value), output));
244 ifPresentApply(linkAttributes.getAdminGroup(), value -> TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt((((AdministrativeGroup) value).getValue()).intValue()), output));
245 ifPresentApply(linkAttributes.getMaxLinkBandwidth(), value -> TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
246 ifPresentApply(linkAttributes.getMaxReservableBandwidth(), value -> TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
247 serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), output);
248 ifPresentApply(linkAttributes.getTeMetric(), value -> TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(((TeMetric) value).getValue()), output));
249 ifPresentApply(linkAttributes.getLinkProtection(), value -> TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(((LinkProtectionType) value).getIntValue()), output));
250 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), output);
251 ifPresentApply(linkAttributes.getMetric(), value -> TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(((Metric) value).getValue().intValue()), output));
252 serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), output);
253 ifPresentApply(linkAttributes.getLinkName(), value -> TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode((String) value)), output));
254 ifPresentApply(linkAttributes.getSrAdjIds(), value -> SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers((List<SrAdjIds>) value, SR_ADJ_ID, output));
255 ifPresentApply(linkAttributes.getSrLanAdjIds(), value -> SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers((List<SrLanAdjIds>) value, output));
256 ifPresentApply(linkAttributes.getPeerNodeSid(), value -> TlvUtil.writeTLV(PEER_NODE_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerNodeSid) value), output));
257 ifPresentApply(linkAttributes.getPeerAdjSid(), value -> TlvUtil.writeTLV(PEER_ADJ_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerAdjSid) value), output));
258 ifPresentApply(linkAttributes.getPeerSetSids(), value -> SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers((List<PeerSetSids>) value, PEER_SET_SID_CODE, output));
259 LOG.trace("Finished serializing Link Attributes");
262 private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
263 // this sub-TLV contains eight 32-bit IEEE floating point numbers
264 if (ubList != null) {
265 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
266 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
267 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
269 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
273 private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
274 if (srlgList != null) {
275 final ByteBuf sharedRLGBuf = Unpooled.buffer();
276 for (final SrlgId srlgId : srlgList) {
277 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
279 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
283 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator) {
284 if (mplsProtocolMask != null) {
285 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
286 final BitArray mask = new BitArray(FLAGS_SIZE);
287 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
288 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
289 mask.toByteBuf(mplsProtocolMaskBuf);
290 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);