Activate findbugs sonar profile
[controller.git] / opendaylight / netconf / netconf-client / src / test / java / org / opendaylight / controller / netconf / client / test / 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.test;
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.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;
40
41
42 /**
43  * Synchronous netconf client suitable for testing
44  */
45 public class TestingNetconfClient implements Closeable {
46
47     public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
48
49     private final String label;
50     private final NetconfClientSession clientSession;
51     private final NetconfClientSessionListener sessionListener;
52     private final long sessionId;
53
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);//TODO: make static
60         this.sessionId = clientSession.getSessionId();
61     }
62
63     private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
64         try {
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);
70         }
71     }
72
73     public Future<NetconfMessage> sendRequest(NetconfMessage message) {
74         return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
75     }
76
77     public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
78             InterruptedException, TimeoutException {
79         return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
80     }
81
82     public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
83             InterruptedException, TimeoutException {
84         return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
85     }
86
87     @Override
88     public void close() throws IOException {
89         clientSession.close();
90     }
91
92     @Override
93     public String toString() {
94         final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
95         sb.append("label=").append(label);
96         sb.append(", sessionId=").append(sessionId);
97         sb.append('}');
98         return sb.toString();
99     }
100
101     public long getSessionId() {
102         return sessionId;
103     }
104
105     public Set<String> getCapabilities() {
106         Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
107         return Sets.newHashSet(clientSession.getServerCapabilities());
108     }
109
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());
117     }
118
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));
126         if (ssh) {
127             b.withProtocol(NetconfClientProtocol.SSH);
128             b.withAuthHandler(maybeAuthHandler.get());
129         } else {
130             b.withProtocol(NetconfClientProtocol.TCP);
131         }
132         return b.build();
133     }
134 }