BUG-7222: Make BGP DS clean up asynchronous
[bgpcep.git] / bgp / testtool / src / main / java / org / opendaylight / protocol / bgp / testtool / TestingListener.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.testtool;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.List;
13 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
14 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
15 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
16 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Testing BGP Listener.
25  */
26 final class TestingListener implements BGPSessionListener {
27     private static final Logger LOG = LoggerFactory.getLogger(TestingListener.class);
28     private final int nPrefixes;
29     private final List<String> extCom;
30     private final boolean multiPathSupport;
31     private int messageCounter = 0;
32
33     TestingListener(final int nPrefixes, final List<String> extCom, final boolean multiPathSupport) {
34         this.nPrefixes = nPrefixes;
35         this.extCom = extCom;
36         this.multiPathSupport = multiPathSupport;
37     }
38
39     @Override
40     public void markUptodate(final TablesKey tablesKey) {
41         LOG.debug("Table marked as up-to-date {}", tablesKey);
42     }
43
44     @Override
45     public void onSessionUp(final BGPSession session) {
46         LOG.info("Client Listener: Session Up.");
47         if (this.nPrefixes > 0) {
48             PrefixesBuilder.advertiseIpv4Prefixes(((BGPSessionImpl) session).getLimiter(), this.nPrefixes, this.extCom, this.multiPathSupport);
49         }
50     }
51
52     @Override
53     public void onSessionDown(final BGPSession session, final Exception e) {
54         LOG.info("Client Listener: Connection lost.");
55         try {
56             session.close();
57         } catch (Exception ie) {
58             LOG.warn("Error closing session", ie);
59         }
60     }
61
62     @Override
63     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
64         LOG.info("Client Listener: Connection lost: {}.", cause);
65     }
66
67     @Override
68     public void onMessage(final BGPSession session, final Notification message) {
69         if (message instanceof Update) {
70             messageCounter++;
71         }
72         LOG.debug("Message received: {}", message.toString());
73     }
74
75     @Override
76     public ListenableFuture<?> releaseConnection() {
77         LOG.info("Client Listener: Connection released.");
78         return Futures.immediateFuture(null);
79     }
80
81     void printCount(final String localAddress) {
82         LOG.info("Peer {} received {} update messages.", localAddress, messageCounter);
83     }
84 }