Move netconf-console to apps/
[netconf.git] / plugins / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / AbstractReadOnlyTx.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.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.common.api.ReadFailedException;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
21 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
22 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
23 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
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 abstract class AbstractReadOnlyTx implements DOMDataTreeReadTransaction {
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractReadOnlyTx.class);
31
32     final NetconfBaseOps netconfOps;
33     final RemoteDeviceId id;
34
35     AbstractReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
36         this.netconfOps = netconfOps;
37         this.id = id;
38     }
39
40     private FluentFuture<Optional<NormalizedNode>> readConfigurationData(
41             final YangInstanceIdentifier path) {
42         return remapException(netconfOps.getConfigRunningData(
43             new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
44     }
45
46     private FluentFuture<Optional<NormalizedNode>> readOperationalData(
47             final YangInstanceIdentifier path) {
48         return remapException(netconfOps.getData(
49             new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path)));
50     }
51
52     static final <T> @NonNull FluentFuture<T> remapException(final ListenableFuture<T> input) {
53         final SettableFuture<T> ret = SettableFuture.create();
54         Futures.addCallback(input, new FutureCallback<T>() {
55
56             @Override
57             public void onSuccess(final T result) {
58                 ret.set(result);
59             }
60
61             @Override
62             public void onFailure(final Throwable cause) {
63                 ret.setException(cause instanceof ReadFailedException ? cause
64                         : new ReadFailedException("NETCONF operation failed", cause));
65             }
66         }, MoreExecutors.directExecutor());
67         return FluentFuture.from(ret);
68     }
69
70     @Override
71     public final void close() {
72         // NOOP
73     }
74
75     @Override
76     public final FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
77             final YangInstanceIdentifier path) {
78         switch (store) {
79             case CONFIGURATION:
80                 return readConfigurationData(path);
81             case OPERATIONAL:
82                 return readOperationalData(path);
83             default:
84                 LOG.info("Unknown datastore type: {}.", store);
85                 throw new IllegalArgumentException(String.format(
86                     "%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
87         }
88     }
89
90     @Override
91     public final FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
92         return read(store, path).transform(optionalNode -> optionalNode != null && optionalNode.isPresent(),
93                 MoreExecutors.directExecutor());
94     }
95
96     @Override
97     public final Object getIdentifier() {
98         return this;
99     }
100 }