Verify dataobject is Route
[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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Optional;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
24 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.BgpRib;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.Rib;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.LocRib;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisherBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
55 import org.opendaylight.yangtools.yang.binding.ChildOf;
56 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
57 import org.opendaylight.yangtools.yang.binding.DataObject;
58 import org.opendaylight.yangtools.yang.binding.Identifiable;
59 import org.opendaylight.yangtools.yang.binding.Identifier;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.common.QName;
63 import org.opendaylight.yangtools.yang.common.QNameModule;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
67 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
68 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
69 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
70 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
71 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
72 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
73 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
74 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
75 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
76 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
77 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
78 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81
82 @Beta
83 public abstract class AbstractRIBSupport<
84         C extends Routes & DataObject & ChoiceIn<Tables>,
85         S extends ChildOf<? super C>,
86         R extends Route & ChildOf<? super S> & Identifiable<I>,
87         I extends Identifier<R>>
88         implements RIBSupport<C, S, R, I> {
89     public static final String ROUTE_KEY = "route-key";
90     private static final Logger LOG = LoggerFactory.getLogger(AbstractRIBSupport.class);
91     private static final NodeIdentifier ADVERTISED_ROUTES = new NodeIdentifier(AdvertizedRoutes.QNAME);
92     private static final NodeIdentifier WITHDRAWN_ROUTES = new NodeIdentifier(WithdrawnRoutes.QNAME);
93     private static final NodeIdentifier DESTINATION_TYPE = new NodeIdentifier(DestinationType.QNAME);
94     private static final InstanceIdentifier<Tables> TABLES_II = InstanceIdentifier.create(BgpRib.class)
95             .child(Rib.class).child(LocRib.class).child(Tables.class);
96     private static final NodeIdentifier ROUTES = new NodeIdentifier(Routes.QNAME);
97     private static final ApplyRoute DELETE_ROUTE = new DeleteRoute();
98     // Instance identifier to table/(choice routes)/(map of route)
99     private final LoadingCache<YangInstanceIdentifier, YangInstanceIdentifier> routesPath = CacheBuilder.newBuilder()
100             .weakValues().build(new CacheLoader<YangInstanceIdentifier, YangInstanceIdentifier>() {
101                 @Override
102                 public YangInstanceIdentifier load(@Nonnull final YangInstanceIdentifier routesTablePaths) {
103                     return routesTablePaths.node(routesContainerIdentifier()).node(routeQName());
104                 }
105             });
106     private final NodeIdentifier routesContainerIdentifier;
107     private final NodeIdentifier routesListIdentifier;
108     private final NodeIdentifier routeAttributesIdentifier;
109     private final Class<C> cazeClass;
110     private final Class<S> containerClass;
111     private final Class<R> listClass;
112     private final ApplyRoute putRoute = new PutRoute();
113     private final MapEntryNode emptyTable;
114     private final QName routeQname;
115     private final Class<? extends AddressFamily> afiClass;
116     private final Class<? extends SubsequentAddressFamily> safiClass;
117     private final NodeIdentifier destinationNid;
118     private final QName pathIdQname;
119     private final NodeIdentifier pathIdNid;
120     private final QName routeKeyQname;
121     private final NodeIdentifier prefixTypeNid;
122     private final NodeIdentifier rdNid;
123     protected final BindingNormalizedNodeSerializer mappingService;
124     protected final YangInstanceIdentifier routeDefaultYii;
125     private final TablesKey tk;
126
127     /**
128      * Default constructor. Requires the QName of the container augmented under the routes choice
129      * node in instantiations of the rib grouping. It is assumed that this container is defined by
130      * the same model which populates it with route grouping instantiation, and by extension with
131      * the route attributes container.
132      *
133      * @param mappingService   Serialization service
134      * @param cazeClass        Binding class of the AFI/SAFI-specific case statement, must not be null
135      * @param containerClass   Binding class of the container in routes choice, must not be null.
136      * @param listClass        Binding class of the route list, nust not be null;
137      * @param afiClass         address Family Class
138      * @param safiClass        SubsequentAddressFamily
139      * @param destContainerQname destination Container Qname
140      */
141     protected AbstractRIBSupport(
142             final BindingNormalizedNodeSerializer mappingService,
143             final Class<C> cazeClass,
144             final Class<S> containerClass,
145             final Class<R> listClass,
146             final Class<? extends AddressFamily> afiClass,
147             final Class<? extends SubsequentAddressFamily> safiClass,
148             final QName destContainerQname) {
149         final QNameModule module = BindingReflections.getQNameModule(cazeClass);
150         this.routesContainerIdentifier
151                 = new NodeIdentifier(BindingReflections.findQName(containerClass).withModule(module));
152         this.routeAttributesIdentifier = new NodeIdentifier(Attributes.QNAME.withModule(module));
153         this.cazeClass = requireNonNull(cazeClass);
154         this.mappingService = requireNonNull(mappingService);
155         this.containerClass = requireNonNull(containerClass);
156         this.listClass = requireNonNull(listClass);
157         this.routeQname = BindingReflections.findQName(listClass).withModule(module);
158         this.routesListIdentifier = new NodeIdentifier(this.routeQname);
159         this.tk = new TablesKey(afiClass, safiClass);
160         this.emptyTable = (MapEntryNode) this.mappingService
161                 .toNormalizedNode(TABLES_II, new TablesBuilder().withKey(tk)
162                         .setAttributes(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib
163                                 .rev180329.rib.tables.AttributesBuilder().build()).build()).getValue();
164         this.afiClass = afiClass;
165         this.safiClass = safiClass;
166         this.destinationNid = new NodeIdentifier(destContainerQname);
167         this.pathIdQname = QName.create(routeQName(), "path-id").intern();
168         this.pathIdNid = new NodeIdentifier(this.pathIdQname);
169         this.routeKeyQname = QName.create(routeQName(), ROUTE_KEY).intern();
170         this.prefixTypeNid = NodeIdentifier.create(QName.create(destContainerQname, "prefix").intern());
171         this.rdNid = NodeIdentifier.create(QName.create(destContainerQname, "route-distinguisher").intern());
172         this.routeDefaultYii =
173                 YangInstanceIdentifier.builder()
174                         .node(BgpRib.QNAME)
175                         .node(Rib.QNAME)
176                         .node(Rib.QNAME)
177                         .node(LocRib.QNAME)
178                         .node(Tables.QNAME)
179                         .node(Tables.QNAME)
180                         .node(Routes.QNAME)
181                         .node(BindingReflections.findQName(containerClass).withModule(module))
182                         .node(this.routeQname)
183                         .node(this.routeQname).build();
184     }
185
186     @Override
187     public final TablesKey getTablesKey() {
188         return this.tk;
189     }
190
191     @Override
192     public final Class<C> routesCaseClass() {
193         return this.cazeClass;
194     }
195
196     @Override
197     public final Class<S> routesContainerClass() {
198         return this.containerClass;
199     }
200
201     @Override
202     public final Class<R> routesListClass() {
203         return this.listClass;
204     }
205
206     @Override
207     public final MapEntryNode emptyTable() {
208         return this.emptyTable;
209     }
210
211     public final QName routeQName() {
212         return this.routeQname;
213     }
214
215     protected final NodeIdentifier prefixNid() {
216         return this.prefixTypeNid;
217     }
218
219     protected final NodeIdentifier routeNid() {
220         return this.routesListIdentifier;
221     }
222
223     @Override
224     public final Class<? extends AddressFamily> getAfi() {
225         return this.afiClass;
226     }
227
228     @Override
229     public final Class<? extends SubsequentAddressFamily> getSafi() {
230         return this.safiClass;
231     }
232
233     /**
234      * Build MpReachNlri object from DOM representation.
235      *
236      * @param routes Collection of MapEntryNode DOM representation of routes
237      * @param hop    CNextHop as it was parsed from Attributes, to be included in MpReach object
238      * @return MpReachNlri
239      */
240     private MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
241         final MpReachNlriBuilder mb = new MpReachNlriBuilder();
242         mb.setAfi(this.getAfi());
243         mb.setSafi(this.getSafi());
244         mb.setCNextHop(hop);
245         mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(buildDestination(routes)).build());
246         return mb.build();
247     }
248
249     /**
250      * Build MpUnReachNlri object from DOM representation.
251      *
252      * @param routes Collection of MapEntryNode DOM representation of routes
253      * @return MpUnreachNlri
254      */
255     private MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
256         final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
257         mb.setAfi(this.getAfi());
258         mb.setSafi(this.getSafi());
259         mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder()
260                 .setDestinationType(buildWithdrawnDestination(routes)).build());
261         return mb.build();
262     }
263
264     protected abstract DestinationType buildDestination(Collection<MapEntryNode> routes);
265
266     protected abstract DestinationType buildWithdrawnDestination(Collection<MapEntryNode> routes);
267
268     /**
269      * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
270      * the RIB routes.
271      *
272      * @return Container identifier, may not be null.
273      */
274     protected final NodeIdentifier routesContainerIdentifier() {
275         return this.routesContainerIdentifier;
276     }
277
278     /**
279      * Return the {@link NodeIdentifier} of the AFI/SAFI-specific container under
280      * the NLRI destination.
281      *
282      * @return Container identifier, may not be null.
283      */
284     private NodeIdentifier destinationContainerIdentifier() {
285         return this.destinationNid;
286     }
287
288     /**
289      * Given the destination as ContainerNode, implementation needs to parse the DOM model
290      * from this point onward:
291      *
292      * {@code /bgp-mp:mp-unreach-nlri/bgp-mp:withdrawn-routes/bgp-mp:destination-type}
293      * and delete the routes from its RIBs.
294      *
295      * @param tx           DOMDataWriteTransaction to be passed into implementation
296      * @param tablePath    YangInstanceIdentifier to be passed into implementation
297      * @param destination  ContainerNode DOM representation of NLRI in Update message
298      * @param routesNodeId NodeIdentifier
299      */
300     private void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
301             final ContainerNode destination, final NodeIdentifier routesNodeId) {
302         processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
303     }
304
305     /**
306      * Given the destination as ContainerNode, implementation needs to parse the DOM model
307      * from this point onward:
308      *
309      * {@code /bgp-mp:mp-reach-nlri/bgp-mp:advertized-routes/bgp-mp:destination-type}
310      * and put the routes to its RIBs.
311      *
312      * @param tx           DOMDataWriteTransaction to be passed into implementation
313      * @param tablePath    YangInstanceIdentifier to be passed into implementation
314      * @param destination  ContainerNode DOM representation of NLRI in Update message
315      * @param attributes   ContainerNode to be passed into implementation
316      * @param routesNodeId NodeIdentifier
317      * @return List of processed route identifiers
318      */
319     private Collection<NodeIdentifierWithPredicates> putDestinationRoutes(final DOMDataWriteTransaction tx,
320             final YangInstanceIdentifier tablePath, final ContainerNode destination, final ContainerNode attributes,
321             final NodeIdentifier routesNodeId) {
322         return processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
323     }
324
325     protected abstract Collection<NodeIdentifierWithPredicates> processDestination(DOMDataWriteTransaction tx,
326             YangInstanceIdentifier routesPath, ContainerNode destination, ContainerNode attributes,
327             ApplyRoute applyFunction);
328
329     private static ContainerNode getDestination(final DataContainerChild<? extends PathArgument, ?> routes,
330             final NodeIdentifier destinationId) {
331         if (routes instanceof ContainerNode) {
332             final java.util.Optional<DataContainerChild<? extends PathArgument, ?>> maybeDestination =
333                     ((ContainerNode) routes).getChild(DESTINATION_TYPE);
334             if (maybeDestination.isPresent()) {
335                 final DataContainerChild<? extends PathArgument, ?> destination = maybeDestination.get();
336                 if (destination instanceof ChoiceNode) {
337                     final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRet =
338                             ((ChoiceNode) destination).getChild(destinationId);
339                     if (maybeRet.isPresent()) {
340                         final DataContainerChild<? extends PathArgument, ?> ret = maybeRet.get();
341                         if (ret instanceof ContainerNode) {
342                             return (ContainerNode) ret;
343                         }
344
345                         LOG.debug("Specified node {} is not a container, ignoring it", ret);
346                     } else {
347                         LOG.debug("Specified container {} is not present in destination {}",
348                                 destinationId, destination);
349                     }
350                 } else {
351                     LOG.warn("Destination {} is not a choice, ignoring it", destination);
352                 }
353             } else {
354                 LOG.debug("Destination is not present in routes {}", routes);
355             }
356         } else {
357             LOG.warn("Advertized routes {} are not a container, ignoring it", routes);
358         }
359
360         return null;
361     }
362
363     @Override
364     public final NodeIdentifier routeAttributesIdentifier() {
365         return this.routeAttributesIdentifier;
366     }
367
368     @Override
369     public final Collection<DataTreeCandidateNode> changedRoutes(final DataTreeCandidateNode routes) {
370         final DataTreeCandidateNode myRoutes = routes.getModifiedChild(this.routesContainerIdentifier);
371         if (myRoutes == null) {
372             return Collections.emptySet();
373         }
374         final DataTreeCandidateNode routesMap = myRoutes.getModifiedChild(routeNid());
375         if (routesMap == null) {
376             return Collections.emptySet();
377         }
378         // Well, given the remote possibility of augmentation, we should perform a filter here,
379         // to make sure the type matches what routeType() reports.
380         return routesMap.getChildNodes();
381     }
382
383     @Override
384     public final YangInstanceIdentifier routesPath(final YangInstanceIdentifier routesTablePaths) {
385         return routesYangInstanceIdentifier(routesTablePaths.node(Routes.QNAME));
386     }
387
388     @Override
389     public final InstanceIdentifier<R> createRouteIdentifier(
390             final KeyedInstanceIdentifier<Tables, TablesKey> tableIId, final I key) {
391         //FIXME Cache
392         return tableIId.child(routesCaseClass(), routesContainerClass()).child(routesListClass(), key);
393     }
394
395     @Override
396     public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
397             final ContainerNode nlri) {
398         deleteRoutes(tx, tablePath, nlri, ROUTES);
399     }
400
401     @Override
402     public final Collection<NodeIdentifierWithPredicates> putRoutes(final DOMDataWriteTransaction tx,
403                                                                     final YangInstanceIdentifier tablePath,
404                                                                     final ContainerNode nlri,
405                                                                     final ContainerNode attributes) {
406         return putRoutes(tx, tablePath, nlri, attributes, ROUTES);
407     }
408
409     @Override
410     public final Update buildUpdate(final Collection<MapEntryNode> advertised, final Collection<MapEntryNode> withdrawn,
411             final Attributes attr) {
412         final UpdateBuilder ub = new UpdateBuilder();
413         final AttributesBuilder ab = new AttributesBuilder(attr);
414         final CNextHop hop = ab.getCNextHop();
415
416         LOG.debug("cnextHop before={}", hop);
417         // do not preserve next hop in attributes if we are using MpReach
418         ab.setCNextHop(null);
419
420         if (!advertised.isEmpty()) {
421             final MpReachNlri mb = buildReach(advertised, hop);
422             ab.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mb).build());
423             LOG.debug("mpreach nexthop={}", mb);
424         }
425         if (!withdrawn.isEmpty()) {
426             final MpUnreachNlri mb = buildUnreach(withdrawn);
427             ab.addAugmentation(Attributes2.class, new Attributes2Builder().setMpUnreachNlri(mb).build());
428             LOG.debug("mpunrach mb={}", mb);
429         }
430
431         ub.setAttributes(ab.build());
432         LOG.debug("update {}", ub.build());
433         return ub.build();
434     }
435
436     @Override
437     @SuppressWarnings("checkstyle:OverloadMethodsDeclarationOrder")
438     public final void deleteRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
439             final ContainerNode nlri, final NodeIdentifier routesNodeId) {
440         final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(WITHDRAWN_ROUTES);
441         if (maybeRoutes.isPresent()) {
442             final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
443             if (destination != null) {
444                 deleteDestinationRoutes(tx, tablePath, destination, routesNodeId);
445             }
446         } else {
447             LOG.debug("Withdrawn routes are not present in NLRI {}", nlri);
448         }
449     }
450
451     @Override
452     public final Collection<NodeIdentifierWithPredicates> putRoutes(final DOMDataWriteTransaction tx,
453                                                              final YangInstanceIdentifier tablePath,
454                                                              final ContainerNode nlri,
455                                                              final ContainerNode attributes,
456                                                              final NodeIdentifier routesNodeId) {
457         final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = nlri.getChild(ADVERTISED_ROUTES);
458         if (maybeRoutes.isPresent()) {
459             final ContainerNode destination = getDestination(maybeRoutes.get(), destinationContainerIdentifier());
460             if (destination != null) {
461                 return putDestinationRoutes(tx, tablePath, destination, attributes, routesNodeId);
462             }
463         } else {
464             LOG.debug("Advertized routes are not present in NLRI {}", nlri);
465         }
466         return Collections.emptyList();
467     }
468
469     private static final class DeleteRoute implements ApplyRoute {
470         @Override
471         public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base,
472                 final NodeIdentifierWithPredicates routeKey, final DataContainerNode<?> route,
473                 final ContainerNode attributes) {
474             tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
475         }
476     }
477
478     private final class PutRoute implements ApplyRoute {
479         @Override
480         public void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base,
481                 final NodeIdentifierWithPredicates routeKey, final DataContainerNode<?> route,
482                 final ContainerNode attributes) {
483             // Build the DataContainer data
484             final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b =
485                     ImmutableNodes.mapEntryBuilder();
486             b.withNodeIdentifier(routeKey);
487
488             route.getValue().forEach(b::withChild);
489             // Add attributes
490             final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb =
491                     Builders.containerBuilder(attributes);
492             cb.withNodeIdentifier(routeAttributesIdentifier());
493             b.withChild(cb.build());
494             tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());
495         }
496     }
497
498     protected final NodeIdentifier routePathIdNid() {
499         return this.pathIdNid;
500     }
501
502     protected final QName pathIdQName() {
503         return this.pathIdQname;
504     }
505
506     protected final QName routeKeyQName() {
507         return this.routeKeyQname;
508     }
509
510     protected final String extractPrefix(final DataContainerNode<? extends PathArgument> route) {
511         return (String) route.getChild(prefixTypeNid).get().getValue();
512     }
513
514     protected final RouteDistinguisher extractRouteDistinguisher(
515             final DataContainerNode<? extends PathArgument> route) {
516         if (route.getChild(this.rdNid).isPresent()) {
517             return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(this.rdNid).get().getValue());
518         }
519         return null;
520     }
521
522     protected final YangInstanceIdentifier routesYangInstanceIdentifier(final YangInstanceIdentifier routesTablePaths) {
523         return this.routesPath.getUnchecked(routesTablePaths);
524     }
525
526     @Override
527     public R fromNormalizedNode(final YangInstanceIdentifier routePath, final NormalizedNode<?, ?> normalizedNode) {
528         final DataObject node = this.mappingService.fromNormalizedNode(routePath, normalizedNode).getValue();
529         verify(node instanceof Route, "node %s is not a Route", node);
530         return (R) node;
531     }
532
533     @Override
534     public Attributes attributeFromContainerNode(final ContainerNode advertisedAttrs) {
535         final YangInstanceIdentifier path = this.routeDefaultYii.node(routeAttributesIdentifier());
536         return (Attributes) this.mappingService.fromNormalizedNode(path, advertisedAttrs).getValue();
537     }
538
539     @Override
540     public ContainerNode attributeToContainerNode(final YangInstanceIdentifier attPath, final Attributes attributes) {
541         final InstanceIdentifier<DataObject> iid = this.mappingService.fromYangInstanceIdentifier(attPath);
542         return (ContainerNode) this.mappingService.toNormalizedNode(iid, attributes).getValue();
543     }
544 }