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;
14 import java.io.Closeable;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
18 import java.util.concurrent.CancellationException;
19 import java.util.concurrent.ExecutionException;
21 import org.opendaylight.controller.netconf.api.NetconfMessage;
22 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
23 import org.opendaylight.protocol.framework.ReconnectStrategy;
24 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.Sets;
31 public class NetconfClient implements Closeable {
33 private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
35 public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
36 private final NetconfClientDispatcher dispatch;
37 private final String label;
38 private final NetconfClientSession clientSession;
39 private final NetconfClientSessionListener sessionListener;
40 private final long sessionId;
41 private final InetSocketAddress address;
43 // TODO test reconnecting constructor
44 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectionAttempts,
45 int attemptMsTimeout, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
46 this(clientLabelForLogging, address, getReconnectStrategy(connectionAttempts, attemptMsTimeout),
47 netconfClientDispatcher);
50 private NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
51 this.label = clientLabelForLogging;
52 dispatch = netconfClientDispatcher;
53 sessionListener = new NetconfClientSessionListener();
54 Future<NetconfClientSession> clientFuture = dispatch.createClient(address, sessionListener, strat);
55 this.address = address;
56 clientSession = get(clientFuture);
57 this.sessionId = clientSession.getSessionId();
60 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
62 return clientFuture.get();
63 } catch (CancellationException e) {
64 throw new RuntimeException("Netconf client interrupted", e);
65 } catch (ExecutionException e) {
66 throw new IllegalStateException("Unable to create netconf client", e);
70 public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
71 return new NetconfClient(clientLabelForLogging,address,strat,netconfClientDispatcher);
74 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
75 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
76 this(clientLabelForLogging, address,
77 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), netconfClientDispatcher);
80 public NetconfClient(String clientLabelForLogging, InetSocketAddress address,
81 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
82 this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
83 DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher);
86 public NetconfMessage sendMessage(NetconfMessage message) {
87 return sendMessage(message, 5, 1000);
90 public NetconfMessage sendMessage(NetconfMessage message, int attempts, int attemptMsDelay) {
91 long startTime = System.currentTimeMillis();
92 Preconditions.checkState(clientSession.isUp(), "Session was not up yet");
93 clientSession.sendMessage(message);
95 return sessionListener.getLastMessage(attempts, attemptMsDelay);
96 } catch (InterruptedException e) {
97 throw new RuntimeException(this + " Cannot read message from " + address, e);
98 } catch (IllegalStateException e) {
99 throw new IllegalStateException(this + " Cannot read message from " + address, e);
101 long diffMillis = System.currentTimeMillis() - startTime;
102 logger.debug("Total time spent waiting for response {}", diffMillis);
107 public void close() throws IOException {
108 clientSession.close();
111 public NetconfClientDispatcher getNetconfClientDispatcher() {
115 private static ReconnectStrategy getReconnectStrategy(int connectionAttempts, int attemptMsTimeout) {
116 return new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
117 Long.valueOf(connectionAttempts), null);
121 public String toString() {
122 final StringBuffer sb = new StringBuffer("NetconfClient{");
123 sb.append("label=").append(label);
124 sb.append(", sessionId=").append(sessionId);
126 return sb.toString();
129 public long getSessionId() {
133 public Set<String> getCapabilities() {
134 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
135 return Sets.newHashSet(clientSession.getServerCapabilities());
138 public NetconfClientSession getClientSession() {
139 return clientSession;