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 static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.Collections;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Update;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.UpdateBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpReachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
55 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
56 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
57 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 public abstract class AbstractRIBSupport implements RIBSupport {
63 private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBSupport.class);
64 private static final NodeIdentifier ADVERTISED_ROUTES = new NodeIdentifier(AdvertizedRoutes.QNAME);
65 private static final NodeIdentifier WITHDRAWN_ROUTES = new NodeIdentifier(WithdrawnRoutes.QNAME);
66 private static final NodeIdentifier DESTINATION_TYPE = new NodeIdentifier(DestinationType.QNAME);
67 private static final NodeIdentifier ROUTES = new NodeIdentifier(Routes.QNAME);
68 private static final ApplyRoute DELETE_ROUTE = new DeleteRoute();
70 private final NodeIdentifier routesContainerIdentifier;
71 private final NodeIdentifier routesListIdentifier;
72 private final NodeIdentifier routeAttributesIdentifier;
73 private final Class<? extends Routes> cazeClass;
74 private final Class<? extends DataObject> containerClass;
75 private final Class<? extends Route> listClass;
76 private final ApplyRoute putRoute = new PutRoute();
77 private final ChoiceNode emptyRoutes;
78 private final QName routeQname;
79 private final Class<? extends AddressFamily> afiClass;
80 private final Class<? extends SubsequentAddressFamily> safiClass;
81 private final NodeIdentifier destinationNid;
84 * Default constructor. Requires the QName of the container augmented under the routes choice
85 * node in instantiations of the rib grouping. It is assumed that this container is defined by
86 * the same model which populates it with route grouping instantiation, and by extension with
87 * the route attributes container.
88 * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
89 * @param containerClass Binding class of the container in routes choice, must not be null.
90 * @param listClass Binding class of the route list, nust not be null;
91 * @param afiClass address Family Class
92 * @param safiClass SubsequentAddressFamily
93 * @param destinationQname destination Qname
95 protected AbstractRIBSupport(final Class<? extends Routes> cazeClass,
96 final Class<? extends DataObject> containerClass,
97 final Class<? extends Route> listClass, final Class<? extends AddressFamily> afiClass,
98 final Class<? extends SubsequentAddressFamily> safiClass,
99 final QName destinationQname) {
100 final QName qname = BindingReflections.findQName(containerClass).intern();
101 this.routesContainerIdentifier = new NodeIdentifier(qname);
102 this.routeAttributesIdentifier = new NodeIdentifier(QName.create(qname,
103 Attributes.QNAME.getLocalName().intern()));
104 this.cazeClass = requireNonNull(cazeClass);
105 this.containerClass = requireNonNull(containerClass);
106 this.listClass = requireNonNull(listClass);
107 this.routeQname = QName.create(qname, BindingReflections.findQName(listClass).intern().getLocalName());
108 this.routesListIdentifier = new NodeIdentifier(this.routeQname);
109 this.emptyRoutes = Builders.choiceBuilder().withNodeIdentifier(ROUTES).addChild(Builders.containerBuilder()
110 .withNodeIdentifier(routesContainerIdentifier()).withChild(ImmutableNodes.mapNodeBuilder(this.routeQname)
111 .build()).build()).build();
112 this.afiClass = afiClass;
113 this.safiClass = safiClass;
114 this.destinationNid = new NodeIdentifier(destinationQname);
119 public final Class<? extends Routes> routesCaseClass() {
120 return this.cazeClass;
125 public final Class<? extends DataObject> routesContainerClass() {
126 return this.containerClass;
131 public final Class<? extends Route> routesListClass() {
132 return this.listClass;
137 public final ChoiceNode emptyRoutes() {
138 return this.emptyRoutes;
141 public final QName routeQName() {
142 return this.routeQname;
145 protected final NodeIdentifier routeNid() {
146 return this.routesListIdentifier;
151 public final Class<? extends AddressFamily> getAfi() {
152 return this.afiClass;
157 public final Class<? extends SubsequentAddressFamily> getSafi() {
158 return this.safiClass;
162 * Build MpReachNlri object from DOM representation.
164 * @param routes Collection of MapEntryNode DOM representation of routes
165 * @param hop CNextHop as it was parsed from Attributes, to be included in MpReach object
166 * @return MpReachNlri
168 private MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
169 final MpReachNlriBuilder mb = new MpReachNlriBuilder();
170 mb.setAfi(this.getAfi());
171 mb.setSafi(this.getSafi());
173 mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(buildDestination(routes)).build());
178 * Build MpUnReachNlri object from DOM representation.
180 * @param routes Collection of MapEntryNode DOM representation of routes
181 * @return MpUnreachNlri
183 private MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
184 final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
185 mb.setAfi(this.getAfi());
186 mb.setSafi(this.getSafi());
187 mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder()
188 .setDestinationType(buildWithdrawnDestination(routes)).build());
193 protected abstract DestinationType buildDestination(@Nonnull Collection<MapEntryNode> routes);
196 protected abstract DestinationType buildWithdrawnDestination(@Nonnull Collection<MapEntryNode> routes);
199 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
202 * @return Container identifier, may not be null.
204 protected final NodeIdentifier routesContainerIdentifier() {
205 return this.routesContainerIdentifier;
209 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
210 * the NLRI destination.
212 * @return Container identifier, may not be null.
214 private NodeIdentifier destinationContainerIdentifier() {
215 return this.destinationNid;
219 * Given the destination as ContainerNode, implementation needs to parse the DOM model
220 * from this point onward:
222 * {@code /bgp-mp:mp-unreach-nlri/bgp-mp:withdrawn-routes/bgp-mp:destination-type}
223 * and delete the routes from 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 routesNodeId NodeIdentifier
230 private void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
231 final ContainerNode destination, final NodeIdentifier routesNodeId) {
232 processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
236 * Given the destination as ContainerNode, implementation needs to parse the DOM model
237 * from this point onward:
239 * {@code /bgp-mp:mp-reach-nlri/bgp-mp:advertized-routes/bgp-mp:destination-type}
240 * and put the routes to its RIBs.
242 * @param tx DOMDataWriteTransaction to be passed into implementation
243 * @param tablePath YangInstanceIdentifier to be passed into implementation
244 * @param destination ContainerNode DOM representation of NLRI in Update message
245 * @param attributes ContainerNode to be passed into implementation
246 * @param routesNodeId NodeIdentifier
248 private void putDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
249 final ContainerNode destination, final ContainerNode attributes, final NodeIdentifier routesNodeId) {
250 processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
253 protected abstract void processDestination(DOMDataWriteTransaction tx, YangInstanceIdentifier routesPath,
254 ContainerNode destination, ContainerNode attributes, ApplyRoute applyFunction);
256 private static ContainerNode getDestination(final DataContainerChild<? extends PathArgument, ?> routes,
257 final NodeIdentifier destinationId) {
258 if (routes instanceof ContainerNode) {
259 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeDestination =
260 ((ContainerNode) routes).getChild(DESTINATION_TYPE);
261 if (maybeDestination.isPresent()) {
262 final DataContainerChild<? extends PathArgument, ?> destination = maybeDestination.get();
263 if (destination instanceof ChoiceNode) {
264 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRet =
265 ((ChoiceNode) destination).getChild(destinationId);
266 if (maybeRet.isPresent()) {
267 final DataContainerChild<? extends PathArgument, ?> ret = maybeRet.get();
268 if (ret instanceof ContainerNode) {
269 return (ContainerNode) ret;
272 LOG.debug("Specified node {} is not a container, ignoring it", ret);
274 LOG.debug("Specified container {} is not present in destination {}",
275 destinationId, destination);
278 LOG.warn("Destination {} is not a choice, ignoring it", destination);
281 LOG.debug("Destination is not present in routes {}", routes);
284 LOG.warn("Advertized routes {} are not a container, ignoring it", routes);
291 public final NodeIdentifier routeAttributesIdentifier() {
292 return this.routeAttributesIdentifier;
296 public final Collection<DataTreeCandidateNode> changedRoutes(final DataTreeCandidateNode routes) {
297 final DataTreeCandidateNode myRoutes = routes.getModifiedChild(this.routesContainerIdentifier);
298 if (myRoutes == null) {
299 return Collections.emptySet();
301 final DataTreeCandidateNode routesMap = myRoutes.getModifiedChild(routeNid());
302 if (routesMap == null) {
303 return Collections.emptySet();
305 // Well, given the remote possibility of augmentation, we should perform a filter here,
306 // to make sure the type matches what routeType() reports.
307 return routesMap.getChildNodes();
311 public final YangInstanceIdentifier routePath(final YangInstanceIdentifier routesPath, final PathArgument routeId) {
312 return routesPath.node(this.routesContainerIdentifier).node(routeNid()).node(routeId);
316 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
317 final ContainerNode nlri) {
318 deleteRoutes(tx, tablePath, nlri, ROUTES);
322 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
323 final ContainerNode nlri, final ContainerNode attributes) {
324 putRoutes(tx, tablePath, nlri, attributes, ROUTES);
329 public final Update buildUpdate(final Collection<MapEntryNode> advertised, final Collection<MapEntryNode> withdrawn,
330 final Attributes attr) {
331 final UpdateBuilder ub = new UpdateBuilder();
332 final AttributesBuilder ab = new AttributesBuilder(attr);
333 final CNextHop hop = ab.getCNextHop();
335 LOG.debug("cnextHop before={}", hop);
336 // do not preserve next hop in attributes if we are using MpReach
337 ab.setCNextHop(null);
339 if (!advertised.isEmpty()) {
340 final MpReachNlri mb = buildReach(advertised, hop);
341 ab.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mb).build());
342 LOG.debug("mpreach nexthop={}", mb);
344 if (!withdrawn.isEmpty()) {
345 final MpUnreachNlri mb = buildUnreach(withdrawn);
346 ab.addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mb).build());
347 LOG.debug("mpunrach mb={}", mb);
350 ub.setAttributes(ab.build());
351 LOG.debug("update {}", ub.build());
356 @SuppressWarnings("checkstyle:OverloadMethodsDeclarationOrder")
357 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
358 final ContainerNode nlri, final NodeIdentifier routesNodeId) {
359 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(WITHDRAWN_ROUTES);
360 if (maybeRoutes.isPresent()) {
361 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
362 if (destination != null) {
363 deleteDestinationRoutes(tx, tablePath, destination, routesNodeId);
366 LOG.debug("Withdrawn routes are not present in NLRI {}", nlri);
371 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
372 final ContainerNode nlri, final ContainerNode attributes, final NodeIdentifier routesNodeId) {
373 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(ADVERTISED_ROUTES);
374 if (maybeRoutes.isPresent()) {
375 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
376 if (destination != null) {
377 putDestinationRoutes(tx, tablePath, destination, attributes, routesNodeId);
380 LOG.debug("Advertized routes are not present in NLRI {}", nlri);
384 private static final class DeleteRoute implements ApplyRoute {
386 public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base,
387 final NodeIdentifierWithPredicates routeKey, final DataContainerNode<?> route,
388 final ContainerNode attributes) {
389 tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
393 private final class PutRoute implements ApplyRoute {
395 public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base,
396 final NodeIdentifierWithPredicates routeKey, final DataContainerNode<?> route,
397 final ContainerNode attributes) {
398 // Build the DataContainer data
399 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b =
400 ImmutableNodes.mapEntryBuilder();
401 b.withNodeIdentifier(routeKey);
403 route.getValue().forEach(b::withChild);
405 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb =
406 Builders.containerBuilder(attributes);
407 cb.withNodeIdentifier(routeAttributesIdentifier());
408 b.withChild(cb.build());
409 tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());