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