5fcbe665b3af9ad42a4574e08e5fd33063f8a013
[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.DOMTransactionChain;
17 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
20 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfTransaction;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Util class for plain patch data to DS.
29  */
30 public final class PlainPatchDataTransactionUtil {
31
32     private static final Logger LOG = LoggerFactory.getLogger(PlainPatchDataTransactionUtil.class);
33
34     private PlainPatchDataTransactionUtil() {
35     }
36
37     /**
38      * Prepare variables for put data to DS. Close {@link DOMTransactionChain} if any inside of object
39      * {@link RestconfStrategy} provided as a parameter if any.
40      *
41      * @param payload       data to put
42      * @param schemaContext reference to {@link EffectiveModelContext}
43      * @param strategy      object that perform the actual DS operations
44      * @return {@link Response}
45      */
46     public static Response patchData(final NormalizedNodeContext payload,
47                                      final RestconfStrategy strategy,
48                                      final EffectiveModelContext schemaContext) {
49
50         final RestconfTransaction transaction = strategy.prepareWriteExecution();
51         YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
52         NormalizedNode data = payload.getData();
53
54         try {
55             LOG.trace("Merge CONFIGURATION within Restconf Patch: {} with payload {}", path, data);
56             TransactionUtil.ensureParentsByMerge(path, schemaContext, transaction);
57             transaction.merge(LogicalDatastoreType.CONFIGURATION, path, data);
58         } catch (final RestconfDocumentedException e) {
59             transaction.cancel();
60             throw new IllegalArgumentException(e);
61         }
62
63         final FluentFuture<? extends CommitInfo> future = transaction.commit();
64         final ResponseFactory response = new ResponseFactory(Status.OK);
65
66         // closes transactionChain if any, may throw
67         FutureCallbackTx.addCallback(future, PatchDataTransactionUtil.PATCH_TX_TYPE, response, strategy, path);
68         return response.build();
69     }
70 }