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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14 import io.netty.channel.nio.NioEventLoopGroup;
15 import io.netty.util.HashedWheelTimer;
16 import io.netty.util.concurrent.Future;
17 import io.netty.util.concurrent.GlobalEventExecutor;
18 import java.io.Closeable;
19 import java.io.IOException;
20 import java.net.InetAddress;
21 import java.net.InetSocketAddress;
22 import java.net.UnknownHostException;
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.controller.netconf.api.NetconfMessage;
29 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
30 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
31 import org.opendaylight.controller.netconf.client.NetconfClientSession;
32 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
33 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
34 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
35 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol;
36 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
37 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
38 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
39 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
43 * Synchronous netconf client suitable for testing
45 public class TestingNetconfClient implements Closeable {
47 public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
49 private final String label;
50 private final NetconfClientSession clientSession;
51 private final NetconfClientSessionListener sessionListener;
52 private final long sessionId;
54 public TestingNetconfClient(String clientLabel,
55 NetconfClientDispatcher netconfClientDispatcher, final NetconfClientConfiguration config) throws InterruptedException {
56 this.label = clientLabel;
57 sessionListener = config.getSessionListener();
58 Future<NetconfClientSession> clientFuture = netconfClientDispatcher.createClient(config);
59 clientSession = get(clientFuture);
60 this.sessionId = clientSession.getSessionId();
63 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
65 return clientFuture.get();
66 } catch (CancellationException e) {
67 throw new RuntimeException("Cancelling " + this, e);
68 } catch (ExecutionException e) {
69 throw new IllegalStateException("Unable to create " + this, e);
73 public Future<NetconfMessage> sendRequest(NetconfMessage message) {
74 return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
77 public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
78 InterruptedException, TimeoutException {
79 return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
82 public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
83 InterruptedException, TimeoutException {
84 return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
88 public void close() throws IOException {
89 clientSession.close();
93 public String toString() {
94 final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
95 sb.append("label=").append(label);
96 sb.append(", sessionId=").append(sessionId);
101 public long getSessionId() {
105 public Set<String> getCapabilities() {
106 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
107 return Sets.newHashSet(clientSession.getServerCapabilities());
110 public static void main(String[] args) throws Exception {
111 HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
112 NioEventLoopGroup nettyGroup = new NioEventLoopGroup();
113 NetconfClientDispatcherImpl netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup, hashedWheelTimer);
114 LoginPassword authHandler = new LoginPassword("admin", "admin");
115 TestingNetconfClient client = new TestingNetconfClient("client", netconfClientDispatcher, getClientConfig("127.0.0.1", 1830, true, Optional.of(authHandler)));
116 System.out.println(client.getCapabilities());
119 private static NetconfClientConfiguration getClientConfig(String host ,int port, boolean ssh, 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);