Merge "Changed maximumEntries to correct int rather than long"
[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 import java.util.List
22 import com.google.common.collect.ImmutableList
23
24 class NetconfMapping {
25
26     public static val NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0")
27     public static val NETCONF_QNAME = new QName(NETCONF_URI,null,"netconf");
28     public static val NETCONF_RPC_QNAME = new QName(NETCONF_QNAME,"rpc");
29     public static val NETCONF_GET_QNAME = new QName(NETCONF_QNAME,"get");
30     public static val NETCONF_GET_CONFIG_QNAME = new QName(NETCONF_QNAME,"get-config");
31     public static val NETCONF_SOURCE_QNAME = new QName(NETCONF_QNAME,"source");
32     public static val NETCONF_RUNNING_QNAME = new QName(NETCONF_QNAME,"running");
33     public static val NETCONF_RPC_REPLY_QNAME = new QName(NETCONF_QNAME,"rpc-reply");
34     public static val NETCONF_OK_QNAME = new QName(NETCONF_QNAME,"ok");
35     public static val NETCONF_DATA_QNAME = new QName(NETCONF_QNAME,"data");
36     
37      static List<Node<?>> RUNNING = Collections.<Node<?>>singletonList(new SimpleNodeTOImpl(NETCONF_RUNNING_QNAME,null,null));
38     public static val CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME,null,RUNNING);
39
40     static val messageId = new AtomicInteger(0);
41     
42    
43
44
45
46     static def Node<?> toFilterStructure(InstanceIdentifier identifier) {
47         var Node<?> previous = null;
48         for (component : identifier.path.reverse) {
49             val Node<?> current = component.toNode(previous);
50             previous = current;
51         }
52         return previous;
53     }
54     
55     static def dispatch Node<?> toNode(NodeIdentifierWithPredicates argument, Node<?> node) {
56         val list = new ArrayList<Node<?>>();
57         for( arg : argument.keyValues.entrySet) {
58             list.add = new SimpleNodeTOImpl(arg.key,null,arg.value);
59         }
60         return new CompositeNodeTOImpl(argument.nodeType,null,list)
61     }
62     
63     static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
64         if(node != null) {
65             return new CompositeNodeTOImpl(argument.nodeType,null,Collections.singletonList(node));
66         } else {
67             return new SimpleNodeTOImpl(argument.nodeType,null,null);
68         }
69     }
70
71     static def CompositeNode toCompositeNode(NetconfMessage message) {
72         return message.toRpcResult().result;
73     }
74
75     static def NetconfMessage toRpcMessage(QName rpc, CompositeNode node) {
76         val rpcPayload = wrap(NETCONF_RPC_QNAME,node);
77         val w3cPayload = NodeUtils.buildShadowDomTree(rpcPayload);
78         w3cPayload.documentElement.setAttribute("message-id","m-"+ messageId.andIncrement);
79         return new NetconfMessage(w3cPayload);
80     }
81
82     static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message) {
83         val rawRpc = message.document.toCompositeNode() as CompositeNode;
84         //rawRpc.
85         
86         return Rpcs.getRpcResult(true,rawRpc,Collections.emptySet());
87     }
88     
89     
90     static def wrap(QName name,Node<?> node) {
91         if(node != null) {
92             return new CompositeNodeTOImpl(name,null,Collections.singletonList(node));
93         }
94         else {
95             return new CompositeNodeTOImpl(name,null,Collections.emptyList());
96         }
97     }
98     
99         static def wrap(QName name,Node<?> additional,Node<?> node) {
100         if(node != null) {
101             return new CompositeNodeTOImpl(name,null,ImmutableList.of(additional,node));
102         }
103         else {
104             return new CompositeNodeTOImpl(name,null,ImmutableList.of(additional));
105         }
106     }
107     
108     
109     public static def Node<?> toCompositeNode(Document document) {
110         return XmlDocumentUtils.toCompositeNode(document) as Node<?>
111     }
112 }