Merge "Bug 615: Removed xtend from Topology Manager"
[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 io.netty.util.concurrent.Future;
12 import io.netty.util.concurrent.GlobalEventExecutor;
13
14 import java.io.Closeable;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.util.Set;
18 import java.util.concurrent.CancellationException;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22
23 import org.opendaylight.controller.netconf.api.NetconfMessage;
24 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
25 import org.opendaylight.controller.netconf.client.NetconfClientSession;
26 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
27 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
28 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
29 import org.opendaylight.protocol.framework.ReconnectStrategy;
30
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.Sets;
33
34
35 /**
36  * Synchronous netconf client suitable for testing
37  */
38 public class TestingNetconfClient implements Closeable {
39
40     public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
41
42     private final String label;
43     private final NetconfClientSession clientSession;
44     private final NetconfClientSessionListener sessionListener;
45     private final long sessionId;
46
47     private TestingNetconfClient(String clientLabel, InetSocketAddress address, ReconnectStrategy strat,
48             NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
49         this.label = clientLabel;
50         sessionListener = new SimpleNetconfClientSessionListener();
51         Future<NetconfClientSession> clientFuture = netconfClientDispatcher.createClient(address, sessionListener, strat);
52         clientSession = get(clientFuture);
53         this.sessionId = clientSession.getSessionId();
54     }
55
56     private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
57         try {
58             return clientFuture.get();
59         } catch (CancellationException e) {
60             throw new RuntimeException("Cancelling " + this, e);
61         } catch (ExecutionException e) {
62             throw new IllegalStateException("Unable to create " + this, e);
63         }
64     }
65
66     public TestingNetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
67                                 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
68         this(clientLabelForLogging, address,
69                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), netconfClientDispatcher);
70     }
71
72     public TestingNetconfClient(String clientLabelForLogging, InetSocketAddress address,
73                                 NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
74         this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
75                 DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher);
76     }
77
78     public Future<NetconfMessage> sendRequest(NetconfMessage message) {
79         return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
80     }
81
82     public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
83             InterruptedException, TimeoutException {
84         return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
85     }
86
87     public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
88             InterruptedException, TimeoutException {
89         return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
90     }
91
92     @Override
93     public void close() throws IOException {
94         clientSession.close();
95     }
96
97     @Override
98     public String toString() {
99         final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
100         sb.append("label=").append(label);
101         sb.append(", sessionId=").append(sessionId);
102         sb.append('}');
103         return sb.toString();
104     }
105
106     public long getSessionId() {
107         return sessionId;
108     }
109
110     public Set<String> getCapabilities() {
111         Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
112         return Sets.newHashSet(clientSession.getServerCapabilities());
113     }
114 }