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