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 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.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.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 public 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 public final synchronized void onSessionUp(NetconfClientSession clientSession) {
59 this.clientSession = Preconditions.checkNotNull(clientSession);
60 LOG.debug("Client session {} went up", clientSession);
64 private synchronized void tearDown(final Exception cause) {
65 final RequestEntry e = requests.poll();
67 e.promise.setFailure(cause);
70 this.clientSession = null;
74 public final void onSessionDown(NetconfClientSession clientSession, Exception e) {
75 LOG.debug("Client Session {} went down unexpectedly", clientSession, e);
80 public final void onSessionTerminated(NetconfClientSession clientSession,
81 NetconfTerminationReason netconfTerminationReason) {
82 LOG.debug("Client Session {} terminated, reason: {}", clientSession,
83 netconfTerminationReason.getErrorMessage());
84 tearDown(new RuntimeException(netconfTerminationReason.getErrorMessage()));
88 public synchronized void onMessage(NetconfClientSession session, NetconfMessage message) {
89 LOG.debug("New message arrived: {}", message);
91 final RequestEntry e = requests.poll();
93 e.promise.setSuccess(message);
96 LOG.info("Ignoring unsolicited message {}", message);
100 public final synchronized Future<NetconfMessage> sendRequest(NetconfMessage message) {
101 final RequestEntry req = new RequestEntry(GlobalEventExecutor.INSTANCE.<NetconfMessage>newPromise(), message);
104 if (clientSession != null) {