BGPCEP-781: Provide Add Path support for all AFI/SAFI
[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.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.rev171207.PathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.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;
54
55 final class EvpnRibSupport extends AbstractRIBSupport<EvpnRoute, EvpnRouteKey> {
56     private static final EvpnRibSupport SINGLETON = new EvpnRibSupport();
57     private static final String ROUTE_KEY = "route-key";
58     private static final Logger LOG = LoggerFactory.getLogger(EvpnRibSupport.class);
59     private static final QName ROUTE_KEY_QNAME = QName.create(EvpnRoute.QNAME, ROUTE_KEY).intern();
60     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(EvpnDestination.QNAME);
61
62     private EvpnRibSupport() {
63         super(EvpnRoutesCase.class, EvpnRoutes.class, EvpnRoute.class, L2vpnAddressFamily.class,
64                 EvpnSubsequentAddressFamily.class, ROUTE_KEY, DestinationEvpn.QNAME);
65     }
66
67     static EvpnRibSupport getInstance() {
68         return SINGLETON;
69     }
70
71     @Override
72     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
73         return ImmutableSet.of();
74     }
75
76     @Override
77     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
78         return ImmutableSet.of();
79     }
80
81     @Override
82     public boolean isComplexRoute() {
83         return true;
84     }
85
86     @Override
87     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
88         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update
89                 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder()
90                 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
91                         .rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination
92                         .evpn._case.DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
93     }
94
95     @Override
96     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
97         return new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder()
98                 .setEvpnDestination(extractRoutes(routes)).build()).build();
99     }
100
101     private static List<EvpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
102         return routes.stream().map(EvpnNlriParser::extractEvpnDestination).collect(Collectors.toList());
103     }
104
105     @Override
106     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
107         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
108         if (destination != null) {
109             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
110                     .getChild(NLRI_ROUTES_LIST);
111             if (maybeRoutes.isPresent()) {
112                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
113                 if (routes instanceof UnkeyedListNode) {
114                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
115                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
116                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
117                         function.apply(tx, base, routeKey, e, attributes);
118                     }
119                 } else {
120                     LOG.warn("Routes {} are not a map", routes);
121                 }
122             }
123         }
124     }
125
126     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode evpn) {
127         final ByteBuf buffer = Unpooled.buffer();
128         final EvpnDestination dest = EvpnNlriParser.extractRouteKeyDestination(evpn);
129         EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
130         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
131                 evpn.getChild(routePathIdNid());
132         return PathIdUtil.createNidKey(routeQName(), ROUTE_KEY_QNAME,
133                 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
134     }
135
136     @Override
137     public EvpnRoute createRoute(final EvpnRoute route, final EvpnRouteKey routeKey, final long pathId,
138             final Attributes attributes) {
139         final EvpnRouteBuilder builder;
140         if (route != null) {
141             builder = new EvpnRouteBuilder(route);
142         } else {
143             builder = new EvpnRouteBuilder();
144         }
145         return builder.setRouteKey(routeKey.getRouteKey())
146                 .setPathId(new PathId(pathId)).setAttributes(attributes).build();
147     }
148
149     @Override
150     public Routes emptyRoutesContainer() {
151         return new EvpnRoutesCaseBuilder().setEvpnRoutes(new EvpnRoutesBuilder().build()).build();
152     }
153
154     @Override
155     public EvpnRouteKey createNewRouteKey(final long pathId, final EvpnRouteKey routeKey) {
156         return new EvpnRouteKey(new PathId(pathId), routeKey.getRouteKey());
157     }
158 }