2 * Copyright (c) 2014 Cisco Systems, Inc. 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.netconf.sal.connect.netconf.sal.tx;
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.sal.connect.api.RemoteDeviceId;
22 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
23 import org.opendaylight.netconf.sal.connect.netconf.util.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;
29 abstract class AbstractReadOnlyTx implements DOMDataTreeReadTransaction {
30 private static final Logger LOG = LoggerFactory.getLogger(AbstractReadOnlyTx.class);
32 final NetconfBaseOps netconfOps;
33 final RemoteDeviceId id;
35 AbstractReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
36 this.netconfOps = netconfOps;
40 private FluentFuture<Optional<NormalizedNode>> readConfigurationData(
41 final YangInstanceIdentifier path) {
42 return remapException(netconfOps.getConfigRunningData(
43 new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
46 private FluentFuture<Optional<NormalizedNode>> readOperationalData(
47 final YangInstanceIdentifier path) {
48 return remapException(netconfOps.getData(
49 new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
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>() {
57 public void onSuccess(final T result) {
62 public void onFailure(final Throwable cause) {
63 ret.setException(cause instanceof ReadFailedException ? cause
64 : new ReadFailedException("NETCONF operation failed", cause));
66 }, MoreExecutors.directExecutor());
67 return FluentFuture.from(ret);
71 public final void close() {
76 public final FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
77 final YangInstanceIdentifier path) {
80 return readConfigurationData(path);
82 return readOperationalData(path);
84 LOG.info("Unknown datastore type: {}.", store);
85 throw new IllegalArgumentException(String.format(
86 "%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
91 public final FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
92 return read(store, path).transform(optionalNode -> optionalNode != null && optionalNode.isPresent(),
93 MoreExecutors.directExecutor());
97 public final Object getIdentifier() {