Integration of RESTCONF fields to NETCONF filters
[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.restconf.nb.rfc8040.handlers.TransactionChainHandler;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 /**
27  * Implementation of RESTCONF operations using {@link DOMTransactionChain} and related concepts.
28  *
29  * @see DOMTransactionChain
30  * @see DOMDataTreeReadWriteTransaction
31  */
32 public final class MdsalRestconfStrategy extends RestconfStrategy {
33     private final DOMTransactionChain transactionChain;
34
35     public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
36         this(new TransactionChainHandler(dataBroker));
37     }
38
39     public MdsalRestconfStrategy(final TransactionChainHandler transactionChainHandler) {
40         transactionChain = requireNonNull(transactionChainHandler).get();
41     }
42
43     @Override
44     public RestconfTransaction prepareWriteExecution() {
45         return new MdsalRestconfTransaction(transactionChain);
46     }
47
48     @Override
49     public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
50                                                                  final YangInstanceIdentifier path) {
51         try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
52             return tx.read(store, path);
53         }
54     }
55
56     @Override
57     public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
58             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
59         return Futures.immediateFailedFuture(new UnsupportedOperationException(
60                 "Reading of selected subtrees is currently not supported in: " + MdsalRestconfStrategy.class));
61     }
62
63     @Override
64     public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
65         try (DOMDataTreeReadTransaction tx = transactionChain.newReadOnlyTransaction()) {
66             return tx.exists(store, path);
67         }
68     }
69
70     @Override
71     public void close() {
72         transactionChain.close();
73     }
74 }