Bug-2848: BGP-LS EPE extension: Peer Sid
[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.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.Arrays;
18 import java.util.List;
19 import java.util.Map.Entry;
20 import org.opendaylight.protocol.bgp.linkstate.attribute.sr.SrLinkAttributesParser;
21 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
22 import org.opendaylight.protocol.util.BitArray;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.Ipv4Util;
25 import org.opendaylight.protocol.util.Ipv6Util;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AdministrativeGroup;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6RouterIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkProtectionType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.MplsProtocolMask;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSetSid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSetSidBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSidBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIdBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrLanAdjId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidth;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.UnreservedBandwidthBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
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.LinkAttributes;
44 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;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 @VisibleForTesting
53 public final class LinkAttributesParser {
54
55     private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
56
57     private LinkAttributesParser() {
58         throw new UnsupportedOperationException();
59     }
60
61     private static final int UNRESERVED_BW_COUNT = 8;
62
63     private static final int BANDWIDTH_LENGTH = 4;
64
65     // MPLS protection mask bits
66     private static final int FLAGS_SIZE = 8;
67
68     private static final int LDP_BIT = 0;
69     private static final int RSVP_BIT = 1;
70
71     /* Link Attribute TLVs */
72     private static final int REMOTE_IPV4_ROUTER_ID = 1030;
73     private static final int REMOTE_IPV6_ROUTER_ID = 1031;
74     private static final int ADMIN_GROUP = 1088;
75     private static final int MAX_BANDWIDTH = 1089;
76     private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
77     private static final int UNRESERVED_BANDWIDTH = 1091;
78     private static final int TE_METRIC = 1092;
79     private static final int LINK_PROTECTION_TYPE = 1093;
80     private static final int MPLS_PROTOCOL = 1094;
81     private static final int METRIC = 1095;
82     private static final int SHARED_RISK_LINK_GROUP = 1096;
83     private static final int LINK_OPAQUE = 1097;
84     private static final int LINK_NAME = 1098;
85     private static final int SR_ADJ_ID = 1099;
86     private static final int SR_LAN_ADJ_ID = 1100;
87     private static final int PEER_SID_CODE = 1036;
88     private static final int PEER_SET_SID_CODE = 1037;
89
90     /**
91      * Parse Link Attributes.
92      *
93      * @param attributes key is the tlv type and value is the value of the tlv
94      * @return {@link LinkStateAttribute}
95      */
96     static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes) {
97         final LinkAttributesBuilder builder = new LinkAttributesBuilder();
98         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
99             LOG.trace("Link attribute TLV {}", entry.getKey());
100             final int key = entry.getKey();
101             final ByteBuf value = entry.getValue();
102             switch (key) {
103             case TlvUtil.LOCAL_IPV4_ROUTER_ID:
104                 final Ipv4RouterIdentifier lipv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
105                 builder.setLocalIpv4RouterId(lipv4);
106                 LOG.debug("Parsed IPv4 Router-ID of local node: {}", lipv4);
107                 break;
108             case TlvUtil.LOCAL_IPV6_ROUTER_ID:
109                 final Ipv6RouterIdentifier lipv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
110                 builder.setLocalIpv6RouterId(lipv6);
111                 LOG.debug("Parsed IPv6 Router-ID of local node: {}", lipv6);
112                 break;
113             case REMOTE_IPV4_ROUTER_ID:
114                 final Ipv4RouterIdentifier ripv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
115                 builder.setRemoteIpv4RouterId(ripv4);
116                 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", ripv4);
117                 break;
118             case REMOTE_IPV6_ROUTER_ID:
119                 final Ipv6RouterIdentifier ripv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
120                 builder.setRemoteIpv6RouterId(ripv6);
121                 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", ripv6);
122                 break;
123             case ADMIN_GROUP:
124                 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
125                 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
126                 break;
127             case MAX_BANDWIDTH:
128                 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
129                 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
130                 break;
131             case MAX_RESERVABLE_BANDWIDTH:
132                 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
133                 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
134                 break;
135             case UNRESERVED_BANDWIDTH:
136                 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);
137                 for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
138                     final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
139                     unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
140                 }
141                 builder.setUnreservedBandwidth(unreservedBandwidth);
142                 LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
143                 break;
144             case TE_METRIC:
145                 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
146                 LOG.debug("Parsed Metric {}", builder.getTeMetric());
147                 break;
148             case LINK_PROTECTION_TYPE:
149                 final int l = value.readShort();
150                 final LinkProtectionType lpt = LinkProtectionType.forValue(l);
151                 if (lpt == null) {
152                     LOG.warn("Link Protection Type not recognized: {}", l);
153                     break;
154                 }
155                 builder.setLinkProtection(lpt);
156                 LOG.debug("Parsed Link Protection Type {}", lpt);
157                 break;
158             case MPLS_PROTOCOL:
159                 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
160                 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
161                 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
162                 break;
163             case METRIC:
164                 // length can 3, 2 or 1
165                 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
166                 LOG.debug("Parsed Metric {}", builder.getMetric());
167                 break;
168             case SHARED_RISK_LINK_GROUP:
169                 final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
170                 while (value.isReadable()) {
171                     sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
172                 }
173                 builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
174                 LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
175                 break;
176             case LINK_OPAQUE:
177                 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
178                 break;
179             case LINK_NAME:
180                 final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
181                 builder.setLinkName(name);
182                 LOG.debug("Parsed Link Name : {}", name);
183                 break;
184             case SR_ADJ_ID:
185                 final SrAdjId srAdjId = new SrAdjIdBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value)).build();
186                 builder.setSrAdjId(srAdjId);
187                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjId);
188                 break;
189             case SR_LAN_ADJ_ID:
190                 final SrLanAdjId srLanAdjId = SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value);
191                 builder.setSrLanAdjId(srLanAdjId);
192                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjId);
193                 break;
194             case PEER_SID_CODE:
195                 final PeerSid peerSid = new PeerSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value)).build();
196                 builder.setPeerSid(peerSid);
197                 LOG.debug("Parsed Peer Segment Identifier :{}", peerSid);
198                 break;
199             case PEER_SET_SID_CODE:
200                 final PeerSetSid peerSetSid = new PeerSetSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value)).build();
201                 builder.setPeerSetSid(peerSetSid);
202                 LOG.debug("Parsed Peer Set Sid :{}", peerSetSid);
203                 break;
204             default:
205                 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
206             }
207         }
208         LOG.trace("Finished parsing Link Attributes.");
209         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
210     }
211
212     static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
213         final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
214         LOG.trace("Started serializing Link Attributes");
215         if (linkAttributes.getLocalIpv4RouterId() != null) {
216             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
217         }
218         if (linkAttributes.getLocalIpv6RouterId() != null) {
219             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
220         }
221         if (linkAttributes.getRemoteIpv4RouterId() != null) {
222             TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
223         }
224         if (linkAttributes.getRemoteIpv6RouterId() != null) {
225             TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
226         }
227         if (linkAttributes.getAdminGroup() != null) {
228             TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
229         }
230         if (linkAttributes.getMaxLinkBandwidth() != null) {
231             TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
232         }
233         if (linkAttributes.getMaxReservableBandwidth() != null) {
234             TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
235         }
236         // this sub-TLV contains eight 32-bit IEEE floating point numbers
237         final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
238         if (ubList != null) {
239             final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
240             for (final UnreservedBandwidth unreservedBandwidth : ubList) {
241                 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
242             }
243             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
244         }
245         if (linkAttributes.getTeMetric() != null) {
246             TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
247         }
248         if (linkAttributes.getLinkProtection() != null) {
249             TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
250         }
251         serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
252         if (linkAttributes.getMetric() != null) {
253             // size of metric can be 1,2 or 3 depending on the protocol
254             TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
255         }
256         final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
257         if (srlgList != null) {
258             final ByteBuf sharedRLGBuf = Unpooled.buffer();
259             for (final SrlgId srlgId : srlgList) {
260                 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
261             }
262             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
263         }
264         if (linkAttributes.getLinkName() != null) {
265             TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
266         }
267         if (linkAttributes.getSrAdjId() != null) {
268             TlvUtil.writeTLV(SR_ADJ_ID, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getSrAdjId()), byteAggregator);
269         }
270         if (linkAttributes.getSrLanAdjId() != null) {
271             TlvUtil.writeTLV(SR_LAN_ADJ_ID, SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifier(linkAttributes.getSrLanAdjId()), byteAggregator);
272         }
273         if (linkAttributes.getPeerSid() != null) {
274             TlvUtil.writeTLV(PEER_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSid()), byteAggregator);
275         }
276         if (linkAttributes.getPeerSetSid() != null) {
277             TlvUtil.writeTLV(PEER_SET_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerSetSid()), byteAggregator);
278         }
279         LOG.trace("Finished serializing Link Attributes");
280     }
281
282     private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
283         if (mplsProtocolMask != null) {
284             final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
285             final BitArray mask = new BitArray(FLAGS_SIZE);
286             mask.set(LDP_BIT, mplsProtocolMask.isLdp());
287             mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
288             mask.toByteBuf(mplsProtocolMaskBuf);
289             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
290         }
291     }
292 }