Bug-7708: Invalid Route Distinguisher error while processing withdraw update containi...
[bgpcep.git] / bgp / 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
9 package org.opendaylight.protocol.bgp.labeled.unicast;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
22 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
23 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
24 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
25 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
26 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.protocol.util.Ipv4Util;
29 import org.opendaylight.protocol.util.Ipv6Util;
30 import org.opendaylight.protocol.util.MplsLabelUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestination;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.ipv6.labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6LabeledUnicastCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
56 import org.opendaylight.yangtools.yang.binding.DataObject;
57
58 public class LUNlriParser implements NlriParser, NlriSerializer {
59
60     public static final int LABEL_LENGTH = 3;
61     private static final byte[] WITHDRAW_LABEL_BYTE_ARRAY = { (byte) 0x80, (byte) 0x00, (byte) 0x00 };
62     private static final int WITHDRAW_LABEL_INT_VALUE = 0x800000;
63
64     @Override
65     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
66         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a Attributes object");
67         final Attributes pathAttributes = (Attributes) attribute;
68         final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
69         final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
70         if (pathAttributes1 != null) {
71             final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
72             if (routes != null) {
73                 final DestinationType destinationType = routes.getDestinationType();
74                 if ( destinationType instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
75                     .labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCase){
76                     final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCase labeledUnicastCase = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCase) routes.getDestinationType();
77                     serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(), false, byteAggregator);
78                 } else if (destinationType instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
79                     .labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCase) {
80                     final  org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCase labeledUnicastCase = ( org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6LabeledUnicastCase) routes.getDestinationType();
81                     serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(), false, byteAggregator);
82                 }
83             }
84         } else if (pathAttributes2 != null) {
85             final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
86             if (mpUnreachNlri.getWithdrawnRoutes() != null) {
87                 final DestinationType destinationType = mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
88                 if (destinationType instanceof DestinationLabeledUnicastCase) {
89                     final DestinationLabeledUnicastCase labeledUnicastCase = (DestinationLabeledUnicastCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
90                     serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(), true, byteAggregator);
91                 } else if(destinationType instanceof DestinationIpv6LabeledUnicastCase) {
92                     final DestinationIpv6LabeledUnicastCase labeledUnicastCase = (DestinationIpv6LabeledUnicastCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
93                     serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(), true, byteAggregator);
94                 }
95             }
96         }
97     }
98
99     protected static void serializeNlri(final List<CLabeledUnicastDestination> dests, final boolean isUnreachNlri,
100             final ByteBuf buffer) {
101         final ByteBuf nlriByteBuf = Unpooled.buffer();
102         for (final CLabeledUnicastDestination dest : dests) {
103             PathIdUtil.writePathId(dest.getPathId(), buffer);
104
105             final List<LabelStack> labelStack = dest.getLabelStack();
106             final IpPrefix prefix = dest.getPrefix();
107             // Serialize the length field
108             // Length field contains one Byte which represents the length of label stack and prefix in bits
109             nlriByteBuf.writeByte(((LABEL_LENGTH * (!isUnreachNlri ? labelStack.size() : 1)) + 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 i = 1;
121             for (final LabelStack labelStackEntry : stack) {
122                 if (i++ == 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         if (prefix.getIpv4Prefix() != null) {
148             return Ipv4Util.bytesForPrefixBegin(prefix.getIpv4Prefix());
149         }
150         return Ipv6Util.bytesForPrefixBegin(prefix.getIpv6Prefix());
151     }
152
153     @Override
154     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
155         parseNlri(nlri, builder, null);
156     }
157
158     private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi, final boolean mPathSupported) {
159         if (!nlri.isReadable()) {
160             return null;
161         }
162         final List<CLabeledUnicastDestination> dests = new ArrayList<>();
163
164         while (nlri.isReadable()) {
165             final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
166             if (mPathSupported) {
167                 builder.setPathId(PathIdUtil.readPathId(nlri));
168             }
169             final short length = nlri.readUnsignedByte();
170             final List<LabelStack> labels = parseLabel(nlri);
171             builder.setLabelStack(labels);
172             final int labelNum = labels != null ? labels.size() : 1;
173             final int prefixLen = length - (LABEL_LENGTH * Byte.SIZE * labelNum);
174             builder.setPrefix(parseIpPrefix(nlri, prefixLen, afi));
175             dests.add(builder.build());
176         }
177         return dests;
178     }
179
180     public static IpPrefix parseIpPrefix(final ByteBuf nlri, final int prefixLen, final Class<? extends AddressFamily> afi) {
181         final int prefixLenInByte = (prefixLen / Byte.SIZE) + (((prefixLen % Byte.SIZE) == 0) ? 0 : 1);
182         if (afi.equals(Ipv4AddressFamily.class)) {
183             return new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
184         } else if (afi.equals(Ipv6AddressFamily.class)) {
185             return new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
186         }
187         return null;
188     }
189
190     public static List<LabelStack> parseLabel(final ByteBuf nlri) {
191         if (!nlri.isReadable()) {
192             return null;
193         }
194         final List<LabelStack> labels = new ArrayList<>();
195         boolean bottomBit;
196         do {
197             final ByteBuf slice = nlri.readSlice(LABEL_LENGTH);
198             bottomBit = MplsLabelUtil.getBottomBit(slice);
199             final MplsLabel mplsLabel = MplsLabelUtil.mplsLabelForByteBuf(slice);
200             if (MplsLabelUtil.intForMplsLabel(mplsLabel) == WITHDRAW_LABEL_INT_VALUE) {
201                 return null;
202             }
203             labels.add(new LabelStackBuilder().setLabelValue(mplsLabel).build());
204         } while (!bottomBit);
205         return labels;
206     }
207
208     @Override
209     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
210         parseNlri(nlri, builder, null);
211     }
212
213     @Override
214     public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpReachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
215         if (!nlri.isReadable()) {
216             return;
217         }
218         final Class<? extends AddressFamily> afi = builder.getAfi();
219         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
220         final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
221
222         DestinationType destination = null;
223         if(afi == Ipv4AddressFamily.class){
224             destination = new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
225                 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
226         } else if(afi == Ipv6AddressFamily.class) {
227             destination = new DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(
228                 new DestinationIpv6LabeledUnicastBuilder()
229                 .setCLabeledUnicastDestination(dst).build()).build();
230         }
231         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
232     }
233
234     @Override
235     public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpUnreachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
236         if (!nlri.isReadable()) {
237             return;
238         }
239         final Class<? extends AddressFamily> afi = builder.getAfi();
240
241         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
242         final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
243
244         DestinationType destination = null;
245         if (afi == Ipv4AddressFamily.class) {
246             destination = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp
247                 .unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
248                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder()
249                     .setCLabeledUnicastDestination(dst).build()).build();
250         } else if (afi == Ipv6AddressFamily.class) {
251             destination = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp
252                 .unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(
253                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.ipv6.labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder()
254                     .setCLabeledUnicastDestination(dst).build()).build();
255         }
256         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(destination).build());
257     }
258 }