Move data processing to update thread
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / spi / AbstractReadOnlyTx.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.netconf.client.mdsal.spi;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.common.api.ReadFailedException;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
21 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
22 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
23 import org.opendaylight.netconf.client.mdsal.impl.NetconfRpcFutureCallback;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 abstract class AbstractReadOnlyTx implements DOMDataTreeReadTransaction {
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractReadOnlyTx.class);
31
32     final NetconfBaseOps netconfOps;
33     final RemoteDeviceId id;
34
35     AbstractReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
36         this.netconfOps = netconfOps;
37         this.id = id;
38     }
39
40     private FluentFuture<Optional<NormalizedNode>> readConfigurationData(
41             final YangInstanceIdentifier path) {
42         return remapException(netconfOps.getConfigRunningData(
43             new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
44     }
45
46     private FluentFuture<Optional<NormalizedNode>> readOperationalData(
47             final YangInstanceIdentifier path) {
48         return remapException(netconfOps.getData(
49             new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
50     }
51
52     static final <T> @NonNull FluentFuture<T> remapException(final ListenableFuture<T> input) {
53         final SettableFuture<T> ret = SettableFuture.create();
54         Futures.addCallback(input, new FutureCallback<T>() {
55
56             @Override
57             public void onSuccess(final T result) {
58                 ret.set(result);
59             }
60
61             @Override
62             public void onFailure(final Throwable cause) {
63                 ret.setException(cause instanceof ReadFailedException ? cause
64                         : new ReadFailedException("NETCONF operation failed", cause));
65             }
66         }, MoreExecutors.directExecutor());
67         return FluentFuture.from(ret);
68     }
69
70     @Override
71     public final void close() {
72         // NOOP
73     }
74
75     @Override
76     public final FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
77             final YangInstanceIdentifier path) {
78         return switch (store) {
79             case CONFIGURATION -> readConfigurationData(path);
80             case OPERATIONAL -> readOperationalData(path);
81             default -> {
82                 LOG.info("Unknown datastore type: {}.", store);
83                 throw new IllegalArgumentException(String.format(
84                     "%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
85             }
86         };
87     }
88
89     @Override
90     public final FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
91         return read(store, path).transform(optionalNode -> optionalNode != null && optionalNode.isPresent(),
92                 MoreExecutors.directExecutor());
93     }
94
95     @Override
96     public final Object getIdentifier() {
97         return this;
98     }
99 }