2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.client;
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;
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicBoolean;
22 public class NetconfClientSessionListener implements
23 SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> {
25 private static final Logger logger = LoggerFactory.getLogger(NetconfClientSessionListener.class);
26 private AtomicBoolean up = new AtomicBoolean(false);
29 public void onSessionUp(NetconfClientSession clientSession) {
34 public void onSessionDown(NetconfClientSession clientSession, Exception e) {
35 logger.debug("Client Session {} down, reason: {}", clientSession, e.getMessage());
40 public void onSessionTerminated(NetconfClientSession clientSession,
41 NetconfTerminationReason netconfTerminationReason) {
42 logger.debug("Client Session {} terminated, reason: {}", clientSession,
43 netconfTerminationReason.getErrorMessage());
48 public synchronized void onMessage(NetconfClientSession session, NetconfMessage message) {
49 synchronized (messages) {
50 this.messages.add(message);
54 private int lastReadMessage = -1;
55 private List<NetconfMessage> messages = Lists.newArrayList();
57 public NetconfMessage getLastMessage(int attempts, int attemptMsDelay) throws InterruptedException {
58 Preconditions.checkState(up.get(), "Session was not up yet");
60 for (int i = 0; i < attempts; i++) {
61 synchronized (messages) {
62 if (messages.size() - 1 > lastReadMessage) {
64 return messages.get(lastReadMessage);
68 if (up.get() == false)
69 throw new IllegalStateException("Session ended while trying to read message");
70 Thread.sleep(attemptMsDelay);
73 throw new IllegalStateException("No netconf message to read");