BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / AbstractRIBSupport.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.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
14 import java.util.Collections;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Beta
37 public abstract class AbstractRIBSupport implements RIBSupport {
38     private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBSupport.class);
39     private static final NodeIdentifier ADVERTIZED_ROUTES = new NodeIdentifier(AdvertizedRoutes.QNAME);
40     private static final NodeIdentifier WITHDRAWN_ROUTES = new NodeIdentifier(WithdrawnRoutes.QNAME);
41     private static final NodeIdentifier DESTINATION_TYPE = new NodeIdentifier(DestinationType.QNAME);
42     protected static final NodeIdentifier ROUTES = new NodeIdentifier(Routes.QNAME);
43
44     private final NodeIdentifier routesContainerIdentifier;
45     private final NodeIdentifier routesListIdentifier;
46     private final NodeIdentifier routeAttributesIdentifier;
47     private final Class<? extends Routes> cazeClass;
48     private final Class<? extends DataObject> containerClass;
49     private final Class<? extends Route> listClass;
50
51
52     /**
53      * Default constructor. Requires the QName of the container augmented under the routes choice
54      * node in instantiations of the rib grouping. It is assumed that this container is defined by
55      * the same model which populates it with route grouping instantiation, and by extension with
56      * the route attributes container.
57      *
58      * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
59      * @param containerClass Binding class of the container in routes choice, must not be null.
60      * @param listClass Binding class of the route list, nust not be null;
61      */
62     protected AbstractRIBSupport(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass, final Class<? extends Route> listClass) {
63         final QName qname = BindingReflections.findQName(containerClass);
64         this.routesContainerIdentifier = new NodeIdentifier(qname);
65         this.routeAttributesIdentifier = new NodeIdentifier(QName.cachedReference(QName.create(qname, PathAttributes.QNAME.getLocalName())));
66         this.cazeClass = Preconditions.checkNotNull(cazeClass);
67         this.containerClass = Preconditions.checkNotNull(containerClass);
68         this.listClass = Preconditions.checkNotNull(listClass);
69         this.routesListIdentifier = new NodeIdentifier(BindingReflections.findQName(listClass));
70     }
71
72     @Override
73     public final Class<? extends Routes> routesCaseClass() {
74         return this.cazeClass;
75     }
76
77     @Override
78     public final Class<? extends DataObject> routesContainerClass() {
79         return this.containerClass;
80     }
81
82     @Override
83     public final Class<? extends Route> routesListClass() {
84         return this.listClass;
85     }
86
87     /**
88      * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
89      * the RIB routes.
90      *
91      * @return Container identifier, may not be null.
92      */
93     protected final NodeIdentifier routesContainerIdentifier() {
94         return this.routesContainerIdentifier;
95     }
96
97     /**
98      * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
99      * the NLRI destination.
100      *
101      * @return Container identifier, may not be null.
102      */
103     @Nonnull protected abstract NodeIdentifier destinationContainerIdentifier();
104
105     /**
106      * Given the destination as ContainerNode, implementation needs to parse the DOM model
107      * from this point onward:
108      *
109      * {@code /bgp-mp:mp-unreach-nlri/bgp-mp:withdrawn-routes/bgp-mp:destination-type }
110      *
111      * and delete the routes from its RIBs.
112      *
113      * @param tx DOMDataWriteTransaction to be passed into implementation
114      * @param tablePath YangInstanceIdentifier to be passed into implementation
115      * @param destination ContainerNode DOM representation of NLRI in Update message
116      */
117     protected abstract void deleteDestinationRoutes(DOMDataWriteTransaction tx, YangInstanceIdentifier tablePath, ContainerNode destination);
118
119     /**
120      * Given the destination as ContainerNode, implementation needs to parse the DOM model
121      * from this point onward:
122      *
123      * {@code /bgp-mp:mp-reach-nlri/bgp-mp:advertized-routes/bgp-mp:destination-type }
124      *
125      * and put the routes to its RIBs.
126      *
127      * @param tx DOMDataWriteTransaction to be passed into implementation
128      * @param tablePath YangInstanceIdentifier to be passed into implementation
129      * @param destination ContainerNode DOM representation of NLRI in Update message
130      * @param attributes ContainerNode to be passed into implementation
131      */
132     protected abstract void putDestinationRoutes(DOMDataWriteTransaction tx, YangInstanceIdentifier tablePath, ContainerNode destination, ContainerNode attributes);
133
134     private static ContainerNode getDestination(final DataContainerChild<? extends PathArgument, ?> routes, final NodeIdentifier destinationId) {
135         if (routes instanceof ContainerNode) {
136             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeDestination = ((ContainerNode)routes).getChild(DESTINATION_TYPE);
137             if (maybeDestination.isPresent()) {
138                 final DataContainerChild<? extends PathArgument, ?> destination = maybeDestination.get();
139                 if (destination instanceof ChoiceNode) {
140                     final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRet = ((ChoiceNode)destination).getChild(destinationId);
141                     if (maybeRet.isPresent()) {
142                         final DataContainerChild<? extends PathArgument, ?> ret = maybeRet.get();
143                         if (ret instanceof ContainerNode) {
144                             return (ContainerNode)ret;
145                         } else {
146                             LOG.debug("Specified node {} is not a container, ignoring it", ret);
147                         }
148                     } else {
149                         LOG.debug("Specified container {} is not present in destination {}", destinationId, destination);
150                     }
151                 } else {
152                     LOG.warn("Destination {} is not a choice, ignoring it", destination);
153                 }
154             } else {
155                 LOG.debug("Destination is not present in routes {}", routes);
156             }
157         } else {
158             LOG.warn("Advertized routes {} are not a container, ignoring it", routes);
159         }
160
161         return null;
162     }
163
164     @Override
165     public final NodeIdentifier routeAttributesIdentifier() {
166         return this.routeAttributesIdentifier;
167     }
168
169     @Override
170     public final Collection<DataTreeCandidateNode> changedRoutes(final DataTreeCandidateNode routes) {
171         LOG.trace("Changed routes called with {} identifier {}", routes, routes.getIdentifier());
172         final DataTreeCandidateNode myRoutes = routes.getModifiedChild(this.routesContainerIdentifier);
173         if (myRoutes == null) {
174             return Collections.emptySet();
175         }
176         LOG.trace("MyRoutes {} identifier {}", myRoutes, myRoutes.getIdentifier());
177         final DataTreeCandidateNode routesMap = myRoutes.getModifiedChild(this.routesListIdentifier);
178         if (routesMap == null) {
179             return Collections.emptySet();
180         }
181         LOG.trace("RoutesMap {} identifier {}", routesMap, routesMap.getIdentifier());
182         // Well, given the remote possibility of augmentation, we should perform a filter here,
183         // to make sure the type matches what routeType() reports.
184         LOG.trace("Returning children {}", routesMap.getChildNodes());
185         return routesMap.getChildNodes();
186     }
187
188     @Override
189     public final YangInstanceIdentifier routePath(final YangInstanceIdentifier routesPath, final PathArgument routeId) {
190         return routesPath.node(this.routesContainerIdentifier).node(this.routesListIdentifier).node(routeId);
191     }
192
193     @Override
194     public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri) {
195         final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(WITHDRAWN_ROUTES);
196         if (maybeRoutes.isPresent()) {
197             final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
198             if (destination != null) {
199                 deleteDestinationRoutes(tx, tablePath, destination);
200             }
201         } else {
202             LOG.debug("Withdrawn routes are not present in NLRI {}", nlri);
203         }
204     }
205
206     @Override
207     public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri, final ContainerNode attributes) {
208         final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(ADVERTIZED_ROUTES);
209         if (maybeRoutes.isPresent()) {
210             final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
211             if (destination != null) {
212                 putDestinationRoutes(tx, tablePath, destination, attributes);
213             }
214         } else {
215             LOG.debug("Advertized routes are not present in NLRI {}", nlri);
216         }
217     }
218 }