YANG revision dates mass-update
[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 com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.concurrent.atomic.LongAdder;
15 import org.opendaylight.mdsal.common.api.CommitInfo;
16 import org.opendaylight.protocol.bgp.rib.impl.BGPSessionImpl;
17 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
18 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
19 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Testing BGP Listener.
28  */
29 final class TestingListener implements BGPSessionListener {
30     private static final Logger LOG = LoggerFactory.getLogger(TestingListener.class);
31     private final int nprefixes;
32     private final List<String> extCom;
33     private final boolean multiPathSupport;
34     private final LongAdder messageCounter = new LongAdder();
35
36     TestingListener(final int nprefixes, final List<String> extCom, final boolean multiPathSupport) {
37         this.nprefixes = nprefixes;
38         this.extCom = extCom;
39         this.multiPathSupport = multiPathSupport;
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,
52                     this.multiPathSupport);
53         }
54     }
55
56     @Override
57     @SuppressWarnings("checkstyle:IllegalCatch")
58     public void onSessionDown(final BGPSession session, final Exception exc) {
59         LOG.info("Client Listener: Connection lost.");
60         try {
61             session.close();
62         } catch (Exception ie) {
63             LOG.warn("Error closing session", ie);
64         }
65     }
66
67     @Override
68     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
69         LOG.info("Client Listener: Connection lost: {}.", cause);
70     }
71
72     @Override
73     public void onMessage(final BGPSession session, final Notification message) {
74         if (message instanceof Update) {
75             this.messageCounter.increment();
76         }
77         LOG.debug("Message received: {}", message.toString());
78     }
79
80     @Override
81     public FluentFuture<? extends CommitInfo> releaseConnection() {
82         LOG.info("Client Listener: Connection released.");
83         return CommitInfo.emptyFluentFuture();
84     }
85
86     @Override
87     public ListenableFuture<?> restartGracefully(final long selectionDeferralTimerSeconds) {
88         return Futures.immediateFailedFuture(
89                 new UnsupportedOperationException("Testtool doesn't support graceful restart"));
90     }
91
92     void printCount(final String localAddress) {
93         LOG.info("Peer {} received {} update messages.", localAddress, this.messageCounter.longValue());
94     }
95 }