Code clean up
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / unicast / 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.unicast;
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.spi.NlriParser;
17 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public abstract class AbstractVpnNlriParser implements NlriParser, NlriSerializer {
34     private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnNlriParser.class);
35
36     protected abstract List<VpnDestination> getWithdrawnVpnDestination(DestinationType dstType);
37
38     protected abstract List<VpnDestination> getAdvertizedVpnDestination(DestinationType dstType);
39
40     protected abstract WithdrawnRoutes getWithdrawnRoutesByDestination(List<VpnDestination> dst);
41
42     protected abstract AdvertizedRoutes getAdvertizedRoutesByDestination(List<VpnDestination> dst);
43
44     private static void serializeNlri(final List<VpnDestination> dests,
45             final boolean isWithdrawnRoute, final ByteBuf buffer) {
46         final ByteBuf nlriByteBuf = Unpooled.buffer();
47         for (final VpnDestination dest : dests) {
48             final List<LabelStack> labelStack = dest.getLabelStack();
49             final IpPrefix prefix = dest.getPrefix();
50             LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
51             AbstractVpnNlriParser.serializeLengtField(prefix, labelStack, nlriByteBuf);
52             LUNlriParser.serializeLabelStackEntries(labelStack, isWithdrawnRoute, nlriByteBuf);
53             RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
54             Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
55                     "Ipv6 or Ipv4 prefix is missing.");
56             LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
57         }
58         buffer.writeBytes(nlriByteBuf);
59     }
60
61     /**
62      * Serialize the length field Length field contains one Byte which represents the length of label stack and prefix
63      * in bits.
64      *
65      * @param prefix      ipPrefix
66      * @param labelStack  list of labelStack
67      * @param nlriByteBuf ByteBuf
68      */
69     static void serializeLengtField(final IpPrefix prefix, final List<LabelStack> labelStack,
70             final ByteBuf nlriByteBuf) {
71         final int prefixLenght = LUNlriParser.getPrefixLength(prefix);
72         int labelStackLenght = 0;
73         if (labelStack != null) {
74             labelStackLenght = LUNlriParser.LABEL_LENGTH * labelStack.size();
75         }
76         nlriByteBuf.writeByte((labelStackLenght + prefixLenght + RouteDistinguisherUtil.RD_LENGTH) * Byte.SIZE);
77     }
78
79     @Override
80     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
81         final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
82         final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
83         List<VpnDestination> vpnDst = null;
84         boolean isWithdrawnRoute = false;
85         if (pathAttributes1 != null) {
86             final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
87             if (routes != null) {
88                 vpnDst = getAdvertizedVpnDestination(routes.getDestinationType());
89             }
90         } else if (pathAttributes2 != null) {
91             final WithdrawnRoutes routes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
92             if (routes != null) {
93                 vpnDst = getWithdrawnVpnDestination(routes.getDestinationType());
94                 isWithdrawnRoute = true;
95             }
96         }
97         if (vpnDst != null) {
98             serializeNlri(vpnDst, isWithdrawnRoute, byteAggregator);
99         }
100     }
101
102     @Override
103     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder,
104         final PeerSpecificParserConstraint constraint) {
105         if (!nlri.isReadable()) {
106             return;
107         }
108         final List<VpnDestination> dst = VpnDestinationUtil.parseNlri(nlri, constraint,
109                 builder.getAfi(), builder.getSafi());
110         builder.setWithdrawnRoutes(getWithdrawnRoutesByDestination(dst));
111     }
112
113     @Override
114     public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder,
115         final PeerSpecificParserConstraint constraint) {
116         if (!nlri.isReadable()) {
117             return;
118         }
119         final List<VpnDestination> dst = VpnDestinationUtil.parseNlri(nlri, constraint,
120                 builder.getAfi(), builder.getSafi());
121         builder.setAdvertizedRoutes(getAdvertizedRoutesByDestination(dst));
122     }
123 }