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;
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 java.util.concurrent.ExecutionException;
18 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
19 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
20 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
21 import org.opendaylight.controller.sal.core.api.RpcImplementation;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.Node;
26 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 public final class NetconfDeviceDataReader implements DataReader<InstanceIdentifier,CompositeNode> {
30 private final RpcImplementation rpc;
31 private final RemoteDeviceId id;
33 public NetconfDeviceDataReader(final RemoteDeviceId id, final RpcImplementation rpc) {
39 public CompositeNode readConfigurationData(final InstanceIdentifier path) {
40 final RpcResult<CompositeNode> result;
42 result = rpc.invokeRpc(NETCONF_GET_CONFIG_QNAME,
43 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, CONFIG_SOURCE_RUNNING, toFilterStructure(path))).get();
44 } catch (final InterruptedException e) {
45 throw onInterruptedException(e);
46 } catch (final ExecutionException e) {
47 throw new RuntimeException(id + ": Read configuration data " + path + " failed", e);
50 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
51 return data == null ? null : (CompositeNode) findNode(data, path);
54 private RuntimeException onInterruptedException(final InterruptedException e) {
55 Thread.currentThread().interrupt();
56 return new RuntimeException(id + ": Interrupted while waiting for response", e);
60 public CompositeNode readOperationalData(final InstanceIdentifier path) {
61 final RpcResult<CompositeNode> result;
63 result = rpc.invokeRpc(NETCONF_GET_QNAME, NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, toFilterStructure(path))).get();
64 } catch (final InterruptedException e) {
65 throw onInterruptedException(e);
66 } catch (final ExecutionException e) {
67 throw new RuntimeException(id + ": Read operational data " + path + " failed", e);
70 final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
71 return (CompositeNode) findNode(data, path);
74 private static Node<?> findNode(final CompositeNode node, final InstanceIdentifier identifier) {
76 Node<?> current = node;
77 for (final InstanceIdentifier.PathArgument arg : identifier.getPath()) {
78 if (current instanceof SimpleNode<?>) {
80 } else if (current instanceof CompositeNode) {
81 final CompositeNode currentComposite = (CompositeNode) current;
83 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
84 if (current == null) {
85 current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
87 if (current == null) {
88 current = currentComposite.getFirstSimpleByName(arg.getNodeType());
90 if (current == null) {
91 current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
93 if (current == null) {