9eaa29c7e25f9ebc881fbb3852d87b6dc927f20b
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / AbstractVpnRIBSupport.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 com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Collectors;
20 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
23 import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastIpv4RIBSupport;
24 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestination;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.route.VpnRoute;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.route.VpnRouteBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.route.VpnRouteKey;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
47 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public abstract class AbstractVpnRIBSupport extends AbstractRIBSupport<VpnRoute, VpnRouteKey> {
57     private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnRIBSupport.class);
58     private final NodeIdentifier nlriRoutesListNid;
59     private final NodeIdentifier prefixTypeNid;
60     private final NodeIdentifier labelStackNid;
61     private final NodeIdentifier lvNid;
62     private final NodeIdentifier rdNid;
63     private final QName routeKey;
64
65     /**
66      * Default constructor. Requires the QName of the container augmented under the routes choice
67      * node in instantiations of the rib grouping. It is assumed that this container is defined by
68      * the same model which populates it with route grouping instantiation, and by extension with
69      * the route attributes container.
70      *
71      * @param cazeClass      Binding class of the AFI/SAFI-specific case statement, must not be null
72      * @param containerClass Binding class of the container in routes choice, must not be null.
73      * @param listClass      Binding class of the route list, nust not be null;
74      */
75     protected AbstractVpnRIBSupport(final Class<? extends Routes> cazeClass,
76             final Class<? extends DataObject> containerClass, final Class<? extends Route> listClass,
77         final Class<? extends AddressFamily> afiClass, final QName vpnDstContainerClassQname) {
78         super(cazeClass, containerClass, listClass, afiClass,
79                 MplsLabeledVpnSubsequentAddressFamily.class, vpnDstContainerClassQname);
80         final QName classQname = BindingReflections.findQName(containerClass).intern();
81         this.routeKey = QName.create(routeQName(), ROUTE_KEY).intern();
82         final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
83         this.nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
84         this.prefixTypeNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "prefix").intern());
85         this.labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
86         this.lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
87         this.rdNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "route-distinguisher").intern());
88     }
89
90     private VpnDestination extractVpnDestination(final DataContainerNode<? extends PathArgument> route) {
91         final VpnDestination dst = new VpnDestinationBuilder()
92             .setPrefix(extractPrefix(route, this.prefixTypeNid))
93             .setLabelStack(LabeledUnicastIpv4RIBSupport.extractLabel(route, this.labelStackNid, this.lvNid))
94             .setRouteDistinguisher(extractRouteDistinguisher(route))
95             .build();
96         return dst;
97     }
98
99     protected abstract IpPrefix extractPrefix(DataContainerNode<? extends PathArgument> route,
100             NodeIdentifier prefixTypeNid);
101
102     private RouteDistinguisher extractRouteDistinguisher(
103             final DataContainerNode<? extends YangInstanceIdentifier.PathArgument> route) {
104         if (route.getChild(this.rdNid).isPresent()) {
105             return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(this.rdNid).get().getValue());
106         }
107         return null;
108     }
109
110     protected abstract DestinationType getAdvertisedDestinationType(List<VpnDestination> dests);
111
112     protected abstract DestinationType getWithdrawnDestinationType(List<VpnDestination> dests);
113
114     @Override
115     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
116         return getAdvertisedDestinationType(extractRoutes(routes));
117     }
118
119     @Override
120     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
121         return getWithdrawnDestinationType(extractRoutes(routes));
122     }
123
124     private List<VpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
125         return routes.stream().map(this::extractVpnDestination).collect(Collectors.toList());
126     }
127
128     @Override
129     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
130         return ImmutableSet.of();
131     }
132
133     @Override
134     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
135         return ImmutableSet.of();
136     }
137
138     @Override
139     public boolean isComplexRoute() {
140         return true;
141     }
142
143     @Override
144     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
145         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
146         if (destination != null) {
147             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes =
148                     destination.getChild(this.nlriRoutesListNid);
149             if (maybeRoutes.isPresent()) {
150                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
151                 if (routes instanceof UnkeyedListNode) {
152                     final UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
153                     LOG.debug("{} routes are found", routeListNode.getSize());
154                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
155                     for (final UnkeyedListEntryNode e : routeListNode.getValue()) {
156                         final NodeIdentifierWithPredicates key = createRouteKey(e);
157                         LOG.debug("Route {} is processed.", key);
158                         function.apply(tx, base, key, e, attributes);
159                     }
160                 } else {
161                     LOG.warn("Routes {} are not a map", routes);
162                 }
163             }
164         } else {
165             LOG.debug("Destination is null.");
166         }
167     }
168
169     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
170         final ByteBuf buffer = Unpooled.buffer();
171         final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
172             .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
173         final ByteBuf nlriByteBuf = Unpooled.buffer();
174
175         for (final VpnDestination dest : Collections.singletonList(dests)) {
176             final IpPrefix prefix = dest.getPrefix();
177             LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
178             AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
179             RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
180             Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
181                     "Ipv6 or Ipv4 prefix is missing.");
182             LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
183         }
184         buffer.writeBytes(nlriByteBuf);
185
186         return new NodeIdentifierWithPredicates(routeQName(), this.routeKey, ByteArray.encodeBase64(buffer));
187     }
188
189     @Override
190     public final VpnRoute createRoute(final VpnRoute route, final VpnRouteKey vpnRouteKey,
191             final long pathId, final Attributes attributes) {
192         final VpnRouteBuilder builder;
193         if (route != null) {
194             builder = new VpnRouteBuilder(route);
195         } else {
196             builder = new VpnRouteBuilder();
197         }
198         return builder.setRouteKey(vpnRouteKey.getRouteKey()).setAttributes(attributes).build();
199     }
200 }