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