2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.labeled.unicast;
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;
56 public class LUNlriParser implements NlriParser, NlriSerializer {
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;
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();
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);
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);
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);
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);
111 serializeLabelStackEntries(labelStack, isUnreachNlri, nlriByteBuf);
112 serializePrefixField(prefix, nlriByteBuf);
114 buffer.writeBytes(nlriByteBuf);
117 public static void serializeLabelStackEntries(final List<LabelStack> stack, final boolean isUnreachNlri,
118 final ByteBuf buffer) {
119 if (!isUnreachNlri) {
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()));
126 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(labelStackEntry.getLabelValue()));
130 buffer.writeBytes(WITHDRAW_LABEL_BYTE_ARRAY);
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));
139 public static int getPrefixLength(final IpPrefix prefix) {
140 if (prefix.getIpv4Prefix() != null) {
141 return Ipv4Util.getPrefixLengthBytes(prefix.getIpv4Prefix().getValue());
143 return Ipv4Util.getPrefixLengthBytes(prefix.getIpv6Prefix().getValue());
146 private static byte[] getPrefixBytes(final IpPrefix prefix) {
147 final ByteBuf buffer = Unpooled.buffer();
149 if (prefix.getIpv4Prefix() != null) {
150 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv4Prefix(), buffer);
152 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), buffer);
154 return ByteArray.readAllBytes(buffer);
157 private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi, final boolean mPathSupported) {
158 if (!nlri.isReadable()) {
161 final List<CLabeledUnicastDestination> dests = new ArrayList<>();
163 while (nlri.isReadable()) {
164 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
165 if (mPathSupported) {
166 builder.setPathId(PathIdUtil.readPathId(nlri));
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());
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));
189 public static List<LabelStack> parseLabel(final ByteBuf nlri) {
190 if (!nlri.isReadable()) {
193 final List<LabelStack> labels = new ArrayList<>();
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) {
202 labels.add(new LabelStackBuilder().setLabelValue(mplsLabel).build());
203 } while (!bottomBit);
208 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
209 final PeerSpecificParserConstraint constraint) throws BGPParsingException {
210 if (!nlri.isReadable()) {
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);
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();
227 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
231 public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpUnreachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
232 if (!nlri.isReadable()) {
235 final Class<? extends AddressFamily> afi = builder.getAfi();
237 final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
238 final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
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();
252 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(destination).build());