371b155f1b3475e10585210c7a7e2130d8f80863
[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.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Util class for plain patch data to DS.
28  */
29 public final class PlainPatchDataTransactionUtil {
30
31     private static final Logger LOG = LoggerFactory.getLogger(PlainPatchDataTransactionUtil.class);
32
33     private PlainPatchDataTransactionUtil() {
34     }
35
36     /**
37      * Prepare variables for put data to DS. Close {@link DOMTransactionChain} if any inside of object
38      * {@link RestconfStrategy} provided as a parameter if any.
39      *
40      * @param payload       data to put
41      * @param schemaContext reference to {@link EffectiveModelContext}
42      * @param strategy      object that perform the actual DS operations
43      * @return {@link Response}
44      */
45     public static Response patchData(final NormalizedNodeContext payload,
46                                      final RestconfStrategy strategy,
47                                      final EffectiveModelContext schemaContext) {
48
49         strategy.prepareReadWriteExecution();
50         YangInstanceIdentifier path = payload.getInstanceIdentifierContext().getInstanceIdentifier();
51         NormalizedNode<?, ?> data = payload.getData();
52
53         try {
54             mergeDataWithinTransaction(LogicalDatastoreType.CONFIGURATION, path, data, strategy, schemaContext);
55         } catch (final RestconfDocumentedException e) {
56             strategy.cancel();
57             throw new IllegalArgumentException(e);
58         }
59
60         final FluentFuture<? extends CommitInfo> future = strategy.commit();
61         final ResponseFactory response = new ResponseFactory(Status.OK);
62
63         FutureCallbackTx.addCallback(future, RestconfDataServiceConstant.PatchData.PATCH_TX_TYPE, response,
64                 strategy.getTransactionChain()); // closes transactionChain if any, may throw
65
66         return response.build();
67     }
68
69     /**
70      * Merge data within one transaction.
71      *
72      * @param dataStore     Datastore to merge data to
73      * @param path          Path for data to be merged
74      * @param payload       Data to be merged
75      * @param strategy      Object that perform the actual DS operations
76      * @param schemaContext global schema context
77      */
78     private static void mergeDataWithinTransaction(final LogicalDatastoreType dataStore,
79                                                    final YangInstanceIdentifier path,
80                                                    final NormalizedNode<?, ?> payload,
81                                                    final RestconfStrategy strategy,
82                                                    final EffectiveModelContext schemaContext) {
83         LOG.trace("Merge {} within Restconf Patch: {} with payload {}", dataStore.name(), path, payload);
84         TransactionUtil.ensureParentsByMerge(path, schemaContext, strategy);
85         strategy.merge(dataStore, path, payload);
86     }
87 }