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