2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
12 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil.DELETE_TX_TYPE;
13 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil.checkItemDoesNotExists;
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.util.Collection;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
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;
35 final class MdsalRestconfTransaction extends RestconfTransaction {
36 private DOMDataTreeReadWriteTransaction rwTx;
38 MdsalRestconfTransaction(final DOMDataBroker dataBroker) {
39 this.rwTx = dataBroker.newReadWriteTransaction();
43 public void cancel() {
51 public void delete(final YangInstanceIdentifier path) {
52 final FluentFuture<Boolean> isExists = verifyNotNull(rwTx).exists(CONFIGURATION, path);
53 DeleteDataTransactionUtil.checkItemExists(isExists, path, DELETE_TX_TYPE);
54 rwTx.delete(CONFIGURATION, path);
58 public void remove(final YangInstanceIdentifier path) {
59 verifyNotNull(rwTx).delete(CONFIGURATION, path);
63 public void merge(final YangInstanceIdentifier path, final NormalizedNode data) {
64 verifyNotNull(rwTx).merge(CONFIGURATION, path, data);
68 public void create(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(YangInstanceIdentifier.create(emptySubTree.getIdentifier()), emptySubTree);
73 TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
75 final Collection<? extends NormalizedNode> children = ((NormalizedNodeContainer<?>) data).body();
76 final BatchedExistenceCheck check =
77 BatchedExistenceCheck.start(verifyNotNull(rwTx), CONFIGURATION, path, children);
79 for (final NormalizedNode child : children) {
80 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
81 verifyNotNull(rwTx).put(CONFIGURATION, childPath, child);
83 // ... finally collect existence checks and abort the transaction if any of them failed.
84 checkExistence(path, check);
86 final FluentFuture<Boolean> isExists = verifyNotNull(rwTx).exists(CONFIGURATION, path);
87 checkItemDoesNotExists(isExists, path);
88 TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
89 verifyNotNull(rwTx).put(CONFIGURATION, path, data);
94 public void replace(final YangInstanceIdentifier path, final NormalizedNode data,
95 final SchemaContext schemaContext) {
96 if (data instanceof MapNode || data instanceof LeafSetNode) {
97 final NormalizedNode emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
98 merge(YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
99 TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
101 for (final NormalizedNode child : ((NormalizedNodeContainer<?>) data).body()) {
102 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
103 verifyNotNull(rwTx).put(CONFIGURATION, childPath, child);
106 TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
107 verifyNotNull(rwTx).put(CONFIGURATION, path, data);
112 public FluentFuture<? extends @NonNull CommitInfo> commit() {
113 final FluentFuture<? extends @NonNull CommitInfo> ret = verifyNotNull(rwTx).commit();
118 private static void checkExistence(final YangInstanceIdentifier path, final BatchedExistenceCheck check) {
119 final Map.Entry<YangInstanceIdentifier, ReadFailedException> failure;
121 failure = check.getFailure();
122 } catch (InterruptedException e) {
123 throw new RestconfDocumentedException("Could not determine the existence of path " + path, e);
126 if (failure != null) {
127 final ReadFailedException e = failure.getValue();
129 throw new RestconfDocumentedException("Data already exists",
130 RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.DATA_EXISTS, failure.getKey());
133 throw new RestconfDocumentedException(
134 "Could not determine the existence of path " + failure.getKey(), e, e.getErrorList());