2 * Copyright (c) 2016 Brocade Communications 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.l3vpn;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
16 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.LabelStack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestinationBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public abstract class AbstractVpnNlriParser implements NlriParser, NlriSerializer {
38 private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnNlriParser.class);
40 protected abstract List<VpnDestination> getWithdrawnVpnDestination(DestinationType dstType);
42 protected abstract List<VpnDestination> getAdvertizedVpnDestination(DestinationType dstType);
44 protected abstract WithdrawnRoutes getWithdrawnRoutesByDestination(List<VpnDestination> dst);
46 protected abstract AdvertizedRoutes getAdvertizedRoutesByDestination(List<VpnDestination> dst);
48 private static void serializeNlri(final List<VpnDestination> dests, final boolean isWithdrawnRoute, final ByteBuf buffer) {
49 final ByteBuf nlriByteBuf = Unpooled.buffer();
50 for (final VpnDestination dest : dests) {
51 final List<LabelStack> labelStack = dest.getLabelStack();
52 final IpPrefix prefix = dest.getPrefix();
53 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
54 AbstractVpnNlriParser.serializeLengtField(prefix, labelStack, nlriByteBuf);
55 LUNlriParser.serializeLabelStackEntries(labelStack, isWithdrawnRoute, nlriByteBuf);
56 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
57 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null, "Ipv6 or Ipv4 prefix is missing.");
58 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
60 buffer.writeBytes(nlriByteBuf);
64 * Serialize the length field Length field contains one Byte which represents the length of label stack and prefix in bits
65 * @param prefix ipPrefix
66 * @param labelStack list of labelStack
67 * @param nlriByteBuf ByteBuf
69 static void serializeLengtField(final IpPrefix prefix, final List<LabelStack> labelStack, final ByteBuf nlriByteBuf) {
70 final int prefixLenght = LUNlriParser.getPrefixLength(prefix);
71 int labelStackLenght = 0;
72 if(labelStack != null) {
73 labelStackLenght = LUNlriParser.LABEL_LENGTH * labelStack.size();
75 nlriByteBuf.writeByte((labelStackLenght + prefixLenght + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
79 private static List<VpnDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi) {
80 if (!nlri.isReadable()) {
83 final List<VpnDestination> dests = new ArrayList<>();
85 while (nlri.isReadable()) {
86 final VpnDestinationBuilder builder = new VpnDestinationBuilder();
87 final short length = nlri.readUnsignedByte();
88 final List<LabelStack> labels = LUNlriParser.parseLabel(nlri);
89 builder.setLabelStack(labels);
90 final int labelNum = labels != null ? labels.size() : 1;
91 final int prefixLen = length - (LUNlriParser.LABEL_LENGTH * Byte.SIZE * labelNum) - (RouteDistinguisherUtil.RD_LENGTH * Byte.SIZE);
92 builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
93 Preconditions.checkState(prefixLen > 0, "A valid VPN IP prefix is required.");
94 builder.setPrefix(LUNlriParser.parseIpPrefix(nlri, prefixLen, afi));
95 dests.add(builder.build());
101 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
102 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a Attributes object");
103 final Attributes pathAttributes = (Attributes) attribute;
104 final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
105 final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
106 List<VpnDestination> vpnDst = null;
107 boolean isWithdrawnRoute = false;
108 if (pathAttributes1 != null) {
109 final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
110 if (routes != null) {
111 vpnDst = getAdvertizedVpnDestination(routes.getDestinationType());
113 } else if (pathAttributes2 != null) {
114 final WithdrawnRoutes routes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
115 if (routes != null) {
116 vpnDst = getWithdrawnVpnDestination(routes.getDestinationType());
117 isWithdrawnRoute = true;
120 if (vpnDst != null) {
121 serializeNlri(vpnDst, isWithdrawnRoute, byteAggregator);
126 public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
127 if (!nlri.isReadable()) {
130 final List<VpnDestination> dst = parseNlri(nlri, builder.getAfi());
131 builder.setWithdrawnRoutes(getWithdrawnRoutesByDestination(dst));
135 public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
136 if (!nlri.isReadable()) {
139 final List<VpnDestination> dst = parseNlri(nlri, builder.getAfi());
140 builder.setAdvertizedRoutes(getAdvertizedRoutesByDestination(dst));