BUG-9079 Make PCEP session recoverable from exception
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPSessionImpl.java
index 22ff18e53e4c088556ee7bf8c20ceb8e828bc3a3..e85362b386b30dfa3e83f3740fd7ac43dbb4bd94 100644 (file)
@@ -175,6 +175,16 @@ public class PCEPSessionImpl extends SimpleChannelInboundHandler<Message> implem
         }
     }
 
+    /**
+     * Handle exception occurred in the PCEP session. The session in error state should be closed
+     * properly so that it can be restored later.
+     */
+    @VisibleForTesting
+    void handleException(final Throwable cause) {
+        LOG.error("Exception captured for session {}, closing session.", this, cause);
+        terminate(TerminationReason.UNKNOWN);
+    }
+
     /**
      * Sends message to serialization.
      *
@@ -360,12 +370,17 @@ public class PCEPSessionImpl extends SimpleChannelInboundHandler<Message> implem
     }
 
     @VisibleForTesting
-    public void sessionUp() {
-        this.listener.onSessionUp(this);
+    void sessionUp() {
+        try {
+            this.listener.onSessionUp(this);
+        } catch (final Exception e) {
+            handleException(e);
+            throw e;
+        }
     }
 
     @VisibleForTesting
-    protected final Queue<Long> getUnknownMessagesTimes() {
+    final Queue<Long> getUnknownMessagesTimes() {
         return this.unknownMessagesTimes;
     }
 
@@ -417,6 +432,11 @@ public class PCEPSessionImpl extends SimpleChannelInboundHandler<Message> implem
         this.sessionUp();
     }
 
+    @Override
+    public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
+        handleException(cause);
+    }
+
     @Override
     public Tlvs localSessionCharacteristics() {
         return this.localOpen.getTlvs();