2 * Copyright (c) 2013 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.linkstate.nlri;
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;
46 * Parser and serializer for Linkstate NLRI.
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;
54 private static final int TYPE_LENGTH = 2;
55 private static final int LENGTH_SIZE = 2;
58 private static final int LOCAL_NODE_DESCRIPTORS = 256;
59 private static final int REMOTE_NODE_DESCRIPTORS = 257;
61 private final boolean isVpn;
63 public LinkstateNlriParser(final boolean isVpn) {
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));
74 builder.setLinkDescriptors(LinkNlriParser.parseLinkDescriptors(buffer.slice()));
79 * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
81 * @param nlri as byte array
82 * @return {@link CLinkstateDestination}
83 * @throws BGPParsingException if parsing was unsuccessful
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);
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;
96 // this parses route distinguisher
97 distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
98 builder.setDistinguisher(distinguisher);
100 // parse source protocol
101 final ProtocolId sp = ProtocolId.forValue(nlri.readByte());
102 builder.setProtocolId(sp);
105 final Identifier identifier = new Identifier(BigInteger.valueOf(nlri.readLong()));
106 builder.setIdentifier(identifier);
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);
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);
122 parseLink(builder, rest);
125 builder.setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, true));
128 builder.setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, false));
131 // node nlri is already parsed as it contains only the common fields for node and link nlri
136 dests.add(builder.build());
142 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
143 if (!nlri.isReadable()) {
146 final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
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());
155 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
156 if (!nlri.isReadable()) {
159 final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
161 builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
162 new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
163 new DestinationLinkstateBuilder().setCLinkstateDestination(dst).build()).build()).build());
167 * Serializes Linkstate NLRI to byte array. We need this as NLRI serves as a key in upper layers.
169 * @param destination Linkstate NLRI to be serialized
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());
176 nlriByteBuf.writeByte(destination.getProtocolId().getIntValue());
177 nlriByteBuf.writeLong(destination.getIdentifier().getValue().longValue());
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);
184 switch (destination.getNlriType()) {
187 if (destination.getPrefixDescriptors() != null) {
188 PrefixNlriParser.serializePrefixDescriptors(destination.getPrefixDescriptors(), nlriByteBuf);
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);
202 LOG.warn("Unknown NLRI Type.");
205 TlvUtil.writeTLV(destination.getNlriType().getIntValue(), nlriByteBuf, buffer);
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()
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();
223 for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
224 serializeNlri(cLinkstateDestination, byteAggregator);
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);