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