Bug 5897 - PATCH merge operation does nothing
[netconf.git] / 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.newReadWriteTransaction(), 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().newReadWriteTransaction(), 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         final DOMDataReadWriteTransaction patchTransaction = this.domDataBroker.newReadWriteTransaction();
142         final List<PATCHStatusEntity> editCollection = new ArrayList<>();
143         List<RestconfError> editErrors;
144         final List<RestconfError> globalErrors = null;
145         int errorCounter = 0;
146
147         for (final PATCHEntity patchEntity : context.getData()) {
148             final PATCHEditOperation operation = PATCHEditOperation.valueOf(patchEntity.getOperation().toUpperCase());
149
150             switch (operation) {
151                 case CREATE:
152                     if (errorCounter == 0) {
153                         try {
154                             postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
155                                     patchEntity.getNode(), globalSchema);
156                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
157                         } catch (final RestconfDocumentedException e) {
158                             editErrors = new ArrayList<>();
159                             editErrors.addAll(e.getErrors());
160                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
161                             errorCounter++;
162                         }
163                     }
164                     break;
165                 case REPLACE:
166                     if (errorCounter == 0) {
167                         try {
168                             putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
169                                     .getTargetNode(), patchEntity.getNode(), globalSchema);
170                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
171                         } catch (final RestconfDocumentedException e) {
172                             editErrors = new ArrayList<>();
173                             editErrors.addAll(e.getErrors());
174                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
175                             errorCounter++;
176                         }
177                     }
178                     break;
179                 case DELETE:
180                     if (errorCounter == 0) {
181                         try {
182                             deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
183                                     .getTargetNode());
184                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
185                         } catch (final RestconfDocumentedException e) {
186                             editErrors = new ArrayList<>();
187                             editErrors.addAll(e.getErrors());
188                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
189                             errorCounter++;
190                         }
191                     }
192                     break;
193                 case REMOVE:
194                     if (errorCounter == 0) {
195                         try {
196                             deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
197                                     .getTargetNode());
198                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
199                         } catch (final RestconfDocumentedException e) {
200                             LOG.error("Error removing {} by {} operation", patchEntity.getTargetNode().toString(),
201                                     patchEntity.getEditId(), e);
202                         }
203                     }
204                     break;
205                 case MERGE:
206                     if (errorCounter == 0) {
207                         try {
208                             mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
209                                     patchEntity.getNode(), globalSchema);
210                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
211                         } catch (RestconfDocumentedException e) {
212                             editErrors = new ArrayList<>();
213                             editErrors.addAll(e.getErrors());
214                             editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
215                             errorCounter++;
216                         }
217                     }
218                     break;
219             }
220         }
221
222         //TODO: make sure possible global errors are filled up correctly and decide transaction submission based on that
223         //globalErrors = new ArrayList<>();
224         if (errorCounter == 0) {
225             final CheckedFuture<Void, TransactionCommitFailedException> submit = patchTransaction.submit();
226             return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true,
227                     globalErrors);
228         } else {
229             patchTransaction.cancel();
230             return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false,
231                     globalErrors);
232         }
233     }
234
235     // POST configuration
236     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
237             final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
238         checkPreconditions();
239         return postDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path, payload, globalSchema);
240     }
241
242     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
243             final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
244         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
245         if (domDataBrokerService.isPresent()) {
246             return postDataViaTransaction(domDataBrokerService.get().newReadWriteTransaction(), CONFIGURATION, path,
247                     payload, mountPoint.getSchemaContext());
248         }
249         final String errMsg = "DOM data broker service isn't available for mount point " + path;
250         LOG.warn(errMsg);
251         throw new RestconfDocumentedException(errMsg);
252     }
253
254     // DELETE configuration
255     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
256             final YangInstanceIdentifier path) {
257         checkPreconditions();
258         return deleteDataViaTransaction(this.domDataBroker.newWriteOnlyTransaction(), CONFIGURATION, path);
259     }
260
261     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
262             final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
263         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
264         if (domDataBrokerService.isPresent()) {
265             return deleteDataViaTransaction(domDataBrokerService.get().newWriteOnlyTransaction(), CONFIGURATION, path);
266         }
267         final String errMsg = "DOM data broker service isn't available for mount point " + path;
268         LOG.warn(errMsg);
269         throw new RestconfDocumentedException(errMsg);
270     }
271
272     // RPC
273     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
274         checkPreconditions();
275         if (this.rpcService == null) {
276             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
277         }
278         LOG.trace("Invoke RPC {} with input: {}", type, input);
279         return this.rpcService.invokeRpc(type, input);
280     }
281
282     public void registerToListenDataChanges(final LogicalDatastoreType datastore, final DataChangeScope scope,
283             final ListenerAdapter listener) {
284         checkPreconditions();
285
286         if (listener.isListening()) {
287             return;
288         }
289
290         final YangInstanceIdentifier path = listener.getPath();
291         final ListenerRegistration<DOMDataChangeListener> registration = this.domDataBroker.registerDataChangeListener(
292                 datastore, path, listener, scope);
293
294         listener.setRegistration(registration);
295     }
296
297     private NormalizedNode<?, ?> readDataViaTransaction(final DOMDataReadTransaction transaction,
298             final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
299         LOG.trace("Read {} via Restconf: {}", datastore.name(), path);
300         final ListenableFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = transaction.read(datastore, path);
301         if (listenableFuture != null) {
302             Optional<NormalizedNode<?, ?>> optional;
303             try {
304                 LOG.debug("Reading result data from transaction.");
305                 optional = listenableFuture.get();
306             } catch (InterruptedException | ExecutionException e) {
307                 LOG.warn("Exception by reading {} via Restconf: {}", datastore.name(), path, e);
308                 throw new RestconfDocumentedException("Problem to get data from transaction.", e.getCause());
309
310             }
311             if (optional != null) {
312                 if (optional.isPresent()) {
313                     return optional.get();
314                 }
315             }
316         }
317         return null;
318     }
319
320     private CheckedFuture<Void, TransactionCommitFailedException> postDataViaTransaction(
321             final DOMDataReadWriteTransaction rWTransaction, final LogicalDatastoreType datastore,
322             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
323         // FIXME: This is doing correct post for container and list children
324         //        not sure if this will work for choice case
325         if(payload instanceof MapNode) {
326             LOG.trace("POST {} via Restconf: {} with payload {}", datastore.name(), path, payload);
327             final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
328             rWTransaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
329             ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
330             for(final MapEntryNode child : ((MapNode) payload).getValue()) {
331                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
332                 checkItemDoesNotExists(rWTransaction, datastore, childPath);
333                 rWTransaction.put(datastore, childPath, child);
334             }
335         } else {
336             checkItemDoesNotExists(rWTransaction,datastore, path);
337             ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
338             rWTransaction.put(datastore, path, payload);
339         }
340         return rWTransaction.submit();
341     }
342
343     private void postDataWithinTransaction(
344             final DOMDataReadWriteTransaction rWTransaction, final LogicalDatastoreType datastore,
345             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
346         // FIXME: This is doing correct post for container and list children
347         //        not sure if this will work for choice case
348         if(payload instanceof MapNode) {
349             LOG.trace("POST {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
350             final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
351             rWTransaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
352             ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
353             for(final MapEntryNode child : ((MapNode) payload).getValue()) {
354                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
355                 checkItemDoesNotExists(rWTransaction, datastore, childPath);
356                 rWTransaction.put(datastore, childPath, child);
357             }
358         } else {
359             checkItemDoesNotExists(rWTransaction,datastore, path);
360             ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
361             rWTransaction.put(datastore, path, payload);
362         }
363     }
364
365     private void checkItemDoesNotExists(final DOMDataReadWriteTransaction rWTransaction,final LogicalDatastoreType store, final YangInstanceIdentifier path) {
366         final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
367         try {
368             if (futureDatastoreData.get()) {
369                 final String errMsg = "Post Configuration via Restconf was not executed because data already exists";
370                 LOG.trace("{}:{}", errMsg, path);
371                 rWTransaction.cancel();
372                 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
373                         ErrorTag.DATA_EXISTS);
374             }
375         } catch (InterruptedException | ExecutionException e) {
376             LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
377         }
378
379     }
380
381     private CheckedFuture<Void, TransactionCommitFailedException> putDataViaTransaction(
382             final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
383             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
384         LOG.trace("Put {} via Restconf: {} with payload {}", datastore.name(), path, payload);
385         ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
386         writeTransaction.put(datastore, path, payload);
387         return writeTransaction.submit();
388     }
389
390     private void putDataWithinTransaction(
391             final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
392             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
393         LOG.trace("Put {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
394         ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
395         writeTransaction.put(datastore, path, payload);
396     }
397
398     private CheckedFuture<Void, TransactionCommitFailedException> deleteDataViaTransaction(
399             final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
400             final YangInstanceIdentifier path) {
401         LOG.trace("Delete {} via Restconf: {}", datastore.name(), path);
402         writeTransaction.delete(datastore, path);
403         return writeTransaction.submit();
404     }
405
406     private void deleteDataWithinTransaction(
407             final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
408             final YangInstanceIdentifier path) {
409         LOG.trace("Delete {} within Restconf PATCH: {}", datastore.name(), path);
410         writeTransaction.delete(datastore, path);
411     }
412
413     private void mergeDataWithinTransaction(
414             final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
415             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
416         LOG.trace("Merge {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
417         ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
418
419         // merging is necessary only for lists otherwise we can call put method
420         if (payload instanceof MapNode) {
421             writeTransaction.merge(datastore, path, payload);
422         } else {
423             writeTransaction.put(datastore, path, payload);
424         }
425     }
426
427     public void setDomDataBroker(final DOMDataBroker domDataBroker) {
428         this.domDataBroker = domDataBroker;
429     }
430
431     private void ensureParentsByMerge(final LogicalDatastoreType store,
432                                       final YangInstanceIdentifier normalizedPath, final DOMDataReadWriteTransaction rwTx, final SchemaContext schemaContext) {
433         final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
434         YangInstanceIdentifier rootNormalizedPath = null;
435
436         final Iterator<PathArgument> it = normalizedPath.getPathArguments().iterator();
437
438         while(it.hasNext()) {
439             final PathArgument pathArgument = it.next();
440             if(rootNormalizedPath == null) {
441                 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
442             }
443
444             // Skip last element, its not a parent
445             if(it.hasNext()) {
446                 normalizedPathWithoutChildArgs.add(pathArgument);
447             }
448         }
449
450         // No parent structure involved, no need to ensure parents
451         if(normalizedPathWithoutChildArgs.isEmpty()) {
452             return;
453         }
454
455         Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
456
457         final NormalizedNode<?, ?> parentStructure =
458                 ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
459         rwTx.merge(store, rootNormalizedPath, parentStructure);
460     }
461
462     public void registerToListenNotification(final NotificationListenerAdapter listener) {
463         checkPreconditions();
464
465         if (listener.isListening()) {
466             return;
467         }
468
469         final SchemaPath path = listener.getSchemaPath();
470         final ListenerRegistration<DOMNotificationListener> registration = this.domNotification
471                 .registerNotificationListener(listener, path);
472
473         listener.setRegistration(registration);
474     }
475 }