Upgrade ietf-{inet,yang}-types to 2013-07-15
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / SimpleSessionListener.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.rib.impl;
9
10 import com.google.common.collect.Lists;
11 import com.google.common.util.concurrent.Uninterruptibles;
12 import java.util.List;
13 import java.util.concurrent.CountDownLatch;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.Assert;
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.rib.rev130925.rib.TablesKey;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Listener for the client.
26  */
27 public final class SimpleSessionListener implements BGPSessionListener {
28
29     private static final Logger LOG = LoggerFactory.getLogger(SimpleSessionListener.class);
30     private final List<Notification> listMsg = Lists.newArrayList();
31     private BGPSession session;
32     private final CountDownLatch sessionLatch = new CountDownLatch(1);
33
34     SimpleSessionListener() {
35     }
36
37     List<Notification> getListMsg() {
38         return this.listMsg;
39     }
40
41     @Override
42     public boolean isSessionActive() {
43         return getSession().isWritable();
44     }
45
46     @Override
47     public void markUptodate(final TablesKey tablesKey) {
48         LOG.debug("Table marked as up-to-date {}", tablesKey);
49     }
50
51     @Override
52     public void onSessionUp(final BGPSession session) {
53         LOG.info("Session Up");
54         this.session = session;
55         sessionLatch.countDown();
56     }
57
58     @Override
59     public void onSessionDown(final BGPSession session, final Exception e) {
60         LOG.debug("Session Down", e);
61     }
62
63     @Override
64     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
65         LOG.debug("Session terminated. Cause : {}", cause.toString());
66     }
67
68     @Override
69     public void onMessage(final BGPSession session, final Notification message) {
70         this.listMsg.add(message);
71         LOG.debug("Message received: {}", message);
72     }
73
74     @Override
75     public void releaseConnection() {
76         LOG.debug("Releasing connection");
77         if (this.session != null) {
78             try {
79                 this.session.close();
80             } catch (final Exception e) {
81                 LOG.warn("Error closing session", e);
82             }
83         }
84     }
85
86     BGPSessionImpl.State getState() {
87         return getSession().getState();
88     }
89
90     BGPSessionImpl getSession() {
91         Assert.assertEquals("Session up", true, Uninterruptibles.awaitUninterruptibly(sessionLatch, 10, TimeUnit.SECONDS));
92         return (BGPSessionImpl) this.session;
93     }
94 }