Bump MDSAL to 4.0.0
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / AbstractMvpnRIBSupport.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
9 package org.opendaylight.protocol.bgp.mvpn.impl;
10
11 import com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.McastVpnSubsequentAddressFamily;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.MvpnRoutes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.mvpn.MvpnChoice;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.mvpn.routes.MvpnRoute;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.mvpn.routes.MvpnRouteBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.mvpn.routes.MvpnRouteKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
32 import org.opendaylight.yangtools.yang.binding.BindingObject;
33 import org.opendaylight.yangtools.yang.binding.ChildOf;
34 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Abstract Mvpn RIBSupport.
51  *
52  * @author Claudio D. Gasparini
53  */
54 abstract class AbstractMvpnRIBSupport<C extends Routes & DataObject & ChoiceIn<Tables>,
55         S extends ChildOf<? super C> & MvpnRoutes> extends AbstractRIBSupport<C, S, MvpnRoute, MvpnRouteKey> {
56     private static final Logger LOG = LoggerFactory.getLogger(AbstractMvpnRIBSupport.class);
57     private final NodeIdentifier nlriRoutesList;
58     private final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects;
59
60     /**
61      * Default constructor. Requires the QName of the container augmented under the routes choice
62      * node in instantiations of the rib grouping. It is assumed that this container is defined by
63      * the same model which populates it with route grouping instantiation, and by extension with
64      * the route attributes container.
65      *
66      * @param mappingService     Serialization service
67      * @param cazeClass          Binding class of the AFI/SAFI-specific case statement, must not be null
68      * @param afiClass           address Family Class
69      * @param destContainerQname destination container Qname
70      * @param destListQname      destinations list Qname
71      */
72     AbstractMvpnRIBSupport(
73             final BindingNormalizedNodeSerializer mappingService,
74             final Class<C> cazeClass,
75             final Class<S> containerClass,
76             final Class<? extends AddressFamily> afiClass,
77             final QName destContainerQname,
78             final QName destListQname) {
79         super(mappingService, cazeClass, containerClass, MvpnRoute.class, afiClass,
80                 McastVpnSubsequentAddressFamily.class, destContainerQname);
81         this.nlriRoutesList = NodeIdentifier.create(destListQname);
82         this.cacheableNlriObjects = ImmutableSet.of(cazeClass);
83
84     }
85
86     @Override
87     public final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects() {
88         return this.cacheableNlriObjects;
89     }
90
91     @Override
92     public final MvpnRoute createRoute(final MvpnRoute route, final MvpnRouteKey key, final Attributes attributes) {
93         final MvpnRouteBuilder builder;
94         if (route != null) {
95             builder = new MvpnRouteBuilder(route);
96         } else {
97             builder = new MvpnRouteBuilder();
98         }
99         return builder.withKey(key).setAttributes(attributes).build();
100     }
101
102     @Override
103     public final MvpnRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
104         return new MvpnRouteKey(pathId, routeKey);
105     }
106
107     final MvpnChoice extractMvpnChoice(final DataContainerNode<? extends PathArgument> route) {
108         final DataObject nn = this.mappingService.fromNormalizedNode(this.routeDefaultYii, route).getValue();
109         return ((MvpnRoute) nn).getMvpnChoice();
110     }
111
112     @Override
113     protected final Collection<NodeIdentifierWithPredicates> processDestination(
114             final DOMDataWriteTransaction tx,
115             final YangInstanceIdentifier routesPath,
116             final ContainerNode destination,
117             final ContainerNode attributes,
118             final ApplyRoute function) {
119         if (destination != null) {
120             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
121                     .getChild(this.nlriRoutesList);
122             if (maybeRoutes.isPresent()) {
123                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
124                 if (routes instanceof UnkeyedListNode) {
125                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
126                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
127                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
128                     for (final UnkeyedListEntryNode mvpnDest : routesList) {
129                         final NodeIdentifierWithPredicates routeKey = createRouteKey(mvpnDest);
130                         function.apply(tx, base, routeKey, mvpnDest, attributes);
131                         keys.add(routeKey);
132                     }
133                     return keys;
134                 } else {
135                     LOG.warn("Routes {} are not a map", routes);
136                 }
137             }
138         }
139         return Collections.emptyList();
140     }
141
142
143     @Override
144     public final PathId extractPathId(final MvpnRouteKey routeListKey) {
145         return routeListKey.getPathId();
146     }
147
148     @Override
149     public final String extractRouteKey(final MvpnRouteKey routeListKey) {
150         return routeListKey.getRouteKey();
151     }
152
153     abstract NodeIdentifierWithPredicates createRouteKey(UnkeyedListEntryNode mvpn);
154 }