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