2297de7525351d7af0e4f86ee5232e5179239199
[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 void markUptodate(final TablesKey tablesKey) {
39         LOG.debug("Table marked as up-to-date {}", tablesKey);
40     }
41
42     @Override
43     public void onSessionUp(final BGPSession session) {
44         LOG.info("Client Listener: Session Up.");
45         if (this.nPrefixes > 0) {
46             PrefixesBuilder.AdvertiseIpv4Prefixes(((BGPSessionImpl) session).getLimiter(), this.nPrefixes, this.extCom, this.multiPathSupport);
47         }
48     }
49
50     @Override
51     public void onSessionDown(final BGPSession session, final Exception e) {
52         LOG.info("Client Listener: Connection lost.");
53         try {
54             session.close();
55         } catch (Exception ie) {
56             LOG.warn("Error closing session", ie);
57         }
58     }
59
60     @Override
61     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
62         LOG.info("Client Listener: Connection lost: {}.", cause);
63     }
64
65     @Override
66     public void onMessage(final BGPSession session, final Notification message) {
67         if (message instanceof Update) {
68             messageCounter++;
69         }
70         LOG.debug("Message received: {}", message.toString());
71     }
72
73     @Override
74     public void releaseConnection() {
75         LOG.info("Client Listener: Connection released.");
76     }
77
78     void printCount(final String localAddress) {
79         LOG.info("Peer {} received {} update messages.", localAddress, messageCounter);
80     }
81 }