Updated sal-netconf-connector mountpoint integration
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfMapping.xtend
1 package org.opendaylight.controller.sal.connect.netconf
2
3 import org.opendaylight.controller.netconf.api.NetconfMessage
4 import org.opendaylight.yangtools.yang.data.api.CompositeNode
5 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
6 import org.opendaylight.yangtools.yang.common.QName
7 import org.opendaylight.yangtools.yang.common.RpcResult
8 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
9 import java.net.URI
10 import java.util.Collections
11 import org.opendaylight.yangtools.yang.data.api.Node
12 import org.opendaylight.yangtools.yang.data.impl.NodeUtils
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
14 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
15 import java.util.ArrayList
16 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
17 import java.util.concurrent.atomic.AtomicInteger
18 import org.w3c.dom.Document
19 import org.w3c.dom.Element
20 import org.opendaylight.controller.sal.common.util.Rpcs
21
22 class NetconfMapping {
23
24     public static val NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0")
25     public static val NETCONF_QNAME = new QName(NETCONF_URI,null,"netconf");
26     public static val NETCONF_RPC_QNAME = new QName(NETCONF_QNAME,"rpc");
27     public static val NETCONF_GET_QNAME = new QName(NETCONF_QNAME,"get");
28     public static val NETCONF_GET_CONFIG_QNAME = new QName(NETCONF_QNAME,"get-config");
29     public static val NETCONF_RPC_REPLY_QNAME = new QName(NETCONF_QNAME,"rpc-reply");
30     public static val NETCONF_OK_QNAME = new QName(NETCONF_QNAME,"ok");
31     public static val NETCONF_DATA_QNAME = new QName(NETCONF_QNAME,"data");
32     
33
34     static val messageId = new AtomicInteger(0);
35
36
37
38     static def Node<?> toFilterStructure(InstanceIdentifier identifier) {
39         var Node<?> previous = null;
40         for (component : identifier.path.reverse) {
41             val Node<?> current = component.toNode(previous);
42             previous = current;
43         }
44         return previous;
45     }
46     
47     static def dispatch Node<?> toNode(NodeIdentifierWithPredicates argument, Node<?> node) {
48         val list = new ArrayList<Node<?>>();
49         for( arg : argument.keyValues.entrySet) {
50             list.add = new SimpleNodeTOImpl(arg.key,null,arg.value);
51         }
52         return new CompositeNodeTOImpl(argument.nodeType,null,list)
53     }
54     
55     static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
56         if(node != null) {
57             return new CompositeNodeTOImpl(argument.nodeType,null,Collections.singletonList(node));
58         } else {
59             return new SimpleNodeTOImpl(argument.nodeType,null,null);
60         }
61     }
62
63     static def CompositeNode toCompositeNode(NetconfMessage message) {
64         return message.toRpcResult().result;
65     }
66
67     static def NetconfMessage toRpcMessage(QName rpc, CompositeNode node) {
68         val rpcPayload = wrap(NETCONF_RPC_QNAME,node);
69         val w3cPayload = NodeUtils.buildShadowDomTree(rpcPayload);
70         w3cPayload.documentElement.setAttribute("message-id","m-"+ messageId.andIncrement);
71         return new NetconfMessage(w3cPayload);
72     }
73
74     static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message) {
75         val rawRpc = message.document.toCompositeNode() as CompositeNode;
76         //rawRpc.
77         
78         return Rpcs.getRpcResult(true,rawRpc,Collections.emptySet());
79     }
80     
81     
82     static def wrap(QName name,Node<?> node) {
83         if(node != null) {
84             return new CompositeNodeTOImpl(name,null,Collections.singletonList(node));
85         }
86         else {
87             return new CompositeNodeTOImpl(name,null,Collections.emptyList());
88         }
89     }
90     
91     
92     public static def Node<?> toCompositeNode(Document document) {
93         return XmlDocumentUtils.toCompositeNode(document) as Node<?>
94     }
95 }