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
8 package org.opendaylight.protocol.bgp.labeled.unicast;
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;
53 public class LUNlriParser implements NlriParser, NlriSerializer {
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;
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();
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)
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)
87 serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(),
88 false, byteAggregator);
91 } else if (pathAttributes2 != null) {
92 final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
93 final WithdrawnRoutes withDrawnRoutes = mpUnreachNlri.getWithdrawnRoutes();
95 if (withDrawnRoutes != null) {
96 final DestinationType destinationType = withDrawnRoutes.getDestinationType();
97 if (destinationType instanceof DestinationLabeledUnicastCase) {
98 final DestinationLabeledUnicastCase labeledUnicastCase = (DestinationLabeledUnicastCase)
100 serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(),
101 true, byteAggregator);
102 } else if (destinationType instanceof DestinationIpv6LabeledUnicastCase) {
103 final DestinationIpv6LabeledUnicastCase labeledUnicastCase = (DestinationIpv6LabeledUnicastCase)
105 serializeNlri(labeledUnicastCase.getDestinationIpv6LabeledUnicast().getCLabeledUnicastDestination(),
106 true, byteAggregator);
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);
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);
125 serializeLabelStackEntries(labelStack, isUnreachNlri, nlriByteBuf);
126 serializePrefixField(prefix, nlriByteBuf);
128 buffer.writeBytes(nlriByteBuf);
131 public static void serializeLabelStackEntries(final List<LabelStack> stack, final boolean isUnreachNlri,
132 final ByteBuf buffer) {
133 if (!isUnreachNlri) {
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()));
140 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(labelStackEntry.getLabelValue()));
144 buffer.writeBytes(WITHDRAW_LABEL_BYTE_ARRAY);
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));
153 public static int getPrefixLength(final IpPrefix prefix) {
154 if (prefix.getIpv4Prefix() != null) {
155 return Ipv4Util.getPrefixLengthBytes(prefix.getIpv4Prefix().getValue());
157 return Ipv4Util.getPrefixLengthBytes(prefix.getIpv6Prefix().getValue());
160 private static byte[] getPrefixBytes(final IpPrefix prefix) {
161 final ByteBuf buffer = Unpooled.buffer();
163 if (prefix.getIpv4Prefix() != null) {
164 Ipv4Util.writeMinimalPrefix(prefix.getIpv4Prefix(), buffer);
166 Ipv6Util.writeMinimalPrefix(prefix.getIpv6Prefix(), buffer);
168 return ByteArray.readAllBytes(buffer);
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));
182 public static List<LabelStack> parseLabel(final ByteBuf nlri) {
183 if (!nlri.isReadable()) {
186 final List<LabelStack> labels = new ArrayList<>();
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) {
195 labels.add(new LabelStackBuilder().setLabelValue(mplsLabel).build());
196 } while (!bottomBit);
200 private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri,
201 final Class<? extends AddressFamily> afi, final boolean multiPathSupported) {
202 if (!nlri.isReadable()) {
205 final List<CLabeledUnicastDestination> dests = new ArrayList<>();
207 while (nlri.isReadable()) {
208 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
209 if (multiPathSupported) {
210 builder.setPathId(PathIdUtil.readPathId(nlri));
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());
224 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
225 final PeerSpecificParserConstraint constraint) throws BGPParsingException {
226 if (!nlri.isReadable()) {
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);
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();
243 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(destination).build());
247 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
248 final PeerSpecificParserConstraint constraint) throws BGPParsingException {
249 if (!nlri.isReadable()) {
252 final Class<? extends AddressFamily> afi = builder.getAfi();
254 final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
255 new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
256 final List<CLabeledUnicastDestination> dst = parseNlri(nlri, afi, mPathSupported);
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();
276 builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(destination).build());