2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
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;
27 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
29 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTx.class);
31 private final NetconfBaseOps netconfOps;
32 private final RemoteDeviceId id;
34 public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
35 this.netconfOps = netconfOps;
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));
44 return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
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));
52 return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
61 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
62 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
65 return readConfigurationData(path);
67 return readOperationalData(path);
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));
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);
85 public Object getIdentifier() {