Code clean up
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / SimpleSessionListener.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.Uninterruptibles;
14 import java.util.List;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.junit.Assert;
19 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
20 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
22 import org.opendaylight.protocol.bgp.rib.spi.State;
23 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Listener for the client.
31  */
32 public final class SimpleSessionListener implements BGPSessionListener, ListenerCheck {
33
34     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
35     @GuardedBy("this")
36     private final List<Notification> listMsg = Lists.newArrayList();
37     private BGPSession session;
38     private final CountDownLatch sessionLatch = new CountDownLatch(1);
39
40     public SimpleSessionListener() {
41     }
42
43     synchronized List<Notification> getListMsg() {
44         return this.listMsg;
45     }
46
47     @Override
48     public void markUptodate(final TablesKey tablesKey) {
49         LOG.debug("Table marked as up-to-date {}", tablesKey);
50     }
51
52     @Override
53     public void onSessionUp(final BGPSession session) {
54         LOG.info("Session Up");
55         this.session = session;
56         this.sessionLatch.countDown();
57     }
58
59     @Override
60     public void onSessionDown(final BGPSession session, final Exception e) {
61         LOG.debug("Session Down", e);
62     }
63
64     @Override
65     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
66         LOG.debug("Session terminated. Cause : {}", cause.toString());
67     }
68
69     @Override
70     public synchronized void onMessage(final BGPSession session, final Notification message) {
71         this.listMsg.add(message);
72         LOG.debug("Message received: {}", message);
73     }
74
75     @Override
76     public ListenableFuture<Void> releaseConnection() {
77         LOG.debug("Releasing connection");
78         if (this.session != null) {
79             try {
80                 this.session.close();
81             } catch (final Exception e) {
82                 LOG.warn("Error closing session", e);
83             }
84         }
85         return Futures.immediateFuture(null);
86     }
87
88     public State getState() {
89         return getSession().getState();
90     }
91
92     BGPSessionImpl getSession() {
93         Assert.assertTrue("Session up", Uninterruptibles.awaitUninterruptibly(this.sessionLatch, 10, TimeUnit.SECONDS));
94         return (BGPSessionImpl) this.session;
95     }
96
97     @Override
98     public int getListMessageSize() {
99         return this.listMsg.size();
100     }
101 }