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 import com.google.common.collect.ImmutableList
24 class NetconfMapping {
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");
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);
40 static val messageId = new AtomicInteger(0);
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);
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);
60 return new CompositeNodeTOImpl(argument.nodeType,null,list)
63 static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
65 return new CompositeNodeTOImpl(argument.nodeType,null,Collections.singletonList(node));
67 return new SimpleNodeTOImpl(argument.nodeType,null,null);
71 static def CompositeNode toCompositeNode(NetconfMessage message) {
72 return message.toRpcResult().result;
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);
82 static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message) {
83 val rawRpc = message.document.toCompositeNode() as CompositeNode;
86 return Rpcs.getRpcResult(true,rawRpc,Collections.emptySet());
90 static def wrap(QName name,Node<?> node) {
92 return new CompositeNodeTOImpl(name,null,Collections.singletonList(node));
95 return new CompositeNodeTOImpl(name,null,Collections.emptyList());
99 static def wrap(QName name,Node<?> additional,Node<?> node) {
101 return new CompositeNodeTOImpl(name,null,ImmutableList.of(additional,node));
104 return new CompositeNodeTOImpl(name,null,ImmutableList.of(additional));
109 public static def Node<?> toCompositeNode(Document document) {
110 return XmlDocumentUtils.toCompositeNode(document) as Node<?>