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.base.Charsets;
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.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.BitSet;
18 import java.util.List;
19 import java.util.Map.Entry;
20 import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.protocol.util.Ipv4Util;
23 import org.opendaylight.protocol.util.Ipv6Util;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.AdministrativeGroup;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4RouterIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6RouterIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkProtectionType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.MplsProtocolMask;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidth;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidthBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 final class LinkAttributesParser {
45 private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
47 private static final int UNRESERVED_BW_COUNT = 8;
49 private static final int BANDWIDTH_LENGTH = 4;
51 // MPLS protection mask bits
52 private static final int LDP_BIT = 7;
53 private static final int RSVP_BIT = 6;
55 /* Link Attribute TLVs */
56 private static final int REMOTE_IPV4_ROUTER_ID = 1030;
57 private static final int REMOTE_IPV6_ROUTER_ID = 1031;
58 private static final int ADMIN_GROUP = 1088;
59 private static final int MAX_BANDWIDTH = 1089;
60 private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
61 private static final int UNRESERVED_BANDWIDTH = 1091;
62 private static final int TE_METRIC = 1092;
63 private static final int LINK_PROTECTION_TYPE = 1093;
64 private static final int MPLS_PROTOCOL = 1094;
65 private static final int METRIC = 1095;
66 private static final int SHARED_RISK_LINK_GROUP = 1096;
67 private static final int LINK_OPAQUE = 1097;
68 private static final int LINK_NAME = 1098;
71 * Parse Link Attributes.
73 * @param attributes key is the tlv type and value is the value of the tlv
74 * @return {@link LinkStateAttribute}
76 static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes) {
77 final LinkAttributesBuilder builder = new LinkAttributesBuilder();
78 for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
79 LOG.trace("Link attribute TLV {}", entry.getKey());
80 final int key = entry.getKey();
81 final ByteBuf value = entry.getValue();
83 case TlvUtil.LOCAL_IPV4_ROUTER_ID:
84 final Ipv4RouterIdentifier lipv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
85 builder.setLocalIpv4RouterId(lipv4);
86 LOG.debug("Parsed IPv4 Router-ID of local node: {}", lipv4);
88 case TlvUtil.LOCAL_IPV6_ROUTER_ID:
89 final Ipv6RouterIdentifier lipv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
90 builder.setLocalIpv6RouterId(lipv6);
91 LOG.debug("Parsed IPv6 Router-ID of local node: {}", lipv6);
93 case REMOTE_IPV4_ROUTER_ID:
94 final Ipv4RouterIdentifier ripv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
95 builder.setRemoteIpv4RouterId(ripv4);
96 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", ripv4);
98 case REMOTE_IPV6_ROUTER_ID:
99 final Ipv6RouterIdentifier ripv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
100 builder.setRemoteIpv6RouterId(ripv6);
101 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", ripv6);
104 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
105 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
108 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
109 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
111 case MAX_RESERVABLE_BANDWIDTH:
112 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
113 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
115 case UNRESERVED_BANDWIDTH:
116 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
117 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
118 final ByteBuf v = value.slice(value.readerIndex(), BANDWIDTH_LENGTH);
119 unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
120 value.skipBytes(BANDWIDTH_LENGTH);
122 builder.setUnreservedBandwidth(unreservedBandwidth);
123 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
126 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
127 LOG.debug("Parsed Metric {}", builder.getTeMetric());
129 case LINK_PROTECTION_TYPE:
130 final int l = value.readShort();
131 final LinkProtectionType lpt = LinkProtectionType.forValue(l);
133 LOG.warn("Link Protection Type not recognized: {}", l);
136 builder.setLinkProtection(lpt);
137 LOG.debug("Parsed Link Protection Type {}", lpt);
140 final BitSet bits = BitSet.valueOf(ByteArray.readAllBytes(value));
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 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
151 while (value.isReadable()) {
152 sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
154 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
155 LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
158 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
161 final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
162 builder.setLinkName(name);
163 LOG.debug("Parsed Link Name : {}", name);
166 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
169 LOG.trace("Finished parsing Link Attributes.");
170 return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
173 static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
174 final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
175 LOG.trace("Started serializing Link Attributes");
176 if (linkAttributes.getLocalIpv4RouterId() != null) {
177 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
179 if (linkAttributes.getLocalIpv6RouterId() != null) {
180 TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
182 if (linkAttributes.getRemoteIpv4RouterId() != null) {
183 TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
185 if (linkAttributes.getRemoteIpv6RouterId() != null) {
186 TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
188 if (linkAttributes.getAdminGroup() != null) {
189 TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
191 if (linkAttributes.getMaxLinkBandwidth() != null) {
192 TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
194 if (linkAttributes.getMaxReservableBandwidth() != null) {
195 TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
197 // this sub-TLV contains eight 32-bit IEEE floating point numbers
198 final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
199 if (ubList != null && !ubList.isEmpty()) {
200 final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
201 for (final UnreservedBandwidth unreservedBandwidth : ubList) {
202 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
204 TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
206 if (linkAttributes.getTeMetric() != null) {
207 TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
209 if (linkAttributes.getLinkProtection() != null) {
210 TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
212 serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
213 if (linkAttributes.getMetric() != null) {
214 // size of metric can be 1,2 or 3 depending on the protocol
215 TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
217 final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
218 if (srlgList != null && !srlgList.isEmpty()) {
219 final ByteBuf sharedRLGBuf = Unpooled.buffer();
220 for (final SrlgId srlgId : srlgList) {
221 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
223 TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
225 if (linkAttributes.getLinkName() != null) {
226 TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
228 LOG.trace("Finished serializing Link Attributes");
231 private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
232 if (mplsProtocolMask != null) {
233 final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
234 final BitSet mask = new BitSet(Byte.SIZE);
235 if (mplsProtocolMask.isLdp() != null) {
236 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
238 if (mplsProtocolMask.isRsvpte() != null) {
239 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
241 mplsProtocolMaskBuf.writeBytes(mask.toByteArray());
242 TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);