Wrap throwable in NetconfDocumentedException for AbstractWriteTx
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / ReadOnlyTx.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
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;
28
29
30 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
31
32     private static final Logger LOG  = LoggerFactory.getLogger(ReadOnlyTx.class);
33
34     private final NetconfBaseOps netconfOps;
35     private final RemoteDeviceId id;
36     private final FutureCallback<DOMRpcResult> loggingCallback;
37
38     public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
39         this.netconfOps = netconfOps;
40         this.id = id;
41
42         // Simple logging callback to log result of read operation
43         loggingCallback = new FutureCallback<DOMRpcResult>() {
44             @Override
45             public void onSuccess(final DOMRpcResult result) {
46                 if(AbstractWriteTx.isSuccess(result)) {
47                     LOG.trace("{}: Reading data successful", id);
48                 } else {
49                     LOG.warn("{}: Reading data unsuccessful: {}", id, result.getErrors());
50                 }
51
52             }
53
54             @Override
55             public void onFailure(final Throwable t) {
56                 LOG.warn("{}: Reading data failed", id, t);
57             }
58         };
59     }
60
61     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
62             final YangInstanceIdentifier path) {
63         final ListenableFuture<Optional<NormalizedNode<?, ?>>> configRunning = netconfOps.getConfigRunningData(loggingCallback, Optional.fromNullable(path));
64
65         return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
66     }
67
68     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
69             final YangInstanceIdentifier path) {
70         final ListenableFuture<Optional<NormalizedNode<?, ?>>> configCandidate = netconfOps.getData(loggingCallback, Optional.fromNullable(path));
71
72         return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
73     }
74
75     @Override
76     public void close() {
77         // NOOP
78     }
79
80     @Override
81     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
82             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
83         switch (store) {
84         case CONFIGURATION: {
85             return readConfigurationData(path);
86         }
87         case OPERATIONAL: {
88             return readOperationalData(path);
89         }
90         }
91
92         throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
93     }
94
95     @Override
96     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
97         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
98
99         try {
100             return Futures.immediateCheckedFuture(data.get().isPresent());
101         } catch (InterruptedException | ExecutionException e) {
102             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Exists failed",e));
103         }
104     }
105
106     @Override
107     public Object getIdentifier() {
108         return this;
109     }
110 }