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
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode
24 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
26 class NetconfMapping {
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")
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");
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);
52 static val messageId = new AtomicInteger(0);
54 static def Node<?> toFilterStructure(InstanceIdentifier identifier) {
55 var Node<?> previous = null;
56 if(identifier.path.empty) {
60 for (component : identifier.path.reverseView) {
61 val Node<?> current = component.toNode(previous);
64 return filter("subtree",previous);
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);
72 return new CompositeNodeTOImpl(argument.nodeType, null, list)
75 static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
77 return new CompositeNodeTOImpl(argument.nodeType, null, Collections.singletonList(node));
79 return new SimpleNodeTOImpl(argument.nodeType, null, null);
83 static def CompositeNode toCompositeNode(NetconfMessage message) {
84 return message.toRpcResult().result;
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);
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) {
100 val nodes = ImmutableList.builder() //
101 .addAll(input.children) //
102 .addAll(node.children.filter[nodeType != inputQName]) //
104 return ImmutableCompositeNode.create(node.nodeType,nodes);
109 static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message) {
110 val rawRpc = message.document.toCompositeNode() as CompositeNode;
113 return Rpcs.getRpcResult(true, rawRpc, Collections.emptySet());
116 static def wrap(QName name, Node<?> node) {
118 return new CompositeNodeTOImpl(name, null, Collections.singletonList(node));
120 return new CompositeNodeTOImpl(name, null, Collections.emptyList());
124 static def wrap(QName name, Node<?> additional, Node<?> node) {
126 return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
128 return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional));
132 static def filter(String type, Node<?> node) {
133 val it = ImmutableCompositeNode.builder(); //
134 setQName(NETCONF_FILTER_QNAME);
135 setAttribute(NETCONF_TYPE_QNAME,type);
137 return add(node).toInstance();
143 public static def Node<?> toCompositeNode(Document document) {
144 return XmlDocumentUtils.toNode(document) as Node<?>