Migrate deprecated submit() to commit() for BGP/BMP
[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.FluentFuture;
11 import java.util.List;
12 import java.util.concurrent.atomic.LongAdder;
13 import org.opendaylight.mdsal.common.api.CommitInfo;
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.rev180329.Update;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.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 final 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,
50                     this.multiPathSupport);
51         }
52     }
53
54     @Override
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     public void onSessionDown(final BGPSession session, final Exception exc) {
57         LOG.info("Client Listener: Connection lost.");
58         try {
59             session.close();
60         } catch (Exception ie) {
61             LOG.warn("Error closing session", ie);
62         }
63     }
64
65     @Override
66     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
67         LOG.info("Client Listener: Connection lost: {}.", cause);
68     }
69
70     @Override
71     public void onMessage(final BGPSession session, final Notification message) {
72         if (message instanceof Update) {
73             this.messageCounter.increment();
74         }
75         LOG.debug("Message received: {}", message.toString());
76     }
77
78     @Override
79     public FluentFuture<? extends CommitInfo> releaseConnection() {
80         LOG.info("Client Listener: Connection released.");
81         return CommitInfo.emptyFluentFuture();
82     }
83
84     void printCount(final String localAddress) {
85         LOG.info("Peer {} received {} update messages.", localAddress, this.messageCounter.longValue());
86     }
87 }