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.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.conf.NetconfClientConfiguration;
30 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol;
31 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
32 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
33 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
34 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
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(String clientLabel,
50 NetconfClientDispatcher netconfClientDispatcher, final NetconfClientConfiguration config) throws InterruptedException {
51 this.label = clientLabel;
52 sessionListener = config.getSessionListener();
53 Future<NetconfClientSession> clientFuture = netconfClientDispatcher.createClient(config);
54 clientSession = get(clientFuture);//TODO: make static
55 this.sessionId = clientSession.getSessionId();
58 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
60 return clientFuture.get();
61 } catch (CancellationException e) {
62 throw new RuntimeException("Cancelling " + this, e);
63 } catch (ExecutionException e) {
64 throw new IllegalStateException("Unable to create " + this, e);
68 public Future<NetconfMessage> sendRequest(NetconfMessage message) {
69 return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
72 public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
73 InterruptedException, TimeoutException {
74 return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
77 public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
78 InterruptedException, TimeoutException {
79 return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
83 public void close() throws IOException {
84 clientSession.close();
88 public String toString() {
89 final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
90 sb.append("label=").append(label);
91 sb.append(", sessionId=").append(sessionId);
96 public long getSessionId() {
100 public Set<String> getCapabilities() {
101 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
102 return Sets.newHashSet(clientSession.getServerCapabilities());
105 public static void main(String[] args) throws Exception {
106 HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
107 NioEventLoopGroup nettyGroup = new NioEventLoopGroup();
108 NetconfClientDispatcherImpl netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup, hashedWheelTimer);
109 LoginPassword authHandler = new LoginPassword("admin", "admin");
110 TestingNetconfClient client = new TestingNetconfClient("client", netconfClientDispatcher, getClientConfig("127.0.0.1", 1830, true, Optional.of(authHandler)));
111 System.out.println(client.getCapabilities());
114 private static NetconfClientConfiguration getClientConfig(String host ,int port, boolean ssh, Optional<? extends AuthenticationHandler> maybeAuthHandler) throws UnknownHostException {
115 InetSocketAddress netconfAddress = new InetSocketAddress(InetAddress.getByName(host), port);
116 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
117 b.withAddress(netconfAddress);
118 b.withSessionListener(new SimpleNetconfClientSessionListener());
119 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
120 NetconfClientConfigurationBuilder.DEFAULT_CONNECTION_TIMEOUT_MILLIS));
122 b.withProtocol(NetconfClientProtocol.SSH);
123 b.withAuthHandler(maybeAuthHandler.get());
125 b.withProtocol(NetconfClientProtocol.TCP);