Merge "Fixed GlobalEventExecutorCloseable - override "unsupported opearaion" methods."
[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 import org.opendaylight.controller.sal.core.api.Provider
18 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession
19 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService
20 import static org.opendaylight.controller.sal.connect.netconf.InventoryUtils.*;
21 import org.opendaylight.controller.sal.core.api.data.DataBrokerService
22 import org.opendaylight.controller.sal.core.api.data.DataModificationTransaction
23 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
24 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
25
26 class NetconfDevice implements Provider, DataReader<InstanceIdentifier, CompositeNode>, RpcImplementation, AutoCloseable {
27
28     var NetconfClient client;
29
30     @Property
31     var InetSocketAddress socketAddress;
32
33     @Property
34     var MountProvisionInstance mountInstance;
35
36     @Property
37     var InstanceIdentifier path;
38
39     Registration<DataReader<InstanceIdentifier, CompositeNode>> operReaderReg
40
41     Registration<DataReader<InstanceIdentifier, CompositeNode>> confReaderReg
42
43     String name
44
45     MountProvisionService mountService
46
47     public new(String name) {
48         this.name = name;
49         this.path = InstanceIdentifier.builder(INVENTORY_PATH).nodeWithKey(INVENTORY_NODE,
50             Collections.singletonMap(INVENTORY_ID, name)).toInstance;
51     }
52
53     def start(NetconfClientDispatcher dispatcher) {
54         client = new NetconfClient(name, socketAddress, dispatcher);
55         confReaderReg = mountInstance.registerConfigurationReader(path, this);
56         operReaderReg = mountInstance.registerOperationalReader(path, this);
57     }
58
59     override readConfigurationData(InstanceIdentifier path) {
60         val result = invokeRpc(NETCONF_GET_CONFIG_QNAME, wrap(NETCONF_GET_CONFIG_QNAME, path.toFilterStructure()));
61         val data = result.result.getFirstCompositeByName(NETCONF_DATA_QNAME);
62         return data?.findNode(path) as CompositeNode;
63     }
64
65     override readOperationalData(InstanceIdentifier path) {
66         val result = invokeRpc(NETCONF_GET_QNAME, wrap(NETCONF_GET_QNAME, path.toFilterStructure()));
67         val data = result.result.getFirstCompositeByName(NETCONF_DATA_QNAME);
68         return data?.findNode(path) as CompositeNode;
69     }
70
71     override getSupportedRpcs() {
72         Collections.emptySet;
73     }
74
75     override invokeRpc(QName rpc, CompositeNode input) {
76         val message = rpc.toRpcMessage(input);
77         val result = client.sendMessage(message);
78         return result.toRpcResult();
79     }
80
81     override getProviderFunctionality() {
82         Collections.emptySet
83     }
84
85     override onSessionInitiated(ProviderSession session) {
86         val dataBroker = session.getService(DataBrokerService);
87         
88         
89         
90         val transaction = dataBroker.beginTransaction
91         if(transaction.operationalNodeNotExisting) {
92             transaction.putOperationalData(path,nodeWithId)
93         }
94         if(transaction.configurationNodeNotExisting) {
95             transaction.putConfigurationData(path,nodeWithId)
96         }
97         transaction.commit().get();
98         mountService = session.getService(MountProvisionService);
99         mountInstance = mountService.createOrGetMountPoint(path);
100     }
101     
102     def getNodeWithId() {
103         val id = new SimpleNodeTOImpl(INVENTORY_ID,null,name);
104         return new CompositeNodeTOImpl(INVENTORY_NODE,null,Collections.singletonList(id));
105     }
106     
107     def boolean configurationNodeNotExisting(DataModificationTransaction transaction) {
108         return null === transaction.readConfigurationData(path);
109     }
110     
111     def boolean operationalNodeNotExisting(DataModificationTransaction transaction) {
112         return null === transaction.readOperationalData(path);
113     }
114
115     def Node<?> findNode(CompositeNode node, InstanceIdentifier identifier) {
116
117         var Node<?> current = node;
118         for (arg : identifier.path) {
119             if (current instanceof SimpleNode<?>) {
120                 return null;
121             } else if (current instanceof CompositeNode) {
122                 val currentComposite = (current as CompositeNode);
123
124                 current = currentComposite.getFirstCompositeByName(arg.nodeType);
125                 if (current == null) {
126                     current = currentComposite.getFirstSimpleByName(arg.nodeType);
127                 }
128                 if (current == null) {
129                     return null;
130                 }
131             }
132         }
133         return current;
134     }
135
136     override close() {
137         confReaderReg?.close()
138         operReaderReg?.close()
139         client?.close()
140     }
141
142 }