Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / MdsalRestconfTransaction.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.rests.transactions;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil.DELETE_TX_TYPE;
12 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil.checkItemDoesNotExists;
13
14 import com.google.common.util.concurrent.FluentFuture;
15 import java.util.Collection;
16 import java.util.Map;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.common.api.ReadFailedException;
21 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.restconf.common.errors.RestconfError;
25 import org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil;
26 import org.opendaylight.restconf.nb.rfc8040.rests.utils.TransactionUtil;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 final class MdsalRestconfTransaction extends RestconfTransaction {
36     private DOMDataTreeReadWriteTransaction rwTx;
37
38     MdsalRestconfTransaction(final DOMDataBroker dataBroker) {
39         this.rwTx = dataBroker.newReadWriteTransaction();
40     }
41
42     @Override
43     public void cancel() {
44         if (rwTx != null) {
45             rwTx.cancel();
46             rwTx = null;
47         }
48     }
49
50     @Override
51     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
52         final FluentFuture<Boolean> isExists = verifyNotNull(rwTx).exists(store, path);
53         DeleteDataTransactionUtil.checkItemExists(isExists, path, DELETE_TX_TYPE);
54         rwTx.delete(store, path);
55     }
56
57     @Override
58     public void remove(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
59         verifyNotNull(rwTx).delete(store, path);
60     }
61
62     @Override
63     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
64         verifyNotNull(rwTx).merge(store, path, data);
65     }
66
67     @Override
68     public void create(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data,
69                        final SchemaContext schemaContext) {
70         if (data instanceof MapNode || data instanceof LeafSetNode) {
71             final NormalizedNode emptySubTree = ImmutableNodes.fromInstanceId(schemaContext, path);
72             merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(emptySubTree.getIdentifier()),
73                 emptySubTree);
74             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
75
76             final Collection<? extends NormalizedNode> children = ((NormalizedNodeContainer<?>) data).body();
77             final BatchedExistenceCheck check =
78                 BatchedExistenceCheck.start(verifyNotNull(rwTx), LogicalDatastoreType.CONFIGURATION, path, children);
79
80             for (final NormalizedNode child : children) {
81                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
82                 verifyNotNull(rwTx).put(store, childPath, child);
83             }
84             // ... finally collect existence checks and abort the transaction if any of them failed.
85             checkExistence(path, check);
86         } else {
87             final FluentFuture<Boolean> isExists = verifyNotNull(rwTx).exists(store, path);
88             checkItemDoesNotExists(isExists, path);
89             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
90             verifyNotNull(rwTx).put(store, path, data);
91         }
92     }
93
94     @Override
95     public void replace(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data,
96                         final SchemaContext schemaContext) {
97         if (data instanceof MapNode || data instanceof LeafSetNode) {
98             final NormalizedNode emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
99             merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.create(emptySubtree.getIdentifier()),
100                 emptySubtree);
101             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
102
103             for (final NormalizedNode child : ((NormalizedNodeContainer<?>) data).body()) {
104                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
105                 verifyNotNull(rwTx).put(store, childPath, child);
106             }
107         } else {
108             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
109             verifyNotNull(rwTx).put(store, path, data);
110         }
111     }
112
113     @Override
114     public FluentFuture<? extends @NonNull CommitInfo> commit() {
115         final FluentFuture<? extends @NonNull CommitInfo> ret = verifyNotNull(rwTx).commit();
116         rwTx = null;
117         return ret;
118     }
119
120     private static void checkExistence(final YangInstanceIdentifier path, final BatchedExistenceCheck check) {
121         final Map.Entry<YangInstanceIdentifier, ReadFailedException> failure;
122         try {
123             failure = check.getFailure();
124         } catch (InterruptedException e) {
125             throw new RestconfDocumentedException("Could not determine the existence of path " + path, e);
126         }
127
128         if (failure != null) {
129             final ReadFailedException e = failure.getValue();
130             if (e == null) {
131                 throw new RestconfDocumentedException("Data already exists",
132                     RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.DATA_EXISTS, failure.getKey());
133             }
134
135             throw new RestconfDocumentedException(
136                 "Could not determine the existence of path " + failure.getKey(), e, e.getErrorList());
137         }
138     }
139 }