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