Remove isCloseMsg check for each rpc
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSession.java
index 0cf2dbc281e1503e0ff1dd8d362285919c527c9a..4368f7ec7940152ef038a1b4320dbc3c858732cf 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.controller.netconf.impl;
 
 import com.google.common.base.Preconditions;
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
 import io.netty.handler.codec.ByteToMessageDecoder;
 import io.netty.handler.codec.MessageToByteEncoder;
 import java.text.SimpleDateFormat;
@@ -45,6 +47,7 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
 
     private Date loginTime;
     private long inRpcSuccess, inRpcFail, outRpcError;
+    private volatile boolean delayedClose;
 
     public NetconfServerSession(final NetconfServerSessionListener sessionListener, final Channel channel, final long sessionId,
             final NetconfHelloMessageAdditionalHeader header) {
@@ -60,6 +63,29 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
         super.sessionUp();
     }
 
+    /**
+     * Close this session after next message is sent.
+     * Suitable for close rpc that needs to send ok response before the session is closed.
+     */
+    public void delayedClose() {
+        this.delayedClose = true;
+    }
+
+    @Override
+    public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
+        final ChannelFuture channelFuture = super.sendMessage(netconfMessage);
+        // delayed close was set, close after the message was sent
+        if(delayedClose) {
+            channelFuture.addListener(new ChannelFutureListener() {
+                @Override
+                public void operationComplete(final ChannelFuture future) throws Exception {
+                    close();
+                }
+            });
+        }
+        return channelFuture;
+    }
+
     public void onIncommingRpcSuccess() {
         inRpcSuccess++;
     }