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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.GlobalEventExecutor;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
18 import org.opendaylight.protocol.framework.ReconnectStrategy;
19 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import javax.net.ssl.SSLContext;
24 import java.io.Closeable;
25 import java.io.IOException;
26 import java.net.InetSocketAddress;
28 import java.util.concurrent.CancellationException;
29 import java.util.concurrent.ExecutionException;
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) throws InterruptedException {
46 this(clientLabelForLogging, address, getReconnectStrategy(connectionAttempts, attemptMsTimeout), Optional
47 .<SSLContext> absent());
50 private NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat,
51 Optional<SSLContext> maybeSSLContext) throws InterruptedException {
52 this.label = clientLabelForLogging;
53 dispatch = new NetconfClientDispatcher(maybeSSLContext);
55 sessionListener = new NetconfClientSessionListener();
56 Future<NetconfClientSession> clientFuture = dispatch.createClient(address, sessionListener, strat);
57 this.address = address;
58 clientSession = get(clientFuture);
59 this.sessionId = clientSession.getSessionId();
62 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
64 return clientFuture.get();
65 } catch (CancellationException e) {
66 throw new RuntimeException("Netconf client interrupted", e);
67 } catch (ExecutionException e) {
68 throw new IllegalStateException("Unable to create netconf client", e);
72 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
73 Optional<SSLContext> maybeSSLContext) throws InterruptedException {
74 this(clientLabelForLogging, address,
75 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), maybeSSLContext);
78 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs)
79 throws InterruptedException {
80 this(clientLabelForLogging, address,
81 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), Optional
82 .<SSLContext> absent());
85 public NetconfClient(String clientLabelForLogging, InetSocketAddress address) throws InterruptedException {
86 this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
87 DEFAULT_CONNECT_TIMEOUT), Optional.<SSLContext> absent());
90 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, Optional<SSLContext> maybeSSLContext)
91 throws InterruptedException {
92 this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
93 DEFAULT_CONNECT_TIMEOUT), maybeSSLContext);
96 public NetconfMessage sendMessage(NetconfMessage message) {
97 return sendMessage(message, 5, 1000);
100 public NetconfMessage sendMessage(NetconfMessage message, int attempts, int attemptMsDelay) {
101 Preconditions.checkState(clientSession.isUp(), "Session was not up yet");
102 clientSession.sendMessage(message);
104 return sessionListener.getLastMessage(attempts, attemptMsDelay);
105 } catch (InterruptedException e) {
106 throw new RuntimeException(this + " Cannot read message from " + address, e);
107 } catch (IllegalStateException e) {
108 throw new IllegalStateException(this + " Cannot read message from " + address, e);
113 public void close() throws IOException {
114 clientSession.close();
118 private static ReconnectStrategy getReconnectStrategy(int connectionAttempts, int attemptMsTimeout) {
119 return new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
120 Long.valueOf(connectionAttempts), null);
124 public String toString() {
125 final StringBuffer sb = new StringBuffer("NetconfClient{");
126 sb.append("label=").append(label);
127 sb.append(", sessionId=").append(sessionId);
129 return sb.toString();
132 public long getSessionId() {
136 public Set<String> getCapabilities() {
137 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
138 return Sets.newHashSet(clientSession.getServerCapabilities());