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 com.google.common.collect.Sets;
13 import io.netty.util.concurrent.Future;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
17 import org.opendaylight.protocol.framework.ReconnectStrategy;
18 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.io.Closeable;
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
26 import java.util.concurrent.CancellationException;
27 import java.util.concurrent.ExecutionException;
29 public class NetconfClient implements Closeable {
31 private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
33 public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
34 private final NetconfClientDispatcher dispatch;
35 private final String label;
36 private final NetconfClientSession clientSession;
37 private final NetconfClientSessionListener sessionListener;
38 private final long sessionId;
39 private final InetSocketAddress address;
41 // TODO test reconnecting constructor
42 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectionAttempts,
43 int attemptMsTimeout, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
44 this(clientLabelForLogging, address, getReconnectStrategy(connectionAttempts, attemptMsTimeout),
45 netconfClientDispatcher);
48 private NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
49 this.label = clientLabelForLogging;
50 dispatch = netconfClientDispatcher;
52 sessionListener = new NetconfClientSessionListener();
53 Future<NetconfClientSession> clientFuture = dispatch.createClient(address, sessionListener, strat);
54 this.address = address;
55 clientSession = get(clientFuture);
56 this.sessionId = clientSession.getSessionId();
59 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
61 return clientFuture.get();
62 } catch (CancellationException e) {
63 throw new RuntimeException("Netconf client interrupted", e);
64 } catch (ExecutionException e) {
65 throw new IllegalStateException("Unable to create netconf client", e);
69 public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
70 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
71 this(clientLabelForLogging, address,
72 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), netconfClientDispatcher);
75 public NetconfClient(String clientLabelForLogging, InetSocketAddress address,
76 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
77 this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
78 DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher);
81 public NetconfMessage sendMessage(NetconfMessage message) {
82 return sendMessage(message, 5, 1000);
85 public NetconfMessage sendMessage(NetconfMessage message, int attempts, int attemptMsDelay) {
86 Preconditions.checkState(clientSession.isUp(), "Session was not up yet");
87 clientSession.sendMessage(message);
89 return sessionListener.getLastMessage(attempts, attemptMsDelay);
90 } catch (InterruptedException e) {
91 throw new RuntimeException(this + " Cannot read message from " + address, e);
92 } catch (IllegalStateException e) {
93 throw new IllegalStateException(this + " Cannot read message from " + address, e);
98 public void close() throws IOException {
99 clientSession.close();
102 private static ReconnectStrategy getReconnectStrategy(int connectionAttempts, int attemptMsTimeout) {
103 return new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
104 Long.valueOf(connectionAttempts), null);
108 public String toString() {
109 final StringBuffer sb = new StringBuffer("NetconfClient{");
110 sb.append("label=").append(label);
111 sb.append(", sessionId=").append(sessionId);
113 return sb.toString();
116 public long getSessionId() {
120 public Set<String> getCapabilities() {
121 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
122 return Sets.newHashSet(clientSession.getServerCapabilities());