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