2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.spi;
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.common.api.data.LogicalDatastoreType;
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.rev130919.Update;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
47 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
53 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
61 public abstract class AbstractRIBSupport implements RIBSupport {
62 private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBSupport.class);
63 private static final NodeIdentifier ADVERTISED_ROUTES = new NodeIdentifier(AdvertizedRoutes.QNAME);
64 private static final NodeIdentifier WITHDRAWN_ROUTES = new NodeIdentifier(WithdrawnRoutes.QNAME);
65 private static final NodeIdentifier DESTINATION_TYPE = new NodeIdentifier(DestinationType.QNAME);
66 private static final NodeIdentifier ROUTES = new NodeIdentifier(Routes.QNAME);
67 private static final ApplyRoute DELETE_ROUTE = new DeleteRoute();
69 private final NodeIdentifier routesContainerIdentifier;
70 private final NodeIdentifier routesListIdentifier;
71 private final NodeIdentifier routeAttributesIdentifier;
72 private final Class<? extends Routes> cazeClass;
73 private final Class<? extends DataObject> containerClass;
74 private final Class<? extends Route> listClass;
75 private final ApplyRoute putRoute = new PutRoute();
76 private final ChoiceNode emptyRoutes;
77 private final QName routeQname;
78 private final Class<? extends AddressFamily> afiClass;
79 private final Class<? extends SubsequentAddressFamily> safiClass;
80 private final NodeIdentifier destinationNid;
83 * Default constructor. Requires the QName of the container augmented under the routes choice
84 * node in instantiations of the rib grouping. It is assumed that this container is defined by
85 * the same model which populates it with route grouping instantiation, and by extension with
86 * the route attributes container.
87 * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
88 * @param containerClass Binding class of the container in routes choice, must not be null.
89 * @param listClass Binding class of the route list, nust not be null;
90 * @param afiClass address Family Class
91 * @param safiClass SubsequentAddressFamily
92 * @param destinationQname destination Qname
94 protected AbstractRIBSupport(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass,
95 final Class<? extends Route> listClass, final Class<? extends AddressFamily> afiClass, final Class<? extends SubsequentAddressFamily> safiClass,
96 final QName destinationQname) {
97 final QName qname = BindingReflections.findQName(containerClass).intern();
98 this.routesContainerIdentifier = new NodeIdentifier(qname);
99 this.routeAttributesIdentifier = new NodeIdentifier(QName.create(qname, Attributes.QNAME.getLocalName().intern()));
100 this.cazeClass = Preconditions.checkNotNull(cazeClass);
101 this.containerClass = Preconditions.checkNotNull(containerClass);
102 this.listClass = Preconditions.checkNotNull(listClass);
103 this.routeQname = QName.create(qname, BindingReflections.findQName(listClass).intern().getLocalName());
104 this.routesListIdentifier = new NodeIdentifier(this.routeQname);
105 this.emptyRoutes = Builders.choiceBuilder().withNodeIdentifier(ROUTES).addChild(Builders.containerBuilder()
106 .withNodeIdentifier(routesContainerIdentifier()).withChild(ImmutableNodes.mapNodeBuilder(this.routeQname).build()).build()).build();
107 this.afiClass = afiClass;
108 this.safiClass = safiClass;
109 this.destinationNid = new NodeIdentifier(destinationQname);
114 public final Class<? extends Routes> routesCaseClass() {
115 return this.cazeClass;
120 public final Class<? extends DataObject> routesContainerClass() {
121 return this.containerClass;
126 public final Class<? extends Route> routesListClass() {
127 return this.listClass;
132 public final ChoiceNode emptyRoutes() {
133 return this.emptyRoutes;
136 protected final QName routeQName() {
137 return this.routeQname;
140 protected final NodeIdentifier routeNid() {
141 return this.routesListIdentifier;
145 * Build MpReachNlri object from DOM representation.
147 * @param routes Collection of MapEntryNode DOM representation of routes
148 * @param hop CNextHop as it was parsed from Attributes, to be included in MpReach object
149 * @return MpReachNlri
151 private MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
152 final MpReachNlriBuilder mb = new MpReachNlriBuilder();
153 mb.setAfi(this.afiClass);
154 mb.setSafi(this.safiClass);
156 mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(buildDestination(routes)).build());
161 * Build MpUnReachNlri object from DOM representation.
163 * @param routes Collection of MapEntryNode DOM representation of routes
164 * @return MpUnreachNlri
166 private MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
167 final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
168 mb.setAfi(this.afiClass);
169 mb.setSafi(this.safiClass);
170 mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(buildWithdrawnDestination(routes)).build());
175 protected abstract DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes);
177 protected abstract DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes);
180 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
183 * @return Container identifier, may not be null.
185 protected final NodeIdentifier routesContainerIdentifier() {
186 return this.routesContainerIdentifier;
190 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
191 * the NLRI destination.
193 * @return Container identifier, may not be null.
195 private NodeIdentifier destinationContainerIdentifier() {
196 return this.destinationNid;
200 * Given the destination as ContainerNode, implementation needs to parse the DOM model
201 * from this point onward:
203 * {@code /bgp-mp:mp-unreach-nlri/bgp-mp:withdrawn-routes/bgp-mp:destination-type }
205 * and delete the routes from its RIBs.
207 * @param tx DOMDataWriteTransaction to be passed into implementation
208 * @param tablePath YangInstanceIdentifier to be passed into implementation
209 * @param destination ContainerNode DOM representation of NLRI in Update message
210 * @param routesNodeId NodeIdentifier
212 private void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
213 final ContainerNode destination, final NodeIdentifier routesNodeId) {
214 processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
218 * Given the destination as ContainerNode, implementation needs to parse the DOM model
219 * from this point onward:
221 * {@code /bgp-mp:mp-reach-nlri/bgp-mp:advertized-routes/bgp-mp:destination-type }
223 * and put the routes to its RIBs.
225 * @param tx DOMDataWriteTransaction to be passed into implementation
226 * @param tablePath YangInstanceIdentifier to be passed into implementation
227 * @param destination ContainerNode DOM representation of NLRI in Update message
228 * @param attributes ContainerNode to be passed into implementation
229 * @param routesNodeId NodeIdentifier
231 private void putDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
232 final ContainerNode destination, final ContainerNode attributes, final NodeIdentifier routesNodeId) {
233 processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
236 protected abstract void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath, final ContainerNode destination,
237 final ContainerNode attributes, final ApplyRoute applyFunction);
239 private static ContainerNode getDestination(final DataContainerChild<? extends PathArgument, ?> routes, final NodeIdentifier destinationId) {
240 if (routes instanceof ContainerNode) {
241 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeDestination = ((ContainerNode)routes).getChild(DESTINATION_TYPE);
242 if (maybeDestination.isPresent()) {
243 final DataContainerChild<? extends PathArgument, ?> destination = maybeDestination.get();
244 if (destination instanceof ChoiceNode) {
245 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRet = ((ChoiceNode)destination).getChild(destinationId);
246 if (maybeRet.isPresent()) {
247 final DataContainerChild<? extends PathArgument, ?> ret = maybeRet.get();
248 if (ret instanceof ContainerNode) {
249 return (ContainerNode)ret;
251 LOG.debug("Specified node {} is not a container, ignoring it", ret);
254 LOG.debug("Specified container {} is not present in destination {}", destinationId, destination);
257 LOG.warn("Destination {} is not a choice, ignoring it", destination);
260 LOG.debug("Destination is not present in routes {}", routes);
263 LOG.warn("Advertized routes {} are not a container, ignoring it", routes);
270 public final NodeIdentifier routeAttributesIdentifier() {
271 return this.routeAttributesIdentifier;
275 public final Collection<DataTreeCandidateNode> changedRoutes(final DataTreeCandidateNode routes) {
276 final DataTreeCandidateNode myRoutes = routes.getModifiedChild(this.routesContainerIdentifier);
277 if (myRoutes == null) {
278 return Collections.emptySet();
280 final DataTreeCandidateNode routesMap = myRoutes.getModifiedChild(routeNid());
281 if (routesMap == null) {
282 return Collections.emptySet();
284 // Well, given the remote possibility of augmentation, we should perform a filter here,
285 // to make sure the type matches what routeType() reports.
286 return routesMap.getChildNodes();
290 public final YangInstanceIdentifier routePath(final YangInstanceIdentifier routesPath, final PathArgument routeId) {
291 return routesPath.node(this.routesContainerIdentifier).node(routeNid()).node(routeId);
295 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri) {
296 deleteRoutes(tx, tablePath, nlri, ROUTES);
300 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri, final ContainerNode attributes) {
301 putRoutes(tx, tablePath, nlri, attributes, ROUTES);
306 public final Update buildUpdate(final Collection<MapEntryNode> advertised, final Collection<MapEntryNode> withdrawn, final Attributes attr) {
307 final UpdateBuilder ub = new UpdateBuilder();
308 final AttributesBuilder ab = new AttributesBuilder(attr);
309 final CNextHop hop = ab.getCNextHop();
311 // do not preserve next hop in attributes if we are using MpReach
312 ab.setCNextHop(null);
314 if (!advertised.isEmpty()) {
315 final MpReachNlri mb = buildReach(advertised, hop);
316 ab.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mb).build());
318 if (!withdrawn.isEmpty()) {
319 final MpUnreachNlri mb = buildUnreach(withdrawn);
320 ab.addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mb).build());
323 ub.setAttributes(ab.build());
328 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri,
329 final NodeIdentifier routesNodeId) {
330 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(WITHDRAWN_ROUTES);
331 if (maybeRoutes.isPresent()) {
332 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
333 if (destination != null) {
334 deleteDestinationRoutes(tx, tablePath, destination, routesNodeId);
337 LOG.debug("Withdrawn routes are not present in NLRI {}", nlri);
342 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri,
343 final ContainerNode attributes, final NodeIdentifier routesNodeId) {
344 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(ADVERTISED_ROUTES);
345 if (maybeRoutes.isPresent()) {
346 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
347 if (destination != null) {
348 putDestinationRoutes(tx, tablePath, destination, attributes, routesNodeId);
351 LOG.debug("Advertized routes are not present in NLRI {}", nlri);
355 private static class DeleteRoute implements ApplyRoute {
357 public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
358 final DataContainerNode<?> route, final ContainerNode attributes) {
359 tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
363 private final class PutRoute implements ApplyRoute {
365 public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
366 final DataContainerNode<?> route, final ContainerNode attributes) {
367 // Build the DataContainer data
368 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b = ImmutableNodes.mapEntryBuilder();
369 b.withNodeIdentifier(routeKey);
371 route.getValue().forEach(b::withChild);
373 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb = Builders.containerBuilder(attributes);
374 cb.withNodeIdentifier(routeAttributesIdentifier());
375 b.withChild(cb.build());
376 tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());