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