Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientSessionListener.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
9 package org.opendaylight.controller.netconf.client;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import org.opendaylight.controller.netconf.api.NetconfMessage;
14 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
15 import org.opendaylight.protocol.framework.SessionListener;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 public class NetconfClientSessionListener implements
23         SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> {
24
25     private static final Logger logger = LoggerFactory.getLogger(NetconfClientSessionListener.class);
26     private AtomicBoolean up = new AtomicBoolean(false);
27
28     @Override
29     public void onSessionUp(NetconfClientSession clientSession) {
30         up.set(true);
31     }
32
33     @Override
34     public void onSessionDown(NetconfClientSession clientSession, Exception e) {
35         logger.debug("Client Session {} down, reason: {}", clientSession, e.getMessage());
36         up.set(false);
37     }
38
39     @Override
40     public void onSessionTerminated(NetconfClientSession clientSession,
41             NetconfTerminationReason netconfTerminationReason) {
42         logger.debug("Client Session {} terminated, reason: {}", clientSession,
43                 netconfTerminationReason.getErrorMessage());
44         up.set(false);
45     }
46
47     @Override
48     public synchronized void onMessage(NetconfClientSession session, NetconfMessage message) {
49         synchronized (messages) {
50             this.messages.add(message);
51         }
52     }
53
54     private int lastReadMessage = -1;
55     private List<NetconfMessage> messages = Lists.newArrayList();
56
57     public NetconfMessage getLastMessage(int attempts, int attemptMsDelay) throws InterruptedException {
58         Preconditions.checkState(up.get(), "Session was not up yet");
59
60         for (int i = 0; i < attempts; i++) {
61             synchronized (messages) {
62                 if (messages.size() - 1 > lastReadMessage) {
63                     lastReadMessage++;
64                     return messages.get(lastReadMessage);
65                 }
66             }
67
68             if (up.get() == false)
69                 throw new IllegalStateException("Session ended while trying to read message");
70             Thread.sleep(attemptMsDelay);
71         }
72
73         throw new IllegalStateException("No netconf message to read");
74     }
75 }