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