Fix checkstyle complains
[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 static org.junit.Assert.assertTrue;
11
12 import com.google.common.collect.Lists;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.Uninterruptibles;
16 import java.util.List;
17 import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.TimeUnit;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
21 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
22 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
23 import org.opendaylight.protocol.bgp.rib.spi.State;
24 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
26 import org.opendaylight.yangtools.yang.binding.Notification;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Listener for the client.
32  */
33 public final class SimpleSessionListener implements BGPSessionListener, ListenerCheck {
34
35     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
36     @GuardedBy("this")
37     private final List<Notification> listMsg = Lists.newArrayList();
38     private BGPSession bgpSession;
39     private final CountDownLatch sessionLatch = new CountDownLatch(1);
40
41     public SimpleSessionListener() {
42     }
43
44     synchronized List<Notification> getListMsg() {
45         return this.listMsg;
46     }
47
48     @Override
49     public void markUptodate(final TablesKey tablesKey) {
50         LOG.debug("Table marked as up-to-date {}", tablesKey);
51     }
52
53     @Override
54     public void onSessionUp(final BGPSession session) {
55         LOG.info("Session Up");
56         this.bgpSession = session;
57         this.sessionLatch.countDown();
58     }
59
60     @Override
61     public void onSessionDown(final BGPSession session, final Exception exception) {
62         LOG.debug("Session Down", exception);
63     }
64
65     @Override
66     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
67         LOG.debug("Session terminated. Cause : {}", cause.toString());
68     }
69
70     @Override
71     public synchronized void onMessage(final BGPSession session, final Notification message) {
72         this.listMsg.add(message);
73         LOG.debug("Message received: {}", message);
74     }
75
76     @Override
77     @SuppressWarnings("checkstyle:IllegalCatch")
78     public ListenableFuture<Void> releaseConnection() {
79         LOG.debug("Releasing connection");
80         if (this.bgpSession != null) {
81             try {
82                 this.bgpSession.close();
83             } catch (final Exception e) {
84                 LOG.warn("Error closing session", e);
85             }
86         }
87         return Futures.immediateFuture(null);
88     }
89
90     public State getState() {
91         return getSession().getState();
92     }
93
94     BGPSessionImpl getSession() {
95         assertTrue("Session up",
96                 Uninterruptibles.awaitUninterruptibly(this.sessionLatch, 10, TimeUnit.SECONDS));
97         return (BGPSessionImpl) this.bgpSession;
98     }
99
100     @Override
101     public int getListMessageSize() {
102         return this.listMsg.size();
103     }
104 }