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