08a2fc009eae571702a444050219114d71c8d5b2
[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.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
39
40 /**
41  * Interface implemented for AFI/SAFI-specific RIB extensions. The extensions need
42  * to register an implementation of this class and the RIB core then calls into it
43  * to inquire about details specific to that particular model.
44  */
45 public interface RIBSupport<
46         C extends Routes & DataObject & ChoiceIn<Tables>,
47         S extends ChildOf<? super C>,
48         R extends Route & ChildOf<? super S> & Identifiable<I>,
49         I extends Identifier<R>> {
50     /**
51      * Return the table-type-specific empty table with routes empty container, as augmented into the
52      * bgp-rib model under /rib/tables/routes choice node. This needs to include all
53      * the skeleton nodes under which the individual routes will be stored.
54      *
55      * @return Protocol-specific case in the routes choice, may not be null.
56      */
57     @Nonnull
58     MapEntryNode emptyTable();
59
60     /**
61      * Return the localized identifier of the attributes route member, as expanded
62      * from the route grouping in the specific augmentation of the base routes choice.
63      *
64      * @return The attributes identifier, may not be null.
65      */
66     @Nonnull
67     NodeIdentifier routeAttributesIdentifier();
68
69     /**
70      * Return class object of the Routes Case statement.
71      *
72      * @return Class
73      */
74     @Nonnull
75     Class<C> routesCaseClass();
76
77     /**
78      * Return class object of the Routes Container statement.
79      *
80      * @return Class
81      */
82     @Nonnull
83     Class<S> routesContainerClass();
84
85     /**
86      * Return class object of the Routes List statement.
87      *
88      * @return Class
89      */
90     @Nonnull
91     Class<R> routesListClass();
92
93     @Nonnull
94     default ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
95         return ImmutableSet.of();
96     }
97
98     @Nonnull
99     default ImmutableCollection<Class<? extends DataObject>> 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 DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
112             @Nonnull ContainerNode nlri);
113
114
115     /**
116      * Given the NLRI as ContainerNode, this method should extract withdrawn routes
117      * from the DOM model and delete them from RIBs.
118      * <p>
119      * Use this method when removing routes stored in RIBs out of the "bgp-rib" module.
120      * Provide {@link NodeIdentifier} with customized "routes" QName.
121      * For default "bgp-rib" RIBs use {@link #deleteRoutes}
122      * </p>
123      *
124      * @param tx           DOMDataWriteTransaction
125      * @param tablePath    YangInstanceIdentifier
126      * @param nlri         ContainerNode DOM representation of NLRI in Update message
127      * @param routesNodeId NodeIdentifier of "routes" data node
128      */
129     void deleteRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
130             @Nonnull ContainerNode nlri, @Nonnull NodeIdentifier routesNodeId);
131
132     /**
133      * Given the NLRI as ContainerNode, this method should extract advertised routes
134      * from the DOM model and put them into RIBs.
135      *
136      * @param tx         DOMDataWriteTransaction
137      * @param tablePath  YangInstanceIdentifier
138      * @param nlri       ContainerNode DOM representation of NLRI in Update message
139      * @param attributes ContainerNode
140      */
141     void putRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
142             @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      */
159     void putRoutes(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier tablePath,
160             @Nonnull ContainerNode nlri, @Nonnull ContainerNode attributes, @Nonnull NodeIdentifier routesNodeId);
161
162     /**
163      * Returns routes that were modified within this RIB support instance.
164      *
165      * @param routes DataTreeCandidateNode
166      * @return collection of modified nodes or empty collection if no node was modified
167      */
168     @Nonnull
169     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     @Nonnull
179     YangInstanceIdentifier routePath(@Nonnull YangInstanceIdentifier routesPath,
180             @Nonnull PathArgument routeId);
181
182     /**
183      * To send routes out, we'd need to transform the DOM representation of route to
184      * binding-aware format. This needs to be done per each AFI/SAFI.
185      *
186      * @param advertised Collection of advertised routes in DOM format
187      * @param withdrawn  Collection of withdrawn routes in DOM format
188      * @param attr       Attributes MpReach is part of Attributes so we need to pass
189      *                   it as argument, create new AttributesBuilder with existing
190      *                   attributes and add MpReach
191      * @return Update message ready to be sent out
192      */
193     @Nonnull
194     Update buildUpdate(
195             @Nonnull Collection<MapEntryNode> advertised,
196             @Nonnull Collection<MapEntryNode> withdrawn,
197             @Nonnull Attributes attr);
198
199     @Nonnull
200     Class<? extends AddressFamily> getAfi();
201
202     @Nonnull
203     Class<? extends SubsequentAddressFamily> getSafi();
204
205     /**
206      * Creates Route table Peer InstanceIdentifier.
207      *
208      * @param tableKey    table InstanceIdentifier
209      * @param newRouteKey route key
210      * @return InstanceIdentifier
211      */
212     @Nonnull
213     InstanceIdentifier<R> createRouteIdentifier(
214             @Nonnull KeyedInstanceIdentifier<Tables, TablesKey> tableKey,
215             @Nonnull I newRouteKey);
216
217     /**
218      * Creates a route with new path Id and attributes.
219      *
220      * @param route route
221      * @param routeKey route key
222      * @param pathId new path Id
223      * @param attributes route attributes
224      * @return Route List key
225      */
226     @Nonnull
227     R createRoute(@Nullable R route, String routeKey, @Nullable long pathId, @Nonnull Attributes attributes);
228
229     /**
230      * Returns TablesKey which we are providing support.
231      *
232      * @return TablesKey
233      */
234     TablesKey getTablesKey();
235
236     interface ApplyRoute {
237         void apply(@Nonnull DOMDataWriteTransaction tx, @Nonnull YangInstanceIdentifier base,
238                 @Nonnull NodeIdentifierWithPredicates routeKey,
239                 @Nonnull DataContainerNode<?> route, ContainerNode attributes);
240     }
241
242     /**
243      * Return the table-type-specific empty routes container, as augmented into the
244      * bgp-peer model under /peer/effect-rib-in/tables/routes choice node. This needs to include all
245      * the skeleton nodes under which the individual routes will be stored.
246      *
247      * @return Protocol-specific case in the routes choice, may not be null.
248      */
249     @Nonnull
250     C emptyRoutesCase();
251
252     /**
253      * Return the table-type-specific empty routes container, as augmented into the
254      * bgp-peer model under /peer/effect-rib-in/tables/routes choice node/routes container. This needs to include all
255      * the skeleton nodes under which the individual routes will be stored.
256      *
257      * @return Protocol-specific container in the routes, may not be null.
258      */
259     @Nonnull
260     S emptyRoutesContainer();
261
262
263
264     /**
265      * Construct a Route List Key using new path Id for Families.
266      *
267      * @param pathId   The path identifier
268      * @param routeKey RouteKey
269      * @return route list Key (RouteKey + pathId)
270      */
271     @Nonnull
272     I createRouteListKey(@Nonnull long pathId, @Nonnull String routeKey);
273 }