Merge "Fix error reporting for PUT/POST"
[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.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
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.util.concurrent.MappingCheckedFuture;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
29
30     private static final Logger LOG  = LoggerFactory.getLogger(ReadOnlyTx.class);
31
32     private final NetconfBaseOps netconfOps;
33     private final RemoteDeviceId id;
34
35     public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
36         this.netconfOps = netconfOps;
37         this.id = id;
38     }
39
40     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
41             final YangInstanceIdentifier path) {
42         final ListenableFuture<Optional<NormalizedNode<?, ?>>> configRunning = netconfOps.getConfigRunningData(
43                 new NetconfRpcFutureCallback("Data read", id), Optional.fromNullable(path));
44
45         return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
46     }
47
48     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
49             final YangInstanceIdentifier path) {
50         final ListenableFuture<Optional<NormalizedNode<?, ?>>> configCandidate = netconfOps.getData(
51                 new NetconfRpcFutureCallback("Data read", id), Optional.fromNullable(path));
52
53         return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
54     }
55
56     @Override
57     public void close() {
58         // NOOP
59     }
60
61     @Override
62     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
63             final LogicalDatastoreType store, final YangInstanceIdentifier path) {
64         switch (store) {
65         case CONFIGURATION: {
66             return readConfigurationData(path);
67         }
68         case OPERATIONAL: {
69             return readOperationalData(path);
70         }
71         }
72
73         throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
74     }
75
76     @Override
77     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
78         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> data = read(store, path);
79         final ListenableFuture<Boolean> result =
80                 Futures.transform(data, (Optional<NormalizedNode<?, ?>> a) -> a != null && a.isPresent());
81         return MappingCheckedFuture.create(result, ReadFailedException.MAPPER);
82     }
83
84     @Override
85     public Object getIdentifier() {
86         return this;
87     }
88 }