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.rev130919.Update;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.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, final Class<? extends DataObject> containerClass,
96 final Class<? extends Route> listClass, final Class<? extends AddressFamily> afiClass, final Class<? extends SubsequentAddressFamily> safiClass,
97 final QName destinationQname) {
98 final QName qname = BindingReflections.findQName(containerClass).intern();
99 this.routesContainerIdentifier = new NodeIdentifier(qname);
100 this.routeAttributesIdentifier = new NodeIdentifier(QName.create(qname, Attributes.QNAME.getLocalName().intern()));
101 this.cazeClass = requireNonNull(cazeClass);
102 this.containerClass = requireNonNull(containerClass);
103 this.listClass = requireNonNull(listClass);
104 this.routeQname = QName.create(qname, BindingReflections.findQName(listClass).intern().getLocalName());
105 this.routesListIdentifier = new NodeIdentifier(this.routeQname);
106 this.emptyRoutes = Builders.choiceBuilder().withNodeIdentifier(ROUTES).addChild(Builders.containerBuilder()
107 .withNodeIdentifier(routesContainerIdentifier()).withChild(ImmutableNodes.mapNodeBuilder(this.routeQname).build()).build()).build();
108 this.afiClass = afiClass;
109 this.safiClass = safiClass;
110 this.destinationNid = new NodeIdentifier(destinationQname);
115 public final Class<? extends Routes> routesCaseClass() {
116 return this.cazeClass;
121 public final Class<? extends DataObject> routesContainerClass() {
122 return this.containerClass;
127 public final Class<? extends Route> routesListClass() {
128 return this.listClass;
133 public final ChoiceNode emptyRoutes() {
134 return this.emptyRoutes;
137 public final QName routeQName() {
138 return this.routeQname;
141 protected final NodeIdentifier routeNid() {
142 return this.routesListIdentifier;
147 public final Class<? extends AddressFamily> getAfi() {
148 return this.afiClass;
153 public final Class<? extends SubsequentAddressFamily> getSafi() {
154 return this.safiClass;
158 * Build MpReachNlri object from DOM representation.
160 * @param routes Collection of MapEntryNode DOM representation of routes
161 * @param hop CNextHop as it was parsed from Attributes, to be included in MpReach object
162 * @return MpReachNlri
164 private MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
165 final MpReachNlriBuilder mb = new MpReachNlriBuilder();
166 mb.setAfi(this.getAfi());
167 mb.setSafi(this.getSafi());
169 mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(buildDestination(routes)).build());
174 * Build MpUnReachNlri object from DOM representation.
176 * @param routes Collection of MapEntryNode DOM representation of routes
177 * @return MpUnreachNlri
179 private MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
180 final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
181 mb.setAfi(this.getAfi());
182 mb.setSafi(this.getSafi());
183 mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(buildWithdrawnDestination(routes)).build());
188 protected abstract DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes);
190 protected abstract DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes);
193 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
196 * @return Container identifier, may not be null.
198 protected final NodeIdentifier routesContainerIdentifier() {
199 return this.routesContainerIdentifier;
203 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
204 * the NLRI destination.
206 * @return Container identifier, may not be null.
208 private NodeIdentifier destinationContainerIdentifier() {
209 return this.destinationNid;
213 * Given the destination as ContainerNode, implementation needs to parse the DOM model
214 * from this point onward:
216 * {@code /bgp-mp:mp-unreach-nlri/bgp-mp:withdrawn-routes/bgp-mp:destination-type }
218 * and delete the routes from its RIBs.
220 * @param tx DOMDataWriteTransaction to be passed into implementation
221 * @param tablePath YangInstanceIdentifier to be passed into implementation
222 * @param destination ContainerNode DOM representation of NLRI in Update message
223 * @param routesNodeId NodeIdentifier
225 private void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
226 final ContainerNode destination, final NodeIdentifier routesNodeId) {
227 processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
231 * Given the destination as ContainerNode, implementation needs to parse the DOM model
232 * from this point onward:
234 * {@code /bgp-mp:mp-reach-nlri/bgp-mp:advertized-routes/bgp-mp:destination-type }
236 * and put the routes to its RIBs.
238 * @param tx DOMDataWriteTransaction to be passed into implementation
239 * @param tablePath YangInstanceIdentifier to be passed into implementation
240 * @param destination ContainerNode DOM representation of NLRI in Update message
241 * @param attributes ContainerNode to be passed into implementation
242 * @param routesNodeId NodeIdentifier
244 private void putDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
245 final ContainerNode destination, final ContainerNode attributes, final NodeIdentifier routesNodeId) {
246 processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
249 protected abstract void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath, final ContainerNode destination,
250 final ContainerNode attributes, final ApplyRoute applyFunction);
252 private static ContainerNode getDestination(final DataContainerChild<? extends PathArgument, ?> routes, final NodeIdentifier destinationId) {
253 if (routes instanceof ContainerNode) {
254 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeDestination = ((ContainerNode)routes).getChild(DESTINATION_TYPE);
255 if (maybeDestination.isPresent()) {
256 final DataContainerChild<? extends PathArgument, ?> destination = maybeDestination.get();
257 if (destination instanceof ChoiceNode) {
258 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRet = ((ChoiceNode)destination).getChild(destinationId);
259 if (maybeRet.isPresent()) {
260 final DataContainerChild<? extends PathArgument, ?> ret = maybeRet.get();
261 if (ret instanceof ContainerNode) {
262 return (ContainerNode)ret;
265 LOG.debug("Specified node {} is not a container, ignoring it", ret);
267 LOG.debug("Specified container {} is not present in destination {}", destinationId, destination);
270 LOG.warn("Destination {} is not a choice, ignoring it", destination);
273 LOG.debug("Destination is not present in routes {}", routes);
276 LOG.warn("Advertized routes {} are not a container, ignoring it", routes);
283 public final NodeIdentifier routeAttributesIdentifier() {
284 return this.routeAttributesIdentifier;
288 public final Collection<DataTreeCandidateNode> changedRoutes(final DataTreeCandidateNode routes) {
289 final DataTreeCandidateNode myRoutes = routes.getModifiedChild(this.routesContainerIdentifier);
290 if (myRoutes == null) {
291 return Collections.emptySet();
293 final DataTreeCandidateNode routesMap = myRoutes.getModifiedChild(routeNid());
294 if (routesMap == null) {
295 return Collections.emptySet();
297 // Well, given the remote possibility of augmentation, we should perform a filter here,
298 // to make sure the type matches what routeType() reports.
299 return routesMap.getChildNodes();
303 public final YangInstanceIdentifier routePath(final YangInstanceIdentifier routesPath, final PathArgument routeId) {
304 return routesPath.node(this.routesContainerIdentifier).node(routeNid()).node(routeId);
308 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri) {
309 deleteRoutes(tx, tablePath, nlri, ROUTES);
313 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri, final ContainerNode attributes) {
314 putRoutes(tx, tablePath, nlri, attributes, ROUTES);
319 public final Update buildUpdate(final Collection<MapEntryNode> advertised, final Collection<MapEntryNode> withdrawn, final Attributes attr) {
320 final UpdateBuilder ub = new UpdateBuilder();
321 final AttributesBuilder ab = new AttributesBuilder(attr);
322 final CNextHop hop = ab.getCNextHop();
324 LOG.debug("cnextHop before={}", hop);
325 // do not preserve next hop in attributes if we are using MpReach
326 ab.setCNextHop(null);
328 if (!advertised.isEmpty()) {
329 final MpReachNlri mb = buildReach(advertised, hop);
330 ab.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mb).build());
331 LOG.debug("mpreach nexthop={}", mb);
333 if (!withdrawn.isEmpty()) {
334 final MpUnreachNlri mb = buildUnreach(withdrawn);
335 ab.addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mb).build());
336 LOG.debug("mpunrach mb={}", mb);
339 ub.setAttributes(ab.build());
340 LOG.debug("update {}", ub.build());
345 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri,
346 final NodeIdentifier routesNodeId) {
347 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(WITHDRAWN_ROUTES);
348 if (maybeRoutes.isPresent()) {
349 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
350 if (destination != null) {
351 deleteDestinationRoutes(tx, tablePath, destination, routesNodeId);
354 LOG.debug("Withdrawn routes are not present in NLRI {}", nlri);
359 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri,
360 final ContainerNode attributes, final NodeIdentifier routesNodeId) {
361 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(ADVERTISED_ROUTES);
362 if (maybeRoutes.isPresent()) {
363 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
364 if (destination != null) {
365 putDestinationRoutes(tx, tablePath, destination, attributes, routesNodeId);
368 LOG.debug("Advertized routes are not present in NLRI {}", nlri);
372 private static final class DeleteRoute implements ApplyRoute {
374 public final void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
375 final DataContainerNode<?> route, final ContainerNode attributes) {
376 tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
380 private final class PutRoute implements ApplyRoute {
382 public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
383 final DataContainerNode<?> route, final ContainerNode attributes) {
384 // Build the DataContainer data
385 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b = ImmutableNodes.mapEntryBuilder();
386 b.withNodeIdentifier(routeKey);
388 route.getValue().forEach(b::withChild);
390 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb = Builders.containerBuilder(attributes);
391 cb.withNodeIdentifier(routeAttributesIdentifier());
392 b.withChild(cb.build());
393 tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());