BUG-6038: Fix race condition when Open message...
[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 java.util.List;
12 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
13 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
14 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
16 import org.opendaylight.yangtools.yang.binding.Notification;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Listener for the client.
22  */
23 public final class SimpleSessionListener implements BGPSessionListener {
24
25     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
26     private final List<Notification> listMsg = Lists.newArrayList();
27     private BGPSession session;
28
29     SimpleSessionListener() {
30     }
31
32     List<Notification> getListMsg() {
33         return this.listMsg;
34     }
35
36     @Override
37     public boolean isSessionActive() {
38         return ((BGPSessionImpl) this.session).isWritable();
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.debug("Session Up");
49         this.session = session;
50     }
51
52     @Override
53     public void onSessionDown(final BGPSession session, final Exception e) {
54         LOG.debug("Session Down", e);
55     }
56
57     @Override
58     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
59         LOG.debug("Session terminated. Cause : {}", cause.toString());
60     }
61
62     @Override
63     public void onMessage(final BGPSession session, final Notification message) {
64         this.listMsg.add(message);
65         LOG.debug("Message received: {}", message);
66     }
67
68     @Override
69     public void releaseConnection() {
70         LOG.debug("Releasing connection");
71         if (this.session != null) {
72             try {
73                 this.session.close();
74             } catch (final Exception e) {
75                 LOG.warn("Error closing session", e);
76             }
77         }
78     }
79
80     BGPSessionImpl.State getState() {
81         return ((BGPSessionImpl) this.session).getState();
82     }
83 }