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.test;
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;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import org.opendaylight.controller.netconf.api.NetconfMessage;
24 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
25 import org.opendaylight.controller.netconf.client.NetconfClientSession;
26 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
27 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
28 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
29 import org.opendaylight.protocol.framework.ReconnectStrategy;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.Sets;
36 * Synchronous netconf client suitable for testing
38 public class TestingNetconfClient implements Closeable {
40 public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
42 private final String label;
43 private final NetconfClientSession clientSession;
44 private final NetconfClientSessionListener sessionListener;
45 private final long sessionId;
47 private TestingNetconfClient(String clientLabel, InetSocketAddress address, ReconnectStrategy strat,
48 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
49 this.label = clientLabel;
50 sessionListener = new SimpleNetconfClientSessionListener();
51 Future<NetconfClientSession> clientFuture = netconfClientDispatcher.createClient(address, sessionListener, strat);
52 clientSession = get(clientFuture);
53 this.sessionId = clientSession.getSessionId();
56 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
58 return clientFuture.get();
59 } catch (CancellationException e) {
60 throw new RuntimeException("Cancelling " + this, e);
61 } catch (ExecutionException e) {
62 throw new IllegalStateException("Unable to create " + this, e);
66 public TestingNetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
67 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
68 this(clientLabelForLogging, address,
69 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), netconfClientDispatcher);
72 public TestingNetconfClient(String clientLabelForLogging, InetSocketAddress address,
73 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
74 this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
75 DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher);
78 public Future<NetconfMessage> sendRequest(NetconfMessage message) {
79 return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
82 public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
83 InterruptedException, TimeoutException {
84 return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
87 public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
88 InterruptedException, TimeoutException {
89 return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
93 public void close() throws IOException {
94 clientSession.close();
98 public String toString() {
99 final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
100 sb.append("label=").append(label);
101 sb.append(", sessionId=").append(sessionId);
103 return sb.toString();
106 public long getSessionId() {
110 public Set<String> getCapabilities() {
111 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
112 return Sets.newHashSet(clientSession.getServerCapabilities());