BUG-6647 Increase code coverage and clean up II
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / SimpleSessionListener.java
index 732a5b7dbfa80cf6dc7f8b8804b0e32f5a222603..6e8f867adfe2978f522b1f065cb5bab19f6d19f8 100644 (file)
@@ -8,10 +8,15 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Uninterruptibles;
 import java.util.List;
-import org.opendaylight.protocol.bgp.parser.BGPSession;
-import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
-import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import org.junit.Assert;
+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.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -19,36 +24,35 @@ import org.slf4j.LoggerFactory;
 /**
  * Listener for the client.
  */
-public class SimpleSessionListener implements ReusableBGPPeer {
-
-    private final List<Notification> listMsg = Lists.newArrayList();
-
-    public boolean up = false;
+public final class SimpleSessionListener implements BGPSessionListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
+    private final List<Notification> listMsg = Lists.newArrayList();
+    private BGPSession session;
+    private final CountDownLatch sessionLatch = new CountDownLatch(1);
 
-    public boolean down = false;
+    SimpleSessionListener() {
+    }
 
-    public List<Notification> getListMsg() {
+    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.up = true;
+        LOG.info("Session Up");
+        this.session = session;
+        this.sessionLatch.countDown();
     }
 
     @Override
     public void onSessionDown(final BGPSession session, final Exception e) {
         LOG.debug("Session Down", e);
-        this.down = true;
     }
 
     @Override
@@ -56,8 +60,30 @@ public class SimpleSessionListener implements ReusableBGPPeer {
         LOG.debug("Session terminated. Cause : {}", cause.toString());
     }
 
+    @Override
+    public void onMessage(final BGPSession session, final Notification message) {
+        this.listMsg.add(message);
+        LOG.debug("Message received: {}", message);
+    }
+
     @Override
     public void releaseConnection() {
         LOG.debug("Releasing connection");
+        if (this.session != null) {
+            try {
+                this.session.close();
+            } catch (final Exception e) {
+                LOG.warn("Error closing session", e);
+            }
+        }
+    }
+
+    BGPSessionImpl.State getState() {
+        return getSession().getState();
+    }
+
+    BGPSessionImpl getSession() {
+        Assert.assertEquals("Session up", true, Uninterruptibles.awaitUninterruptibly(this.sessionLatch, 10, TimeUnit.SECONDS));
+        return (BGPSessionImpl) this.session;
     }
 }