2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.l3vpn;
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 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;
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;
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.
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;
72 protected AbstractVpnRIBSupport(final Class<? extends Routes> cazeClass,
73 final Class<? extends DataObject> containerClass, final Class<? extends Route> listClass,
74 final Class<? extends AddressFamily> afiClass, final QName vpnDstContainerClassQname) {
75 super(cazeClass, containerClass, listClass, afiClass,
76 MplsLabeledVpnSubsequentAddressFamily.class, vpnDstContainerClassQname);
77 final QName classQname = BindingReflections.findQName(containerClass).intern();
78 this.routeKey = QName.create(routeQName(), ROUTE_KEY).intern();
79 final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
80 this.nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
81 this.prefixTypeNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "prefix").intern());
82 this.labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
83 this.lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
84 this.rdNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "route-distinguisher").intern());
87 private VpnDestination extractVpnDestination(final DataContainerNode<? extends PathArgument> route) {
88 final VpnDestination dst = new VpnDestinationBuilder()
89 .setPrefix(extractPrefix(route, this.prefixTypeNid))
90 .setLabelStack(LabeledUnicastIpv4RIBSupport.extractLabel(route, this.labelStackNid, this.lvNid))
91 .setRouteDistinguisher(extractRouteDistinguisher(route))
96 protected abstract IpPrefix extractPrefix(DataContainerNode<? extends PathArgument> route,
97 NodeIdentifier prefixTypeNid);
99 private RouteDistinguisher extractRouteDistinguisher(
100 final DataContainerNode<? extends YangInstanceIdentifier.PathArgument> route) {
101 if (route.getChild(this.rdNid).isPresent()) {
102 return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(this.rdNid).get().getValue());
107 protected abstract DestinationType getAdvertisedDestinationType(List<VpnDestination> dests);
109 protected abstract DestinationType getWithdrawnDestinationType(List<VpnDestination> dests);
113 protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
114 return getAdvertisedDestinationType(extractRoutes(routes));
119 protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
120 return getWithdrawnDestinationType(extractRoutes(routes));
123 private List<VpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
124 return routes.stream().map(this::extractVpnDestination).collect(Collectors.toList());
129 public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
130 return ImmutableSet.of();
135 public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
136 return ImmutableSet.of();
140 public boolean isComplexRoute() {
145 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
146 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
147 if (destination != null) {
148 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes =
149 destination.getChild(this.nlriRoutesListNid);
150 if (maybeRoutes.isPresent()) {
151 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
152 if (routes instanceof UnkeyedListNode) {
153 final UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
154 LOG.debug("{} routes are found", routeListNode.getSize());
155 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
156 for (final UnkeyedListEntryNode e : routeListNode.getValue()) {
157 final NodeIdentifierWithPredicates key = createRouteKey(e);
158 LOG.debug("Route {} is processed.", key);
159 function.apply(tx, base, key, e, attributes);
162 LOG.warn("Routes {} are not a map", routes);
166 LOG.debug("Destination is null.");
170 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
171 final ByteBuf buffer = Unpooled.buffer();
172 final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
173 .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
174 final ByteBuf nlriByteBuf = Unpooled.buffer();
176 for (final VpnDestination dest : Collections.singletonList(dests)) {
177 final IpPrefix prefix = dest.getPrefix();
178 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
179 AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
180 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
181 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
182 "Ipv6 or Ipv4 prefix is missing.");
183 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
185 buffer.writeBytes(nlriByteBuf);
187 return new NodeIdentifierWithPredicates(routeQName(), this.routeKey, ByteArray.encodeBase64(buffer));