Bug 1894: Add LISP configuration options to etc/custom.properties in Karaf
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / netconf / client / TestingNetconfClient.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.client;
10
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;
23 import java.util.Set;
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;
35
36
37 /**
38  * Synchronous netconf client suitable for testing
39  */
40 public class TestingNetconfClient implements Closeable {
41
42     public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
43
44     private final String label;
45     private final NetconfClientSession clientSession;
46     private final NetconfClientSessionListener sessionListener;
47     private final long sessionId;
48
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();
56     }
57
58     private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
59         try {
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);
65         }
66     }
67
68     public Future<NetconfMessage> sendRequest(NetconfMessage message) {
69         return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
70     }
71
72     public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
73             InterruptedException, TimeoutException {
74         return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
75     }
76
77     public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
78             InterruptedException, TimeoutException {
79         return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
80     }
81
82     @Override
83     public void close() throws IOException {
84         clientSession.close();
85     }
86
87     @Override
88     public String toString() {
89         final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
90         sb.append("label=").append(label);
91         sb.append(", sessionId=").append(sessionId);
92         sb.append('}');
93         return sb.toString();
94     }
95
96     public long getSessionId() {
97         return sessionId;
98     }
99
100     public Set<String> getCapabilities() {
101         Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
102         return Sets.newHashSet(clientSession.getServerCapabilities());
103     }
104
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());
112     }
113
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));
121         if (ssh) {
122             b.withProtocol(NetconfClientProtocol.SSH);
123             b.withAuthHandler(maybeAuthHandler.get());
124         } else {
125             b.withProtocol(NetconfClientProtocol.TCP);
126         }
127         return b.build();
128     }
129 }