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.unicast;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
23 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
24 import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastIpv4RIBSupport;
25 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
26 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.MplsLabeledVpnSubsequentAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.L3vpnIpRoute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRoute;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRouteBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRouteKey;
41 import org.opendaylight.yangtools.yang.binding.ChildOf;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public abstract class AbstractVpnRIBSupport<C extends Routes & DataObject, S extends ChildOf<? super C> & L3vpnIpRoute>
58 extends AbstractRIBSupport<C, S, VpnRoute, VpnRouteKey> {
59 private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnRIBSupport.class);
60 private final NodeIdentifier nlriRoutesListNid;
61 private final NodeIdentifier labelStackNid;
62 private final NodeIdentifier lvNid;
65 * Default constructor. Requires the QName of the container augmented under the routes choice
66 * node in instantiations of the rib grouping. It is assumed that this container is defined by
67 * the same model which populates it with route grouping instantiation, and by extension with
68 * the route attributes container.
70 * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
71 * @param containerClass Binding class of the container in routes choice, must not be null.
73 protected AbstractVpnRIBSupport(
74 final BindingNormalizedNodeSerializer mappingService,
75 final Class<C> cazeClass,
76 final Class<S> containerClass,
77 final Class<? extends AddressFamily> afiClass,
78 final QName vpnDstContainerClassQname) {
79 super(mappingService, cazeClass, containerClass, VpnRoute.class, afiClass,
80 MplsLabeledVpnSubsequentAddressFamily.class, vpnDstContainerClassQname);
81 final QName classQname = BindingReflections.findQName(containerClass).intern();
82 final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
83 this.nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
84 this.labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
85 this.lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
88 private VpnDestination extractVpnDestination(final DataContainerNode<? extends PathArgument> route) {
89 return new VpnDestinationBuilder()
90 .setPrefix(createPrefix(extractPrefix(route)))
91 .setLabelStack(LabeledUnicastIpv4RIBSupport.extractLabel(route, this.labelStackNid, this.lvNid))
92 .setRouteDistinguisher(extractRouteDistinguisher(route))
93 .setPathId(PathIdUtil.buildPathId(route, routePathIdNid()))
97 protected abstract IpPrefix createPrefix(String prefix);
99 protected abstract DestinationType getAdvertisedDestinationType(List<VpnDestination> dests);
101 protected abstract DestinationType getWithdrawnDestinationType(List<VpnDestination> dests);
104 protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
105 return getAdvertisedDestinationType(extractRoutes(routes));
109 protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
110 return getWithdrawnDestinationType(extractRoutes(routes));
113 private List<VpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
114 return routes.stream().map(this::extractVpnDestination).collect(Collectors.toList());
118 protected Collection<NodeIdentifierWithPredicates> processDestination(final DOMDataTreeWriteTransaction tx,
119 final YangInstanceIdentifier routesPath,
120 final ContainerNode destination,
121 final ContainerNode attributes,
122 final ApplyRoute function) {
123 if (destination != null) {
124 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes =
125 destination.getChild(this.nlriRoutesListNid);
126 if (maybeRoutes.isPresent()) {
127 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
128 if (routes instanceof UnkeyedListNode) {
129 final UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
130 LOG.debug("{} routes are found", routeListNode.getSize());
131 final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
132 final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
133 final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
134 for (final UnkeyedListEntryNode vpnDest : routesList) {
135 final NodeIdentifierWithPredicates key = createRouteKey(vpnDest);
136 LOG.debug("Route {} is processed.", key);
137 function.apply(tx, base, key, vpnDest, attributes);
142 LOG.warn("Routes {} are not a map", routes);
146 LOG.debug("Destination is null.");
148 return Collections.emptyList();
151 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
152 final ByteBuf buffer = Unpooled.buffer();
153 final VpnDestination dests = new VpnDestinationBuilder().setPrefix(createPrefix(extractPrefix(l3vpn)))
154 .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
155 final ByteBuf nlriByteBuf = Unpooled.buffer();
157 for (final VpnDestination dest : Collections.singletonList(dests)) {
158 final IpPrefix prefix = dest.getPrefix();
159 LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
160 AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
161 RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
162 Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
163 "Ipv6 or Ipv4 prefix is missing.");
164 LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
166 buffer.writeBytes(nlriByteBuf);
168 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
169 l3vpn.getChild(routePathIdNid());
170 return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
171 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
175 public final VpnRoute createRoute(final VpnRoute route, final VpnRouteKey key, final Attributes attributes) {
176 final VpnRouteBuilder builder;
178 builder = new VpnRouteBuilder(route);
180 builder = new VpnRouteBuilder();
182 return builder.withKey(key).setAttributes(attributes).build();
186 public VpnRouteKey createRouteListKey(final PathId pathId, final String vpnRouteKey) {
187 return new VpnRouteKey(pathId, vpnRouteKey);
191 public PathId extractPathId(final VpnRouteKey routeListKey) {
192 return routeListKey.getPathId();
196 public final String extractRouteKey(final VpnRouteKey routeListKey) {
197 return routeListKey.getRouteKey();