Bump upstreams to 2022.09
[bgpcep.git] / bgp / extensions / labeled-unicast / src / main / java / org / opendaylight / protocol / bgp / labeled / unicast / LUNlriParser.java
1 /*
2  * Copyright (c) 2015 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.labeled.unicast;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
17 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
18 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
19 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
20 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
21 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
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.protocol.util.MplsLabelUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestination;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.ipv6.labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6LabeledUnicastCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReach;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreach;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlriBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlriBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.mp.unreach.nlri.WithdrawnRoutes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.mp.unreach.nlri.WithdrawnRoutesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
52
53 public class LUNlriParser implements NlriParser, NlriSerializer {
54
55     public static final int LABEL_LENGTH = 3;
56     private static final byte[] WITHDRAW_LABEL_BYTE_ARRAY = { (byte) 0x80, (byte) 0x00, (byte) 0x00 };
57     private static final int WITHDRAW_LABEL_INT_VALUE = 0x800000;
58
59     @Override
60     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
61         final AttributesReach pathAttributes1 = pathAttributes.augmentation(AttributesReach.class);
62         final AttributesUnreach pathAttributes2 = pathAttributes.augmentation(AttributesUnreach.class);
63         if (pathAttributes1 != null) {
64             final AdvertizedRoutes routes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
65             if (routes != null) {
66                 final DestinationType destinationType = routes.getDestinationType();
67                 if (destinationType
68                     instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
69                     .labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type
70                     .DestinationLabeledUnicastCase labeledUnicastCase) {
71                     serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(),
72                         false, byteAggregator);
73                 } else if (destinationType
74                         instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
75                         .bgp.labeled.unicast.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination
76                         .type.DestinationIpv6LabeledUnicastCase labeledUnicastCase) {
77                     serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(),
78                         false, byteAggregator);
79                 }
80             }
81         } else if (pathAttributes2 != null) {
82             final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
83             final WithdrawnRoutes withDrawnRoutes = mpUnreachNlri.getWithdrawnRoutes();
84
85             if (withDrawnRoutes != null) {
86                 final DestinationType destinationType = withDrawnRoutes.getDestinationType();
87                 if (destinationType instanceof DestinationLabeledUnicastCase labeledUnicastCase) {
88                     serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(),
89                         true, byteAggregator);
90                 } else if (destinationType instanceof DestinationIpv6LabeledUnicastCase labeledUnicastCase) {
91                     serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(),
92                         true, byteAggregator);
93                 }
94             }
95         }
96     }
97
98     protected static void serializeNlri(final List<CLabeledUnicastDestination> dests, final boolean isUnreachNlri,
99             final ByteBuf buffer) {
100         final ByteBuf nlriByteBuf = Unpooled.buffer();
101         for (final CLabeledUnicastDestination dest : dests) {
102             PathIdUtil.writePathId(dest.getPathId(), buffer);
103
104             final List<LabelStack> labelStack = dest.getLabelStack();
105             final IpPrefix prefix = dest.getPrefix();
106             // Serialize the length field
107             // Length field contains one Byte which represents the length of label stack and prefix in bits
108             nlriByteBuf.writeByte((LABEL_LENGTH * (!isUnreachNlri ? labelStack.size() : 1)
109                     + getPrefixLength(prefix)) * Byte.SIZE);
110
111             serializeLabelStackEntries(labelStack, isUnreachNlri, nlriByteBuf);
112             serializePrefixField(prefix, nlriByteBuf);
113         }
114         buffer.writeBytes(nlriByteBuf);
115     }
116
117     public static void serializeLabelStackEntries(final List<LabelStack> stack, final boolean isUnreachNlri,
118             final ByteBuf buffer) {
119         if (!isUnreachNlri) {
120             int entry = 1;
121             for (final LabelStack labelStackEntry : stack) {
122                 if (entry++ == stack.size()) {
123                     // mark last label stack entry with bottom-bit
124                     buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabelWithBottomBit(labelStackEntry.getLabelValue()));
125                 } else {
126                     buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(labelStackEntry.getLabelValue()));
127                 }
128             }
129         } else {
130             buffer.writeBytes(WITHDRAW_LABEL_BYTE_ARRAY);
131         }
132     }
133
134     public static void serializePrefixField(final IpPrefix prefix, final ByteBuf buffer) {
135         final byte[] prefixBytes = getPrefixBytes(prefix);
136         buffer.writeBytes(Arrays.copyOfRange(prefixBytes, 1, prefixBytes.length));
137     }
138
139     public static int getPrefixLength(final IpPrefix prefix) {
140         if (prefix.getIpv4Prefix() != null) {
141             return Ipv4Util.getPrefixLengthBytes(prefix.getIpv4Prefix().getValue());
142         }
143         return Ipv4Util.getPrefixLengthBytes(prefix.getIpv6Prefix().getValue());
144     }
145
146     private static byte[] getPrefixBytes(final IpPrefix prefix) {
147         final ByteBuf buffer = Unpooled.buffer();
148
149         if (prefix.getIpv4Prefix() != null) {
150             Ipv4Util.writeMinimalPrefix(prefix.getIpv4Prefix(), buffer);
151         } else {
152             Ipv6Util.writeMinimalPrefix(prefix.getIpv6Prefix(), buffer);
153         }
154         return ByteArray.readAllBytes(buffer);
155     }
156
157     public static IpPrefix parseIpPrefix(final ByteBuf nlri, final int prefixLen, final AddressFamily afi) {
158         final int prefixLenInByte = prefixLen / Byte.SIZE + (prefixLen % Byte.SIZE == 0 ? 0 : 1);
159         if (afi.equals(Ipv4AddressFamily.VALUE)) {
160             return new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
161         } else if (afi.equals(Ipv6AddressFamily.VALUE)) {
162             return new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
163         }
164         return null;
165     }
166
167     public static List<LabelStack> parseLabel(final ByteBuf nlri) {
168         if (!nlri.isReadable()) {
169             return null;
170         }
171         final List<LabelStack> labels = new ArrayList<>();
172         boolean bottomBit;
173         do {
174             final ByteBuf slice = nlri.readSlice(LABEL_LENGTH);
175             bottomBit = MplsLabelUtil.getBottomBit(slice);
176             final MplsLabel mplsLabel = MplsLabelUtil.mplsLabelForByteBuf(slice);
177             if (MplsLabelUtil.intForMplsLabel(mplsLabel) == WITHDRAW_LABEL_INT_VALUE) {
178                 return null;
179             }
180             labels.add(new LabelStackBuilder().setLabelValue(mplsLabel).build());
181         } while (!bottomBit);
182         return labels;
183     }
184
185     private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri, final AddressFamily afi,
186             final boolean multiPathSupported) {
187         if (!nlri.isReadable()) {
188             return null;
189         }
190         final List<CLabeledUnicastDestination> dests = new ArrayList<>();
191
192         while (nlri.isReadable()) {
193             final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
194             if (multiPathSupported) {
195                 builder.setPathId(PathIdUtil.readPathId(nlri));
196             }
197             final short length = nlri.readUnsignedByte();
198             final List<LabelStack> labels = parseLabel(nlri);
199             builder.setLabelStack(labels);
200             final int labelNum = labels != null ? labels.size() : 1;
201             final int prefixLen = length - LABEL_LENGTH * Byte.SIZE * labelNum;
202             builder.setPrefix(parseIpPrefix(nlri, prefixLen, afi));
203             dests.add(builder.build());
204         }
205         return dests;
206     }
207
208     @Override
209     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
210         final PeerSpecificParserConstraint constraint) throws BGPParsingException {
211         if (!nlri.isReadable()) {
212             return;
213         }
214         final AddressFamily afi = builder.getAfi();
215         final boolean multiPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
216             new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
217         final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, multiPathSupported);
218
219         DestinationType destination = null;
220         if (afi.equals(Ipv4AddressFamily.VALUE)) {
221             destination = new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
222                 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
223         } else if (afi.equals(Ipv6AddressFamily.VALUE)) {
224             destination = new DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(
225                 new DestinationIpv6LabeledUnicastBuilder()
226                 .setCLabeledUnicastDestination(dst).build()).build();
227         }
228         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
229     }
230
231     @Override
232     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
233             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
234         if (!nlri.isReadable()) {
235             return;
236         }
237         final AddressFamily afi = builder.getAfi();
238
239         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
240             new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
241         final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
242
243         DestinationType destination = null;
244         if (afi.equals(Ipv4AddressFamily.VALUE)) {
245             destination = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast
246                     .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
247                     .DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
248                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast
249                         .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination
250                         .labeled.unicast._case.DestinationLabeledUnicastBuilder()
251                         .setCLabeledUnicastDestination(dst).build()).build();
252         } else if (afi.equals(Ipv6AddressFamily.VALUE)) {
253             destination = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast
254                     .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
255                     .DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(
256                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast
257                         .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.ipv6
258                         .labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder()
259                         .setCLabeledUnicastDestination(dst).build()).build();
260         }
261         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(destination).build());
262     }
263 }