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