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