Bump versions to 0.21.8-SNAPSHOT
[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.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.Uninterruptibles;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.TimeUnit;
19 import org.checkerframework.checker.lock.qual.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     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
35
36     @GuardedBy("this")
37     private final List<Notification<?>> listMsg = new ArrayList<>();
38     private final CountDownLatch sessionLatch = new CountDownLatch(1);
39
40     private BGPSession bgpSession;
41
42     @Override
43     public void markUptodate(final TablesKey tablesKey) {
44         LOG.debug("Table marked as up-to-date {}", tablesKey);
45     }
46
47     @Override
48     public void onSessionUp(final BGPSession session) {
49         LOG.info("Session Up");
50         bgpSession = session;
51         sessionLatch.countDown();
52     }
53
54     @Override
55     public void onSessionDown(final BGPSession session, final Exception exception) {
56         LOG.debug("Session Down", exception);
57     }
58
59     @Override
60     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
61         LOG.debug("Session terminated. Cause : {}", cause.toString());
62     }
63
64     @Override
65     public synchronized void onMessage(final BGPSession session, final Notification<?> message) {
66         listMsg.add(message);
67         LOG.debug("Message received: {}", message);
68     }
69
70     @Override
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     public ListenableFuture<Void> releaseConnection() {
73         LOG.debug("Releasing connection");
74         if (bgpSession != null) {
75             try {
76                 bgpSession.close();
77             } catch (final Exception e) {
78                 LOG.warn("Error closing session", e);
79             }
80         }
81         return Futures.immediateFuture(null);
82     }
83
84     @Override
85     public ListenableFuture<?> restartGracefully(final long selectionDeferralTimerSeconds) {
86         return Futures.immediateFailedFuture(
87                 new UnsupportedOperationException("SimpleSessionListener doesn't support graceful restart"));
88     }
89
90     public State getState() {
91         return getSession().getState();
92     }
93
94     BGPSessionImpl getSession() {
95         assertTrue("Session up",
96                 Uninterruptibles.awaitUninterruptibly(sessionLatch, 10, TimeUnit.SECONDS));
97         return (BGPSessionImpl) bgpSession;
98     }
99
100     @Override
101     public synchronized List<Notification<?>> getListMsg() {
102         return listMsg;
103     }
104
105     @Override
106     public int getListMessageSize() {
107         return listMsg.size();
108     }
109 }