Bump upstreams to 2022.09
[bgpcep.git] / bgp / extensions / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / mcast / AbstractL3vpnMcastIpRIBSupport.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.l3vpn.mcast;
9
10 import com.google.common.collect.ImmutableCollection;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.stream.Collectors;
17 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
20 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
21 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.L3vpnMcastRoutes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.McastMplsLabeledVpnSubsequentAddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestinationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.L3vpnMcastRoute;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
31 import org.opendaylight.yangtools.yang.binding.BindingObject;
32 import org.opendaylight.yangtools.yang.binding.ChildOf;
33 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
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.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
41 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
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 /**
49  * Abstract L3VPN Multicast RIBSupport.
50  *
51  * @author Claudio D. Gasparini
52  */
53 abstract class AbstractL3vpnMcastIpRIBSupport<
54         C extends Routes & DataObject & ChoiceIn<Tables>,
55         S extends ChildOf<? super C> & L3vpnMcastRoutes>
56         extends AbstractRIBSupport<C, S, L3vpnMcastRoute> {
57     private static final Logger LOG = LoggerFactory.getLogger(AbstractL3vpnMcastIpRIBSupport.class);
58     private final NodeIdentifier nlriRoutesList;
59     private final NodeIdentifier rdNid;
60     private final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects;
61
62     /**
63      * Default constructor. Requires the QName of the container augmented under the routes choice
64      * node in instantiations of the rib grouping. It is assumed that this container is defined by
65      * the same model which populates it with route grouping instantiation, and by extension with
66      * the route attributes container.
67      *
68      * @param mappingService     Serialization service
69      * @param cazeClass          Binding class of the AFI/SAFI-specific case statement, must not be null
70      * @param afiClass           address Family Class
71      * @param destContainerQname destination container Qname
72      * @param destListQname      destinations list Qname
73      */
74     AbstractL3vpnMcastIpRIBSupport(
75             final BindingNormalizedNodeSerializer mappingService,
76             final Class<C> cazeClass, final QName cazeQName,
77             final Class<S> containerClass,
78             final AddressFamily afiClass,
79             final QName destContainerQname,
80             final QName destListQname) {
81         super(mappingService, cazeClass, containerClass, L3vpnMcastRoute.class, afiClass,
82                 McastMplsLabeledVpnSubsequentAddressFamily.VALUE, destContainerQname);
83         this.nlriRoutesList = NodeIdentifier.create(destListQname);
84         this.rdNid = NodeIdentifier.create(QName.create(cazeQName, "route-distinguisher").intern());
85         this.cacheableNlriObjects = ImmutableSet.of(cazeClass);
86     }
87
88     @Override
89     public final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects() {
90         return this.cacheableNlriObjects;
91     }
92
93     protected abstract IpPrefix createPrefix(String prefix);
94
95     @Override
96     protected final Collection<NodeIdentifierWithPredicates> processDestination(
97             final DOMDataTreeWriteTransaction tx,
98             final YangInstanceIdentifier routesPath,
99             final ContainerNode destination,
100             final ContainerNode attributes,
101             final ApplyRoute function) {
102         if (destination != null) {
103             final DataContainerChild routes = destination.childByArg(nlriRoutesList);
104             if (routes != null) {
105                 if (routes instanceof UnkeyedListNode) {
106                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
107                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).body();
108                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
109                     for (final UnkeyedListEntryNode l3vpnDest : routesList) {
110                         final YangInstanceIdentifier.NodeIdentifierWithPredicates routeKey = createRouteKey(l3vpnDest);
111                         function.apply(tx, base, routeKey, l3vpnDest, attributes);
112                         keys.add(routeKey);
113                     }
114                     return keys;
115                 }
116                 LOG.warn("Routes {} are not a map", routes);
117             }
118         }
119         return Collections.emptyList();
120     }
121
122     final List<L3vpnMcastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
123         return routes.stream().map(this::extractDestinations).collect(Collectors.toList());
124     }
125
126     final L3vpnMcastDestination extractDestinations(final DataContainerNode destination) {
127         return new L3vpnMcastDestinationBuilder()
128                 .setRouteDistinguisher(RouteDistinguisherUtil.extractRouteDistinguisher(destination, rdNid))
129                 .setPrefix(createPrefix(extractPrefix(destination)))
130                 .setPathId(PathIdUtil.buildPathId(destination, routePathIdNid()))
131                 .build();
132     }
133
134     abstract NodeIdentifierWithPredicates createRouteKey(UnkeyedListEntryNode l3vpn);
135 }