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