Migrate netconf to MD-SAL APIs
[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 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
9
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;
25
26 public final class ReadOnlyTx implements DOMDataTreeReadTransaction {
27
28     private static final Logger LOG  = LoggerFactory.getLogger(ReadOnlyTx.class);
29
30     private final NetconfBaseOps netconfOps;
31     private final RemoteDeviceId id;
32
33     public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
34         this.netconfOps = netconfOps;
35         this.id = id;
36     }
37
38     private FluentFuture<Optional<NormalizedNode<?, ?>>> readConfigurationData(
39             final YangInstanceIdentifier path) {
40         return remapException(netconfOps.getConfigRunningData(
41             new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
42     }
43
44     private FluentFuture<Optional<NormalizedNode<?, ?>>> readOperationalData(
45             final YangInstanceIdentifier path) {
46         return remapException(netconfOps.getData(
47             new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
48     }
49
50     private static <T> FluentFuture<T> remapException(FluentFuture<T> input) {
51         final SettableFuture<T> ret = SettableFuture.create();
52         input.addCallback(new FutureCallback<T>() {
53
54             @Override
55             public void onSuccess(T result) {
56                 ret.set(result);
57             }
58
59             @Override
60             public void onFailure(Throwable cause) {
61                 ret.setException(cause instanceof ReadFailedException ? cause
62                         : new ReadFailedException("NETCONF operation failed", cause));
63             }
64         }, MoreExecutors.directExecutor());
65         return ret;
66     }
67
68     @Override
69     public void close() {
70         // NOOP
71     }
72
73     @Override
74     public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
75             final YangInstanceIdentifier path) {
76         switch (store) {
77             case CONFIGURATION:
78                 return readConfigurationData(path);
79             case OPERATIONAL:
80                 return readOperationalData(path);
81             default:
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));
85         }
86     }
87
88     @Override
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());
92     }
93
94     @Override
95     public Object getIdentifier() {
96         return this;
97     }
98 }