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 io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.stream.Collectors;
18 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
21 import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastIpv4RIBSupport;
22 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
23 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.MplsLabeledVpnSubsequentAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisherBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRoute;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRouteBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.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 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
130 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
131 if (destination != null) {
132 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes =
133 destination.getChild(this.nlriRoutesListNid);
134 if (maybeRoutes.isPresent()) {
135 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
136 if (routes instanceof UnkeyedListNode) {
137 final UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
138 LOG.debug("{} routes are found", routeListNode.getSize());
139 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
140 for (final UnkeyedListEntryNode e : routeListNode.getValue()) {
141 final NodeIdentifierWithPredicates key = createRouteKey(e);
142 LOG.debug("Route {} is processed.", key);
143 function.apply(tx, base, key, e, attributes);
146 LOG.warn("Routes {} are not a map", routes);
150 LOG.debug("Destination is null.");
154 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
155 final ByteBuf buffer = Unpooled.buffer();
156 final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
157 .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
158 final ByteBuf nlriByteBuf = Unpooled.buffer();
160 for (final VpnDestination dest : Collections.singletonList(dests)) {
161 final IpPrefix prefix = dest.getPrefix();
162 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
163 AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
164 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
165 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
166 "Ipv6 or Ipv4 prefix is missing.");
167 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
169 buffer.writeBytes(nlriByteBuf);
171 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
172 l3vpn.getChild(routePathIdNid());
173 return PathIdUtil.createNidKey(routeQName(), this.routeKey,
174 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
178 public final VpnRoute createRoute(final VpnRoute route, final String vpnRouteKey,
179 final long pathId, final Attributes attributes) {
180 final VpnRouteBuilder builder;
182 builder = new VpnRouteBuilder(route);
184 builder = new VpnRouteBuilder();
186 return builder.setKey(new VpnRouteKey(new PathId(pathId), vpnRouteKey)).setAttributes(attributes).build();
190 public VpnRouteKey createRouteListKey(final long pathId, final String vpnRouteKey) {
191 return new VpnRouteKey(new PathId(pathId), vpnRouteKey);