0099ac6842ada1c1a6bce4081ee041ba794adaf5
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / EvpnRibSupport.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.evpn.impl;
9
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.rev171213.EvpnSubsequentAddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.L2vpnAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.destination.EvpnDestination;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.EvpnRoutes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.evpn.routes.EvpnRoute;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.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.rev171213.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.rev171213.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.rev171207.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;
47
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);
53
54     private EvpnRibSupport() {
55         super(EvpnRoutesCase.class, EvpnRoutes.class, EvpnRoute.class,
56                 L2vpnAddressFamily.class, EvpnSubsequentAddressFamily.class, DestinationEvpn.QNAME);
57     }
58
59     static EvpnRibSupport getInstance() {
60         return SINGLETON;
61     }
62
63     @Nonnull
64     @Override
65     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
66         return ImmutableSet.of();
67     }
68
69     @Nonnull
70     @Override
71     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
72         return ImmutableSet.of();
73     }
74
75     @Override
76     public boolean isComplexRoute() {
77         return true;
78     }
79
80     @Nonnull
81     @Override
82     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
83         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update
84                 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder()
85                 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
86                         .rev171213.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination
87                         .evpn._case.DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
88     }
89
90     @Nonnull
91     @Override
92     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
93         return new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder()
94                 .setEvpnDestination(extractRoutes(routes)).build()).build();
95     }
96
97     private static List<EvpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
98         return routes.stream().map(EvpnNlriParser::extractEvpnDestination).collect(Collectors.toList());
99     }
100
101     @Override
102     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
103         final ContainerNode destination, final ContainerNode attributes, 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 = routesPath.node(routesContainerIdentifier()).node(routeNid());
111                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
112                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
113                         function.apply(tx, base, routeKey, e, attributes);
114                     }
115                 } else {
116                     LOG.warn("Routes {} are not a map", routes);
117                 }
118             }
119         }
120     }
121
122     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode evpn) {
123         final ByteBuf buffer = Unpooled.buffer();
124         final EvpnDestination dest = EvpnNlriParser.extractRouteKeyDestination(evpn);
125         EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
126         return new NodeIdentifierWithPredicates(routeQName(), ROUTE_KEY, ByteArray.encodeBase64(buffer));
127     }
128
129 }