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
8 package org.opendaylight.netconf.client;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.collect.Sets;
13 import io.netty.channel.nio.NioEventLoopGroup;
14 import io.netty.util.HashedWheelTimer;
15 import io.netty.util.concurrent.Future;
16 import io.netty.util.concurrent.GlobalEventExecutor;
17 import java.io.Closeable;
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import java.net.UnknownHostException;
22 import java.util.Optional;
24 import java.util.concurrent.CancellationException;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import org.opendaylight.netconf.api.NetconfMessage;
29 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
30 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol;
31 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
32 import org.opendaylight.netconf.nettyutil.NeverReconnectStrategy;
33 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
34 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
38 * Synchronous netconf client suitable for testing.
40 public class TestingNetconfClient implements Closeable {
42 public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
44 private final String label;
45 private final NetconfClientSession clientSession;
46 private final NetconfClientSessionListener sessionListener;
47 private final long sessionId;
49 public TestingNetconfClient(final String clientLabel,
50 final NetconfClientDispatcher netconfClientDispatcher,
51 final NetconfClientConfiguration config) throws InterruptedException {
52 this.label = clientLabel;
53 sessionListener = config.getSessionListener();
54 Future<NetconfClientSession> clientFuture = netconfClientDispatcher.createClient(config);
55 clientSession = get(clientFuture);
56 this.sessionId = clientSession.getSessionId();
59 private static NetconfClientSession get(final Future<NetconfClientSession> clientFuture)
60 throws InterruptedException {
62 return clientFuture.get();
63 } catch (CancellationException e) {
64 throw new RuntimeException("Cancelling " + TestingNetconfClient.class.getSimpleName(), e);
65 } catch (ExecutionException e) {
66 throw new IllegalStateException("Unable to create " + TestingNetconfClient.class.getSimpleName(), e);
70 public Future<NetconfMessage> sendRequest(final NetconfMessage message) {
71 return ((SimpleNetconfClientSessionListener) sessionListener).sendRequest(message);
74 public NetconfMessage sendMessage(final NetconfMessage message, final int attemptMsDelay) throws ExecutionException,
75 InterruptedException, TimeoutException {
76 return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
79 public NetconfMessage sendMessage(final NetconfMessage message) throws ExecutionException,
80 InterruptedException, TimeoutException {
81 return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
85 public void close() throws IOException {
86 clientSession.close();
90 public String toString() {
91 final StringBuilder sb = new StringBuilder("TestingNetconfClient{");
92 sb.append("label=").append(label);
93 sb.append(", sessionId=").append(sessionId);
98 public long getSessionId() {
102 public Set<String> getCapabilities() {
103 checkState(clientSession != null, "Client was not initialized successfully");
104 return Sets.newHashSet(clientSession.getServerCapabilities());
107 public static void main(final String[] args) throws Exception {
108 HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
109 NioEventLoopGroup nettyGroup = new NioEventLoopGroup();
110 NetconfClientDispatcherImpl netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup,
112 LoginPasswordHandler authHandler = new LoginPasswordHandler("admin", "admin");
113 TestingNetconfClient client = new TestingNetconfClient("client", netconfClientDispatcher,
114 getClientConfig("127.0.0.1", 1830, true, Optional.of(authHandler)));
115 System.console().writer().println(client.getCapabilities());
118 private static NetconfClientConfiguration getClientConfig(final String host, final int port, final boolean ssh,
119 final Optional<? extends AuthenticationHandler> maybeAuthHandler) throws UnknownHostException {
120 InetSocketAddress netconfAddress = new InetSocketAddress(InetAddress.getByName(host), port);
121 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
122 b.withAddress(netconfAddress);
123 b.withSessionListener(new SimpleNetconfClientSessionListener());
124 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
125 NetconfClientConfigurationBuilder.DEFAULT_CONNECTION_TIMEOUT_MILLIS));
127 b.withProtocol(NetconfClientProtocol.SSH);
128 b.withAuthHandler(maybeAuthHandler.get());
130 b.withProtocol(NetconfClientProtocol.TCP);