Merge "Add test for generated code checking list of dependencies."
[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 import org.opendaylight.yangtools.yang.data.api.SimpleNode
24 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
25
26 class NetconfMapping {
27
28     public static val NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0")
29     public static val NETCONF_MONITORING_URI = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"
30     public static val NETCONF_NOTIFICATION_URI = URI.create("urn:ietf:params:xml:ns:netconf:notification:1.0")
31     
32     
33     public static val NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf");
34     public static val NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
35     public static val NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
36     public static val NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
37     public static val NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
38     public static val NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
39     public static val NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
40     public static val NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
41     public static val NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
42     public static val NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok");
43     public static val NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
44     public static val NETCONF_CREATE_SUBSCRIPTION_QNAME = QName.create(NETCONF_NOTIFICATION_URI,null,"create-subscription");
45     public static val NETCONF_CANCEL_SUBSCRIPTION_QNAME = QName.create(NETCONF_NOTIFICATION_URI,null,"cancel-subscription");
46     public static val IETF_NETCONF_MONITORING_MODULE = QName.create(NETCONF_MONITORING_URI, "2010-10-04","ietf-netconf-monitoring");
47
48     static List<Node<?>> RUNNING = Collections.<Node<?>>singletonList(
49         new SimpleNodeTOImpl(NETCONF_RUNNING_QNAME, null, null));
50     public static val CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME, null, RUNNING);
51
52     static val messageId = new AtomicInteger(0);
53
54     static def Node<?> toFilterStructure(InstanceIdentifier identifier) {
55         var Node<?> previous = null;
56         if(identifier.path.empty) {
57             return null;
58         }
59         
60         for (component : identifier.path.reverseView) {
61             val Node<?> current = component.toNode(previous);
62             previous = current;
63         }
64         return filter("subtree",previous);
65     }
66
67     static def dispatch Node<?> toNode(NodeIdentifierWithPredicates argument, Node<?> node) {
68         val list = new ArrayList<Node<?>>();
69         for (arg : argument.keyValues.entrySet) {
70             list.add = new SimpleNodeTOImpl(arg.key, null, arg.value);
71         }
72         return new CompositeNodeTOImpl(argument.nodeType, null, list)
73     }
74
75     static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
76         if (node != null) {
77             return new CompositeNodeTOImpl(argument.nodeType, null, Collections.singletonList(node));
78         } else {
79             return new SimpleNodeTOImpl(argument.nodeType, null, null);
80         }
81     }
82
83     static def CompositeNode toCompositeNode(NetconfMessage message) {
84         return message.toRpcResult().result;
85     }
86
87     static def NetconfMessage toRpcMessage(QName rpc, CompositeNode node) {
88         val rpcPayload = wrap(NETCONF_RPC_QNAME, flattenInput(node));
89         val w3cPayload = NodeUtils.buildShadowDomTree(rpcPayload);
90         w3cPayload.documentElement.setAttribute("message-id", "m-" + messageId.andIncrement);
91         return new NetconfMessage(w3cPayload);
92     }
93     
94     def static flattenInput(CompositeNode node) {
95         val inputQName = QName.create(node.nodeType,"input");
96         val input = node.getFirstCompositeByName(inputQName);
97         if(input == null) return node;
98         if(input instanceof CompositeNode) {
99             
100             val nodes = ImmutableList.builder() //
101                 .addAll(input.children) //
102                 .addAll(node.children.filter[nodeType != inputQName]) //
103                 .build()
104             return ImmutableCompositeNode.create(node.nodeType,nodes);
105         } 
106         
107     }
108
109     static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message) {
110         val rawRpc = message.document.toCompositeNode() as CompositeNode;
111
112         //rawRpc.
113         return Rpcs.getRpcResult(true, rawRpc, Collections.emptySet());
114     }
115
116     static def wrap(QName name, Node<?> node) {
117         if (node != null) {
118             return new CompositeNodeTOImpl(name, null, Collections.singletonList(node));
119         } else {
120             return new CompositeNodeTOImpl(name, null, Collections.emptyList());
121         }
122     }
123
124     static def wrap(QName name, Node<?> additional, Node<?> node) {
125         if (node != null) {
126             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
127         } else {
128             return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional));
129         }
130     }
131
132     static def filter(String type, Node<?> node) {
133         val it = ImmutableCompositeNode.builder(); //
134         setQName(NETCONF_FILTER_QNAME);
135         setAttribute(NETCONF_TYPE_QNAME,type);
136         if (node != null) {
137             return add(node).toInstance();
138         } else {
139             return toInstance();
140         }
141     }
142
143     public static def Node<?> toCompositeNode(Document document) {
144         return XmlDocumentUtils.toNode(document) as Node<?>
145     }
146 }