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.test;
11 import java.io.Closeable;
12 import java.io.IOException;
14 import java.util.concurrent.CancellationException;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
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;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Sets;
28 import io.netty.util.concurrent.Future;
32 * Synchronous netconf client suitable for testing
34 public class TestingNetconfClient implements Closeable {
36 public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
38 private final String label;
39 private final NetconfClientSession clientSession;
40 private final NetconfClientSessionListener sessionListener;
41 private final long sessionId;
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();
52 private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
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);
62 public Future<NetconfMessage> sendRequest(NetconfMessage message) {
63 return ((SimpleNetconfClientSessionListener)sessionListener).sendRequest(message);
66 public NetconfMessage sendMessage(NetconfMessage message, int attemptMsDelay) throws ExecutionException,
67 InterruptedException, TimeoutException {
68 return sendRequest(message).get(attemptMsDelay, TimeUnit.MILLISECONDS);
71 public NetconfMessage sendMessage(NetconfMessage message) throws ExecutionException,
72 InterruptedException, TimeoutException {
73 return sendMessage(message, DEFAULT_CONNECT_TIMEOUT);
77 public void close() throws IOException {
78 clientSession.close();
82 public String toString() {
83 final StringBuffer sb = new StringBuffer("TestingNetconfClient{");
84 sb.append("label=").append(label);
85 sb.append(", sessionId=").append(sessionId);
90 public long getSessionId() {
94 public Set<String> getCapabilities() {
95 Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
96 return Sets.newHashSet(clientSession.getServerCapabilities());