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.base.Optional;
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.stream.Collectors;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser;
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.rev160321.EvpnSubsequentAddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.L2vpnAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.application.rib.tables.routes.EvpnRoutesCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.destination.EvpnDestination;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.routes.EvpnRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.routes.evpn.routes.EvpnRoute;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpn;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 final class EvpnRibSupport extends AbstractRIBSupport {
49 private static final EvpnRibSupport SINGLETON = new EvpnRibSupport();
50 private static final Logger LOG = LoggerFactory.getLogger(EvpnRibSupport.class);
51 private static final QName ROUTE_KEY = QName.create(EvpnRoute.QNAME, "route-key").intern();
52 private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(EvpnDestination.QNAME);
54 private EvpnRibSupport() {
55 super(EvpnRoutesCase.class, EvpnRoutes.class, EvpnRoute.class, L2vpnAddressFamily.class, EvpnSubsequentAddressFamily.class, DestinationEvpn.QNAME);
58 static EvpnRibSupport getInstance() {
64 public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
65 return ImmutableSet.of();
70 public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
71 return ImmutableSet.of();
75 public boolean isComplexRoute() {
81 protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
82 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.update.attributes.mp.reach.nlri.advertized
83 .routes.destination.type.DestinationEvpnCaseBuilder().setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.
84 ns.yang.bgp.evpn.rev160321.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.
85 DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
90 protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
91 return new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
94 private List<EvpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
95 return routes.stream().map(EvpnNlriParser::extractEvpnDestination).collect(Collectors.toList());
99 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
100 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
101 if (destination != null) {
102 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
103 if (maybeRoutes.isPresent()) {
104 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
105 if (routes instanceof UnkeyedListNode) {
106 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
107 for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
108 final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
109 function.apply(tx, base, routeKey, e, attributes);
112 LOG.warn("Routes {} are not a map", routes);
118 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode evpn) {
119 final ByteBuf buffer = Unpooled.buffer();
120 final EvpnDestination dest = EvpnNlriParser.extractRouteKeyDestination(evpn);
121 EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
122 return new NodeIdentifierWithPredicates(routeQName(), ROUTE_KEY, ByteArray.readAllBytes(buffer));