BUG-4827: BGP Add path Labeled ribSupport
[bgpcep.git] / bgp / 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
9 package org.opendaylight.protocol.bgp.labeled.unicast;
10
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.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.rev100924.IpPrefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestination;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
35 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;
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.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
37 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;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51
52 public class LUNlriParser implements NlriParser, NlriSerializer {
53
54     public static final int LABEL_LENGTH = 3;
55
56     @Override
57     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
58         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a Attributes object");
59         final Attributes pathAttributes = (Attributes) attribute;
60         final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
61         final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
62         if (pathAttributes1 != null) {
63             final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
64             if ((routes != null) && (routes.getDestinationType() instanceof 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)) {
65                 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();
66                 serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(), byteAggregator);
67             }
68         } else if (pathAttributes2 != null) {
69             final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
70             if ((mpUnreachNlri.getWithdrawnRoutes() != null) && (mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationLabeledUnicastCase)) {
71                 final DestinationLabeledUnicastCase labeledUnicastCase = (DestinationLabeledUnicastCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
72                 serializeNlri(labeledUnicastCase.getDestinationLabeledUnicast().getCLabeledUnicastDestination(), byteAggregator);
73             }
74         }
75     }
76
77     protected static void serializeNlri(final List<CLabeledUnicastDestination> dests, final ByteBuf buffer) {
78         final ByteBuf nlriByteBuf = Unpooled.buffer();
79         for (final CLabeledUnicastDestination dest: dests) {
80             PathIdUtil.writePathId(dest.getPathId(), buffer);
81
82             final List<LabelStack> labelStack = dest.getLabelStack();
83             final IpPrefix prefix = dest.getPrefix();
84             // Serialize the length field
85             // Length field contains one Byte which represents the length of label stack and prefix in bits
86             nlriByteBuf.writeByte(((LABEL_LENGTH * labelStack.size()) + getPrefixLength(prefix)) * Byte.SIZE);
87
88             serializeLabelStackEntries(labelStack, nlriByteBuf);
89             serializePrefixField(prefix, nlriByteBuf);
90         }
91         buffer.writeBytes(nlriByteBuf);
92     }
93
94     public static void serializeLabelStackEntries(final List<LabelStack> stack, final ByteBuf buffer) {
95         int i = 1;
96         for (final LabelStack labelStackEntry : stack) {
97             if (i++ == stack.size()) {
98                 //mark last label stack entry with bottom-bit
99                 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabelWithBottomBit(labelStackEntry.getLabelValue()));
100             } else {
101                 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(labelStackEntry.getLabelValue()));
102             }
103         }
104     }
105
106     public static void serializePrefixField(final IpPrefix prefix, final ByteBuf buffer) {
107         final byte[] prefixBytes = getPrefixBytes(prefix);
108         buffer.writeBytes(Arrays.copyOfRange(prefixBytes, 1, prefixBytes.length));
109     }
110
111     public static int getPrefixLength(final IpPrefix prefix) {
112         if (prefix.getIpv4Prefix() != null) {
113             return Ipv4Util.getPrefixLengthBytes(prefix.getIpv4Prefix().getValue());
114         }
115         return Ipv4Util.getPrefixLengthBytes(prefix.getIpv6Prefix().getValue());
116     }
117
118     private static byte[] getPrefixBytes(final IpPrefix prefix) {
119         if (prefix.getIpv4Prefix() != null) {
120             return Ipv4Util.bytesForPrefixBegin(prefix.getIpv4Prefix());
121         }
122         return Ipv6Util.bytesForPrefixBegin(prefix.getIpv6Prefix());
123     }
124
125     @Override
126     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
127         parseNlri(nlri, builder, null);
128     }
129
130     private static List<CLabeledUnicastDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi, final boolean mPathSupported) {
131         if (!nlri.isReadable()) {
132             return null;
133         }
134         final List<CLabeledUnicastDestination> dests = new ArrayList<>();
135
136         while (nlri.isReadable()) {
137             final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
138             if (mPathSupported) {
139                 builder.setPathId(PathIdUtil.readPathId(nlri));
140             }
141             final short length = nlri.readUnsignedByte();
142             builder.setLabelStack(parseLabel(nlri));
143             final int labelNum = builder.getLabelStack().size();
144             final int prefixLen = length - (LABEL_LENGTH * Byte.SIZE * labelNum);
145             builder.setPrefix(parseIpPrefix(nlri, prefixLen, afi));
146             dests.add(builder.build());
147         }
148         return dests;
149     }
150
151     public static IpPrefix parseIpPrefix(final ByteBuf nlri, final int prefixLen, final Class<? extends AddressFamily> afi) {
152         final int prefixLenInByte = (prefixLen / Byte.SIZE) + (((prefixLen % Byte.SIZE) == 0) ? 0 : 1);
153         if (afi.equals(Ipv4AddressFamily.class)) {
154             return new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
155         } else if (afi.equals(Ipv6AddressFamily.class)) {
156             return new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(nlri, prefixLenInByte), prefixLen));
157         }
158         return null;
159     }
160
161     public static List<LabelStack> parseLabel(final ByteBuf nlri) {
162         if (!nlri.isReadable()) {
163             return null;
164         }
165         final List<LabelStack> labels = new ArrayList<>();
166         boolean bottomBit = false;
167         do {
168             final ByteBuf slice = nlri.readSlice(LABEL_LENGTH);
169             bottomBit = MplsLabelUtil.getBottomBit(slice);
170             labels.add(new LabelStackBuilder().setLabelValue(MplsLabelUtil.mplsLabelForByteBuf(slice)).build());
171         } while (!bottomBit);
172         return labels;
173     }
174
175     @Override
176     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
177         parseNlri(nlri, builder, null);
178     }
179
180     @Override
181     public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpReachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
182         if (!nlri.isReadable()) {
183             return;
184         }
185         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
186         final List<CLabeledUnicastDestination> dst = parseNlri(nlri, builder.getAfi(), mPathSupported);
187
188         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
189             new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
190                 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(
191                     dst).build()).build()).build());
192     }
193
194     @Override
195     public void parseNlri(@Nonnull final ByteBuf nlri, @Nonnull final MpUnreachNlriBuilder builder, @Nullable final PeerSpecificParserConstraint constraint) throws BGPParsingException {
196         if (!nlri.isReadable()) {
197             return;
198         }
199         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint, new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
200         final List<CLabeledUnicastDestination> dst = parseNlri(nlri, builder.getAfi(), mPathSupported);
201
202         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
203             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.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
204                 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().setCLabeledUnicastDestination(
205                     dst).build()).build()).build());
206     }
207 }