Bug 977: Return RpcError result on neconf failure
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultCloseSession.java
index cc503f60c3938947b8acb29882a9cd161bafda5c..b7a98bae83acae9a1ee0f525a015db251c998f3e 100644 (file)
@@ -8,33 +8,30 @@
 
 package org.opendaylight.controller.netconf.impl.mapping.operations;
 
-import org.opendaylight.controller.netconf.api.NetconfSession;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
-import org.opendaylight.controller.netconf.mapping.api.DefaultNetconfOperation;
-import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
-import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
+import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
 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.Document;
 import org.w3c.dom.Element;
 
-public class DefaultCloseSession extends AbstractNetconfOperation implements DefaultNetconfOperation {
+import com.google.common.base.Optional;
+
+import java.util.Collections;
+
+public class DefaultCloseSession extends AbstractSingletonNetconfOperation {
     public static final String CLOSE_SESSION = "close-session";
-    private NetconfSession netconfSession;
+    private final AutoCloseable sessionResources;
 
-    public DefaultCloseSession(String netconfSessionIdForReporting) {
+    public DefaultCloseSession(String netconfSessionIdForReporting, AutoCloseable sessionResources) {
         super(netconfSessionIdForReporting);
+        this.sessionResources = sessionResources;
     }
 
     @Override
-    protected HandlingPriority canHandle(String operationName, String netconfOperationNamespace) {
-        if (operationName.equals(CLOSE_SESSION) == false)
-            return HandlingPriority.CANNOT_HANDLE;
-        if (netconfOperationNamespace.equals(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0) == false)
-            return HandlingPriority.CANNOT_HANDLE;
-
-        return HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
+    protected String getOperationName() {
+        return CLOSE_SESSION;
     }
 
     /**
@@ -43,18 +40,17 @@ public class DefaultCloseSession extends AbstractNetconfOperation implements Def
      * instances
      */
     @Override
-    protected Element handle(Document document, XmlElement operationElement, NetconfOperationRouter opRouter)
+    protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement)
             throws NetconfDocumentedException {
-        opRouter.close();
-        return document.createElement(XmlNetconfConstants.OK);
-    }
-
-    @Override
-    public void setNetconfSession(NetconfSession s) {
-        this.netconfSession = s;
-    }
-
-    public NetconfSession getNetconfSession() {
-        return netconfSession;
+        try {
+            sessionResources.close();
+        } catch (Exception e) {
+            throw new NetconfDocumentedException("Unable to properly close session "
+                    + getNetconfSessionIdForReporting(), NetconfDocumentedException.ErrorType.application,
+                    NetconfDocumentedException.ErrorTag.operation_failed,
+                    NetconfDocumentedException.ErrorSeverity.error, Collections.singletonMap(
+                    NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
+        }
+        return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
     }
 }