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 io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.util.ArrayList;
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.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
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.loc.rib.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.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.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 final class EvpnRibSupport extends AbstractRIBSupport<EvpnRoutesCase, EvpnRoutes, EvpnRoute, EvpnRouteKey> {
53 private static final Logger LOG = LoggerFactory.getLogger(EvpnRibSupport.class);
55 private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(EvpnDestination.QNAME);
56 private static final EvpnRoutes EMPTY_CONTAINER
57 = new EvpnRoutesBuilder().setEvpnRoute(Collections.emptyList()).build();
58 private static final EvpnRoutesCase EMPTY_CASE =
59 new EvpnRoutesCaseBuilder().setEvpnRoutes(EMPTY_CONTAINER).build();
60 private static EvpnRibSupport SINGLETON;
62 private EvpnRibSupport(final BindingNormalizedNodeSerializer mappingService) {
67 L2vpnAddressFamily.class,
68 EvpnSubsequentAddressFamily.class,
69 DestinationEvpn.QNAME);
72 static synchronized EvpnRibSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
73 if (SINGLETON == null) {
74 SINGLETON = new EvpnRibSupport(mappingService);
80 protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
81 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
82 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder()
83 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
84 .rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination
85 .evpn._case.DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
89 protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
90 return new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder()
91 .setEvpnDestination(extractRoutes(routes)).build()).build();
94 private static List<EvpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
95 return routes.stream().map(EvpnNlriParser::extractEvpnDestination).collect(Collectors.toList());
99 protected Collection<NodeIdentifierWithPredicates> processDestination(final DOMDataWriteTransaction tx,
100 final YangInstanceIdentifier routesPath,
101 final ContainerNode destination,
102 final ContainerNode attributes,
103 final ApplyRoute function) {
104 if (destination != null) {
105 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
106 .getChild(NLRI_ROUTES_LIST);
107 if (maybeRoutes.isPresent()) {
108 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
109 if (routes instanceof UnkeyedListNode) {
110 final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
111 final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
112 final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
113 for (final UnkeyedListEntryNode evpnDest : routesList) {
114 final NodeIdentifierWithPredicates routeKey = createRouteKey(evpnDest);
115 function.apply(tx, base, routeKey, evpnDest, attributes);
120 LOG.warn("Routes {} are not a map", routes);
124 return Collections.emptyList();
127 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode evpn) {
128 final ByteBuf buffer = Unpooled.buffer();
129 final EvpnDestination dest = EvpnNlriParser.extractRouteKeyDestination(evpn);
130 EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
131 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
132 evpn.getChild(routePathIdNid());
133 return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
134 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
138 public EvpnRoute createRoute(final EvpnRoute route, final String routeKey, final long pathId,
139 final Attributes attributes) {
140 final EvpnRouteBuilder builder;
142 builder = new EvpnRouteBuilder(route);
144 builder = new EvpnRouteBuilder();
146 return builder.withKey(createRouteListKey(pathId, routeKey)).setAttributes(attributes).build();
150 public EvpnRoutesCase emptyRoutesCase() {
155 public EvpnRoutes emptyRoutesContainer() {
156 return EMPTY_CONTAINER;
160 public EvpnRouteKey createRouteListKey(final long pathId, final String routeKey) {
161 return new EvpnRouteKey(new PathId(pathId), routeKey);
165 public List<EvpnRoute> routesFromContainer(final EvpnRoutes container) {
166 return container.getEvpnRoute();