Add LLGR restart utilities
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / SimpleSessionListener.java
index c7e3d3aaca2c454a82bb39d4622b212244dd4020..8826ddfdbadb57b4269464753fd21aca32da0221 100644 (file)
@@ -7,12 +7,22 @@
  */
 package org.opendaylight.protocol.bgp.rib.impl;
 
+import static org.junit.Assert.assertTrue;
+
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
+import org.opendaylight.protocol.bgp.rib.spi.State;
+import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -20,42 +30,36 @@ import org.slf4j.LoggerFactory;
 /**
  * Listener for the client.
  */
-public class SimpleSessionListener implements BGPSessionListener {
-
-    private final List<Notification> listMsg = Lists.newArrayList();
-
-    public boolean up = false;
+public final class SimpleSessionListener implements BGPSessionListener, ListenerCheck {
 
     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
-
-    public boolean down = false;
-
-    private BGPSession session;
+    @GuardedBy("this")
+    private final List<Notification> listMsg = Lists.newArrayList();
+    private BGPSession bgpSession;
+    private final CountDownLatch sessionLatch = new CountDownLatch(1);
 
     public SimpleSessionListener() {
     }
 
-    public List<Notification> getListMsg() {
+    synchronized List<Notification> getListMsg() {
         return this.listMsg;
     }
 
     @Override
-    public void onMessage(final BGPSession session, final Notification message) {
-        this.listMsg.add(message);
-        LOG.debug("Message received: {}", message);
+    public void markUptodate(final TablesKey tablesKey) {
+        LOG.debug("Table marked as up-to-date {}", tablesKey);
     }
 
     @Override
     public void onSessionUp(final BGPSession session) {
-        LOG.debug("Session Up");
-        this.session = session;
-        this.up = true;
+        LOG.info("Session Up");
+        this.bgpSession = session;
+        this.sessionLatch.countDown();
     }
 
     @Override
-    public void onSessionDown(final BGPSession session, final Exception e) {
-        LOG.debug("Session Down", e);
-        this.down = true;
+    public void onSessionDown(final BGPSession session, final Exception exception) {
+        LOG.debug("Session Down", exception);
     }
 
     @Override
@@ -64,25 +68,43 @@ public class SimpleSessionListener implements BGPSessionListener {
     }
 
     @Override
-    public void releaseConnection() {
+    public synchronized void onMessage(final BGPSession session, final Notification message) {
+        this.listMsg.add(message);
+        LOG.debug("Message received: {}", message);
+    }
+
+    @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public ListenableFuture<Void> releaseConnection() {
         LOG.debug("Releasing connection");
-        if (this.session != null) {
+        if (this.bgpSession != null) {
             try {
-                this.session.close();
+                this.bgpSession.close();
             } catch (final Exception e) {
                 LOG.warn("Error closing session", e);
             }
-            this.session = null;
         }
+        return Futures.immediateFuture(null);
     }
 
     @Override
-    public boolean isSessionActive() {
-        return true;
+    public ListenableFuture<?> restartGracefully(final long selectionDeferralTimerSeconds) {
+        return Futures.immediateFailedFuture(
+                new UnsupportedOperationException("SimpleSessionListener doesn't support graceful restart"));
+    }
+
+    public State getState() {
+        return getSession().getState();
+    }
+
+    BGPSessionImpl getSession() {
+        assertTrue("Session up",
+                Uninterruptibles.awaitUninterruptibly(this.sessionLatch, 10, TimeUnit.SECONDS));
+        return (BGPSessionImpl) this.bgpSession;
     }
 
     @Override
-    public void markUptodate(final TablesKey tablesKey) {
-        LOG.debug("Table marked as up-to-date {}", tablesKey);
+    public int getListMessageSize() {
+        return this.listMsg.size();
     }
 }