64a57ab7c1dd9e3d96ef15124357a03cbdf0a4c8
[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.spi.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.rev150210.AdministrativeGroup;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6RouterIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkProtectionType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.MplsProtocolMask;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.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.rev150210.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.rev150210.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.rev150210.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.rev150210.link.state.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
117                 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
118                     final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
119                     unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
120                 }
121                 builder.setUnreservedBandwidth(unreservedBandwidth);
122                 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
123                 break;
124             case TE_METRIC:
125                 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
126                 LOG.debug("Parsed Metric {}", builder.getTeMetric());
127                 break;
128             case LINK_PROTECTION_TYPE:
129                 final int l = value.readShort();
130                 final LinkProtectionType lpt = LinkProtectionType.forValue(l);
131                 if (lpt == null) {
132                     LOG.warn("Link Protection Type not recognized: {}", l);
133                     break;
134                 }
135                 builder.setLinkProtection(lpt);
136                 LOG.debug("Parsed Link Protection Type {}", lpt);
137                 break;
138             case MPLS_PROTOCOL:
139                 final BitSet bits = BitSet.valueOf(ByteArray.readAllBytes(value));
140                 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
141                 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
142                 break;
143             case METRIC:
144                 // length can 3, 2 or 1
145                 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
146                 LOG.debug("Parsed Metric {}", builder.getMetric());
147                 break;
148             case SHARED_RISK_LINK_GROUP:
149                 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
150                 while (value.isReadable()) {
151                     sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
152                 }
153                 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
154                 LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
155                 break;
156             case LINK_OPAQUE:
157                 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
158                 break;
159             case LINK_NAME:
160                 final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
161                 builder.setLinkName(name);
162                 LOG.debug("Parsed Link Name : {}", name);
163                 break;
164             default:
165                 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
166             }
167         }
168         LOG.trace("Finished parsing Link Attributes.");
169         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
170     }
171
172     static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
173         final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
174         LOG.trace("Started serializing Link Attributes");
175         if (linkAttributes.getLocalIpv4RouterId() != null) {
176             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
177         }
178         if (linkAttributes.getLocalIpv6RouterId() != null) {
179             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
180         }
181         if (linkAttributes.getRemoteIpv4RouterId() != null) {
182             TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
183         }
184         if (linkAttributes.getRemoteIpv6RouterId() != null) {
185             TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
186         }
187         if (linkAttributes.getAdminGroup() != null) {
188             TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
189         }
190         if (linkAttributes.getMaxLinkBandwidth() != null) {
191             TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
192         }
193         if (linkAttributes.getMaxReservableBandwidth() != null) {
194             TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
195         }
196         // this sub-TLV contains eight 32-bit IEEE floating point numbers
197         final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
198         if (ubList != null) {
199             final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
200             for (final UnreservedBandwidth unreservedBandwidth : ubList) {
201                 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
202             }
203             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
204         }
205         if (linkAttributes.getTeMetric() != null) {
206             TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
207         }
208         if (linkAttributes.getLinkProtection() != null) {
209             TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
210         }
211         serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
212         if (linkAttributes.getMetric() != null) {
213             // size of metric can be 1,2 or 3 depending on the protocol
214             TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
215         }
216         final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
217         if (srlgList != null) {
218             final ByteBuf sharedRLGBuf = Unpooled.buffer();
219             for (final SrlgId srlgId : srlgList) {
220                 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
221             }
222             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
223         }
224         if (linkAttributes.getLinkName() != null) {
225             TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
226         }
227         LOG.trace("Finished serializing Link Attributes");
228     }
229
230     private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
231         if (mplsProtocolMask != null) {
232             final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
233             final BitSet mask = new BitSet(Byte.SIZE);
234             if (mplsProtocolMask.isLdp() != null) {
235                 mask.set(LDP_BIT, mplsProtocolMask.isLdp());
236             }
237             if (mplsProtocolMask.isRsvpte() != null) {
238                 mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
239             }
240             mplsProtocolMaskBuf.writeBytes(mask.toByteArray());
241             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
242         }
243     }
244 }