Update MRI projects for Aluminium
[bgpcep.git] / bgp / extensions / 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 static com.google.common.base.Verify.verify;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
23 import org.opendaylight.protocol.bgp.evpn.impl.nlri.EvpnNlriParser;
24 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
25 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.EvpnSubsequentAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.L2vpnAddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.bgp.rib.rib.loc.rib.tables.routes.EvpnRoutesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestination;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.EvpnRoutes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.EvpnRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRoute;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRouteBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.routes.evpn.routes.EvpnRouteKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpn;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
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<EvpnRoutesCase, EvpnRoutes, EvpnRoute, EvpnRouteKey> {
56     private static final Logger LOG = LoggerFactory.getLogger(EvpnRibSupport.class);
57
58     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(EvpnDestination.QNAME);
59     private static final EvpnRoutes EMPTY_CONTAINER
60             =  new EvpnRoutesBuilder().setEvpnRoute(Collections.emptyList()).build();
61     private static EvpnRibSupport SINGLETON;
62
63     private EvpnRibSupport(final BindingNormalizedNodeSerializer mappingService) {
64         super(mappingService,
65                 EvpnRoutesCase.class,
66                 EvpnRoutes.class,
67                 EvpnRoute.class,
68                 L2vpnAddressFamily.class,
69                 EvpnSubsequentAddressFamily.class,
70                 DestinationEvpn.QNAME);
71     }
72
73     static synchronized EvpnRibSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
74         if (SINGLETON == null) {
75             SINGLETON = new EvpnRibSupport(mappingService);
76         }
77         return SINGLETON;
78     }
79
80     @Override
81     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
82         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update
83                 .attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder()
84                 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
85                     .bgp.evpn.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination
86                         .evpn._case.DestinationEvpnBuilder().setEvpnDestination(extractRoutes(routes)).build()).build();
87     }
88
89     @Override
90     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
91         return new DestinationEvpnCaseBuilder().setDestinationEvpn(new DestinationEvpnBuilder()
92                 .setEvpnDestination(extractRoutes(routes)).build()).build();
93     }
94
95     private static List<EvpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
96         return routes.stream().map(EvpnNlriParser::extractEvpnDestination).collect(Collectors.toList());
97     }
98
99     @Override
100     protected Collection<NodeIdentifierWithPredicates> processDestination(final DOMDataTreeWriteTransaction tx,
101                                                                           final YangInstanceIdentifier routesPath,
102                                                                           final ContainerNode destination,
103                                                                           final ContainerNode attributes,
104                                                                           final ApplyRoute function) {
105         if (destination != null) {
106             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
107                     .getChild(NLRI_ROUTES_LIST);
108             if (maybeRoutes.isPresent()) {
109                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
110                 if (routes instanceof UnkeyedListNode) {
111                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
112                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
113                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
114                     for (final UnkeyedListEntryNode evpnDest : routesList) {
115                         final NodeIdentifierWithPredicates routeKey = createRouteKey(evpnDest);
116                         function.apply(tx, base, routeKey, evpnDest, attributes);
117                         keys.add(routeKey);
118                     }
119                     return keys;
120                 } else {
121                     LOG.warn("Routes {} are not a map", routes);
122                 }
123             }
124         }
125         return Collections.emptyList();
126     }
127
128     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode evpn) {
129         final ByteBuf buffer = Unpooled.buffer();
130         final EvpnDestination dest = EvpnNlriParser.extractRouteKeyDestination(evpn);
131         EvpnNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
132         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
133                 evpn.getChild(routePathIdNid());
134         return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
135                 ByteArray.encodeBase64(buffer), maybePathIdLeaf);
136     }
137
138     @Override
139     public EvpnRoute createRoute(final EvpnRoute route, final EvpnRouteKey key, final Attributes attributes) {
140         final EvpnRouteBuilder builder;
141         if (route != null) {
142             builder = new EvpnRouteBuilder(route);
143         } else {
144             builder = new EvpnRouteBuilder();
145         }
146         return builder.withKey(key).setAttributes(attributes).build();
147     }
148
149     @Override
150     public EvpnRoutes emptyRoutesContainer() {
151         return EMPTY_CONTAINER;
152     }
153
154     @Override
155     public EvpnRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
156         return new EvpnRouteKey(pathId, routeKey);
157     }
158
159     @Override
160     public PathId extractPathId(final EvpnRouteKey routeListKey) {
161         return routeListKey.getPathId();
162     }
163
164     @Override
165     public String extractRouteKey(final EvpnRouteKey routeListKey) {
166         return routeListKey.getRouteKey();
167     }
168
169     @Override
170     public Map<EvpnRouteKey, EvpnRoute> extractAdjRibInRoutes(final Routes routes) {
171         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120
172             .bgp.rib.rib.peer.adj.rib.in.tables.routes.EvpnRoutesCase, "Unrecognized routes %s", routes);
173         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120
174                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.EvpnRoutesCase) routes).getEvpnRoutes().nonnullEvpnRoute();
175     }
176 }