f55b149a88a39d228ce5b184be6077a80d3f6786
[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_VALUE;
11
12 import com.google.common.collect.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
26 import org.opendaylight.yangtools.yang.binding.ChildOf;
27 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.Identifier;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
41
42 /**
43  * Interface implemented for AFI/SAFI-specific RIB extensions. The extensions need
44  * to register an implementation of this class and the RIB core then calls into it
45  * to inquire about details specific to that particular model.
46  */
47 public interface RIBSupport<
48         C extends Routes & DataObject & ChoiceIn<Tables>,
49         S extends ChildOf<? super C>,
50         R extends Route & ChildOf<? super S> & Identifiable<I>,
51         I extends Identifier<R>> {
52     /**
53      * Return the table-type-specific empty table with routes empty container, as augmented into the
54      * bgp-rib model under /rib/tables/routes choice node. This needs to include all
55      * the skeleton nodes under which the individual routes will be stored.
56      *
57      * @return Protocol-specific case in the routes choice, may not be null.
58      */
59     @Nonnull
60     MapEntryNode emptyTable();
61
62     /**
63      * Return the localized identifier of the attributes route member, as expanded
64      * from the route grouping in the specific augmentation of the base routes choice.
65      *
66      * @return The attributes identifier, may not be null.
67      */
68     @Nonnull
69     NodeIdentifier routeAttributesIdentifier();
70
71     /**
72      * Return class object of the Routes Case statement.
73      *
74      * @return Class
75      */
76     @Nonnull
77     Class<C> routesCaseClass();
78
79     /**
80      * Return class object of the Routes Container statement.
81      *
82      * @return Class
83      */
84     @Nonnull
85     Class<S> routesContainerClass();
86
87     /**
88      * Return class object of the Routes List statement.
89      *
90      * @return Class
91      */
92     @Nonnull
93     Class<R> routesListClass();
94
95     @Nonnull
96     default ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
97         return ImmutableSet.of();
98     }
99
100     @Nonnull
101     default ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
102         return ImmutableSet.of();
103     }
104
105     /**
106      * Given the NLRI as ContainerNode, this method should extract withdrawn routes
107      * from the DOM model and delete them from RIBs.
108      *
109      * @param tx        DOMDataWriteTransaction
110      * @param tablePath YangInstanceIdentifier
111      * @param nlri      ContainerNode DOM representation of NLRI in Update message
112      */
113     void deleteRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
114             @Nonnull ContainerNode nlri);
115
116
117     /**
118      * Given the NLRI as ContainerNode, this method should extract withdrawn routes
119      * from the DOM model and delete them from RIBs.
120      * <p>
121      * Use this method when removing routes stored in RIBs out of the "bgp-rib" module.
122      * Provide {@link NodeIdentifier} with customized "routes" QName.
123      * For default "bgp-rib" RIBs use {@link #deleteRoutes}
124      * </p>
125      *
126      * @param tx           DOMDataWriteTransaction
127      * @param tablePath    YangInstanceIdentifier
128      * @param nlri         ContainerNode DOM representation of NLRI in Update message
129      * @param routesNodeId NodeIdentifier of "routes" data node
130      */
131     void deleteRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
132             @Nonnull ContainerNode nlri, @Nonnull NodeIdentifier routesNodeId);
133
134     /**
135      * Given the NLRI as ContainerNode, this method should extract advertised routes
136      * from the DOM model and put them into RIBs.
137      *
138      * @param tx         DOMDataWriteTransaction
139      * @param tablePath  YangInstanceIdentifier
140      * @param nlri       ContainerNode DOM representation of NLRI in Update message
141      * @param attributes ContainerNode
142      * @return List of processed route Identifiers
143      */
144     Collection<NodeIdentifierWithPredicates> putRoutes(@Nonnull DOMDataWriteTransaction tx,
145             @Nonnull YangInstanceIdentifier tablePath, @Nonnull ContainerNode nlri, @Nonnull ContainerNode attributes);
146
147     /**
148      * Given the NLRI as ContainerNode, this method should extract advertised routes
149      * from the DOM model and put them into RIBs.
150      * <p>
151      * Use this method when putting routes stored in RIBs out of the "bgp-rib" module.
152      * Provide {@link NodeIdentifier} with customized "routes" QName.
153      * For default "bgp-rib" RIBs use {@link #putRoutes}
154      * </p>
155      *
156      * @param tx           DOMDataWriteTransaction
157      * @param tablePath    YangInstanceIdentifier
158      * @param nlri         ContainerNode DOM representation of NLRI in Update message
159      * @param attributes   ContainerNode
160      * @param routesNodeId NodeIdentifier of "routes" data node
161      * @return List of processed routes identifiers
162      */
163     Collection<NodeIdentifierWithPredicates> putRoutes(@Nonnull DOMDataWriteTransaction tx,
164             @Nonnull YangInstanceIdentifier tablePath, @Nonnull ContainerNode nlri, @Nonnull ContainerNode attributes,
165             @Nonnull NodeIdentifier routesNodeId);
166
167     /**
168      * Returns routes that were modified within this RIB support instance.
169      *
170      * @param routes DataTreeCandidateNode
171      * @return collection of modified nodes or empty collection if no node was modified
172      */
173     @Nonnull
174     Collection<DataTreeCandidateNode> changedRoutes(@Nonnull DataTreeCandidateNode routes);
175
176     /**
177      * Constructs an instance identifier path to routeId.
178      *
179      * @param routesPath YangInstanceIdentifier base path
180      * @param routeId    PathArgument leaf path
181      * @return YangInstanceIdentifier with routesPath + specific RIB support routes path + routeId
182      */
183     @Nonnull
184     default YangInstanceIdentifier routePath(@Nonnull final YangInstanceIdentifier routesPath,
185                                              @Nonnull final PathArgument routeId) {
186         return routesPath(routesPath).node(routeId);
187     }
188
189     /**
190      * Constructs an instance identifier path to routes list.
191      *
192      * @param routesPath YangInstanceIdentifier base path
193      * @return YangInstanceIdentifier with routesPath + specific RIB support routes path
194      */
195     @Nonnull
196     YangInstanceIdentifier routesPath(@Nonnull YangInstanceIdentifier routesPath);
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
210     Update buildUpdate(
211             @Nonnull Collection<MapEntryNode> advertised,
212             @Nonnull Collection<MapEntryNode> withdrawn,
213             @Nonnull Attributes attr);
214
215     @Nonnull
216     Class<? extends AddressFamily> getAfi();
217
218     @Nonnull
219     Class<? extends SubsequentAddressFamily> getSafi();
220
221     /**
222      * Creates Route table Peer InstanceIdentifier.
223      *
224      * @param tableKey    table InstanceIdentifier
225      * @param newRouteKey route key
226      * @return InstanceIdentifier
227      */
228     @Nonnull
229     InstanceIdentifier<R> createRouteIdentifier(
230             @Nonnull KeyedInstanceIdentifier<Tables, TablesKey> tableKey,
231             @Nonnull I newRouteKey);
232
233     /**
234      * Creates a route with new path Id and attributes.
235      *
236      * @param route route
237      * @param routeKey route key
238      * @param pathId new path Id
239      * @param attributes route attributes
240      * @return Route List key
241      */
242     @Nonnull
243     R createRoute(@Nullable R route, String routeKey, @Nullable long pathId, @Nonnull Attributes attributes);
244
245     /**
246      * Returns TablesKey which we are providing support.
247      *
248      * @return TablesKey
249      */
250     TablesKey getTablesKey();
251
252     interface ApplyRoute {
253         void apply(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier base,
254                    @Nonnull NodeIdentifierWithPredicates routeKey,
255                    @Nonnull DataContainerNode<?> route, ContainerNode attributes);
256     }
257
258     /**
259      * Return the table-type-specific empty routes container, as augmented into the
260      * bgp-peer model under /peer/effect-rib-in/tables/routes choice node. This needs to include all
261      * the skeleton nodes under which the individual routes will be stored.
262      *
263      * @return Protocol-specific case in the routes choice, may not be null.
264      */
265     @Nonnull
266     @Deprecated
267     C emptyRoutesCase();
268
269     /**
270      * Return the table-type-specific empty routes container, as augmented into the
271      * bgp-peer model under /peer/effect-rib-in/tables/routes choice node/routes container. This needs to include all
272      * the skeleton nodes under which the individual routes will be stored.
273      *
274      * @return Protocol-specific container in the routes, may not be null.
275      */
276     @Nonnull
277     S emptyRoutesContainer();
278
279     /**
280      * Construct a Route List Key using new path Id for Families.
281      *
282      * @param pathId   The path identifier
283      * @param routeKey RouteKey
284      * @return route list Key (RouteKey + pathId)
285      */
286     @Nonnull
287     I createRouteListKey(@Nonnull long pathId, @Nonnull String routeKey);
288
289     /**
290      * Construct a Route List Key using new path Id for Families.
291      *
292      * @param routeKey RouteKey
293      * @return route list Key (RouteKey + empty pathId)
294      */
295     @Nonnull
296     default I createRouteListKey(@Nonnull final String routeKey) {
297         return createRouteListKey(NON_PATH_ID_VALUE, routeKey);
298     }
299 }