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