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 public final QName routeQName() {
137 return this.routeQname;
140 protected final NodeIdentifier routeNid() {
141 return this.routesListIdentifier;
146 public final Class<? extends AddressFamily> getAfi() {
147 return this.afiClass;
152 public final Class<? extends SubsequentAddressFamily> getSafi() {
153 return this.safiClass;
157 * Build MpReachNlri object from DOM representation.
159 * @param routes Collection of MapEntryNode DOM representation of routes
160 * @param hop CNextHop as it was parsed from Attributes, to be included in MpReach object
161 * @return MpReachNlri
163 private MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
164 final MpReachNlriBuilder mb = new MpReachNlriBuilder();
165 mb.setAfi(this.getAfi());
166 mb.setSafi(this.getSafi());
168 mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(buildDestination(routes)).build());
173 * Build MpUnReachNlri object from DOM representation.
175 * @param routes Collection of MapEntryNode DOM representation of routes
176 * @return MpUnreachNlri
178 private MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
179 final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
180 mb.setAfi(this.getAfi());
181 mb.setSafi(this.getSafi());
182 mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(buildWithdrawnDestination(routes)).build());
187 protected abstract DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes);
189 protected abstract DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes);
192 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
195 * @return Container identifier, may not be null.
197 protected final NodeIdentifier routesContainerIdentifier() {
198 return this.routesContainerIdentifier;
202 * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
203 * the NLRI destination.
205 * @return Container identifier, may not be null.
207 private NodeIdentifier destinationContainerIdentifier() {
208 return this.destinationNid;
212 * Given the destination as ContainerNode, implementation needs to parse the DOM model
213 * from this point onward:
215 * {@code /bgp-mp:mp-unreach-nlri/bgp-mp:withdrawn-routes/bgp-mp:destination-type }
217 * and delete the routes from its RIBs.
219 * @param tx DOMDataWriteTransaction to be passed into implementation
220 * @param tablePath YangInstanceIdentifier to be passed into implementation
221 * @param destination ContainerNode DOM representation of NLRI in Update message
222 * @param routesNodeId NodeIdentifier
224 private void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
225 final ContainerNode destination, final NodeIdentifier routesNodeId) {
226 processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
230 * Given the destination as ContainerNode, implementation needs to parse the DOM model
231 * from this point onward:
233 * {@code /bgp-mp:mp-reach-nlri/bgp-mp:advertized-routes/bgp-mp:destination-type }
235 * and put the routes to its RIBs.
237 * @param tx DOMDataWriteTransaction to be passed into implementation
238 * @param tablePath YangInstanceIdentifier to be passed into implementation
239 * @param destination ContainerNode DOM representation of NLRI in Update message
240 * @param attributes ContainerNode to be passed into implementation
241 * @param routesNodeId NodeIdentifier
243 private void putDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
244 final ContainerNode destination, final ContainerNode attributes, final NodeIdentifier routesNodeId) {
245 processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
248 protected abstract void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath, final ContainerNode destination,
249 final ContainerNode attributes, final ApplyRoute applyFunction);
251 private static ContainerNode getDestination(final DataContainerChild<? extends PathArgument, ?> routes, final NodeIdentifier destinationId) {
252 if (routes instanceof ContainerNode) {
253 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeDestination = ((ContainerNode)routes).getChild(DESTINATION_TYPE);
254 if (maybeDestination.isPresent()) {
255 final DataContainerChild<? extends PathArgument, ?> destination = maybeDestination.get();
256 if (destination instanceof ChoiceNode) {
257 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRet = ((ChoiceNode)destination).getChild(destinationId);
258 if (maybeRet.isPresent()) {
259 final DataContainerChild<? extends PathArgument, ?> ret = maybeRet.get();
260 if (ret instanceof ContainerNode) {
261 return (ContainerNode)ret;
263 LOG.debug("Specified node {} is not a container, ignoring it", ret);
266 LOG.debug("Specified container {} is not present in destination {}", destinationId, destination);
269 LOG.warn("Destination {} is not a choice, ignoring it", destination);
272 LOG.debug("Destination is not present in routes {}", routes);
275 LOG.warn("Advertized routes {} are not a container, ignoring it", routes);
282 public final NodeIdentifier routeAttributesIdentifier() {
283 return this.routeAttributesIdentifier;
287 public final Collection<DataTreeCandidateNode> changedRoutes(final DataTreeCandidateNode routes) {
288 final DataTreeCandidateNode myRoutes = routes.getModifiedChild(this.routesContainerIdentifier);
289 if (myRoutes == null) {
290 return Collections.emptySet();
292 final DataTreeCandidateNode routesMap = myRoutes.getModifiedChild(routeNid());
293 if (routesMap == null) {
294 return Collections.emptySet();
296 // Well, given the remote possibility of augmentation, we should perform a filter here,
297 // to make sure the type matches what routeType() reports.
298 return routesMap.getChildNodes();
302 public final YangInstanceIdentifier routePath(final YangInstanceIdentifier routesPath, final PathArgument routeId) {
303 return routesPath.node(this.routesContainerIdentifier).node(routeNid()).node(routeId);
307 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri) {
308 deleteRoutes(tx, tablePath, nlri, ROUTES);
312 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri, final ContainerNode attributes) {
313 putRoutes(tx, tablePath, nlri, attributes, ROUTES);
318 public final Update buildUpdate(final Collection<MapEntryNode> advertised, final Collection<MapEntryNode> withdrawn, final Attributes attr) {
319 final UpdateBuilder ub = new UpdateBuilder();
320 final AttributesBuilder ab = new AttributesBuilder(attr);
321 final CNextHop hop = ab.getCNextHop();
323 LOG.debug("cnextHop before={}", hop);
324 // do not preserve next hop in attributes if we are using MpReach
325 ab.setCNextHop(null);
327 if (!advertised.isEmpty()) {
328 final MpReachNlri mb = buildReach(advertised, hop);
329 ab.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mb).build());
330 LOG.debug("mpreach nexthop={}", mb);
332 if (!withdrawn.isEmpty()) {
333 final MpUnreachNlri mb = buildUnreach(withdrawn);
334 ab.addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mb).build());
335 LOG.debug("mpunrach mb={}", mb);
338 ub.setAttributes(ab.build());
339 LOG.debug("update {}", ub.build());
344 public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri,
345 final NodeIdentifier routesNodeId) {
346 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(WITHDRAWN_ROUTES);
347 if (maybeRoutes.isPresent()) {
348 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
349 if (destination != null) {
350 deleteDestinationRoutes(tx, tablePath, destination, routesNodeId);
353 LOG.debug("Withdrawn routes are not present in NLRI {}", nlri);
358 public final void putRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath, final ContainerNode nlri,
359 final ContainerNode attributes, final NodeIdentifier routesNodeId) {
360 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(ADVERTISED_ROUTES);
361 if (maybeRoutes.isPresent()) {
362 final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
363 if (destination != null) {
364 putDestinationRoutes(tx, tablePath, destination, attributes, routesNodeId);
367 LOG.debug("Advertized routes are not present in NLRI {}", nlri);
371 private static final class DeleteRoute implements ApplyRoute {
373 public final void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
374 final DataContainerNode<?> route, final ContainerNode attributes) {
375 tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
379 private final class PutRoute implements ApplyRoute {
381 public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
382 final DataContainerNode<?> route, final ContainerNode attributes) {
383 // Build the DataContainer data
384 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b = ImmutableNodes.mapEntryBuilder();
385 b.withNodeIdentifier(routeKey);
387 route.getValue().forEach(b::withChild);
389 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb = Builders.containerBuilder(attributes);
390 cb.withNodeIdentifier(routeAttributesIdentifier());
391 b.withChild(cb.build());
392 tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());