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.netconf.client;
11 import com.google.common.base.Preconditions;
12 import io.netty.util.concurrent.Future;
13 import io.netty.util.concurrent.GlobalEventExecutor;
14 import io.netty.util.concurrent.Promise;
15 import java.util.ArrayDeque;
16 import java.util.Queue;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.netconf.api.NetconfMessage;
19 import org.opendaylight.netconf.api.NetconfTerminationReason;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class SimpleNetconfClientSessionListener implements NetconfClientSessionListener {
24 private static final class RequestEntry {
25 private final Promise<NetconfMessage> promise;
26 private final NetconfMessage request;
28 RequestEntry(Promise<NetconfMessage> future, NetconfMessage request) {
29 this.promise = Preconditions.checkNotNull(future);
30 this.request = Preconditions.checkNotNull(request);
34 private static final Logger LOG = LoggerFactory.getLogger(SimpleNetconfClientSessionListener.class);
37 private final Queue<RequestEntry> requests = new ArrayDeque<>();
40 private NetconfClientSession clientSession;
43 private void dispatchRequest() {
44 while (!requests.isEmpty()) {
45 final RequestEntry e = requests.peek();
46 if (e.promise.setUncancellable()) {
47 LOG.debug("Sending message {}", e.request);
48 clientSession.sendMessage(e.request);
52 LOG.debug("Message {} has been cancelled, skipping it", e.request);
58 @SuppressWarnings("checkstyle:hiddenField")
59 public final synchronized void onSessionUp(NetconfClientSession clientSession) {
60 this.clientSession = Preconditions.checkNotNull(clientSession);
61 LOG.debug("Client session {} went up", clientSession);
65 private synchronized void tearDown(final Exception cause) {
66 final RequestEntry e = requests.poll();
68 e.promise.setFailure(cause);
71 this.clientSession = null;
75 @SuppressWarnings("checkstyle:hiddenField")
76 public final void onSessionDown(NetconfClientSession clientSession, Exception exception) {
77 LOG.debug("Client Session {} went down unexpectedly", clientSession, exception);
82 @SuppressWarnings("checkstyle:hiddenField")
83 public final void onSessionTerminated(NetconfClientSession clientSession,
84 NetconfTerminationReason netconfTerminationReason) {
85 LOG.debug("Client Session {} terminated, reason: {}", clientSession,
86 netconfTerminationReason.getErrorMessage());
87 tearDown(new RuntimeException(netconfTerminationReason.getErrorMessage()));
91 public synchronized void onMessage(NetconfClientSession session, NetconfMessage message) {
92 LOG.debug("New message arrived: {}", message);
94 final RequestEntry e = requests.poll();
96 e.promise.setSuccess(message);
99 LOG.info("Ignoring unsolicited message {}", message);
103 public final synchronized Future<NetconfMessage> sendRequest(NetconfMessage message) {
104 final RequestEntry req = new RequestEntry(GlobalEventExecutor.INSTANCE.newPromise(), message);
107 if (clientSession != null) {