Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / mapping / AbstractNetconfOperation.java
index 6b12a1999270608264053c7edbf9ab1cc5bc6b55..707274ed4a1522235c2004c5adca7c4bf9d4e731 100644 (file)
@@ -8,24 +8,25 @@
 
 package org.opendaylight.controller.netconf.util.mapping;
 
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import com.google.common.base.Optional;
+import java.util.Map;
+import org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
-import org.opendaylight.controller.netconf.util.xml.XmlElement;
-import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
-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;
 
-import java.util.Map;
-
 public abstract class AbstractNetconfOperation implements NetconfOperation {
     private final String netconfSessionIdForReporting;
 
-    protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
+    protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
     }
 
@@ -34,18 +35,20 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
     }
 
     @Override
-    public HandlingPriority canHandle(Document message) {
-        OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
+    public HandlingPriority canHandle(final Document message) throws DocumentedException {
+        OperationNameAndNamespace operationNameAndNamespace = null;
+        operationNameAndNamespace = new OperationNameAndNamespace(message);
         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
     }
 
-    public static class OperationNameAndNamespace {
+    public static final class OperationNameAndNamespace {
         private final String operationName, namespace;
+        private final XmlElement operationElement;
 
-        public OperationNameAndNamespace(Document message) {
-            XmlElement requestElement = getRequestElementWithCheck(message);
-
-            XmlElement operationElement = requestElement.getOnlyChildElement();
+        public OperationNameAndNamespace(final Document message) throws DocumentedException {
+            XmlElement requestElement = null;
+            requestElement = getRequestElementWithCheck(message);
+            operationElement = requestElement.getOnlyChildElement();
             operationName = operationElement.getName();
             namespace = operationElement.getNamespace();
         }
@@ -57,14 +60,18 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         public String getNamespace() {
             return namespace;
         }
+
+        public XmlElement getOperationElement() {
+            return operationElement;
+        }
     }
 
-    protected static XmlElement getRequestElementWithCheck(Document message) {
+    protected static XmlElement getRequestElementWithCheck(final Document message) throws DocumentedException {
         return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
     }
 
-    protected HandlingPriority canHandle(String operationName, String operationNamespace) {
+    protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
         return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
                 ? getHandlingPriority()
                 : HandlingPriority.CANNOT_HANDLE;
@@ -81,8 +88,8 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
     protected abstract String getOperationName();
 
     @Override
-    public Document handle(Document requestMessage,
-            NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
+    public Document handle(final Document requestMessage,
+            final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
 
         XmlElement requestElement = getRequestElementWithCheck(requestMessage);
 
@@ -92,13 +99,12 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         Map<String, Attr> attributes = requestElement.getAttributes();
 
         Element response = handle(document, operationElement, subsequentOperation);
-        Element rpcReply = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
-                XmlNetconfConstants.RPC_REPLY_KEY);
+        Element rpcReply = XmlUtil.createElement(document, XmlMappingConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
 
         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());
+            Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
             NodeList list = response.getChildNodes();
             while(list.getLength()!=0) {
                 responseNS.appendChild(list.item(0));
@@ -106,15 +112,15 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
             rpcReply.appendChild(responseNS);
         }
 
-        for (String attrName : attributes.keySet()) {
-            rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
+        for (Attr attribute : attributes.values()) {
+            rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
         }
         document.appendChild(rpcReply);
         return document;
     }
 
     protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
-            throws NetconfDocumentedException;
+            throws DocumentedException;
 
     @Override
     public String toString() {