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 io.netty.util.concurrent.Future;
12 import io.netty.util.concurrent.GlobalEventExecutor;
13 import io.netty.util.concurrent.Promise;
15 import java.util.ArrayDeque;
16 import java.util.Queue;
18 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.controller.netconf.api.NetconfMessage;
21 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.common.base.Preconditions;
27 public class SimpleNetconfClientSessionListener implements NetconfClientSessionListener {
28 private static final class RequestEntry {
29 final Promise<NetconfMessage> promise;
30 final NetconfMessage request;
32 public RequestEntry(Promise<NetconfMessage> future, NetconfMessage request) {
33 this.promise = Preconditions.checkNotNull(future);
34 this.request = Preconditions.checkNotNull(request);
38 private static final Logger logger = LoggerFactory.getLogger(SimpleNetconfClientSessionListener.class);
41 private final Queue<RequestEntry> requests = new ArrayDeque<>();
44 private NetconfClientSession clientSession;
47 private void dispatchRequest() {
48 while (!requests.isEmpty()) {
49 final RequestEntry e = requests.peek();
50 if (e.promise.setUncancellable()) {
51 logger.debug("Sending message {}", e.request);
52 clientSession.sendMessage(e.request);
56 logger.debug("Message {} has been cancelled, skipping it", e.request);
62 public final synchronized void onSessionUp(NetconfClientSession clientSession) {
63 this.clientSession = Preconditions.checkNotNull(clientSession);
64 logger.debug("Client session {} went up", clientSession);
68 private synchronized void tearDown(final Exception cause) {
69 final RequestEntry e = requests.poll();
71 e.promise.setFailure(cause);
74 this.clientSession = null;
78 public final void onSessionDown(NetconfClientSession clientSession, Exception e) {
79 logger.debug("Client Session {} went down unexpectedly", clientSession, e);
84 public final void onSessionTerminated(NetconfClientSession clientSession,
85 NetconfTerminationReason netconfTerminationReason) {
86 logger.debug("Client Session {} terminated, reason: {}", clientSession,
87 netconfTerminationReason.getErrorMessage());
88 tearDown(new RuntimeException(netconfTerminationReason.getErrorMessage()));
92 public synchronized void onMessage(NetconfClientSession session, NetconfMessage message) {
93 logger.debug("New message arrived: {}", message);
95 final RequestEntry e = requests.poll();
97 e.promise.setSuccess(message);
100 logger.info("Ignoring unsolicited message {}", message);
104 public final synchronized Future<NetconfMessage> sendRequest(NetconfMessage message) {
105 final RequestEntry req = new RequestEntry(GlobalEventExecutor.INSTANCE.<NetconfMessage>newPromise(), message);
108 if (clientSession != null) {