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.controller.sal.connect.netconf.sal.tx;
10 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.CONFIG_SOURCE_RUNNING;
11 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
12 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
13 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
14 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
16 import com.google.common.base.Function;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
23 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
25 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
26 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.controller.sal.core.api.RpcImplementation;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.Node;
32 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public final class NetconfDeviceReadOnlyTx implements DOMDataReadOnlyTransaction {
40 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceReadOnlyTx.class);
42 private final RpcImplementation rpc;
43 private final DataNormalizer normalizer;
44 private final RemoteDeviceId id;
46 public NetconfDeviceReadOnlyTx(final RpcImplementation rpc, final DataNormalizer normalizer, final RemoteDeviceId id) {
48 this.normalizer = normalizer;
52 public ListenableFuture<Optional<NormalizedNode<?, ?>>> readConfigurationData(final YangInstanceIdentifier path) {
53 final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
54 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, CONFIG_SOURCE_RUNNING, toFilterStructure(path)));
56 return Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
58 public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
59 checkReadSuccess(result, path);
61 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
62 final CompositeNode node = (CompositeNode) findNode(data, path);
65 Optional.<NormalizedNode<?, ?>>absent() :
66 transform(path, node);
71 private void checkReadSuccess(final RpcResult<CompositeNode> result, final YangInstanceIdentifier path) {
72 LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
73 Preconditions.checkArgument(result.isSuccessful(), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
76 private Optional<NormalizedNode<?, ?>> transform(final YangInstanceIdentifier path, final CompositeNode node) {
78 return Optional.absent();
81 return Optional.<NormalizedNode<?, ?>>of(normalizer.toNormalized(path, node).getValue());
82 } catch (final Exception e) {
83 LOG.error("{}: Unable to normalize data for {}, data: {}", id, path, node, e);
88 public ListenableFuture<Optional<NormalizedNode<?, ?>>> readOperationalData(final YangInstanceIdentifier path) {
89 final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(path)));
91 return Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
93 public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
94 checkReadSuccess(result, path);
96 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
97 final CompositeNode node = (CompositeNode) findNode(data, path);
100 Optional.<NormalizedNode<?, ?>>absent() :
101 transform(path, node);
106 private static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
108 Node<?> current = node;
109 for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
110 if (current instanceof SimpleNode<?>) {
112 } else if (current instanceof CompositeNode) {
113 final CompositeNode currentComposite = (CompositeNode) current;
115 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
116 if (current == null) {
117 current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
119 if (current == null) {
120 current = currentComposite.getFirstSimpleByName(arg.getNodeType());
122 if (current == null) {
123 current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
125 if (current == null) {
134 public void close() {
139 public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
140 final YangInstanceIdentifier legacyPath = toLegacyPath(normalizer, path, id);
143 case CONFIGURATION : {
144 return readConfigurationData(legacyPath);
147 return readOperationalData(legacyPath);
151 throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
154 static YangInstanceIdentifier toLegacyPath(final DataNormalizer normalizer, final YangInstanceIdentifier path, final RemoteDeviceId id) {
156 return normalizer.toLegacy(path);
157 } catch (final DataNormalizationException e) {
158 throw new IllegalArgumentException(id + ": Cannot normalize path " + path, e);
163 public Object getIdentifier() {