2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.connect.netconf
10 import org.opendaylight.controller.netconf.api.NetconfMessage
11 import org.opendaylight.yangtools.yang.data.api.CompositeNode
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
13 import org.opendaylight.yangtools.yang.common.QName
14 import org.opendaylight.yangtools.yang.common.RpcResult
15 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
17 import java.util.Collections
18 import org.opendaylight.yangtools.yang.data.api.Node
19 import org.opendaylight.yangtools.yang.data.impl.NodeUtils
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
22 import java.util.ArrayList
23 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
24 import java.util.concurrent.atomic.AtomicInteger
25 import org.w3c.dom.Document
26 import org.w3c.dom.Element
27 import org.opendaylight.controller.sal.common.util.Rpcs
29 import com.google.common.collect.ImmutableList
30 import org.opendaylight.yangtools.yang.data.api.SimpleNode
31 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
32 import com.google.common.base.Preconditions
33 import com.google.common.base.Optional
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext
35 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils
37 class NetconfMapping {
39 public static val NETCONF_URI = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0")
40 public static val NETCONF_MONITORING_URI = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"
41 public static val NETCONF_NOTIFICATION_URI = URI.create("urn:ietf:params:xml:ns:netconf:notification:1.0")
44 public static val NETCONF_QNAME = QName.create(NETCONF_URI, null, "netconf");
45 public static val NETCONF_RPC_QNAME = QName.create(NETCONF_QNAME, "rpc");
46 public static val NETCONF_GET_QNAME = QName.create(NETCONF_QNAME, "get");
47 public static val NETCONF_FILTER_QNAME = QName.create(NETCONF_QNAME, "filter");
48 public static val NETCONF_TYPE_QNAME = QName.create(NETCONF_QNAME, "type");
49 public static val NETCONF_GET_CONFIG_QNAME = QName.create(NETCONF_QNAME, "get-config");
50 public static val NETCONF_EDIT_CONFIG_QNAME = QName.create(NETCONF_QNAME, "edit-config");
51 public static val NETCONF_DELETE_CONFIG_QNAME = QName.create(NETCONF_QNAME, "delete-config");
52 public static val NETCONF_ACTION_QNAME = QName.create(NETCONF_QNAME, "action");
53 public static val NETCONF_COMMIT_QNAME = QName.create(NETCONF_QNAME, "commit");
55 public static val NETCONF_CONFIG_QNAME = QName.create(NETCONF_QNAME, "config");
56 public static val NETCONF_SOURCE_QNAME = QName.create(NETCONF_QNAME, "source");
57 public static val NETCONF_TARGET_QNAME = QName.create(NETCONF_QNAME, "target");
59 public static val NETCONF_CANDIDATE_QNAME = QName.create(NETCONF_QNAME, "candidate");
60 public static val NETCONF_RUNNING_QNAME = QName.create(NETCONF_QNAME, "running");
63 public static val NETCONF_RPC_REPLY_QNAME = QName.create(NETCONF_QNAME, "rpc-reply");
64 public static val NETCONF_OK_QNAME = QName.create(NETCONF_QNAME, "ok");
65 public static val NETCONF_DATA_QNAME = QName.create(NETCONF_QNAME, "data");
66 public static val NETCONF_CREATE_SUBSCRIPTION_QNAME = QName.create(NETCONF_NOTIFICATION_URI,null,"create-subscription");
67 public static val NETCONF_CANCEL_SUBSCRIPTION_QNAME = QName.create(NETCONF_NOTIFICATION_URI,null,"cancel-subscription");
68 public static val IETF_NETCONF_MONITORING_MODULE = QName.create(NETCONF_MONITORING_URI, "2010-10-04","ietf-netconf-monitoring");
70 static List<Node<?>> RUNNING = Collections.<Node<?>>singletonList(
71 new SimpleNodeTOImpl(NETCONF_RUNNING_QNAME, null, null));
72 public static val CONFIG_SOURCE_RUNNING = new CompositeNodeTOImpl(NETCONF_SOURCE_QNAME, null, RUNNING);
74 static val messageId = new AtomicInteger(0);
76 static def Node<?> toFilterStructure(InstanceIdentifier identifier) {
77 var Node<?> previous = null;
78 if(identifier.path.empty) {
82 for (component : identifier.path.reverseView) {
83 val Node<?> current = component.toNode(previous);
86 return filter("subtree",previous);
89 static def dispatch Node<?> toNode(NodeIdentifierWithPredicates argument, Node<?> node) {
90 val list = new ArrayList<Node<?>>();
91 for (arg : argument.keyValues.entrySet) {
92 list.add = new SimpleNodeTOImpl(arg.key, null, arg.value);
94 return new CompositeNodeTOImpl(argument.nodeType, null, list)
97 static def dispatch Node<?> toNode(PathArgument argument, Node<?> node) {
99 return new CompositeNodeTOImpl(argument.nodeType, null, Collections.singletonList(node));
101 return new SimpleNodeTOImpl(argument.nodeType, null, null);
105 static def CompositeNode toCompositeNode(NetconfMessage message,Optional<SchemaContext> ctx) {
106 return null//message.toRpcResult().result;
109 static def NetconfMessage toRpcMessage(QName rpc, CompositeNode node,Optional<SchemaContext> ctx) {
110 val rpcPayload = wrap(NETCONF_RPC_QNAME, flattenInput(node));
111 val w3cPayload = NodeUtils.buildShadowDomTree(rpcPayload);
112 w3cPayload.documentElement.setAttribute("message-id", "m-" + messageId.andIncrement);
113 return new NetconfMessage(w3cPayload);
116 def static flattenInput(CompositeNode node) {
117 val inputQName = QName.create(node.nodeType,"input");
118 val input = node.getFirstCompositeByName(inputQName);
119 if(input == null) return node;
120 if(input instanceof CompositeNode) {
122 val nodes = ImmutableList.builder() //
123 .addAll(input.children) //
124 .addAll(node.children.filter[nodeType != inputQName]) //
126 return ImmutableCompositeNode.create(node.nodeType,nodes);
131 static def RpcResult<CompositeNode> toRpcResult(NetconfMessage message,QName rpc,Optional<SchemaContext> context) {
132 var CompositeNode rawRpc;
133 if(context.present) {
134 if(isDataRetrievalReply(rpc)) {
136 val xmlData = message.document.dataSubtree
137 val dataNodes = XmlDocumentUtils.toDomNodes(xmlData,Optional.of(context.get.dataDefinitions))
139 val it = ImmutableCompositeNode.builder()
140 setQName(NETCONF_RPC_REPLY_QNAME)
141 add(ImmutableCompositeNode.create(NETCONF_DATA_QNAME,dataNodes));
143 rawRpc = it.toInstance;
146 val rpcSchema = context.get.operations.findFirst[QName == rpc]
147 rawRpc = message.document.toCompositeNode() as CompositeNode;
153 rawRpc = message.document.toCompositeNode() as CompositeNode;
156 return Rpcs.getRpcResult(true, rawRpc, Collections.emptySet());
159 def static Element getDataSubtree(Document doc) {
160 doc.getElementsByTagNameNS(NETCONF_URI.toString,"data").item(0) as Element
163 def static boolean isDataRetrievalReply(QName it) {
164 return NETCONF_URI == namespace && ( localName == NETCONF_GET_CONFIG_QNAME.localName || localName == NETCONF_GET_QNAME.localName)
167 static def wrap(QName name, Node<?> node) {
169 return new CompositeNodeTOImpl(name, null, Collections.singletonList(node));
171 return new CompositeNodeTOImpl(name, null, Collections.emptyList());
175 static def wrap(QName name, Node<?> additional, Node<?> node) {
177 return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional, node));
179 return new CompositeNodeTOImpl(name, null, ImmutableList.of(additional));
183 static def filter(String type, Node<?> node) {
184 val it = ImmutableCompositeNode.builder(); //
185 setQName(NETCONF_FILTER_QNAME);
186 setAttribute(NETCONF_TYPE_QNAME,type);
188 return add(node).toInstance();
194 public static def Node<?> toCompositeNode(Document document) {
195 return XmlDocumentUtils.toDomNode(document) as Node<?>
198 public static def checkValidReply(NetconfMessage input, NetconfMessage output) {
199 val inputMsgId = input.document.documentElement.getAttribute("message-id")
200 val outputMsgId = output.document.documentElement.getAttribute("message-id")
201 Preconditions.checkState(inputMsgId == outputMsgId,"Rpc request and reply message IDs must be same.");