Bump MDSAL to 4.0.0
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / RIBSupport.java
1 /*
2  * Copyright (c) 2015 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.rib.spi;
9
10 import static org.opendaylight.protocol.bgp.parser.spi.PathIdUtil.NON_PATH_ID;
11
12 import com.google.common.collect.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.List;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
28 import org.opendaylight.yangtools.yang.binding.BindingObject;
29 import org.opendaylight.yangtools.yang.binding.ChildOf;
30 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.Identifiable;
33 import org.opendaylight.yangtools.yang.binding.Identifier;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
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.DataContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
45
46 /**
47  * Interface implemented for AFI/SAFI-specific RIB extensions. The extensions need
48  * to register an implementation of this class and the RIB core then calls into it
49  * to inquire about details specific to that particular model.
50  */
51 public interface RIBSupport<
52         C extends Routes & DataObject & ChoiceIn<Tables>,
53         S extends ChildOf<? super C>,
54         R extends Route & ChildOf<? super S> & Identifiable<I>,
55         I extends Identifier<R>> {
56     /**
57      * Return the table-type-specific empty table with routes empty container, as augmented into the
58      * bgp-rib model under /rib/tables/routes choice node. This needs to include all
59      * the skeleton nodes under which the individual routes will be stored.
60      *
61      * @return Protocol-specific case in the routes choice, may not be null.
62      */
63     @Nonnull
64     MapEntryNode emptyTable();
65
66     /**
67      * Return the localized identifier of the attributes route member, as expanded
68      * from the route grouping in the specific augmentation of the base routes choice.
69      *
70      * @return The attributes identifier, may not be null.
71      */
72     @Nonnull
73     NodeIdentifier routeAttributesIdentifier();
74
75     /**
76      * Return class object of the Routes Case statement.
77      *
78      * @return Class
79      */
80     @Nonnull
81     Class<C> routesCaseClass();
82
83     /**
84      * Return class object of the Routes Container statement.
85      *
86      * @return Class
87      */
88     @Nonnull
89     Class<S> routesContainerClass();
90
91     /**
92      * Return class object of the Routes List statement.
93      *
94      * @return Class
95      */
96     @Nonnull
97     Class<R> routesListClass();
98
99     @Nonnull
100     default ImmutableCollection<Class<? extends BindingObject>> cacheableAttributeObjects() {
101         return ImmutableSet.of();
102     }
103
104     @Nonnull
105     default ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects() {
106         return ImmutableSet.of();
107     }
108
109     /**
110      * Given the NLRI as ContainerNode, this method should extract withdrawn routes
111      * from the DOM model and delete them from RIBs.
112      *
113      * @param tx        DOMDataWriteTransaction
114      * @param tablePath YangInstanceIdentifier
115      * @param nlri      ContainerNode DOM representation of NLRI in Update message
116      */
117     void deleteRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
118             @Nonnull ContainerNode nlri);
119
120
121     /**
122      * Given the NLRI as ContainerNode, this method should extract withdrawn routes
123      * from the DOM model and delete them from RIBs.
124      * <p>
125      * Use this method when removing routes stored in RIBs out of the "bgp-rib" module.
126      * Provide {@link NodeIdentifier} with customized "routes" QName.
127      * For default "bgp-rib" RIBs use {@link #deleteRoutes}
128      * </p>
129      *
130      * @param tx           DOMDataWriteTransaction
131      * @param tablePath    YangInstanceIdentifier
132      * @param nlri         ContainerNode DOM representation of NLRI in Update message
133      * @param routesNodeId NodeIdentifier of "routes" data node
134      */
135     void deleteRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
136             @Nonnull ContainerNode nlri, @Nonnull NodeIdentifier routesNodeId);
137
138     /**
139      * Given the NLRI as ContainerNode, this method should extract advertised routes
140      * from the DOM model and put them into RIBs.
141      *
142      * @param tx         DOMDataWriteTransaction
143      * @param tablePath  YangInstanceIdentifier
144      * @param nlri       ContainerNode DOM representation of NLRI in Update message
145      * @param attributes ContainerNode
146      * @return List of processed route Identifiers
147      */
148     Collection<NodeIdentifierWithPredicates> putRoutes(@Nonnull DOMDataWriteTransaction tx,
149             @Nonnull YangInstanceIdentifier tablePath, @Nonnull ContainerNode nlri, @Nonnull ContainerNode attributes);
150
151     /**
152      * Given the NLRI as ContainerNode, this method should extract advertised routes
153      * from the DOM model and put them into RIBs.
154      * <p>
155      * Use this method when putting routes stored in RIBs out of the "bgp-rib" module.
156      * Provide {@link NodeIdentifier} with customized "routes" QName.
157      * For default "bgp-rib" RIBs use {@link #putRoutes}
158      * </p>
159      *
160      * @param tx           DOMDataWriteTransaction
161      * @param tablePath    YangInstanceIdentifier
162      * @param nlri         ContainerNode DOM representation of NLRI in Update message
163      * @param attributes   ContainerNode
164      * @param routesNodeId NodeIdentifier of "routes" data node
165      * @return List of processed routes identifiers
166      */
167     Collection<NodeIdentifierWithPredicates> putRoutes(@Nonnull DOMDataWriteTransaction tx,
168             @Nonnull YangInstanceIdentifier tablePath, @Nonnull ContainerNode nlri, @Nonnull ContainerNode attributes,
169             @Nonnull NodeIdentifier routesNodeId);
170
171     /**
172      * Returns routes that were modified within this RIB support instance.
173      *
174      * @param routes DataTreeCandidateNode
175      * @return collection of modified nodes or empty collection if no node was modified
176      */
177     @Nonnull
178     Collection<DataTreeCandidateNode> changedRoutes(@Nonnull DataTreeCandidateNode routes);
179
180     /**
181      * Constructs an instance identifier path to routeId.
182      *
183      * @param routesPath YangInstanceIdentifier base path
184      * @param routeId    PathArgument leaf path
185      * @return YangInstanceIdentifier with routesPath + specific RIB support routes path + routeId
186      */
187     @Nonnull
188     default YangInstanceIdentifier routePath(@Nonnull final YangInstanceIdentifier routesPath,
189                                              @Nonnull final PathArgument routeId) {
190         return routesPath(routesPath).node(routeId);
191     }
192
193     /**
194      * Constructs an instance identifier path to routes list.
195      *
196      * @param routesPath YangInstanceIdentifier base path
197      * @return YangInstanceIdentifier with routesPath + specific RIB support routes path
198      */
199     @Nonnull
200     YangInstanceIdentifier routesPath(@Nonnull YangInstanceIdentifier routesPath);
201
202     /**
203      * Return the relative path from the generic routes container to the AFI/SAFI specific route list.
204      *
205      * @return Relative path.
206      */
207     @Nonnull
208     List<PathArgument> relativeRoutesPath();
209
210     /**
211      * To send routes out, we'd need to transform the DOM representation of route to
212      * binding-aware format. This needs to be done per each AFI/SAFI.
213      *
214      * @param advertised Collection of advertised routes in DOM format
215      * @param withdrawn  Collection of withdrawn routes in DOM format
216      * @param attr       Attributes MpReach is part of Attributes so we need to pass
217      *                   it as argument, create new AttributesBuilder with existing
218      *                   attributes and add MpReach
219      * @return Update message ready to be sent out
220      */
221     @Nonnull
222     Update buildUpdate(
223             @Nonnull Collection<MapEntryNode> advertised,
224             @Nonnull Collection<MapEntryNode> withdrawn,
225             @Nonnull Attributes attr);
226
227     @Nonnull
228     Class<? extends AddressFamily> getAfi();
229
230     @Nonnull
231     Class<? extends SubsequentAddressFamily> getSafi();
232
233     /**
234      * Creates Route table Peer InstanceIdentifier.
235      *
236      * @param tableKey    table InstanceIdentifier
237      * @param newRouteKey route key
238      * @return InstanceIdentifier
239      */
240     @Nonnull
241     InstanceIdentifier<R> createRouteIdentifier(
242             @Nonnull KeyedInstanceIdentifier<Tables, TablesKey> tableKey,
243             @Nonnull I newRouteKey);
244
245     /**
246      * Creates a route with new path Id and attributes.
247      *
248      * @param route route
249      * @param key route key
250      * @param attributes route attributes
251      * @return Route List key
252      */
253     @Nonnull
254     R createRoute(@Nullable R route, @Nonnull I key, @Nonnull Attributes attributes);
255
256     /**
257      * Returns TablesKey which we are providing support.
258      *
259      * @return TablesKey
260      */
261     TablesKey getTablesKey();
262
263     /**
264      * Translates supplied YANG Instance Identifier and NormalizedNode into Binding Route.
265      *
266      * @param routerId Binding Instance Identifier
267      * @param normalizedNode NormalizedNode representing Route
268      * @return Route
269      */
270     R fromNormalizedNode(YangInstanceIdentifier routerId, NormalizedNode<?, ?> normalizedNode);
271
272     /**
273      * Translates supplied YANG Instance Identifier and NormalizedNode into Binding data Attribute.
274      * @param advertisedAttrs NormalizedNode representing attributes
275      * @return Attribute
276      */
277     Attributes attributeFromContainerNode(ContainerNode advertisedAttrs);
278
279     /**
280      * Translates supplied Binding Instance Identifier and data into NormalizedNode representation.
281      * @param routePath Binding Instance Identifier pointing to data
282      * @param attributes Data object representing Attributes
283      * @return NormalizedNode representation
284      */
285     ContainerNode attributeToContainerNode(YangInstanceIdentifier routePath, Attributes attributes);
286
287     interface ApplyRoute {
288         void apply(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier base,
289                    @Nonnull NodeIdentifierWithPredicates routeKey,
290                    @Nonnull DataContainerNode<?> route, ContainerNode attributes);
291     }
292
293     /**
294      * Return the table-type-specific empty routes container, as augmented into the
295      * bgp-peer model under /peer/effect-rib-in/tables/routes choice node. This needs to include all
296      * the skeleton nodes under which the individual routes will be stored.
297      *
298      * @return Protocol-specific case in the routes choice, may not be null.
299      */
300     @Nonnull
301     @Deprecated
302     C emptyRoutesCase();
303
304     /**
305      * Return the table-type-specific empty routes container, as augmented into the
306      * bgp-peer model under /peer/effect-rib-in/tables/routes choice node/routes container. This needs to include all
307      * the skeleton nodes under which the individual routes will be stored.
308      *
309      * @return Protocol-specific container in the routes, may not be null.
310      */
311     @Nonnull
312     S emptyRoutesContainer();
313
314     /**
315      * Construct a Route List Key using new path Id for Families.
316      *
317      * @param pathId   The path identifier
318      * @param routeKey RouteKey
319      * @return route list Key (RouteKey + pathId)
320      */
321     @Nonnull
322     I createRouteListKey(@Nonnull PathId pathId, @Nonnull String routeKey);
323
324     /**
325      * Construct a Route List Key.
326      *
327      * @param routeKey RouteKey
328      * @return route list Key (RouteKey + empty pathId)
329      */
330     @Nonnull
331     default I createRouteListKey(@Nonnull final String routeKey) {
332         return createRouteListKey(NON_PATH_ID, routeKey);
333     }
334
335     /**
336      * Given a route list key, return the associated path ID.
337      *
338      * @param routeListKey Route list key
339      * @return Path ID
340      */
341     @Nonnull
342     PathId extractPathId(@Nonnull I routeListKey);
343
344     /**
345      * Given a route list key, return the associated path ID.
346      *
347      * @param routeListKey Route list key
348      * @return RouteKey
349      */
350     @Nonnull
351     String extractRouteKey(@Nonnull I routeListKey);
352
353     /**
354      * Extract a route list from the adj-rib-in instantiation of table routes.
355      *
356      * @param routes Table route choice
357      * @return A potentially empty list of routes
358      */
359     @Nonnull
360     List<R> extractAdjRibInRoutes(Routes routes);
361 }