2 * Copyright (c) 2020 Lumina Networks, Inc. and others. All rights reserved.
3 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
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;
29 * Util class for plain patch data to DS.
31 public final class PlainPatchDataTransactionUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(PlainPatchDataTransactionUtil.class);
35 private PlainPatchDataTransactionUtil() {
39 * Prepare variables for put data to DS. Close {@link DOMTransactionChain} inside of object
40 * {@link TransactionVarsWrapper} provided as a parameter.
44 * @param schemaContext
45 * reference to {@link EffectiveModelContext}
46 * @param transactionNode
47 * wrapper of variables for transaction
48 * @return {@link Response}
50 public static Response patchData(final NormalizedNodeContext payload,
51 final TransactionVarsWrapper transactionNode,
52 final EffectiveModelContext schemaContext) {
54 final DOMTransactionChain transactionChain = transactionNode.getTransactionChain();
55 final DOMDataTreeReadWriteTransaction tx = transactionChain.newReadWriteTransaction();
57 YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
58 NormalizedNode<?, ?> data = payload.getData();
61 mergeDataWithinTransaction(LogicalDatastoreType.CONFIGURATION, path, data, tx, schemaContext);
62 } catch (final RestconfDocumentedException e) {
64 transactionChain.close();
66 throw new IllegalArgumentException(e);
69 final FluentFuture<? extends CommitInfo> future = tx.commit();
70 final ResponseFactory response = new ResponseFactory(Status.OK);
72 FutureCallbackTx.addCallback(future,
73 RestconfDataServiceConstant.PatchData.PATCH_TX_TYPE,
75 transactionChain); // closes transactionChain, may throw
77 return response.build();
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
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);