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