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