da00045a23ff5ce509275f9b6cd05580ea6b3e1a
[bgpcep.git] / bgp / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / nlri / MvpnIpv4NlriHandler.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.bgp.mvpn.impl.nlri;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
13 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
14 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
15 import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
16 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.ipv4.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationMvpnIpv4AdvertizedCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.ipv4.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationMvpnIpv4WithdrawnCase;
29
30 /**
31  * https://tools.ietf.org/html/rfc6514#section-4.
32  *
33  * @author Claudio D. Gasparini
34  */
35 public final class MvpnIpv4NlriHandler implements NlriParser, NlriSerializer {
36     @Override
37     public void parseNlri(
38             final ByteBuf nlri,
39             final MpReachNlriBuilder builder,
40             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
41         if (!nlri.isReadable()) {
42             return;
43         }
44         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
45                 new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
46         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder()
47                 .setDestinationType(Ipv4NlriHandler.parseIpv4ReachNlri(nlri, mPathSupported)).build());
48     }
49
50
51     @Override
52     public void parseNlri(
53             final ByteBuf nlri,
54             final MpUnreachNlriBuilder builder,
55             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
56         if (!nlri.isReadable()) {
57             return;
58         }
59         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
60                 new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
61         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder()
62                 .setDestinationType(Ipv4NlriHandler.parseIpv4UnreachNlri(nlri, mPathSupported)).build());
63     }
64
65     @Override
66     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
67         final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
68         final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
69         if (pathAttributes1 != null) {
70             final AdvertizedRoutes routes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
71             if (routes != null && routes.getDestinationType() instanceof DestinationMvpnIpv4AdvertizedCase) {
72                 final DestinationMvpnIpv4AdvertizedCase reach
73                         = (DestinationMvpnIpv4AdvertizedCase) routes.getDestinationType();
74                 Ipv4NlriHandler.serializeNlri(reach.getDestinationMvpn().getMvpnDestination(),
75                         byteAggregator);
76             }
77         } else if (pathAttributes2 != null) {
78             final WithdrawnRoutes withdrawnRoutes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
79             if (withdrawnRoutes != null
80                     && withdrawnRoutes.getDestinationType() instanceof DestinationMvpnIpv4WithdrawnCase) {
81                 final DestinationMvpnIpv4WithdrawnCase reach
82                         = (DestinationMvpnIpv4WithdrawnCase) withdrawnRoutes.getDestinationType();
83                 Ipv4NlriHandler.serializeNlri(reach.getDestinationMvpn().getMvpnDestination(),
84                         byteAggregator);
85             }
86         }
87     }
88 }