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.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
22 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
24 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
25 import org.opendaylight.controller.sal.core.api.RpcImplementation;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.Node;
30 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public final class NetconfDeviceReadOnlyTx implements DOMDataReadOnlyTransaction {
37 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceReadOnlyTx.class);
39 private final RpcImplementation rpc;
40 private final DataNormalizer normalizer;
42 public NetconfDeviceReadOnlyTx(final RpcImplementation rpc, final DataNormalizer normalizer) {
44 this.normalizer = normalizer;
47 public ListenableFuture<Optional<NormalizedNode<?, ?>>> readConfigurationData(final YangInstanceIdentifier path) {
48 final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
49 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, CONFIG_SOURCE_RUNNING, toFilterStructure(path)));
51 return Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
53 public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
54 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
55 final CompositeNode node = (CompositeNode) findNode(data, path);
58 Optional.<NormalizedNode<?, ?>>absent() :
59 transform(path, node);
64 private Optional<NormalizedNode<?, ?>> transform(final YangInstanceIdentifier path, final CompositeNode node) {
66 return Optional.absent();
69 return Optional.<NormalizedNode<?, ?>>of(normalizer.toNormalized(path, node).getValue());
70 } catch (final Exception e) {
71 LOG.error("Unable to normalize data for {}, data: {}", path, node, e);
76 public ListenableFuture<Optional<NormalizedNode<?, ?>>> readOperationalData(final YangInstanceIdentifier path) {
77 final ListenableFuture<RpcResult<CompositeNode>> future = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(path)));
79 return Futures.transform(future, new Function<RpcResult<CompositeNode>, Optional<NormalizedNode<?, ?>>>() {
81 public Optional<NormalizedNode<?, ?>> apply(final RpcResult<CompositeNode> result) {
82 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
83 final CompositeNode node = (CompositeNode) findNode(data, path);
86 Optional.<NormalizedNode<?, ?>>absent() :
87 transform(path, node);
92 private static Node<?> findNode(final CompositeNode node, final YangInstanceIdentifier identifier) {
94 Node<?> current = node;
95 for (final YangInstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
96 if (current instanceof SimpleNode<?>) {
98 } else if (current instanceof CompositeNode) {
99 final CompositeNode currentComposite = (CompositeNode) current;
101 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
102 if (current == null) {
103 current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
105 if (current == null) {
106 current = currentComposite.getFirstSimpleByName(arg.getNodeType());
108 if (current == null) {
109 current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
111 if (current == null) {
120 public void close() {
125 public ListenableFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
126 final YangInstanceIdentifier legacyPath = toLegacyPath(normalizer, path);
129 case CONFIGURATION : {
130 return readConfigurationData(legacyPath);
133 return readOperationalData(legacyPath);
137 throw new IllegalArgumentException(String.format("Cannot read data %s for %s datastore, unknown datastore type", path, store));
140 static YangInstanceIdentifier toLegacyPath(final DataNormalizer normalizer, final YangInstanceIdentifier path) {
142 return normalizer.toLegacy(path);
143 } catch (final DataNormalizationException e) {
144 throw new IllegalArgumentException("Cannot normalize path " + path, e);
149 public Object getIdentifier() {