Reduce exception guard
[netconf.git] / restconf / restconf-nb / 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.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;
14
15 import com.google.common.util.concurrent.FluentFuture;
16 import java.util.Collection;
17 import java.util.Map;
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.nb.rfc8040.rests.utils.DeleteDataTransactionUtil;
25 import org.opendaylight.restconf.nb.rfc8040.rests.utils.TransactionUtil;
26 import org.opendaylight.yangtools.yang.common.ErrorTag;
27 import org.opendaylight.yangtools.yang.common.ErrorType;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35
36 final class MdsalRestconfTransaction extends RestconfTransaction {
37     private DOMDataTreeReadWriteTransaction rwTx;
38
39     MdsalRestconfTransaction(final DOMDataBroker dataBroker) {
40         rwTx = dataBroker.newReadWriteTransaction();
41     }
42
43     @Override
44     public void cancel() {
45         if (rwTx != null) {
46             rwTx.cancel();
47             rwTx = null;
48         }
49     }
50
51     @Override
52     public void delete(final YangInstanceIdentifier path) {
53         final FluentFuture<Boolean> isExists = verifyNotNull(rwTx).exists(CONFIGURATION, path);
54         DeleteDataTransactionUtil.checkItemExists(isExists, path, DELETE_TX_TYPE);
55         rwTx.delete(CONFIGURATION, path);
56     }
57
58     @Override
59     public void remove(final YangInstanceIdentifier path) {
60         verifyNotNull(rwTx).delete(CONFIGURATION, path);
61     }
62
63     @Override
64     public void merge(final YangInstanceIdentifier path, final NormalizedNode data) {
65         verifyNotNull(rwTx).merge(CONFIGURATION, path, data);
66     }
67
68     @Override
69     public void create(final YangInstanceIdentifier path, final NormalizedNode data,
70                        final EffectiveModelContext schemaContext) {
71         if (data instanceof MapNode || data instanceof LeafSetNode) {
72             final NormalizedNode emptySubTree = ImmutableNodes.fromInstanceId(schemaContext, path);
73             merge(YangInstanceIdentifier.create(emptySubTree.getIdentifier()), 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), CONFIGURATION, path, children);
79
80             for (final NormalizedNode child : children) {
81                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
82                 verifyNotNull(rwTx).put(CONFIGURATION, 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(CONFIGURATION, path);
88             checkItemDoesNotExists(isExists, path);
89             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
90             verifyNotNull(rwTx).put(CONFIGURATION, path, data);
91         }
92     }
93
94     @Override
95     public void replace(final YangInstanceIdentifier path, final NormalizedNode data,
96                         final EffectiveModelContext schemaContext) {
97         if (data instanceof MapNode || data instanceof LeafSetNode) {
98             final NormalizedNode emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
99             merge(YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
100             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
101
102             for (final NormalizedNode child : ((NormalizedNodeContainer<?>) data).body()) {
103                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
104                 verifyNotNull(rwTx).put(CONFIGURATION, childPath, child);
105             }
106         } else {
107             TransactionUtil.ensureParentsByMerge(path, schemaContext, this);
108             verifyNotNull(rwTx).put(CONFIGURATION, path, data);
109         }
110     }
111
112     @Override
113     public FluentFuture<? extends @NonNull CommitInfo> commit() {
114         final FluentFuture<? extends @NonNull CommitInfo> ret = verifyNotNull(rwTx).commit();
115         rwTx = null;
116         return ret;
117     }
118
119     private static void checkExistence(final YangInstanceIdentifier path, final BatchedExistenceCheck check) {
120         final Map.Entry<YangInstanceIdentifier, ReadFailedException> failure;
121         try {
122             failure = check.getFailure();
123         } catch (InterruptedException e) {
124             throw new RestconfDocumentedException("Could not determine the existence of path " + path, e);
125         }
126
127         if (failure != null) {
128             final ReadFailedException e = failure.getValue();
129             if (e == null) {
130                 throw new RestconfDocumentedException("Data already exists",
131                     ErrorType.PROTOCOL, ErrorTag.DATA_EXISTS, failure.getKey());
132             }
133
134             throw new RestconfDocumentedException(
135                 "Could not determine the existence of path " + failure.getKey(), e, e.getErrorList());
136         }
137     }
138 }