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