BUG-5790: BGP Test tool
[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 java.util.List;
11 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
12 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
13 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
14 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
17 import org.opendaylight.yangtools.yang.binding.Notification;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Testing BGP Listener.
23  */
24 final class TestingListener implements BGPSessionListener {
25     private static final Logger LOG = LoggerFactory.getLogger(TestingListener.class);
26     private final int nPrefixes;
27     private final List<String> extCom;
28     private final boolean multiPathSupport;
29     private int messageCounter = 0;
30
31     TestingListener(final int nPrefixes, final List<String> extCom, final boolean multiPathSupport) {
32         this.nPrefixes = nPrefixes;
33         this.extCom = extCom;
34         this.multiPathSupport = multiPathSupport;
35     }
36
37     @Override
38     public boolean isSessionActive() {
39         return true;
40     }
41
42     @Override
43     public void markUptodate(final TablesKey tablesKey) {
44         LOG.debug("Table marked as up-to-date {}", tablesKey);
45     }
46
47     @Override
48     public void onSessionUp(final BGPSession session) {
49         LOG.info("Client Listener: Session Up.");
50         if (this.nPrefixes > 0) {
51             PrefixesBuilder.AdvertiseIpv4Prefixes(((BGPSessionImpl) session).getLimiter(), this.nPrefixes, this.extCom, this.multiPathSupport);
52         }
53     }
54
55     @Override
56     public void onSessionDown(final BGPSession session, final Exception e) {
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             messageCounter++;
74         }
75         LOG.debug("Message received: {}", message.toString());
76     }
77
78     @Override
79     public void releaseConnection() {
80         LOG.info("Client Listener: Connection released.");
81     }
82
83     void printCount(final String localAddress) {
84         LOG.info("Peer {} received {} update messages.", localAddress, messageCounter);
85     }
86 }