2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static org.junit.Assert.assertTrue;
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;
31 * Listener for the client.
33 public final class SimpleSessionListener implements BGPSessionListener, ListenerCheck {
35 private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
37 private final List<Notification> listMsg = Lists.newArrayList();
38 private BGPSession bgpSession;
39 private final CountDownLatch sessionLatch = new CountDownLatch(1);
41 public SimpleSessionListener() {
44 synchronized List<Notification> getListMsg() {
49 public void markUptodate(final TablesKey tablesKey) {
50 LOG.debug("Table marked as up-to-date {}", tablesKey);
54 public void onSessionUp(final BGPSession session) {
55 LOG.info("Session Up");
56 this.bgpSession = session;
57 this.sessionLatch.countDown();
61 public void onSessionDown(final BGPSession session, final Exception exception) {
62 LOG.debug("Session Down", exception);
66 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
67 LOG.debug("Session terminated. Cause : {}", cause.toString());
71 public synchronized void onMessage(final BGPSession session, final Notification message) {
72 this.listMsg.add(message);
73 LOG.debug("Message received: {}", message);
77 @SuppressWarnings("checkstyle:IllegalCatch")
78 public ListenableFuture<Void> releaseConnection() {
79 LOG.debug("Releasing connection");
80 if (this.bgpSession != null) {
82 this.bgpSession.close();
83 } catch (final Exception e) {
84 LOG.warn("Error closing session", e);
87 return Futures.immediateFuture(null);
90 public State getState() {
91 return getSession().getState();
94 BGPSessionImpl getSession() {
95 assertTrue("Session up",
96 Uninterruptibles.awaitUninterruptibly(this.sessionLatch, 10, TimeUnit.SECONDS));
97 return (BGPSessionImpl) this.bgpSession;
101 public int getListMessageSize() {
102 return this.listMsg.size();