Rib support refactoring II
[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.Optional;
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.stream.Collectors;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastRIBSupport;
22 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev160413.l3vpn.ip.destination.type.VpnDestinationBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * @author Kevin Wang
51  */
52 public abstract class AbstractVpnRIBSupport extends AbstractRIBSupport {
53     private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnRIBSupport.class);
54     private final NodeIdentifier nlriRoutesListNid;
55     private final NodeIdentifier prefixTypeNid;
56     private final NodeIdentifier labelStackNid;
57     private final NodeIdentifier lvNid;
58     private final NodeIdentifier rdNid;
59     private final QName routeKey;
60
61     /**
62      * Default constructor. Requires the QName of the container augmented under the routes choice
63      * node in instantiations of the rib grouping. It is assumed that this container is defined by
64      * the same model which populates it with route grouping instantiation, and by extension with
65      * the route attributes container.
66      *
67      * @param cazeClass      Binding class of the AFI/SAFI-specific case statement, must not be null
68      * @param containerClass Binding class of the container in routes choice, must not be null.
69      * @param listClass      Binding class of the route list, nust not be null;
70      */
71     protected AbstractVpnRIBSupport(Class<? extends Routes> cazeClass, Class<? extends DataObject> containerClass, Class<? extends Route> listClass,
72         Class<? extends AddressFamily> afiClass, final QName vpnDstContainerClassQname) {
73         super(cazeClass, containerClass, listClass, afiClass, MplsLabeledVpnSubsequentAddressFamily.class, vpnDstContainerClassQname);
74         final QName classQname = BindingReflections.findQName(containerClass).intern();
75         routeKey = QName.create(routeQName(), "route-key").intern();
76         final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
77         nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
78         prefixTypeNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "prefix").intern());
79         labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
80         lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
81         rdNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "route-distinguisher").intern());
82     }
83
84     private VpnDestination extractVpnDestination(DataContainerNode<? extends PathArgument> route) {
85         final VpnDestination dst = new VpnDestinationBuilder()
86             .setPrefix(LabeledUnicastRIBSupport.extractPrefix(route, prefixTypeNid))
87             .setLabelStack(LabeledUnicastRIBSupport.extractLabel(route, labelStackNid, lvNid))
88             .setRouteDistinguisher(extractRouteDistinguisher(route))
89             .build();
90         return dst;
91     }
92
93     private RouteDistinguisher extractRouteDistinguisher(final DataContainerNode<? extends YangInstanceIdentifier.PathArgument> route) {
94         if (route.getChild(rdNid).isPresent()) {
95             return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(rdNid).get().getValue());
96         }
97         return null;
98     }
99
100     protected abstract DestinationType getAdvertisedDestinationType(List<VpnDestination> dests);
101
102     protected abstract DestinationType getWithdrawnDestinationType(List<VpnDestination> dests);
103
104     @Nonnull
105     @Override
106     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
107         return getAdvertisedDestinationType(extractRoutes(routes));
108     }
109
110     @Nonnull
111     @Override
112     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
113         return getWithdrawnDestinationType(extractRoutes(routes));
114     }
115
116     private List<VpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
117         return routes.stream().map(this::extractVpnDestination).collect(Collectors.toList());
118     }
119
120     @Nonnull
121     @Override
122     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
123         return ImmutableSet.of();
124     }
125
126     @Nonnull
127     @Override
128     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
129         return ImmutableSet.of();
130     }
131
132     @Override
133     public boolean isComplexRoute() {
134         return true;
135     }
136
137     @Override
138     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
139                                     final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
140         if (destination != null) {
141             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(this.nlriRoutesListNid);
142             if (maybeRoutes.isPresent()) {
143                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
144                 if (routes instanceof UnkeyedListNode) {
145                     UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
146                     LOG.debug("{} routes are found", routeListNode.getSize());
147                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
148                     for (final UnkeyedListEntryNode e : routeListNode.getValue()) {
149                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
150                         LOG.debug("Route {} is processed.", routeKey);
151                         function.apply(tx, base, routeKey, e, attributes);
152                     }
153                 } else {
154                     LOG.warn("Routes {} are not a map", routes);
155                 }
156             }
157         } else {
158             LOG.debug("Destination is null.");
159         }
160     }
161
162     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
163         final ByteBuf buffer = Unpooled.buffer();
164
165         final VpnDestination dest = extractVpnDestination(l3vpn);
166         AbstractVpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
167         return new NodeIdentifierWithPredicates(routeQName(), this.routeKey, ByteArray.readAllBytes(buffer));
168     }
169 }