Remove SchemaContextRef
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / PlainPatchDataTransactionUtil.java
1 /*
2  * Copyright (c) 2020 Lumina Networks, Inc. and others.  All rights reserved.
3  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
10
11 import com.google.common.util.concurrent.FluentFuture;
12 import javax.ws.rs.core.Response;
13 import javax.ws.rs.core.Response.Status;
14 import org.opendaylight.mdsal.common.api.CommitInfo;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
18 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
19 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Util class for plain patch data to DS.
30  */
31 public final class PlainPatchDataTransactionUtil {
32
33     private static final Logger LOG = LoggerFactory.getLogger(PlainPatchDataTransactionUtil.class);
34
35     private PlainPatchDataTransactionUtil() {
36     }
37
38     /**
39      * Prepare variables for put data to DS. Close {@link DOMTransactionChain} inside of object
40      * {@link TransactionVarsWrapper} provided as a parameter.
41      *
42      * @param payload
43      *             data to put
44      * @param schemaContext
45      *             reference to {@link EffectiveModelContext}
46      * @param transactionNode
47      *             wrapper of variables for transaction
48      * @return {@link Response}
49      */
50     public static Response patchData(final NormalizedNodeContext payload,
51                                      final TransactionVarsWrapper transactionNode,
52                                      final EffectiveModelContext schemaContext) {
53
54         final DOMTransactionChain transactionChain = transactionNode.getTransactionChain();
55         final DOMDataTreeReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
56
57         YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
58         NormalizedNode<?, ?> data = payload.getData();
59
60         try {
61             mergeDataWithinTransaction(LogicalDatastoreType.CONFIGURATION, path, data, tx, schemaContext);
62         } catch (final RestconfDocumentedException e) {
63             tx.cancel();
64             transactionChain.close();
65
66             throw new IllegalArgumentException(e);
67         }
68
69         final FluentFuture<? extends CommitInfo> future = tx.commit();
70         final ResponseFactory response = new ResponseFactory(Status.OK);
71
72         FutureCallbackTx.addCallback(future,
73                 RestconfDataServiceConstant.PatchData.PATCH_TX_TYPE,
74                 response,
75                 transactionChain); // closes transactionChain, may throw
76
77         return response.build();
78     }
79
80     /**
81      * Merge data within one transaction.
82      * @param dataStore Datastore to merge data to
83      * @param path Path for data to be merged
84      * @param payload Data to be merged
85      * @param writeTransaction Transaction
86      * @param schemaContext global schema context
87      */
88     private static void mergeDataWithinTransaction(final LogicalDatastoreType dataStore,
89                                                    final YangInstanceIdentifier path,
90                                                    final NormalizedNode<?, ?> payload,
91                                                    final DOMDataTreeWriteTransaction writeTransaction,
92                                                    final EffectiveModelContext schemaContext) {
93         LOG.trace("Merge {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
94         TransactionUtil.ensureParentsByMerge(path, schemaContext, writeTransaction);
95         writeTransaction.merge(dataStore, path, payload);
96     }
97 }