b5fc04efe8f81827da4d26d079991f0dcd226e48
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / attribute / LinkAttributesParser.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.linkstate.attribute;
9
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;
42
43 final class LinkAttributesParser {
44
45     private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
46
47     private static final int UNRESERVED_BW_COUNT = 8;
48
49     private static final int BANDWIDTH_LENGTH = 4;
50
51     // MPLS protection mask bits
52     private static final int LDP_BIT = 7;
53     private static final int RSVP_BIT = 6;
54
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;
69
70     /**
71      * Parse Link Attributes.
72      *
73      * @param attributes key is the tlv type and value is the value of the tlv
74      * @return {@link LinkStateAttribute}
75      */
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();
82             switch (key) {
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);
87                 break;
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);
92                 break;
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);
97                 break;
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);
102                 break;
103             case ADMIN_GROUP:
104                 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
105                 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
106                 break;
107             case MAX_BANDWIDTH:
108                 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
109                 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
110                 break;
111             case MAX_RESERVABLE_BANDWIDTH:
112                 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
113                 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
114                 break;
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);
121                 }
122                 builder.setUnreservedBandwidth(unreservedBandwidth);
123                 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
124                 break;
125             case TE_METRIC:
126                 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
127                 LOG.debug("Parsed Metric {}", builder.getTeMetric());
128                 break;
129             case LINK_PROTECTION_TYPE:
130                 final int l = value.readShort();
131                 final LinkProtectionType lpt = LinkProtectionType.forValue(l);
132                 if (lpt == null) {
133                     LOG.warn("Link Protection Type not recognized: {}", l);
134                     break;
135                 }
136                 builder.setLinkProtection(lpt);
137                 LOG.debug("Parsed Link Protection Type {}", lpt);
138                 break;
139             case MPLS_PROTOCOL:
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());
143                 break;
144             case METRIC:
145                 // length can 3, 2 or 1
146                 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
147                 LOG.debug("Parsed Metric {}", builder.getMetric());
148                 break;
149             case SHARED_RISK_LINK_GROUP:
150                 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
151                 while (value.isReadable()) {
152                     sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
153                 }
154                 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
155                 LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
156                 break;
157             case LINK_OPAQUE:
158                 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
159                 break;
160             case LINK_NAME:
161                 final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
162                 builder.setLinkName(name);
163                 LOG.debug("Parsed Link Name : {}", name);
164                 break;
165             default:
166                 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
167             }
168         }
169         LOG.trace("Finished parsing Link Attributes.");
170         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
171     }
172
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);
178         }
179         if (linkAttributes.getLocalIpv6RouterId() != null) {
180             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
181         }
182         if (linkAttributes.getRemoteIpv4RouterId() != null) {
183             TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
184         }
185         if (linkAttributes.getRemoteIpv6RouterId() != null) {
186             TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
187         }
188         if (linkAttributes.getAdminGroup() != null) {
189             TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
190         }
191         if (linkAttributes.getMaxLinkBandwidth() != null) {
192             TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
193         }
194         if (linkAttributes.getMaxReservableBandwidth() != null) {
195             TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
196         }
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());
203             }
204             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
205         }
206         if (linkAttributes.getTeMetric() != null) {
207             TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
208         }
209         if (linkAttributes.getLinkProtection() != null) {
210             TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
211         }
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);
216         }
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());
222             }
223             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
224         }
225         if (linkAttributes.getLinkName() != null) {
226             TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
227         }
228         LOG.trace("Finished serializing Link Attributes");
229     }
230
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());
237             }
238             if (mplsProtocolMask.isRsvpte() != null) {
239                 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
240             }
241             mplsProtocolMaskBuf.writeBytes(mask.toByteArray());
242             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
243         }
244     }
245 }