MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / AbstractVpnNlriParser.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.l3vpn;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.List;
14 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
15 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
18 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public abstract class AbstractVpnNlriParser implements NlriParser, NlriSerializer {
35     private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnNlriParser.class);
36
37     protected abstract List<VpnDestination> getWithdrawnVpnDestination(DestinationType dstType);
38
39     protected abstract List<VpnDestination> getAdvertizedVpnDestination(DestinationType dstType);
40
41     protected abstract WithdrawnRoutes getWithdrawnRoutesByDestination(List<VpnDestination> dst);
42
43     protected abstract AdvertizedRoutes getAdvertizedRoutesByDestination(List<VpnDestination> dst);
44
45     private static void serializeNlri(final List<VpnDestination> dests,
46             final boolean isWithdrawnRoute, final ByteBuf buffer) {
47         final ByteBuf nlriByteBuf = Unpooled.buffer();
48         for (final VpnDestination dest : dests) {
49             final List<LabelStack> labelStack = dest.getLabelStack();
50             final IpPrefix prefix = dest.getPrefix();
51             LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
52             AbstractVpnNlriParser.serializeLengtField(prefix, labelStack, nlriByteBuf);
53             LUNlriParser.serializeLabelStackEntries(labelStack, isWithdrawnRoute, nlriByteBuf);
54             RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
55             Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
56                     "Ipv6 or Ipv4 prefix is missing.");
57             LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
58         }
59         buffer.writeBytes(nlriByteBuf);
60     }
61
62     /**
63      * Serialize the length field Length field contains one Byte which represents the length of label stack and prefix
64      * in bits.
65      *
66      * @param prefix      ipPrefix
67      * @param labelStack  list of labelStack
68      * @param nlriByteBuf ByteBuf
69      */
70     static void serializeLengtField(final IpPrefix prefix, final List<LabelStack> labelStack,
71             final ByteBuf nlriByteBuf) {
72         final int prefixLenght = LUNlriParser.getPrefixLength(prefix);
73         int labelStackLenght = 0;
74         if (labelStack != null) {
75             labelStackLenght = LUNlriParser.LABEL_LENGTH * labelStack.size();
76         }
77         nlriByteBuf.writeByte((labelStackLenght + prefixLenght + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
78     }
79
80     @Override
81     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
82         Preconditions.checkArgument(attribute instanceof Attributes,
83                 "Attribute parameter is not a Attributes object");
84         final Attributes pathAttributes = (Attributes) attribute;
85         final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
86         final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
87         List<VpnDestination> vpnDst = null;
88         boolean isWithdrawnRoute = false;
89         if (pathAttributes1 != null) {
90             final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
91             if (routes != null) {
92                 vpnDst = getAdvertizedVpnDestination(routes.getDestinationType());
93             }
94         } else if (pathAttributes2 != null) {
95             final WithdrawnRoutes routes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
96             if (routes != null) {
97                 vpnDst = getWithdrawnVpnDestination(routes.getDestinationType());
98                 isWithdrawnRoute = true;
99             }
100         }
101         if (vpnDst != null) {
102             serializeNlri(vpnDst, isWithdrawnRoute, byteAggregator);
103         }
104     }
105
106     @Override
107     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
108         if (!nlri.isReadable()) {
109             return;
110         }
111         final List<VpnDestination> dst = VpnDestinationUtil.parseNlri(nlri, builder.getAfi());
112         builder.setWithdrawnRoutes(getWithdrawnRoutesByDestination(dst));
113     }
114
115     @Override
116     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
117         if (!nlri.isReadable()) {
118             return;
119         }
120         final List<VpnDestination> dst = VpnDestinationUtil.parseNlri(nlri, builder.getAfi());
121         builder.setAdvertizedRoutes(getAdvertizedRoutesByDestination(dst));
122     }
123 }