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 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;
58 public class LUNlriParser implements NlriParser, NlriSerializer {
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;
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();
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);
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);
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 if (prefix.getIpv4Prefix() != null) {
148 return Ipv4Util.bytesForPrefixBegin(prefix.getIpv4Prefix());
150 return Ipv6Util.bytesForPrefixBegin(prefix.getIpv6Prefix());
154 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
155 parseNlri(nlri, builder, null);
158 private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi, final boolean mPathSupported) {
159 if (!nlri.isReadable()) {
162 final List<CLabeledUnicastDestination> dests = new ArrayList<>();
164 while (nlri.isReadable()) {
165 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
166 if (mPathSupported) {
167 builder.setPathId(PathIdUtil.readPathId(nlri));
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());
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));
190 public static List<LabelStack> parseLabel(final ByteBuf nlri) {
191 if (!nlri.isReadable()) {
194 final List<LabelStack> labels = new ArrayList<>();
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) {
203 labels.add(new LabelStackBuilder().setLabelValue(mplsLabel).build());
204 } while (!bottomBit);
209 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
210 parseNlri(nlri, builder, null);
214 public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpReachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
215 if (!nlri.isReadable()) {
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);
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();
231 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
235 public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpUnreachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
236 if (!nlri.isReadable()) {
239 final Class<? extends AddressFamily> afi = builder.getAfi();
241 final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
242 final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
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();
256 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(destination).build());