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 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.rib.spi.AbstractRIBSupport;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.route.VpnRoute;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.route.VpnRouteBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.route.VpnRouteKey;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
47 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public abstract class AbstractVpnRIBSupport extends AbstractRIBSupport<VpnRoute, VpnRouteKey> {
57 private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnRIBSupport.class);
58 private final NodeIdentifier nlriRoutesListNid;
59 private final NodeIdentifier prefixTypeNid;
60 private final NodeIdentifier labelStackNid;
61 private final NodeIdentifier lvNid;
62 private final NodeIdentifier rdNid;
63 private final QName routeKey;
66 * Default constructor. Requires the QName of the container augmented under the routes choice
67 * node in instantiations of the rib grouping. It is assumed that this container is defined by
68 * the same model which populates it with route grouping instantiation, and by extension with
69 * the route attributes container.
71 * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
72 * @param containerClass Binding class of the container in routes choice, must not be null.
73 * @param listClass Binding class of the route list, nust not be null;
75 protected AbstractVpnRIBSupport(final Class<? extends Routes> cazeClass,
76 final Class<? extends DataObject> containerClass, final Class<? extends Route> listClass,
77 final Class<? extends AddressFamily> afiClass, final QName vpnDstContainerClassQname) {
78 super(cazeClass, containerClass, listClass, afiClass,
79 MplsLabeledVpnSubsequentAddressFamily.class, vpnDstContainerClassQname);
80 final QName classQname = BindingReflections.findQName(containerClass).intern();
81 this.routeKey = QName.create(routeQName(), ROUTE_KEY).intern();
82 final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
83 this.nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
84 this.prefixTypeNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "prefix").intern());
85 this.labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
86 this.lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
87 this.rdNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "route-distinguisher").intern());
90 private VpnDestination extractVpnDestination(final DataContainerNode<? extends PathArgument> route) {
91 final VpnDestination dst = new VpnDestinationBuilder()
92 .setPrefix(extractPrefix(route, this.prefixTypeNid))
93 .setLabelStack(LabeledUnicastIpv4RIBSupport.extractLabel(route, this.labelStackNid, this.lvNid))
94 .setRouteDistinguisher(extractRouteDistinguisher(route))
99 protected abstract IpPrefix extractPrefix(DataContainerNode<? extends PathArgument> route,
100 NodeIdentifier prefixTypeNid);
102 private RouteDistinguisher extractRouteDistinguisher(
103 final DataContainerNode<? extends YangInstanceIdentifier.PathArgument> route) {
104 if (route.getChild(this.rdNid).isPresent()) {
105 return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(this.rdNid).get().getValue());
110 protected abstract DestinationType getAdvertisedDestinationType(List<VpnDestination> dests);
112 protected abstract DestinationType getWithdrawnDestinationType(List<VpnDestination> dests);
115 protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
116 return getAdvertisedDestinationType(extractRoutes(routes));
120 protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
121 return getWithdrawnDestinationType(extractRoutes(routes));
124 private List<VpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
125 return routes.stream().map(this::extractVpnDestination).collect(Collectors.toList());
129 public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
130 return ImmutableSet.of();
134 public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
135 return ImmutableSet.of();
139 public boolean isComplexRoute() {
144 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
145 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
146 if (destination != null) {
147 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes =
148 destination.getChild(this.nlriRoutesListNid);
149 if (maybeRoutes.isPresent()) {
150 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
151 if (routes instanceof UnkeyedListNode) {
152 final UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
153 LOG.debug("{} routes are found", routeListNode.getSize());
154 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
155 for (final UnkeyedListEntryNode e : routeListNode.getValue()) {
156 final NodeIdentifierWithPredicates key = createRouteKey(e);
157 LOG.debug("Route {} is processed.", key);
158 function.apply(tx, base, key, e, attributes);
161 LOG.warn("Routes {} are not a map", routes);
165 LOG.debug("Destination is null.");
169 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
170 final ByteBuf buffer = Unpooled.buffer();
171 final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
172 .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
173 final ByteBuf nlriByteBuf = Unpooled.buffer();
175 for (final VpnDestination dest : Collections.singletonList(dests)) {
176 final IpPrefix prefix = dest.getPrefix();
177 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
178 AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
179 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
180 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
181 "Ipv6 or Ipv4 prefix is missing.");
182 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
184 buffer.writeBytes(nlriByteBuf);
186 return new NodeIdentifierWithPredicates(routeQName(), this.routeKey, ByteArray.encodeBase64(buffer));
190 public final VpnRoute createRoute(final VpnRoute route, final VpnRouteKey vpnRouteKey,
191 final long pathId, final Attributes attributes) {
192 final VpnRouteBuilder builder;
194 builder = new VpnRouteBuilder(route);
196 builder = new VpnRouteBuilder();
198 return builder.setRouteKey(vpnRouteKey.getRouteKey()).setAttributes(attributes).build();