1 package org.opendaylight.controller.sal.connect.netconf
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
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
22 class NetconfMapping {
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");
34 static val messageId = new AtomicInteger(0);
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);
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);
52 return new CompositeNodeTOImpl(argument.nodeType,null,list)
55 static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
57 return new CompositeNodeTOImpl(argument.nodeType,null,Collections.singletonList(node));
59 return new SimpleNodeTOImpl(argument.nodeType,null,null);
63 static def CompositeNode toCompositeNode(NetconfMessage message) {
64 return message.toRpcResult().result;
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);
74 static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message) {
75 val rawRpc = message.document.toCompositeNode() as CompositeNode;
78 return Rpcs.getRpcResult(true,rawRpc,Collections.emptySet());
82 static def wrap(QName name,Node<?> node) {
84 return new CompositeNodeTOImpl(name,null,Collections.singletonList(node));
87 return new CompositeNodeTOImpl(name,null,Collections.emptyList());
92 public static def Node<?> toCompositeNode(Document document) {
93 return XmlDocumentUtils.toCompositeNode(document) as Node<?>