Move sal-netconf-connector to plugins/
[netconf.git] / plugins / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / FieldsAwareReadOnlyTx.java
1 /*
2  * Copyright © 2020 FRINX s.r.o. 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 java.util.List;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.netconf.dom.api.tx.NetconfDOMFieldsReadTransaction;
16 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
17 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
18 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfRpcFutureCallback;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class FieldsAwareReadOnlyTx extends AbstractReadOnlyTx implements NetconfDOMFieldsReadTransaction {
25     private static final Logger LOG = LoggerFactory.getLogger(FieldsAwareReadOnlyTx.class);
26
27     public FieldsAwareReadOnlyTx(final NetconfBaseOps netconfOps, final RemoteDeviceId id) {
28         super(netconfOps, id);
29     }
30
31     @Override
32     public FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
33             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
34         switch (store) {
35             case CONFIGURATION:
36                 return readConfigurationData(path, fields);
37             case OPERATIONAL:
38                 return readOperationalData(path, fields);
39             default:
40                 LOG.warn("Unknown datastore type: {}.", store);
41                 throw new IllegalArgumentException(String.format(
42                         "%s, Cannot read data %s with fields %s for %s datastore, unknown datastore type",
43                         id, path, fields, store));
44         }
45     }
46
47     private @NonNull FluentFuture<Optional<NormalizedNode>> readConfigurationData(
48             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
49         return remapException(netconfOps.getConfigRunningData(
50                 new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path), fields));
51     }
52
53     private @NonNull FluentFuture<Optional<NormalizedNode>> readOperationalData(final YangInstanceIdentifier path,
54             final List<YangInstanceIdentifier> fields) {
55         return remapException(netconfOps.getData(
56                 new NetconfRpcFutureCallback("Data read", id), Optional.ofNullable(path), fields));
57     }
58 }