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