Updated sal-netconf-connector mountpoint integration
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfDevice.xtend
1 package org.opendaylight.controller.sal.connect.netconf
2
3 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance
4 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
5 import org.opendaylight.controller.md.sal.common.api.data.DataReader
6 import org.opendaylight.yangtools.yang.data.api.CompositeNode
7 import org.opendaylight.controller.netconf.client.NetconfClient
8 import org.opendaylight.controller.sal.core.api.RpcImplementation
9 import static extension org.opendaylight.controller.sal.connect.netconf.NetconfMapping.*
10 import java.net.InetSocketAddress
11 import org.opendaylight.yangtools.yang.data.api.Node
12 import org.opendaylight.yangtools.yang.data.api.SimpleNode
13 import org.opendaylight.yangtools.yang.common.QName
14 import java.util.Collections
15 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher
16 import org.opendaylight.yangtools.concepts.Registration
17
18 class NetconfDevice implements DataReader<InstanceIdentifier, CompositeNode>, RpcImplementation {
19
20     var NetconfClient client;
21
22     @Property
23     var InetSocketAddress socketAddress;
24
25     @Property
26     val MountProvisionInstance mountInstance;
27
28     @Property
29     val InstanceIdentifier path;
30     
31     Registration<DataReader<InstanceIdentifier,CompositeNode>> operReaderReg
32     
33     Registration<DataReader<InstanceIdentifier,CompositeNode>> confReaderReg
34     
35     public new(MountProvisionInstance mount,InstanceIdentifier path) {
36         _mountInstance = mount;
37         _path = path;
38     }
39
40     def start(NetconfClientDispatcher dispatcher) {
41         client = new NetconfClient("sal-netconf-connector", socketAddress, dispatcher);
42         
43         confReaderReg = mountInstance.registerConfigurationReader(path,this);
44         operReaderReg = mountInstance.registerOperationalReader(path,this);
45     }
46
47     override readConfigurationData(InstanceIdentifier path) {
48         val result = invokeRpc(NETCONF_GET_CONFIG_QNAME, wrap(NETCONF_GET_CONFIG_QNAME, path.toFilterStructure()));
49         val data = result.result.getFirstCompositeByName(NETCONF_DATA_QNAME);
50         return data?.findNode(path) as CompositeNode;
51     }
52
53     override readOperationalData(InstanceIdentifier path) {
54         val result = invokeRpc(NETCONF_GET_QNAME, wrap(NETCONF_GET_QNAME, path.toFilterStructure()));
55         val data = result.result.getFirstCompositeByName(NETCONF_DATA_QNAME);
56         return data?.findNode(path) as CompositeNode;
57     }
58
59     override getSupportedRpcs() {
60         Collections.emptySet;
61     }
62
63     override invokeRpc(QName rpc, CompositeNode input) {
64         val message = rpc.toRpcMessage(input);
65         val result = client.sendMessage(message);
66         return result.toRpcResult();
67     }
68
69     def Node<?> findNode(CompositeNode node, InstanceIdentifier identifier) {
70
71         var Node<?> current = node;
72         for (arg : identifier.path) {
73             if (current instanceof SimpleNode<?>) {
74                 return null;
75             } else if (current instanceof CompositeNode) {
76                 val currentComposite = (current as CompositeNode);
77
78                 current = currentComposite.getFirstCompositeByName(arg.nodeType);
79                 if (current == null) {
80                     current = currentComposite.getFirstSimpleByName(arg.nodeType);
81                 }
82                 if (current == null) {
83                     return null;
84                 }
85             }
86         }
87         return current;
88     }
89     
90     public def stop() {
91         confReaderReg?.close()
92         operReaderReg?.close()
93     }
94
95 }