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.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
26 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
27 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
41 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTx.class);
43 private final NetconfBaseOps netconfOps;
44 private final RemoteDeviceId id;
45 private final FutureCallback<DOMRpcResult> loggingCallback;
47 private final long requestTimeoutMillis;
49 public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id, final long requestTimeoutMillis) {
50 this.netconfOps = netconfOps;
52 this.requestTimeoutMillis = requestTimeoutMillis;
54 // Simple logging callback to log result of read operation
55 loggingCallback = new FutureCallback<DOMRpcResult>() {
57 public void onSuccess(final DOMRpcResult result) {
58 if(AbstractWriteTx.isSuccess(result)) {
59 LOG.trace("{}: Reading data successful", id);
61 LOG.warn("{}: Reading data unsuccessful: {}", id, result.getErrors());
67 public void onFailure(final Throwable t) {
68 LOG.warn("{}: Reading data failed", id, t);
73 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
74 final YangInstanceIdentifier path) {
75 final ListenableFuture<DOMRpcResult> configRunning = netconfOps.getConfigRunning(loggingCallback, Optional.fromNullable(path));
77 final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configRunning, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
79 public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
80 checkReadSuccess(result, path);
82 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
83 return NormalizedNodes.findNode(dataNode, path.getPathArguments());
87 if(!readWithTimeout("readConfigurationData", configRunning)) {
91 return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
94 private DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> findDataNode(final DOMRpcResult result) {
95 return ((ContainerNode) result.getResult()).getChild(NetconfMessageTransformUtil.toId(NetconfMessageTransformUtil.NETCONF_DATA_QNAME)).get();
98 private void checkReadSuccess(final DOMRpcResult result, final YangInstanceIdentifier path) {
100 Preconditions.checkArgument(AbstractWriteTx.isSuccess(result), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
101 } catch (final IllegalArgumentException e) {
102 LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
107 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
108 final YangInstanceIdentifier path) {
109 final ListenableFuture<DOMRpcResult> configCandidate = netconfOps.get(loggingCallback, Optional.fromNullable(path));
111 // Find data node and normalize its content
112 final ListenableFuture<Optional<NormalizedNode<?, ?>>> transformedFuture = Futures.transform(configCandidate, new Function<DOMRpcResult, Optional<NormalizedNode<?, ?>>>() {
114 public Optional<NormalizedNode<?, ?>> apply(final DOMRpcResult result) {
115 checkReadSuccess(result, path);
117 final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataNode = findDataNode(result);
118 return NormalizedNodes.findNode(dataNode, path.getPathArguments());
122 if(!readWithTimeout("readOperationalData", configCandidate)) {
126 return MappingCheckedFuture.create(transformedFuture, ReadFailedException.MAPPER);
130 public void close() {
135 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
136 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
138 case CONFIGURATION: {
139 return readConfigurationData(path);
142 return readOperationalData(path);
146 throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
150 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
151 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
154 return Futures.immediateCheckedFuture(data.get().isPresent());
155 } catch (InterruptedException | ExecutionException e) {
156 return Futures.immediateFailedCheckedFuture(new ReadFailedException("Exists failed",e));
161 public Object getIdentifier() {
165 private boolean readWithTimeout(String operation, ListenableFuture<DOMRpcResult> future) {
167 future.get(requestTimeoutMillis, TimeUnit.MILLISECONDS);
168 } catch (InterruptedException | ExecutionException e) {
169 LOG.error("{}: {} failed with error", id, operation, e);
170 throw new RuntimeException(id + ": readOperationalData failed");
171 } catch (TimeoutException e) {
172 LOG.warn("{}: Unable to {} after {} milliseconds", id, operation, requestTimeoutMillis, e);