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