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.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
21 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
22 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
23 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
24 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
25 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.protocol.util.ByteBufWriteUtil;
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.rev180329.labeled.unicast.LabelStack;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestination;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
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.DestinationIpv6LabeledUnicastCaseBuilder;
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.DestinationLabeledUnicastCaseBuilder;
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.ipv6.labeled.unicast._case.DestinationIpv6LabeledUnicastBuilder;
39 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;
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.DestinationIpv6LabeledUnicastCase;
41 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;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.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.rev180329.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.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();
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.rev180329.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.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();
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)
90 mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
91 serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(),
92 true, byteAggregator);
93 } else if(destinationType instanceof DestinationIpv6LabeledUnicastCase) {
94 final DestinationIpv6LabeledUnicastCase labeledUnicastCase = (DestinationIpv6LabeledUnicastCase)
95 mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
96 serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(),
97 true, byteAggregator);
103 protected static void serializeNlri(final List<CLabeledUnicastDestination> dests, final boolean isUnreachNlri,
104 final ByteBuf buffer) {
105 final ByteBuf nlriByteBuf = Unpooled.buffer();
106 for (final CLabeledUnicastDestination dest : dests) {
107 PathIdUtil.writePathId(dest.getPathId(), buffer);
109 final List<LabelStack> labelStack = dest.getLabelStack();
110 final IpPrefix prefix = dest.getPrefix();
111 // Serialize the length field
112 // Length field contains one Byte which represents the length of label stack and prefix in bits
113 nlriByteBuf.writeByte(((LABEL_LENGTH * (!isUnreachNlri ? labelStack.size() : 1)) + getPrefixLength(prefix)) * Byte.SIZE);
115 serializeLabelStackEntries(labelStack, isUnreachNlri, nlriByteBuf);
116 serializePrefixField(prefix, nlriByteBuf);
118 buffer.writeBytes(nlriByteBuf);
121 public static void serializeLabelStackEntries(final List<LabelStack> stack, final boolean isUnreachNlri,
122 final ByteBuf buffer) {
123 if (!isUnreachNlri) {
125 for (final LabelStack labelStackEntry : stack) {
126 if (i++ == stack.size()) {
127 // mark last label stack entry with bottom-bit
128 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabelWithBottomBit(labelStackEntry.getLabelValue()));
130 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(labelStackEntry.getLabelValue()));
134 buffer.writeBytes(WITHDRAW_LABEL_BYTE_ARRAY);
138 public static void serializePrefixField(final IpPrefix prefix, final ByteBuf buffer) {
139 final byte[] prefixBytes = getPrefixBytes(prefix);
140 buffer.writeBytes(Arrays.copyOfRange(prefixBytes, 1, prefixBytes.length));
143 public static int getPrefixLength(final IpPrefix prefix) {
144 if (prefix.getIpv4Prefix() != null) {
145 return Ipv4Util.getPrefixLengthBytes(prefix.getIpv4Prefix().getValue());
147 return Ipv4Util.getPrefixLengthBytes(prefix.getIpv6Prefix().getValue());
150 private static byte[] getPrefixBytes(final IpPrefix prefix) {
151 final ByteBuf buffer = Unpooled.buffer();
153 if (prefix.getIpv4Prefix() != null) {
154 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv4Prefix(), buffer);
156 ByteBufWriteUtil.writeMinimalPrefix(prefix.getIpv6Prefix(), buffer);
158 return ByteArray.readAllBytes(buffer);
162 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
163 parseNlri(nlri, builder, null);
166 private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi, final boolean mPathSupported) {
167 if (!nlri.isReadable()) {
170 final List<CLabeledUnicastDestination> dests = new ArrayList<>();
172 while (nlri.isReadable()) {
173 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
174 if (mPathSupported) {
175 builder.setPathId(PathIdUtil.readPathId(nlri));
177 final short length = nlri.readUnsignedByte();
178 final List<LabelStack> labels = parseLabel(nlri);
179 builder.setLabelStack(labels);
180 final int labelNum = labels != null ? labels.size() : 1;
181 final int prefixLen = length - (LABEL_LENGTH * Byte.SIZE * labelNum);
182 builder.setPrefix(parseIpPrefix(nlri, prefixLen, afi));
183 dests.add(builder.build());
188 public static IpPrefix parseIpPrefix(final ByteBuf nlri, final int prefixLen, final Class<? extends AddressFamily> afi) {
189 final int prefixLenInByte = (prefixLen / Byte.SIZE) + (((prefixLen % Byte.SIZE) == 0) ? 0 : 1);
190 if (afi.equals(Ipv4AddressFamily.class)) {
191 return new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
192 } else if (afi.equals(Ipv6AddressFamily.class)) {
193 return new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
198 public static List<LabelStack> parseLabel(final ByteBuf nlri) {
199 if (!nlri.isReadable()) {
202 final List<LabelStack> labels = new ArrayList<>();
205 final ByteBuf slice = nlri.readSlice(LABEL_LENGTH);
206 bottomBit = MplsLabelUtil.getBottomBit(slice);
207 final MplsLabel mplsLabel = MplsLabelUtil.mplsLabelForByteBuf(slice);
208 if (MplsLabelUtil.intForMplsLabel(mplsLabel) == WITHDRAW_LABEL_INT_VALUE) {
211 labels.add(new LabelStackBuilder().setLabelValue(mplsLabel).build());
212 } while (!bottomBit);
217 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
218 parseNlri(nlri, builder, null);
222 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
223 final PeerSpecificParserConstraint constraint) throws BGPParsingException {
224 if (!nlri.isReadable()) {
227 final Class<? extends AddressFamily> afi = builder.getAfi();
228 final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
229 new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
230 final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
232 DestinationType destination = null;
233 if(afi == Ipv4AddressFamily.class){
234 destination = new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
235 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(dst).build()).build();
236 } else if(afi == Ipv6AddressFamily.class) {
237 destination = new DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(
238 new DestinationIpv6LabeledUnicastBuilder()
239 .setCLabeledUnicastDestination(dst).build()).build();
241 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
245 public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpUnreachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
246 if (!nlri.isReadable()) {
249 final Class<? extends AddressFamily> afi = builder.getAfi();
251 final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
252 final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
254 DestinationType destination = null;
255 if (afi == Ipv4AddressFamily.class) {
256 destination = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp
257 .unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
258 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()
259 .setCLabeledUnicastDestination(dst).build()).build();
260 } else if (afi == Ipv6AddressFamily.class) {
261 destination = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.update.attributes.mp
262 .unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6LabeledUnicastCaseBuilder().setDestinationIpv6LabeledUnicast(
263 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()
264 .setCLabeledUnicastDestination(dst).build()).build();
266 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(destination).build());