2 * Copyright (c) 2016 Cisco 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.evpn.impl;
10 import com.google.common.collect.ImmutableCollection;
11 import com.google.common.collect.ImmutableSet;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
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.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser;
21 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
22 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.EvpnSubsequentAddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.L2vpnAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.bgp.rib.rib.peer.effective.rib.in.tables.routes.EvpnRoutesCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestination;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.EvpnRoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRoute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.EvpnRouteKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpn;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
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.MapEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 final class EvpnRibSupport extends AbstractRIBSupport<EvpnRoute, EvpnRouteKey> {
56 private static final EvpnRibSupport SINGLETON = new EvpnRibSupport();
57 private static final Logger LOG = LoggerFactory.getLogger(EvpnRibSupport.class);
58 private static final QName ROUTE_KEY_QNAME = QName.create(EvpnRoute.QNAME, ROUTE_KEY).intern();
59 private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(EvpnDestination.QNAME);
61 private EvpnRibSupport() {
62 super(EvpnRoutesCase.class, EvpnRoutes.class, EvpnRoute.class, L2vpnAddressFamily.class,
63 EvpnSubsequentAddressFamily.class, DestinationEvpn.QNAME);
66 static EvpnRibSupport getInstance() {
71 public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
72 return ImmutableSet.of();
76 public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
77 return ImmutableSet.of();
81 public boolean isComplexRoute() {
86 protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
87 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
88 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder()
89 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
90 .rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination
91 .evpn._case.DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
95 protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
96 return new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder()
97 .setEvpnDestination(extractRoutes(routes)).build()).build();
100 private static List<EvpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
101 return routes.stream().map(EvpnNlriParser::extractEvpnDestination).collect(Collectors.toList());
105 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
106 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
107 if (destination != null) {
108 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
109 .getChild(NLRI_ROUTES_LIST);
110 if (maybeRoutes.isPresent()) {
111 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
112 if (routes instanceof UnkeyedListNode) {
113 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
114 for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
115 final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
116 function.apply(tx, base, routeKey, e, attributes);
119 LOG.warn("Routes {} are not a map", routes);
125 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode evpn) {
126 final ByteBuf buffer = Unpooled.buffer();
127 final EvpnDestination dest = EvpnNlriParser.extractRouteKeyDestination(evpn);
128 EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
129 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
130 evpn.getChild(routePathIdNid());
131 return PathIdUtil.createNidKey(routeQName(), ROUTE_KEY_QNAME,
132 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
136 public EvpnRoute createRoute(final EvpnRoute route, final String routeKey, final long pathId,
137 final Attributes attributes) {
138 final EvpnRouteBuilder builder;
140 builder = new EvpnRouteBuilder(route);
142 builder = new EvpnRouteBuilder();
144 return builder.setKey(new EvpnRouteKey(new PathId(pathId), routeKey)).setAttributes(attributes).build();
148 public Routes emptyRoutesContainer() {
149 return new EvpnRoutesCaseBuilder().setEvpnRoutes(new EvpnRoutesBuilder().build()).build();
153 public EvpnRouteKey createRouteListKey(final long pathId, final String routeKey) {
154 return new EvpnRouteKey(new PathId(pathId), routeKey);