BUG-472 Initial EXI encoder/decoder implementation in Netconf
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / mapping / AbstractNetconfOperation.java
index 86081b7f81d0f342669fdb1c7124b74dbff3157b..b7bbd3c6a70199cac0915d412568aae7d796d594 100644 (file)
@@ -8,10 +8,12 @@
 
 package org.opendaylight.controller.netconf.util.mapping;
 
+import java.util.Map;
+
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
 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;
@@ -20,7 +22,7 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 
-import java.util.Map;
+import com.google.common.base.Optional;
 
 public abstract class AbstractNetconfOperation implements NetconfOperation {
     private final String netconfSessionIdForReporting;
@@ -29,7 +31,7 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
     }
 
-    public String getNetconfSessionIdForReporting() {
+    public final String getNetconfSessionIdForReporting() {
         return netconfSessionIdForReporting;
     }
 
@@ -39,7 +41,7 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
     }
 
-    public static class OperationNameAndNamespace {
+    public static final class OperationNameAndNamespace {
         private final String operationName, namespace;
 
         public OperationNameAndNamespace(Document message) {
@@ -64,26 +66,40 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
     }
 
-    protected abstract HandlingPriority canHandle(String operationName, String netconfOperationNamespace);
+    protected HandlingPriority canHandle(String operationName, String operationNamespace) {
+        return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
+                ? getHandlingPriority()
+                : HandlingPriority.CANNOT_HANDLE;
+    }
+
+    protected HandlingPriority getHandlingPriority() {
+        return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
+    }
+
+    protected String getOperationNamespace() {
+        return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
+    }
+
+    protected abstract String getOperationName();
 
     @Override
-    public Document handle(Document message, NetconfOperationRouter opRouter) throws NetconfDocumentedException {
+    public Document handle(Document requestMessage,
+            NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
 
-        XmlElement requestElement = getRequestElementWithCheck(message);
+        XmlElement requestElement = getRequestElementWithCheck(requestMessage);
 
         Document document = XmlUtil.newDocument();
 
         XmlElement operationElement = requestElement.getOnlyChildElement();
         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);
+        Element response = handle(document, operationElement, subsequentOperation);
+        Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.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));
@@ -98,11 +114,20 @@ public abstract class AbstractNetconfOperation implements NetconfOperation {
         return document;
     }
 
-    protected abstract Element handle(Document document, XmlElement operationElement, NetconfOperationRouter opRouter)
+    protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
             throws NetconfDocumentedException;
 
     @Override
     public String toString() {
-        return getClass() + "{" + netconfSessionIdForReporting + '}';
+        final StringBuffer sb = new StringBuffer(getClass().getName());
+        try {
+            sb.append("{name=").append(getOperationName());
+        } catch(UnsupportedOperationException e) {
+            // no problem
+        }
+        sb.append(", namespace=").append(getOperationNamespace());
+        sb.append(", session=").append(netconfSessionIdForReporting);
+        sb.append('}');
+        return sb.toString();
     }
 }