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.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.mdsal.common.api.ReadFailedException;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public final class ReadOnlyTx implements DOMDataTreeReadTransaction {
30 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTx.class);
32 private final NetconfBaseOps netconfOps;
33 private final RemoteDeviceId id;
35 public ReadOnlyTx(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 private static <T> 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);
76 public 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 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 Object getIdentifier() {