Merge "BUG-2712: do not hold lock while updating future"
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / nlri / LinkstateNlriParser.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.bgp.linkstate.nlri;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
19 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Identifier;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.RouteDistinguisher;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestinationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.c.linkstate.destination.LocalNodeDescriptors;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptors;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Parser and serializer for Linkstate NLRI.
47  */
48 public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
49     private static final Logger LOG = LoggerFactory.getLogger(LinkstateNlriParser.class);
50     private static final int ROUTE_DISTINGUISHER_LENGTH = 8;
51     private static final int PROTOCOL_ID_LENGTH = 1;
52     private static final int IDENTIFIER_LENGTH = 8;
53
54     private static final int TYPE_LENGTH = 2;
55     private static final int LENGTH_SIZE = 2;
56
57
58     private static final int LOCAL_NODE_DESCRIPTORS = 256;
59     private static final int REMOTE_NODE_DESCRIPTORS = 257;
60
61     private final boolean isVpn;
62
63     public LinkstateNlriParser(final boolean isVpn) {
64         this.isVpn = isVpn;
65     }
66
67     private static NodeIdentifier parseLink(final CLinkstateDestinationBuilder builder, final ByteBuf buffer) throws BGPParsingException {
68         final int type = buffer.readUnsignedShort();
69         final int length = buffer.readUnsignedShort();
70         final NodeIdentifier remote = null;
71         if (type == REMOTE_NODE_DESCRIPTORS) {
72             builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) NodeNlriParser.parseNodeDescriptors(buffer.readSlice(length), false));
73         }
74         builder.setLinkDescriptors(LinkNlriParser.parseLinkDescriptors(buffer.slice()));
75         return remote;
76     }
77
78     /**
79      * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
80      *
81      * @param nlri as byte array
82      * @return {@link CLinkstateDestination}
83      * @throws BGPParsingException if parsing was unsuccessful
84      */
85     public static List<CLinkstateDestination> parseNlri(final ByteBuf nlri, final boolean isVpn) throws BGPParsingException {
86         final List<CLinkstateDestination> dests = new ArrayList<>();
87         while (nlri.isReadable()) {
88             final CLinkstateDestinationBuilder builder = new CLinkstateDestinationBuilder();
89             final NlriType type = NlriType.forValue(nlri.readUnsignedShort());
90             builder.setNlriType(type);
91
92             // length means total length of the tlvs including route distinguisher not including the type field
93             final int length = nlri.readUnsignedShort();
94             RouteDistinguisher distinguisher = null;
95             if (isVpn) {
96                 // this parses route distinguisher
97                 distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
98                 builder.setDistinguisher(distinguisher);
99             }
100             // parse source protocol
101             final ProtocolId sp = ProtocolId.forValue(nlri.readByte());
102             builder.setProtocolId(sp);
103
104             // parse identifier
105             final Identifier identifier = new Identifier(BigInteger.valueOf(nlri.readLong()));
106             builder.setIdentifier(identifier);
107
108             // if we are dealing with linkstate nodes/links, parse local node descriptor
109             NodeIdentifier localDescriptor = null;
110             final int localtype = nlri.readUnsignedShort();
111             final int locallength = nlri.readUnsignedShort();
112             if (localtype == LOCAL_NODE_DESCRIPTORS) {
113                 localDescriptor = NodeNlriParser.parseNodeDescriptors(nlri.readSlice(locallength), true);
114             }
115             builder.setLocalNodeDescriptors((LocalNodeDescriptors) localDescriptor);
116             final int restLength = length - (isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
117                 - TYPE_LENGTH - LENGTH_SIZE - locallength;
118             LOG.trace("Restlength {}", restLength);
119             final ByteBuf rest = nlri.readSlice(restLength);
120             switch (type) {
121             case Link:
122                 parseLink(builder, rest);
123                 break;
124             case Ipv4Prefix:
125                 builder.setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, true));
126                 break;
127             case Ipv6Prefix:
128                 builder.setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, false));
129                 break;
130             case Node:
131                 // node nlri is already parsed as it contains only the common fields for node and link nlri
132                 break;
133             default:
134                 break;
135             }
136             dests.add(builder.build());
137         }
138         return dests;
139     }
140
141     @Override
142     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
143         if (!nlri.isReadable()) {
144             return;
145         }
146         final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
147
148         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
149             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
150                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder().setCLinkstateDestination(
151                     dst).build()).build()).build());
152     }
153
154     @Override
155     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
156         if (!nlri.isReadable()) {
157             return;
158         }
159         final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
160
161         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
162             new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
163                 new DestinationLinkstateBuilder().setCLinkstateDestination(dst).build()).build()).build());
164     }
165
166     /**
167      * Serializes Linkstate NLRI to byte array. We need this as NLRI serves as a key in upper layers.
168      *
169      * @param destination Linkstate NLRI to be serialized
170      */
171     public static void serializeNlri(final CLinkstateDestination destination, final ByteBuf buffer) {
172         final ByteBuf nlriByteBuf = Unpooled.buffer();
173         if (destination.getDistinguisher() != null) {
174             nlriByteBuf.writeBytes(destination.getDistinguisher().getValue().toByteArray());
175         }
176         nlriByteBuf.writeByte(destination.getProtocolId().getIntValue());
177         nlriByteBuf.writeLong(destination.getIdentifier().getValue().longValue());
178
179         // serialize local node descriptors
180         final ByteBuf ldescs = Unpooled.buffer();
181         NodeNlriParser.serializeNodeDescriptors(destination.getLocalNodeDescriptors(), ldescs);
182         TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS, ldescs, nlriByteBuf);
183
184         switch (destination.getNlriType()) {
185         case Ipv4Prefix:
186         case Ipv6Prefix:
187             if (destination.getPrefixDescriptors() != null) {
188                 PrefixNlriParser.serializePrefixDescriptors(destination.getPrefixDescriptors(), nlriByteBuf);
189             }
190             break;
191         case Link:
192             final ByteBuf rdescs = Unpooled.buffer();
193             NodeNlriParser.serializeNodeDescriptors(destination.getRemoteNodeDescriptors(), rdescs);
194             TlvUtil.writeTLV(REMOTE_NODE_DESCRIPTORS, rdescs, nlriByteBuf);
195             if (destination.getLinkDescriptors() != null) {
196                 LinkNlriParser.serializeLinkDescriptors(destination.getLinkDescriptors(), nlriByteBuf);
197             }
198             break;
199         case Node:
200             break;
201         default:
202             LOG.warn("Unknown NLRI Type.");
203             break;
204         }
205         TlvUtil.writeTLV(destination.getNlriType().getIntValue(), nlriByteBuf, buffer);
206     }
207
208     @Override
209     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
210         Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
211         final PathAttributes pathAttributes = (PathAttributes) attribute;
212         final PathAttributes1 pathAttributes1 = pathAttributes.getAugmentation(PathAttributes1.class);
213         final PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
214         if (pathAttributes1 != null) {
215             final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
216             if (routes != null &&
217                 routes.getDestinationType()
218                 instanceof
219                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase) {
220                 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase
221                 linkstateCase = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase) routes.getDestinationType();
222
223                 for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
224                     serializeNlri(cLinkstateDestination, byteAggregator);
225                 }
226             }
227         } else if (pathAttributes2 != null) {
228             final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
229             if (mpUnreachNlri.getWithdrawnRoutes() != null && mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationLinkstateCase) {
230                 final DestinationLinkstateCase linkstateCase = (DestinationLinkstateCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
231                 for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
232                     serializeNlri(cLinkstateDestination, byteAggregator);
233                 }
234             }
235         }
236     }
237 }