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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
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 com.google.common.util.concurrent.MoreExecutors;
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.mdsal.common.api.MappingCheckedFuture;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
21 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public final class ReadOnlyTx implements DOMDataReadOnlyTransaction {
31 private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTx.class);
33 private final NetconfBaseOps netconfOps;
34 private final RemoteDeviceId id;
36 public ReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
37 this.netconfOps = netconfOps;
41 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readConfigurationData(
42 final YangInstanceIdentifier path) {
43 final ListenableFuture<Optional<NormalizedNode<?, ?>>> configRunning = netconfOps.getConfigRunningData(
44 new NetconfRpcFutureCallback("Data read", id), Optional.fromNullable(path));
46 return MappingCheckedFuture.create(configRunning, ReadFailedException.MAPPER);
49 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readOperationalData(
50 final YangInstanceIdentifier path) {
51 final ListenableFuture<Optional<NormalizedNode<?, ?>>> configCandidate = netconfOps.getData(
52 new NetconfRpcFutureCallback("Data read", id), Optional.fromNullable(path));
54 return MappingCheckedFuture.create(configCandidate, ReadFailedException.MAPPER);
63 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
64 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
67 return readConfigurationData(path);
69 return readOperationalData(path);
71 LOG.info("Unknown datastore type: {}.", store);
72 throw new IllegalArgumentException(String.format(
73 "%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
79 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
80 final YangInstanceIdentifier path) {
81 final ListenableFuture<Boolean> result = Futures.transform(read(store, path),
82 optionalNode -> optionalNode != null && optionalNode.isPresent(), MoreExecutors.directExecutor());
83 return MappingCheckedFuture.create(result, ReadFailedException.MAPPER);
87 public Object getIdentifier() {