Merge "Introduce checked sendMessage() method"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / mapping / AbstractNetconfOperation.java
index 5850e64a0591a4d58c320eb67330e1dc0aa55cae..ba6364662cc5aae73bd0701e5084d263221c4575 100644 (file)
@@ -20,6 +20,7 @@ import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 public abstract class AbstractNetconfOperation implements NetconfOperation {
     private final String netconfSessionIdForReporting;
@@ -28,7 +29,7 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
     }
 
-    public String getNetconfSessionIdForReporting() {
+    public final String getNetconfSessionIdForReporting() {
         return netconfSessionIdForReporting;
     }
 
@@ -66,7 +67,7 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
     protected abstract HandlingPriority canHandle(String operationName, String netconfOperationNamespace);
 
     @Override
-    public Document handle(Document message, NetconfOperationRouter opRouter) throws NetconfDocumentedException {
+    public final Document handle(Document message, NetconfOperationRouter opRouter) throws NetconfDocumentedException {
 
         XmlElement requestElement = getRequestElementWithCheck(message);
 
@@ -76,15 +77,23 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         Map<String, Attr> attributes = requestElement.getAttributes();
 
         Element response = handle(document, operationElement, opRouter);
-
         Element rpcReply = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
                 XmlNetconfConstants.RPC_REPLY_KEY);
-        rpcReply.appendChild(response);
 
-        for (String attrName : attributes.keySet()) {
-            rpcReply.setAttribute(attrName, attributes.get(attrName).getNodeValue());
+        if(XmlElement.fromDomElement(response).hasNamespace()) {
+            rpcReply.appendChild(response);
+        } else {
+            Element responseNS = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, response.getNodeName());
+            NodeList list = response.getChildNodes();
+            while(list.getLength()!=0) {
+                responseNS.appendChild(list.item(0));
+            }
+            rpcReply.appendChild(responseNS);
         }
 
+        for (String attrName : attributes.keySet()) {
+            rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
+        }
         document.appendChild(rpcReply);
         return document;
     }