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.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;
31 * Listener for the client.
33 public final class SimpleSessionListener implements BGPSessionListener, ListenerCheck {
34 private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
37 private final List<Notification<?>> listMsg = new ArrayList<>();
38 private final CountDownLatch sessionLatch = new CountDownLatch(1);
40 private BGPSession bgpSession;
43 public void markUptodate(final TablesKey tablesKey) {
44 LOG.debug("Table marked as up-to-date {}", tablesKey);
48 public void onSessionUp(final BGPSession session) {
49 LOG.info("Session Up");
51 sessionLatch.countDown();
55 public void onSessionDown(final BGPSession session, final Exception exception) {
56 LOG.debug("Session Down", exception);
60 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
61 LOG.debug("Session terminated. Cause : {}", cause.toString());
65 public synchronized void onMessage(final BGPSession session, final Notification<?> message) {
67 LOG.debug("Message received: {}", message);
71 @SuppressWarnings("checkstyle:IllegalCatch")
72 public ListenableFuture<Void> releaseConnection() {
73 LOG.debug("Releasing connection");
74 if (bgpSession != null) {
77 } catch (final Exception e) {
78 LOG.warn("Error closing session", e);
81 return Futures.immediateFuture(null);
85 public ListenableFuture<?> restartGracefully(final long selectionDeferralTimerSeconds) {
86 return Futures.immediateFailedFuture(
87 new UnsupportedOperationException("SimpleSessionListener doesn't support graceful restart"));
90 public State getState() {
91 return getSession().getState();
94 BGPSessionImpl getSession() {
95 assertTrue("Session up",
96 Uninterruptibles.awaitUninterruptibly(sessionLatch, 10, TimeUnit.SECONDS));
97 return (BGPSessionImpl) bgpSession;
101 public synchronized List<Notification<?>> getListMsg() {
106 public int getListMessageSize() {
107 return listMsg.size();