6367739b18857e2bbdfeecdc1e92d66aadbd22b6
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / mcast / nlri / L3vpnMcastIpv4NlriHandler.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.l3vpn.mcast.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.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationL3vpnMcastIpv4AdvertizedCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationL3vpnMcastIpv4AdvertizedCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.l3vpn.mcast.ipv4.advertized._case.DestinationIpv4L3vpnMcastBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationL3vpnMcastIpv4WithdrawnCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationL3vpnMcastIpv4WithdrawnCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
32
33 /**
34  * https://tools.ietf.org/html/rfc6514#section-4.
35  *
36  * @author Claudio D. Gasparini
37  */
38 public final class L3vpnMcastIpv4NlriHandler implements NlriParser, NlriSerializer {
39     @Override
40     public void parseNlri(
41             final ByteBuf nlri,
42             final MpReachNlriBuilder builder,
43             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
44         if (!nlri.isReadable()) {
45             return;
46         }
47         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
48                 new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
49         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder()
50                 .setDestinationType(new DestinationL3vpnMcastIpv4AdvertizedCaseBuilder()
51                         .setDestinationIpv4L3vpnMcast(new DestinationIpv4L3vpnMcastBuilder()
52                                 .setL3vpnMcastDestination(L3vpnMcastNlriSerializer.extractDest(nlri, mPathSupported))
53                                 .build()).build()).build());
54     }
55
56
57     @Override
58     public void parseNlri(
59             final ByteBuf nlri,
60             final MpUnreachNlriBuilder builder,
61             final PeerSpecificParserConstraint constraint) throws BGPParsingException {
62         if (!nlri.isReadable()) {
63             return;
64         }
65         final boolean mPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
66                 new BgpTableTypeImpl(builder.getAfi(), builder.getSafi()));
67         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder()
68                 .setDestinationType(new DestinationL3vpnMcastIpv4WithdrawnCaseBuilder().setDestinationIpv4L3vpnMcast(
69                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417
70                                 .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.l3vpn
71                                 .mcast.ipv4.withdrawn._case.DestinationIpv4L3vpnMcastBuilder()
72                                 .setL3vpnMcastDestination(L3vpnMcastNlriSerializer.extractDest(nlri, mPathSupported))
73                                 .build()).build()).build());
74     }
75
76     @Override
77     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
78         final Attributes1 pathAttributes1 = pathAttributes.augmentation(Attributes1.class);
79         final Attributes2 pathAttributes2 = pathAttributes.augmentation(Attributes2.class);
80         if (pathAttributes1 != null) {
81             final AdvertizedRoutes routes = pathAttributes1.getMpReachNlri().getAdvertizedRoutes();
82             if (routes != null && routes.getDestinationType() instanceof DestinationL3vpnMcastIpv4AdvertizedCase) {
83                 final DestinationL3vpnMcastIpv4AdvertizedCase reach
84                         = (DestinationL3vpnMcastIpv4AdvertizedCase) routes.getDestinationType();
85                 L3vpnMcastNlriSerializer.serializeNlri(reach.getDestinationIpv4L3vpnMcast().getL3vpnMcastDestination(),
86                         byteAggregator);
87             }
88         } else if (pathAttributes2 != null) {
89             final WithdrawnRoutes withdrawnRoutes = pathAttributes2.getMpUnreachNlri().getWithdrawnRoutes();
90             if (withdrawnRoutes != null
91                     && withdrawnRoutes.getDestinationType() instanceof DestinationL3vpnMcastIpv4WithdrawnCase) {
92                 final DestinationL3vpnMcastIpv4WithdrawnCase reach
93                         = (DestinationL3vpnMcastIpv4WithdrawnCase) withdrawnRoutes.getDestinationType();
94                 L3vpnMcastNlriSerializer.serializeNlri(reach.getDestinationIpv4L3vpnMcast().getL3vpnMcastDestination(),
95                         byteAggregator);
96             }
97         }
98     }
99 }