X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fclient%2FNetconfClient.java;h=8b8c886a1c0328b558a4992c7d67b6b99b71800c;hb=67a2d25f63cc03d6291f9b3110e70fac812c3793;hp=b8372b618c2d2b20bfaf1e1e8117b9d065b7cb4f;hpb=b452c756f5608b365bcb48168f43ec861c5d9d20;p=controller.git diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClient.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClient.java index b8372b618c..8b8c886a1c 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClient.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClient.java @@ -8,19 +8,9 @@ package org.opendaylight.controller.netconf.client; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GlobalEventExecutor; -import org.opendaylight.controller.netconf.api.NetconfMessage; -import org.opendaylight.protocol.framework.NeverReconnectStrategy; -import org.opendaylight.protocol.framework.ReconnectStrategy; -import org.opendaylight.protocol.framework.TimedReconnectStrategy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import javax.net.ssl.SSLContext; import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; @@ -28,6 +18,16 @@ import java.util.Set; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.protocol.framework.NeverReconnectStrategy; +import org.opendaylight.protocol.framework.ReconnectStrategy; +import org.opendaylight.protocol.framework.TimedReconnectStrategy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Sets; + public class NetconfClient implements Closeable { private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class); @@ -42,16 +42,14 @@ public class NetconfClient implements Closeable { // TODO test reconnecting constructor public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectionAttempts, - int attemptMsTimeout) throws InterruptedException { - this(clientLabelForLogging, address, getReconnectStrategy(connectionAttempts, attemptMsTimeout), Optional - . absent()); + int attemptMsTimeout, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException { + this(clientLabelForLogging, address, getReconnectStrategy(connectionAttempts, attemptMsTimeout), + netconfClientDispatcher); } - private NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, - Optional maybeSSLContext) throws InterruptedException { + private NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException { this.label = clientLabelForLogging; - dispatch = new NetconfClientDispatcher(maybeSSLContext); - + dispatch = netconfClientDispatcher; sessionListener = new NetconfClientSessionListener(); Future clientFuture = dispatch.createClient(address, sessionListener, strat); this.address = address; @@ -69,28 +67,35 @@ public class NetconfClient implements Closeable { } } - public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs, - Optional maybeSSLContext) throws InterruptedException { - this(clientLabelForLogging, address, - new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), maybeSSLContext); + public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException { + return new NetconfClient(clientLabelForLogging,address,strat,netconfClientDispatcher); } - public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs) - throws InterruptedException { + public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher,NetconfClientSessionListener listener) throws InterruptedException { + return new NetconfClient(clientLabelForLogging,address,strat,netconfClientDispatcher,listener); + } + + public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs, + NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException { this(clientLabelForLogging, address, - new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), Optional - . absent()); + new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), netconfClientDispatcher); } - public NetconfClient(String clientLabelForLogging, InetSocketAddress address) throws InterruptedException { + public NetconfClient(String clientLabelForLogging, InetSocketAddress address, + NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException { this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, - DEFAULT_CONNECT_TIMEOUT), Optional. absent()); + DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher); } - public NetconfClient(String clientLabelForLogging, InetSocketAddress address, Optional maybeSSLContext) - throws InterruptedException { - this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, - DEFAULT_CONNECT_TIMEOUT), maybeSSLContext); + public NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, + NetconfClientDispatcher netconfClientDispatcher, NetconfClientSessionListener listener) throws InterruptedException{ + this.label = clientLabelForLogging; + dispatch = netconfClientDispatcher; + sessionListener = listener; + Future clientFuture = dispatch.createClient(address, sessionListener, strat); + this.address = address; + clientSession = get(clientFuture); + this.sessionId = clientSession.getSessionId(); } public NetconfMessage sendMessage(NetconfMessage message) { @@ -98,7 +103,9 @@ public class NetconfClient implements Closeable { } public NetconfMessage sendMessage(NetconfMessage message, int attempts, int attemptMsDelay) { + long startTime = System.currentTimeMillis(); Preconditions.checkState(clientSession.isUp(), "Session was not up yet"); + //logger.debug("Sending message: {}",XmlUtil.toString(message.getDocument())); clientSession.sendMessage(message); try { return sessionListener.getLastMessage(attempts, attemptMsDelay); @@ -106,13 +113,19 @@ public class NetconfClient implements Closeable { throw new RuntimeException(this + " Cannot read message from " + address, e); } catch (IllegalStateException e) { throw new IllegalStateException(this + " Cannot read message from " + address, e); + } finally { + long diffMillis = System.currentTimeMillis() - startTime; + logger.debug("Total time spent waiting for response {}", diffMillis); } } @Override public void close() throws IOException { clientSession.close(); - dispatch.close(); + } + + public NetconfClientDispatcher getNetconfClientDispatcher() { + return dispatch; } private static ReconnectStrategy getReconnectStrategy(int connectionAttempts, int attemptMsTimeout) { @@ -137,4 +150,8 @@ public class NetconfClient implements Closeable { Preconditions.checkState(clientSession != null, "Client was not initialized successfully"); return Sets.newHashSet(clientSession.getServerCapabilities()); } + + public NetconfClientSession getClientSession() { + return clientSession; + } }