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;
37 public final class NetconfDeviceReadOnlyTx implements DOMDataReadOnlyTransaction {
39 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceReadOnlyTx.class);
41 private final RpcImplementation rpc;
42 private final DataNormalizer normalizer;
43 private final RemoteDeviceId id;
45 public NetconfDeviceReadOnlyTx(final RpcImplementation rpc, final DataNormalizer normalizer, final RemoteDeviceId id) {
47 this.normalizer = normalizer;
51 public ListenableFuture<Optional<NormalizedNode<?, ?>>> readConfigurationData(final YangInstanceIdentifier path) {
52 final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
53 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, CONFIG_SOURCE_RUNNING, toFilterStructure(path)));
55 return Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
57 public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
58 checkReadSuccess(result, path);
60 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
61 final CompositeNode node = (CompositeNode) findNode(data, path);
64 Optional.<NormalizedNode<?, ?>>absent() :
65 transform(path, node);
70 private void checkReadSuccess(final RpcResult<CompositeNode> result, final YangInstanceIdentifier path) {
71 LOG.warn("{}: Unable to read data: {}, errors: {}", id, path, result.getErrors());
72 Preconditions.checkArgument(result.isSuccessful(), "%s: Unable to read data: %s, errors: %s", id, path, result.getErrors());
75 private Optional<NormalizedNode<?, ?>> transform(final YangInstanceIdentifier path, final CompositeNode node) {
77 return Optional.absent();
80 return Optional.<NormalizedNode<?, ?>>of(normalizer.toNormalized(path, node).getValue());
81 } catch (final Exception e) {
82 LOG.error("{}: Unable to normalize data for {}, data: {}", id, path, node, e);
87 public ListenableFuture<Optional<NormalizedNode<?, ?>>> readOperationalData(final YangInstanceIdentifier path) {
88 final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(path)));
90 return Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
92 public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
93 checkReadSuccess(result, path);
95 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
96 final CompositeNode node = (CompositeNode) findNode(data, path);
99 Optional.<NormalizedNode<?, ?>>absent() :
100 transform(path, node);
105 private static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
107 Node<?> current = node;
108 for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
109 if (current instanceof SimpleNode<?>) {
111 } else if (current instanceof CompositeNode) {
112 final CompositeNode currentComposite = (CompositeNode) current;
114 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
115 if (current == null) {
116 current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
118 if (current == null) {
119 current = currentComposite.getFirstSimpleByName(arg.getNodeType());
121 if (current == null) {
122 current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
124 if (current == null) {
133 public void close() {
138 public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
139 final YangInstanceIdentifier legacyPath = toLegacyPath(normalizer, path, id);
142 case CONFIGURATION : {
143 return readConfigurationData(legacyPath);
146 return readOperationalData(legacyPath);
150 throw new IllegalArgumentException(String.format("%s, Cannot read data %s for %s datastore, unknown datastore type", id, path, store));
153 static YangInstanceIdentifier toLegacyPath(final DataNormalizer normalizer, final YangInstanceIdentifier path, final RemoteDeviceId id) {
155 return normalizer.toLegacy(path);
156 } catch (final DataNormalizationException e) {
157 throw new IllegalArgumentException(id + ": Cannot normalize path " + path, e);
162 public Object getIdentifier() {