Code clean up
[bgpcep.git] / pcep / pcc-mock / src / test / java / org / opendaylight / protocol / pcep / pcc / mock / TestingSessionListener.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.pcep.pcc.mock;
10
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.util.concurrent.Uninterruptibles;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.TimeUnit;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.junit.Assert;
19 import org.opendaylight.protocol.pcep.PCEPSession;
20 import org.opendaylight.protocol.pcep.PCEPSessionListener;
21 import org.opendaylight.protocol.pcep.PCEPTerminationReason;
22 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public final class TestingSessionListener implements PCEPSessionListener, ListenerCheck {
28
29     private static final Logger LOG = LoggerFactory.getLogger(TestingSessionListener.class);
30     private final CountDownLatch sessionLatch = new CountDownLatch(1);
31
32     @GuardedBy("this")
33     private final List<Message> messages = new ArrayList<>();
34
35     private boolean up = false;
36     private PCEPSession session = null;
37
38     @Override
39     public synchronized void onMessage(final PCEPSession psession, final Message message) {
40         LOG.debug("Received message: {}", message);
41         this.messages.add(message);
42     }
43
44     @Override
45     public void onSessionUp(final PCEPSession psession) {
46         LOG.debug("Session up.");
47         this.up = true;
48         this.session = psession;
49         this.sessionLatch.countDown();
50
51     }
52
53     @Override
54     public void onSessionDown(final PCEPSession psession, final Exception exception) {
55         LOG.debug("Session down. Cause : {} ", exception, exception);
56         this.up = false;
57         this.session = null;
58     }
59
60     @Override
61     public void onSessionTerminated(final PCEPSession psession, final PCEPTerminationReason cause) {
62         LOG.debug("Session terminated. Cause : {}", cause);
63     }
64
65     synchronized List<Message> messages() {
66         return ImmutableList.copyOf(this.messages);
67     }
68
69     boolean isUp() {
70         return this.up;
71     }
72
73     public PCEPSession getSession() {
74         Assert.assertTrue("Session up", Uninterruptibles.awaitUninterruptibly(this.sessionLatch, 10, TimeUnit.SECONDS));
75         return this.session;
76     }
77
78     @Override
79     public synchronized int getListMessageSize() {
80         return this.messages.size();
81     }
82 }