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.MoreExecutors;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.ReadFailedException;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
18 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
19 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
20 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public final class ReadOnlyTx implements DOMDataTreeReadTransaction {
28 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTx.class);
30 private final NetconfBaseOps netconfOps;
31 private final RemoteDeviceId id;
33 public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
34 this.netconfOps = netconfOps;
38 private FluentFuture<Optional<NormalizedNode<?, ?>>> readConfigurationData(
39 final YangInstanceIdentifier path) {
40 return remapException(netconfOps.getConfigRunningData(
41 new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
44 private FluentFuture<Optional<NormalizedNode<?, ?>>> readOperationalData(
45 final YangInstanceIdentifier path) {
46 return remapException(netconfOps.getData(
47 new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
50 private static <T> FluentFuture<T> remapException(FluentFuture<T> input) {
51 final SettableFuture<T> ret = SettableFuture.create();
52 input.addCallback(new FutureCallback<T>() {
55 public void onSuccess(T result) {
60 public void onFailure(Throwable cause) {
61 ret.setException(cause instanceof ReadFailedException ? cause
62 : new ReadFailedException("NETCONF operation failed", cause));
64 }, MoreExecutors.directExecutor());
74 public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
75 final YangInstanceIdentifier path) {
78 return readConfigurationData(path);
80 return readOperationalData(path);
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));
89 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
90 return read(store, path).transform(optionalNode -> optionalNode != null && optionalNode.isPresent(),
91 MoreExecutors.directExecutor());
95 public Object getIdentifier() {