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