Merge "BUG-692 Improve log message when negotiation fails"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceDataReader.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  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.controller.sal.connect.netconf.sal;
9
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;
15
16 import java.util.concurrent.ExecutionException;
17
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;
27
28 public final class NetconfDeviceDataReader implements DataReader<InstanceIdentifier,CompositeNode> {
29
30     private final RpcImplementation rpc;
31     private final RemoteDeviceId id;
32
33     public NetconfDeviceDataReader(final RemoteDeviceId id, final RpcImplementation rpc) {
34         this.id = id;
35         this.rpc = rpc;
36     }
37
38     @Override
39     public CompositeNode readConfigurationData(final InstanceIdentifier path) {
40         final RpcResult<CompositeNode> result;
41         try {
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);
48         }
49
50         final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
51         return data == null ? null : (CompositeNode) findNode(data, path);
52     }
53
54     private RuntimeException onInterruptedException(final InterruptedException e) {
55         Thread.currentThread().interrupt();
56         return new RuntimeException(id + ": Interrupted while waiting for response", e);
57     }
58
59     @Override
60     public CompositeNode readOperationalData(final InstanceIdentifier path) {
61         final RpcResult<CompositeNode> result;
62         try {
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);
68         }
69
70         final CompositeNode data = result.getResult().getFirstCompositeByName(NETCONF_DATA_QNAME);
71         return (CompositeNode) findNode(data, path);
72     }
73
74     private static Node<?> findNode(final CompositeNode node, final InstanceIdentifier identifier) {
75
76         Node<?> current = node;
77         for (final InstanceIdentifier.PathArgument arg : identifier.getPathArguments()) {
78             if (current instanceof SimpleNode<?>) {
79                 return null;
80             } else if (current instanceof CompositeNode) {
81                 final CompositeNode currentComposite = (CompositeNode) current;
82
83                 current = currentComposite.getFirstCompositeByName(arg.getNodeType());
84                 if (current == null) {
85                     current = currentComposite.getFirstCompositeByName(arg.getNodeType().withoutRevision());
86                 }
87                 if (current == null) {
88                     current = currentComposite.getFirstSimpleByName(arg.getNodeType());
89                 }
90                 if (current == null) {
91                     current = currentComposite.getFirstSimpleByName(arg.getNodeType().withoutRevision());
92                 }
93                 if (current == null) {
94                     return null;
95                 }
96             }
97         }
98         return current;
99     }
100 }