BUG-4802: LS-SR enabling multiple SIDs
[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.List;
18 import java.util.Map.Entry;
19 import org.opendaylight.protocol.bgp.linkstate.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.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.AdministrativeGroup;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4RouterIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6RouterIdentifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkProtectionType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.MplsProtocolMask;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerAdjSidBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerNodeSidBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSetSids;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.PeerSetSidsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIds;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrAdjIdsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.link.state.SrLanAdjIds;
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.rev150820.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_NODE_SID_CODE = 1101;
88     private static final int PEER_ADJ_SID_CODE = 1102;
89     private static final int PEER_SET_SID_CODE = 1103;
90
91     /**
92      * Parse Link Attributes.
93      *
94      * @param attributes key is the tlv type and value is the value of the tlv
95      * @param protocolId to differentiate parsing methods
96      * @return {@link LinkStateAttribute}
97      */
98     static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes, final ProtocolId protocolId) {
99         final LinkAttributesBuilder builder = new LinkAttributesBuilder();
100         final List<SrAdjIds> srAdjIds = new ArrayList<SrAdjIds>();
101         final List<SrLanAdjIds> srLanAdjIds = new ArrayList<SrLanAdjIds>();
102         final List<PeerSetSids> peerSetSids = new ArrayList<PeerSetSids>();
103         for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
104             LOG.trace("Link attribute TLV {}", entry.getKey());
105             final int key = entry.getKey();
106             final ByteBuf value = entry.getValue();
107             switch (key) {
108             case TlvUtil.LOCAL_IPV4_ROUTER_ID:
109                 builder.setLocalIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
110                 LOG.debug("Parsed IPv4 Router-ID of local node: {}", builder.getLocalIpv4RouterId());
111                 break;
112             case TlvUtil.LOCAL_IPV6_ROUTER_ID:
113                 builder.setLocalIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
114                 LOG.debug("Parsed IPv6 Router-ID of local node: {}", builder.getLocalIpv6RouterId());
115                 break;
116             case REMOTE_IPV4_ROUTER_ID:
117                 builder.setRemoteIpv4RouterId(new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value)));
118                 LOG.debug("Parsed IPv4 Router-ID of remote node: {}", builder.getRemoteIpv4RouterId());
119                 break;
120             case REMOTE_IPV6_ROUTER_ID:
121                 builder.setRemoteIpv6RouterId(new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value)));
122                 LOG.debug("Parsed IPv6 Router-ID of remote node: {}", builder.getRemoteIpv6RouterId());
123                 break;
124             case ADMIN_GROUP:
125                 builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
126                 LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
127                 break;
128             case MAX_BANDWIDTH:
129                 builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
130                 LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
131                 break;
132             case MAX_RESERVABLE_BANDWIDTH:
133                 builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
134                 LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
135                 break;
136             case UNRESERVED_BANDWIDTH:
137                 parseUnreservedBandwidth(value, builder);
138                 break;
139             case TE_METRIC:
140                 builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
141                 LOG.debug("Parsed Metric {}", builder.getTeMetric());
142                 break;
143             case LINK_PROTECTION_TYPE:
144                 builder.setLinkProtection(LinkProtectionType.forValue(value.readShort()));
145                 LOG.debug("Parsed Link Protection Type {}", builder.getLinkProtection());
146                 break;
147             case MPLS_PROTOCOL:
148                 final BitArray bits = BitArray.valueOf(value, FLAGS_SIZE);
149                 builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
150                 LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
151                 break;
152             case METRIC:
153                 // length can 3, 2 or 1
154                 builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
155                 LOG.debug("Parsed Metric {}", builder.getMetric());
156                 break;
157             case SHARED_RISK_LINK_GROUP:
158                 parseSrlg(value, builder);
159                 break;
160             case LINK_OPAQUE:
161                 LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
162                 break;
163             case LINK_NAME:
164                 builder.setLinkName(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
165                 LOG.debug("Parsed Link Name : {}", builder.getLinkName());
166                 break;
167             case SR_ADJ_ID:
168                 srAdjIds.add(new SrAdjIdsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, protocolId)).build());
169                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srAdjIds.get(srAdjIds.size()-1));
170                 break;
171             case SR_LAN_ADJ_ID:
172                 srLanAdjIds.add(SrLinkAttributesParser.parseLanAdjacencySegmentIdentifier(value, protocolId));
173                 LOG.debug("Parsed Adjacency Segment Identifier :{}", srLanAdjIds.get(srLanAdjIds.size()-1));
174                 break;
175             case PEER_NODE_SID_CODE:
176                 builder.setPeerNodeSid(new PeerNodeSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
177                 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerNodeSid());
178                 break;
179             case PEER_ADJ_SID_CODE:
180                 builder.setPeerAdjSid(new PeerAdjSidBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
181                 LOG.debug("Parsed Peer Segment Identifier :{}", builder.getPeerAdjSid());
182                 break;
183             case PEER_SET_SID_CODE:
184                 peerSetSids.add(new PeerSetSidsBuilder(SrLinkAttributesParser.parseAdjacencySegmentIdentifier(value, null)).build());
185                 LOG.debug("Parsed Peer Set Sid :{}", peerSetSids.get(peerSetSids.size()-1));
186                 break;
187             default:
188                 LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
189             }
190         }
191         if (!srAdjIds.isEmpty()) {
192             builder.setSrAdjIds(srAdjIds);
193         }
194         if (!srLanAdjIds.isEmpty()) {
195             builder.setSrLanAdjIds(srLanAdjIds);
196         }
197         if (!peerSetSids.isEmpty()) {
198             builder.setPeerSetSids(peerSetSids);
199         }
200         LOG.trace("Finished parsing Link Attributes.");
201         return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
202     }
203
204     private static void parseUnreservedBandwidth(final ByteBuf value, final LinkAttributesBuilder builder) {
205         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);
206         for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
207             final ByteBuf v = value.readSlice(BANDWIDTH_LENGTH);
208             unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
209         }
210         builder.setUnreservedBandwidth(unreservedBandwidth);
211         LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
212     }
213
214     private static void parseSrlg(final ByteBuf value, final LinkAttributesBuilder builder) {
215         final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
216         while (value.isReadable()) {
217             sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
218         }
219         builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
220         LOG.debug("Parsed Shared Risk Link Groups {}", builder.getSharedRiskLinkGroups());
221     }
222
223     static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
224         final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
225         LOG.trace("Started serializing Link Attributes");
226         if (linkAttributes.getLocalIpv4RouterId() != null) {
227             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
228         }
229         if (linkAttributes.getLocalIpv6RouterId() != null) {
230             TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
231         }
232         if (linkAttributes.getRemoteIpv4RouterId() != null) {
233             TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
234         }
235         if (linkAttributes.getRemoteIpv6RouterId() != null) {
236             TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
237         }
238         if (linkAttributes.getAdminGroup() != null) {
239             TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
240         }
241         if (linkAttributes.getMaxLinkBandwidth() != null) {
242             TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
243         }
244         if (linkAttributes.getMaxReservableBandwidth() != null) {
245             TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
246         }
247         serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
248         if (linkAttributes.getTeMetric() != null) {
249             TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
250         }
251         if (linkAttributes.getLinkProtection() != null) {
252             TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
253         }
254         serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
255         if (linkAttributes.getMetric() != null) {
256             // size of metric can be 1,2 or 3 depending on the protocol
257             TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
258         }
259         serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
260         if (linkAttributes.getLinkName() != null) {
261             TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
262         }
263         if (linkAttributes.getSrAdjIds() != null) {
264             SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getSrAdjIds(), SR_ADJ_ID, byteAggregator);
265         }
266         if (linkAttributes.getSrLanAdjIds() != null) {
267             SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers(linkAttributes.getSrLanAdjIds(), SR_LAN_ADJ_ID, byteAggregator);
268         }
269         if (linkAttributes.getPeerNodeSid() != null) {
270             TlvUtil.writeTLV(PEER_NODE_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerNodeSid()), byteAggregator);
271         }
272         if (linkAttributes.getPeerAdjSid() != null) {
273             TlvUtil.writeTLV(PEER_ADJ_SID_CODE, SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerAdjSid()), byteAggregator);
274         }
275         if (linkAttributes.getPeerSetSids() != null) {
276             SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getPeerSetSids(), PEER_SET_SID_CODE, byteAggregator);
277         }
278         LOG.trace("Finished serializing Link Attributes");
279     }
280
281     private static void serializeUnreservedBw(final List<UnreservedBandwidth> ubList, final ByteBuf byteAggregator) {
282         // this sub-TLV contains eight 32-bit IEEE floating point numbers
283         if (ubList != null) {
284             final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
285             for (final UnreservedBandwidth unreservedBandwidth : ubList) {
286                 unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
287             }
288             TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
289         }
290     }
291
292     private static void serializeSrlg(final List<SrlgId> srlgList, final ByteBuf byteAggregator) {
293         if (srlgList != null) {
294             final ByteBuf sharedRLGBuf = Unpooled.buffer();
295             for (final SrlgId srlgId : srlgList) {
296                 sharedRLGBuf.writeInt(srlgId.getValue().intValue());
297             }
298             TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
299         }
300     }
301
302     private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
303         if (mplsProtocolMask != null) {
304             final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
305             final BitArray mask = new BitArray(FLAGS_SIZE);
306             mask.set(LDP_BIT, mplsProtocolMask.isLdp());
307             mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
308             mask.toByteBuf(mplsProtocolMaskBuf);
309             TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
310         }
311     }
312 }