8ce20f7dac40efc0d6dcfb5828716ee404b2345c
[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.base.Preconditions;
12 import com.google.common.collect.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.stream.Collectors;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
24 import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastIpv4RIBSupport;
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.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.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
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(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass, final Class<? extends Route> listClass,
73         final 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         this.routeKey = QName.create(routeQName(), "route-key").intern();
77         final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
78         this.nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
79         this.prefixTypeNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "prefix").intern());
80         this.labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
81         this.lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
82         this.rdNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "route-distinguisher").intern());
83     }
84
85     private VpnDestination extractVpnDestination(final DataContainerNode<? extends PathArgument> route) {
86         final VpnDestination dst = new VpnDestinationBuilder()
87             .setPrefix(extractPrefix(route, this.prefixTypeNid))
88             .setLabelStack(LabeledUnicastIpv4RIBSupport.extractLabel(route, this.labelStackNid, this.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(this.rdNid).isPresent()) {
98             return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(this.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                     final 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 key = createRouteKey(e);
153                         LOG.debug("Route {} is processed.", key);
154                         function.apply(tx, base, key, 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         final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
168             .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
169         final ByteBuf nlriByteBuf = Unpooled.buffer();
170
171         for (final VpnDestination dest : Collections.singletonList(dests)) {
172             final IpPrefix prefix = dest.getPrefix();
173             LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
174             AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
175             RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
176             Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null, "Ipv6 or Ipv4 prefix is missing.");
177             LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
178         }
179         buffer.writeBytes(nlriByteBuf);
180
181         return new NodeIdentifierWithPredicates(routeQName(), this.routeKey, ByteArray.encodeBase64(buffer));
182     }
183 }