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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
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;
30 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
32 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTx.class);
34 private final NetconfBaseOps netconfOps;
35 private final RemoteDeviceId id;
36 private final FutureCallback<DOMRpcResult> loggingCallback;
38 public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
39 this.netconfOps = netconfOps;
42 // Simple logging callback to log result of read operation
43 loggingCallback = new FutureCallback<DOMRpcResult>() {
45 public void onSuccess(final DOMRpcResult result) {
46 if(AbstractWriteTx.isSuccess(result)) {
47 LOG.trace("{}: Reading data successful", id);
49 LOG.warn("{}: Reading data unsuccessful: {}", id, result.getErrors());
55 public void onFailure(final Throwable t) {
56 LOG.warn("{}: Reading data failed", id, t);
61 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
62 final YangInstanceIdentifier path) {
63 final ListenableFuture<Optional<NormalizedNode<?, ?>>> configRunning = netconfOps.getConfigRunningData(loggingCallback, Optional.fromNullable(path));
65 return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
68 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
69 final YangInstanceIdentifier path) {
70 final ListenableFuture<Optional<NormalizedNode<?, ?>>> configCandidate = netconfOps.getData(loggingCallback, Optional.fromNullable(path));
72 return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
81 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
82 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
85 return readConfigurationData(path);
88 return readOperationalData(path);
92 throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
96 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
97 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
100 return Futures.immediateCheckedFuture(data.get().isPresent());
101 } catch (InterruptedException | ExecutionException e) {
102 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Exists failed",e));
107 public Object getIdentifier() {