Merge "Bug 2358: Fixed warnings in Restconf"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultCloseSession.java
index b7a98bae83acae9a1ee0f525a015db251c998f3e..303352c9c557f111cabde8866e4376698cb96190 100644 (file)
@@ -8,21 +8,27 @@
 
 package org.opendaylight.controller.netconf.impl.mapping.operations;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import java.util.Collections;
 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.controller.netconf.impl.NetconfServerSession;
 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
-import com.google.common.base.Optional;
-
-import java.util.Collections;
+public class DefaultCloseSession extends AbstractSingletonNetconfOperation implements DefaultNetconfOperation {
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultCloseSession.class);
 
-public class DefaultCloseSession extends AbstractSingletonNetconfOperation {
     public static final String CLOSE_SESSION = "close-session";
+
     private final AutoCloseable sessionResources;
+    private NetconfServerSession session;
 
     public DefaultCloseSession(String netconfSessionIdForReporting, AutoCloseable sessionResources) {
         super(netconfSessionIdForReporting);
@@ -44,13 +50,20 @@ public class DefaultCloseSession extends AbstractSingletonNetconfOperation {
             throws NetconfDocumentedException {
         try {
             sessionResources.close();
+            Preconditions.checkNotNull(session, "Session was not set").delayedClose();
+            LOG.info("Session {} closing", session.getSessionId());
         } 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()));
+                        NetconfDocumentedException.ErrorSeverity.error.toString(), e.getMessage()));
         }
         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
     }
+
+    @Override
+    public void setNetconfSession(final NetconfServerSession s) {
+        this.session = s;
+    }
 }