Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / MdsalRestconfStrategy.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 java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.List;
16 import java.util.Optional;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
21 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 /**
26  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
27  *
28  * @see DOMTransactionChain
29  * @see DOMDataTreeReadWriteTransaction
30  */
31 public final class MdsalRestconfStrategy extends RestconfStrategy {
32     private final DOMDataBroker dataBroker;
33
34     public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
35         this.dataBroker = requireNonNull(dataBroker);
36     }
37
38     @Override
39     public RestconfTransaction prepareWriteExecution() {
40         return new MdsalRestconfTransaction(dataBroker);
41     }
42
43     @Override
44     public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
45             final YangInstanceIdentifier path) {
46         try (DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
47             return tx.read(store, path);
48         }
49     }
50
51     @Override
52     public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
53             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
54         return Futures.immediateFailedFuture(new UnsupportedOperationException(
55                 "Reading of selected subtrees is currently not supported in: " + MdsalRestconfStrategy.class));
56     }
57
58     @Override
59     public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
60         try (DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
61             return tx.exists(store, path);
62         }
63     }
64 }