1ce14aee89b824eb84d7b3d3e134f21b9406678d
[netconf.git] / opendaylight / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / restconf / impl / BrokerFacade.java
1 /*
2  * Copyright (c) 2014 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.netconf.sal.restconf.impl;
9
10 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import javax.ws.rs.core.Response.Status;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
32 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
33 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
34 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
35 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
37 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
39 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
40 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
41 import org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class BrokerFacade {
55     private final static Logger LOG = LoggerFactory.getLogger(BrokerFacade.class);
56
57     private final static BrokerFacade INSTANCE = new BrokerFacade();
58     private volatile DOMRpcService rpcService;
59     private volatile ConsumerSession context;
60     private DOMDataBroker domDataBroker;
61     private DOMNotificationService domNotification;
62
63     private BrokerFacade() {
64     }
65
66     public void setRpcService(final DOMRpcService router) {
67         this.rpcService = router;
68     }
69
70     public void setDomNotificationService(final DOMNotificationService domNotification) {
71         this.domNotification = domNotification;
72     }
73
74     public void setContext(final ConsumerSession context) {
75         this.context = context;
76     }
77
78     public static BrokerFacade getInstance() {
79         return BrokerFacade.INSTANCE;
80     }
81
82     private void checkPreconditions() {
83         if ((this.context == null) || (this.domDataBroker == null)) {
84             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
85         }
86     }
87
88     // READ configuration
89     public NormalizedNode<?, ?> readConfigurationData(final YangInstanceIdentifier path) {
90         checkPreconditions();
91         return readDataViaTransaction(this.domDataBroker.newReadOnlyTransaction(), CONFIGURATION, path);
92     }
93
94     public NormalizedNode<?, ?> readConfigurationData(final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
95         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
96         if (domDataBrokerService.isPresent()) {
97             return readDataViaTransaction(domDataBrokerService.get().newReadOnlyTransaction(), CONFIGURATION, path);
98         }
99         final String errMsg = "DOM data broker service isn't available for mount point " + path;
100         LOG.warn(errMsg);
101         throw new RestconfDocumentedException(errMsg);
102     }
103
104     // READ operational
105     public NormalizedNode<?, ?> readOperationalData(final YangInstanceIdentifier path) {
106         checkPreconditions();
107         return readDataViaTransaction(this.domDataBroker.newReadOnlyTransaction(), OPERATIONAL, path);
108     }
109
110     public NormalizedNode<?, ?> readOperationalData(final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
111         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
112         if (domDataBrokerService.isPresent()) {
113             return readDataViaTransaction(domDataBrokerService.get().newReadOnlyTransaction(), OPERATIONAL, path);
114         }
115         final String errMsg = "DOM data broker service isn't available for mount point " + path;
116         LOG.warn(errMsg);
117         throw new RestconfDocumentedException(errMsg);
118     }
119
120     // PUT configuration
121     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPut(
122             final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
123         checkPreconditions();
124         return putDataViaTransaction(this.domDataBroker, CONFIGURATION, path, payload, globalSchema);
125     }
126
127     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPut(
128             final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
129         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
130         if (domDataBrokerService.isPresent()) {
131             return putDataViaTransaction(domDataBrokerService.get(), CONFIGURATION, path,
132                     payload, mountPoint.getSchemaContext());
133         }
134         final String errMsg = "DOM data broker service isn't available for mount point " + path;
135         LOG.warn(errMsg);
136         throw new RestconfDocumentedException(errMsg);
137     }
138
139     public PATCHStatusContext patchConfigurationDataWithinTransaction(final PATCHContext context,
140                                                                       final SchemaContext globalSchema)
141             throws TransactionCommitFailedException {
142         final DOMDataReadWriteTransaction patchTransaction = this.domDataBroker.newReadWriteTransaction();
143         final List<PATCHStatusEntity> editCollection = new ArrayList<>();
144         List<RestconfError> editErrors;
145         final List<RestconfError> globalErrors = null;
146         int errorCounter = 0;
147
148         for (final PATCHEntity patchEntity : context.getData()) {
149             final PATCHEditOperation operation = PATCHEditOperation.valueOf(patchEntity.getOperation().toUpperCase());
150
151             switch (operation) {
152                 case CREATE:
153                     if (errorCounter == 0) {
154                         try {
155                             postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
156                                     patchEntity.getNode(), globalSchema);
157                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
158                         } catch (final RestconfDocumentedException e) {
159                             editErrors = new ArrayList<>();
160                             editErrors.addAll(e.getErrors());
161                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
162                             errorCounter++;
163                         }
164                     }
165                     break;
166                 case REPLACE:
167                     if (errorCounter == 0) {
168                         try {
169                             putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
170                                     .getTargetNode(), patchEntity.getNode(), globalSchema);
171                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
172                         } catch (final RestconfDocumentedException e) {
173                             editErrors = new ArrayList<>();
174                             editErrors.addAll(e.getErrors());
175                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
176                             errorCounter++;
177                         }
178                     }
179                     break;
180                 case DELETE:
181                     if (errorCounter == 0) {
182                         try {
183                             deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
184                                     .getTargetNode());
185                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
186                         } catch (final RestconfDocumentedException e) {
187                             editErrors = new ArrayList<>();
188                             editErrors.addAll(e.getErrors());
189                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
190                             errorCounter++;
191                         }
192                     }
193                     break;
194                 case REMOVE:
195                     if (errorCounter == 0) {
196                         try {
197                             deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
198                                     .getTargetNode());
199                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
200                         } catch (final RestconfDocumentedException e) {
201                             LOG.error("Error removing {} by {} operation", patchEntity.getTargetNode().toString(),
202                                     patchEntity.getEditId(), e);
203                         }
204                     }
205                     break;
206                 case MERGE:
207                     if (errorCounter == 0) {
208                         try {
209                             mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
210                                     patchEntity.getNode(), globalSchema);
211                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
212                         } catch (final RestconfDocumentedException e) {
213                             editErrors = new ArrayList<>();
214                             editErrors.addAll(e.getErrors());
215                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
216                             errorCounter++;
217                         }
218                     }
219                     break;
220             }
221         }
222
223         //TODO: make sure possible global errors are filled up correctly and decide transaction submission based on that
224         //globalErrors = new ArrayList<>();
225         if (errorCounter == 0) {
226             patchTransaction.submit().checkedGet();
227             return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true,
228                     globalErrors);
229         } else {
230             patchTransaction.cancel();
231             return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false,
232                     globalErrors);
233         }
234     }
235
236     // POST configuration
237     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
238             final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
239         checkPreconditions();
240         return postDataViaTransaction(this.domDataBroker, CONFIGURATION, path, payload, globalSchema);
241     }
242
243     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
244             final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
245         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
246         if (domDataBrokerService.isPresent()) {
247             return postDataViaTransaction(domDataBrokerService.get(), CONFIGURATION, path,
248                     payload, mountPoint.getSchemaContext());
249         }
250         final String errMsg = "DOM data broker service isn't available for mount point " + path;
251         LOG.warn(errMsg);
252         throw new RestconfDocumentedException(errMsg);
253     }
254
255     // DELETE configuration
256     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
257             final YangInstanceIdentifier path) {
258         checkPreconditions();
259         return deleteDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path);
260     }
261
262     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
263             final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
264         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
265         if (domDataBrokerService.isPresent()) {
266             return deleteDataViaTransaction(domDataBrokerService.get().newReadWriteTransaction(), CONFIGURATION, path);
267         }
268         final String errMsg = "DOM data broker service isn't available for mount point " + path;
269         LOG.warn(errMsg);
270         throw new RestconfDocumentedException(errMsg);
271     }
272
273     // RPC
274     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
275         checkPreconditions();
276         if (this.rpcService == null) {
277             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
278         }
279         LOG.trace("Invoke RPC {} with input: {}", type, input);
280         return this.rpcService.invokeRpc(type, input);
281     }
282
283     public void registerToListenDataChanges(final LogicalDatastoreType datastore, final DataChangeScope scope,
284             final ListenerAdapter listener) {
285         checkPreconditions();
286
287         if (listener.isListening()) {
288             return;
289         }
290
291         final YangInstanceIdentifier path = listener.getPath();
292         final ListenerRegistration<DOMDataChangeListener> registration = this.domDataBroker.registerDataChangeListener(
293                 datastore, path, listener, scope);
294
295         listener.setRegistration(registration);
296     }
297
298     private NormalizedNode<?, ?> readDataViaTransaction(final DOMDataReadTransaction transaction,
299             final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
300         LOG.trace("Read {} via Restconf: {}", datastore.name(), path);
301         final ListenableFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = transaction.read(datastore, path);
302         if (listenableFuture != null) {
303             Optional<NormalizedNode<?, ?>> optional;
304             try {
305                 LOG.debug("Reading result data from transaction.");
306                 optional = listenableFuture.get();
307             } catch (InterruptedException | ExecutionException e) {
308                 LOG.warn("Exception by reading {} via Restconf: {}", datastore.name(), path, e);
309                 throw new RestconfDocumentedException("Problem to get data from transaction.", e.getCause());
310
311             }
312             if (optional != null) {
313                 if (optional.isPresent()) {
314                     return optional.get();
315                 }
316             }
317         }
318         return null;
319     }
320
321     private CheckedFuture<Void, TransactionCommitFailedException> postDataViaTransaction(
322             final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore,
323             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
324         // FIXME: This is doing correct post for container and list children
325         //        not sure if this will work for choice case
326         DOMDataReadWriteTransaction transaction = domDataBroker.newReadWriteTransaction();
327         if(payload instanceof MapNode) {
328             LOG.trace("POST {} via Restconf: {} with payload {}", datastore.name(), path, payload);
329             final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
330             try {
331                 transaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
332             } catch (RuntimeException e) {
333                 // FIXME: Figure out and catch specific RunTimeExceptions thrown by NETCONF instead of generic one.
334                 //        to make this cleaner and easier to maintain.
335                 transaction.cancel();
336                 transaction = domDataBroker.newReadWriteTransaction();
337                 LOG.debug("Empty subtree merge failed", e);
338             }
339             if (!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
340                 transaction.cancel();
341                 transaction = domDataBroker.newReadWriteTransaction();
342             }
343             for(final MapEntryNode child : ((MapNode) payload).getValue()) {
344                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
345                 checkItemDoesNotExists(transaction, datastore, childPath);
346                 transaction.put(datastore, childPath, child);
347             }
348         } else {
349             checkItemDoesNotExists(transaction,datastore, path);
350             if(!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
351                 transaction.cancel();
352                 transaction = domDataBroker.newReadWriteTransaction();
353             }
354             transaction.put(datastore, path, payload);
355         }
356         return transaction.submit();
357     }
358
359     private void postDataWithinTransaction(
360             final DOMDataReadWriteTransaction rWTransaction, final LogicalDatastoreType datastore,
361             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
362         // FIXME: This is doing correct post for container and list children
363         //        not sure if this will work for choice case
364         if(payload instanceof MapNode) {
365             LOG.trace("POST {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
366             final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
367             rWTransaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
368             ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
369             for(final MapEntryNode child : ((MapNode) payload).getValue()) {
370                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
371                 checkItemDoesNotExists(rWTransaction, datastore, childPath);
372                 rWTransaction.put(datastore, childPath, child);
373             }
374         } else {
375             checkItemDoesNotExists(rWTransaction,datastore, path);
376             ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
377             rWTransaction.put(datastore, path, payload);
378         }
379     }
380
381     private void checkItemExists(final DOMDataReadWriteTransaction rWTransaction,
382                                  final LogicalDatastoreType store, final YangInstanceIdentifier path) {
383         final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
384         try {
385             if (!futureDatastoreData.get()) {
386                 final String errMsg = "Operation via Restconf was not executed because data does not exist";
387                 LOG.trace("{}:{}", errMsg, path);
388                 rWTransaction.cancel();
389                 throw new RestconfDocumentedException("Data does not exist for path: " + path, ErrorType.PROTOCOL,
390                         ErrorTag.DATA_MISSING);
391             }
392         } catch (InterruptedException | ExecutionException e) {
393             LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
394         }
395     }
396
397     private void checkItemDoesNotExists(final DOMDataReadWriteTransaction rWTransaction,
398                                         final LogicalDatastoreType store, final YangInstanceIdentifier path) {
399         final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
400         try {
401             if (futureDatastoreData.get()) {
402                 final String errMsg = "Operation via Restconf was not executed because data already exists";
403                 LOG.trace("{}:{}", errMsg, path);
404                 rWTransaction.cancel();
405                 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
406                         ErrorTag.DATA_EXISTS);
407             }
408         } catch (InterruptedException | ExecutionException e) {
409             LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
410         }
411     }
412
413     private CheckedFuture<Void, TransactionCommitFailedException> putDataViaTransaction(
414             final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore,
415             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext)
416     {
417         DOMDataReadWriteTransaction transaction = domDataBroker.newReadWriteTransaction();
418         LOG.trace("Put {} via Restconf: {} with payload {}", datastore.name(), path, payload);
419         if (!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
420             transaction.cancel();
421             transaction = domDataBroker.newReadWriteTransaction();
422         }
423         transaction.put(datastore, path, payload);
424         return transaction.submit();
425     }
426
427     private void putDataWithinTransaction(
428             final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
429             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
430         LOG.trace("Put {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
431         ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
432         writeTransaction.put(datastore, path, payload);
433     }
434
435     private CheckedFuture<Void, TransactionCommitFailedException> deleteDataViaTransaction(
436             final DOMDataReadWriteTransaction readWriteTransaction, final LogicalDatastoreType datastore,
437             final YangInstanceIdentifier path) {
438         LOG.trace("Delete {} via Restconf: {}", datastore.name(), path);
439         checkItemExists(readWriteTransaction, datastore, path);
440         readWriteTransaction.delete(datastore, path);
441         return readWriteTransaction.submit();
442     }
443
444     private void deleteDataWithinTransaction(
445             final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
446             final YangInstanceIdentifier path) {
447         LOG.trace("Delete {} within Restconf PATCH: {}", datastore.name(), path);
448         writeTransaction.delete(datastore, path);
449     }
450
451     private void mergeDataWithinTransaction(
452             final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
453             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
454         LOG.trace("Merge {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
455         ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
456
457         // merging is necessary only for lists otherwise we can call put method
458         if (payload instanceof MapNode) {
459             writeTransaction.merge(datastore, path, payload);
460         } else {
461             writeTransaction.put(datastore, path, payload);
462         }
463     }
464
465     public void setDomDataBroker(final DOMDataBroker domDataBroker) {
466         this.domDataBroker = domDataBroker;
467     }
468
469     private boolean ensureParentsByMerge(final LogicalDatastoreType store,
470                                       final YangInstanceIdentifier normalizedPath, final DOMDataReadWriteTransaction rwTx, final SchemaContext schemaContext) {
471
472         boolean mergeResult = true;
473         final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
474         YangInstanceIdentifier rootNormalizedPath = null;
475
476         final Iterator<PathArgument> it = normalizedPath.getPathArguments().iterator();
477
478         while(it.hasNext()) {
479             final PathArgument pathArgument = it.next();
480             if(rootNormalizedPath == null) {
481                 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
482             }
483
484             // Skip last element, its not a parent
485             if(it.hasNext()) {
486                 normalizedPathWithoutChildArgs.add(pathArgument);
487             }
488         }
489
490         // No parent structure involved, no need to ensure parents
491         if(normalizedPathWithoutChildArgs.isEmpty()) {
492             return mergeResult;
493         }
494
495         Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
496
497         final NormalizedNode<?, ?> parentStructure =
498                 ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
499         try {
500             rwTx.merge(store, rootNormalizedPath, parentStructure);
501         } catch (RuntimeException e) {
502             /*
503              * Catching the exception here, logging it and proceeding further
504              * for the following reasons.
505              *
506              * 1. For MD-SAL store if it fails we'll go with the next call
507              * anyway and let the failure happen there. 2. For NETCONF devices
508              * that can not handle these calls such as creation of empty lists
509              * etc, instead of failing we'll go with the actual call. Devices
510              * should be able to handle the actual calls made without the need
511              * to create parents. So instead of failing we will give a device a
512              * chance to configure the management entity in question. 3. If this
513              * merge call is handled properly by MD-SAL data store or a Netconf
514              * device this is a no-op.
515              */
516              // FIXME: Figure out and catch specific RunTimeExceptions thrown by NETCONF instead of generic one.
517              //        to make this cleaner and easier to maintain.
518             mergeResult = false;
519             LOG.debug("Exception while creating the parent in ensureParentsByMerge. Proceeding with the actual request", e);
520         }
521         return mergeResult;
522     }
523
524     public void registerToListenNotification(final NotificationListenerAdapter listener) {
525         checkPreconditions();
526
527         if (listener.isListening()) {
528             return;
529         }
530
531         final SchemaPath path = listener.getSchemaPath();
532         final ListenerRegistration<DOMNotificationListener> registration = this.domNotification
533                 .registerNotificationListener((DOMNotificationListener) listener, path);
534
535         listener.setRegistration(registration);
536     }
537 }