From ba7242728c95c9fff87847843c9dcc3a0b88de7b Mon Sep 17 00:00:00 2001 From: Tomas Olvecky Date: Thu, 23 Jan 2014 17:03:17 +0100 Subject: [PATCH] Add configurable connection timeout to netconf client. Make connection timeout configurable. Previous behavior was having hardcoded connection timeout set to 1 minute. Make NetconfConnectorModule more configurable, allow (and this is new default) to retry on connection failures forever. Change-Id: Ia9280cff1b751a4e7318e1e0331175c960823dad Signed-off-by: Tomas Olvecky --- .../netconf/NetconfConnectorModule.java | 72 +++++++++++-------- .../yang/odl-sal-netconf-connector-cfg.yang | 19 +++++ .../netconf/persist/impl/ConfigPusher.java | 22 +++--- .../impl/osgi/ConfigPersisterActivator.java | 2 +- .../client/NetconfClientDispatcher.java | 16 ++--- .../NetconfClientSessionNegotiator.java | 5 +- ...NetconfClientSessionNegotiatorFactory.java | 16 ++--- .../client/NetconfSshClientDispatcher.java | 20 +++--- .../client/SSHNetconfClientLiveTest.java | 2 +- .../impl/NetconfServerSessionNegotiator.java | 11 ++- ...NetconfServerSessionNegotiatorFactory.java | 6 +- .../impl/osgi/NetconfImplActivator.java | 3 +- .../netconf/impl/ConcurrentClientsTest.java | 64 +++++++++-------- .../impl/NetconfDispatcherImplTest.java | 5 +- .../netconf/it/AbstractNetconfConfigTest.java | 60 ++++++++++++++++ .../it/NetconfConfigPersisterITTest.java | 43 +++++------ .../netconf/it/NetconfITSecureTest.java | 53 +++++--------- .../controller/netconf/it/NetconfITTest.java | 26 +------ .../netconf/it/NetconfMonitoringITTest.java | 25 +------ .../AbstractNetconfSessionNegotiator.java | 10 +-- .../handler/NetconfMessageAggregator.java | 10 ++- .../netconf/util/osgi/NetconfConfigUtil.java | 33 +++++++-- 22 files changed, 291 insertions(+), 232 deletions(-) create mode 100644 opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/AbstractNetconfConfigTest.java diff --git a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java index cedc0d4d8f..a04baaa912 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java +++ b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/config/yang/md/sal/connector/netconf/NetconfConnectorModule.java @@ -7,18 +7,9 @@ */ package org.opendaylight.controller.config.yang.md.sal.connector.netconf; +import com.google.common.net.InetAddresses; import io.netty.channel.EventLoopGroup; import io.netty.util.concurrent.GlobalEventExecutor; - -import java.io.File; -import java.io.InputStream; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import javax.net.ssl.SSLContext; - import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.client.NetconfSshClientDispatcher; import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler; @@ -31,17 +22,25 @@ import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingPr import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider; import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders; import org.osgi.framework.BundleContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import static com.google.common.base.Preconditions.*; +import java.io.File; +import java.io.InputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; -import com.google.common.base.Optional; -import com.google.common.net.InetAddresses; +import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkCondition; +import static org.opendaylight.controller.config.api.JmxAttributeValidationException.checkNotNull; /** * */ public final class NetconfConnectorModule extends org.opendaylight.controller.config.yang.md.sal.connector.netconf.AbstractNetconfConnectorModule { + private static final Logger logger = LoggerFactory.getLogger(NetconfConnectorModule.class); private static ExecutorService GLOBAL_PROCESSING_EXECUTOR = null; private static AbstractCachingSchemaSourceProvider GLOBAL_NETCONF_SOURCE_PROVIDER = null; @@ -56,14 +55,20 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co } @Override - public void validate(){ - super.validate(); - checkState(getAddress() != null,"Address must be set."); + protected void customValidation() { + checkNotNull(getAddress(), addressJmxAttribute); //checkState(getAddress().getIpv4Address() != null || getAddress().getIpv6Address() != null,"Address must be set."); - checkState(getPort() != null,"Port must be set."); - checkState(getDomRegistry() != null,"Dom Registry must be provided."); - } + checkNotNull(getPort(), portJmxAttribute); + checkNotNull(getDomRegistry(), portJmxAttribute); + checkNotNull(getDomRegistry(), domRegistryJmxAttribute); + + checkNotNull(getConnectionTimeoutMillis(), connectionTimeoutMillisJmxAttribute); + checkCondition(getConnectionTimeoutMillis() > 0, "must be > 0", connectionTimeoutMillisJmxAttribute); + checkNotNull(getBetweenAttemptsTimeoutMillis(), betweenAttemptsTimeoutMillisJmxAttribute); + checkCondition(getBetweenAttemptsTimeoutMillis() > 0, "must be > 0", betweenAttemptsTimeoutMillisJmxAttribute); + + } @Override public java.lang.AutoCloseable createInstance() { @@ -71,10 +76,15 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co getDomRegistryDependency(); NetconfDevice device = new NetconfDevice(getIdentifier().getInstanceName()); String addressValue = getAddress(); - - - int attemptMsTimeout = 60*1000; - int connectionAttempts = 5; + + Long connectionAttempts; + if (getMaxConnectionAttempts() != null && getMaxConnectionAttempts() > 0) { + connectionAttempts = getMaxConnectionAttempts(); + } else { + logger.trace("Setting {} on {} to infinity", maxConnectionAttemptsJmxAttribute, this); + connectionAttempts = null; + } + long clientConnectionTimeoutMillis = getConnectionTimeoutMillis(); /* * Uncomment after Switch to IP Address if(getAddress().getIpv4Address() != null) { @@ -83,8 +93,12 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co addressValue = getAddress().getIpv6Address().getValue(); } */ - ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null, - Long.valueOf(connectionAttempts), null); + double sleepFactor = 1.0; + int minSleep = 1000; + Long maxSleep = null; + Long deadline = null; + ReconnectStrategy strategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, getBetweenAttemptsTimeoutMillis(), + minSleep, sleepFactor, maxSleep, connectionAttempts, deadline); device.setReconnectStrategy(strategy); @@ -96,7 +110,7 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co device.setSocketAddress(socketAddress); device.setEventExecutor(getEventExecutorDependency()); - device.setDispatcher(createDispatcher()); + device.setDispatcher(createDispatcher(clientConnectionTimeoutMillis)); device.setSchemaSourceProvider(getGlobalNetconfSchemaProvider(bundleContext)); getDomRegistryDependency().registerProvider(device, bundleContext); @@ -124,14 +138,14 @@ public final class NetconfConnectorModule extends org.opendaylight.controller.co return GLOBAL_NETCONF_SOURCE_PROVIDER; } - private NetconfClientDispatcher createDispatcher() { + private NetconfClientDispatcher createDispatcher(long clientConnectionTimeoutMillis) { EventLoopGroup bossGroup = getBossThreadGroupDependency(); EventLoopGroup workerGroup = getWorkerThreadGroupDependency(); if(getTcpOnly()) { - return new NetconfClientDispatcher( bossGroup, workerGroup); + return new NetconfClientDispatcher( bossGroup, workerGroup, clientConnectionTimeoutMillis); } else { AuthenticationHandler authHandler = new LoginPassword(getUsername(),getPassword()); - return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup); + return new NetconfSshClientDispatcher(authHandler , bossGroup, workerGroup, clientConnectionTimeoutMillis); } } diff --git a/opendaylight/md-sal/sal-netconf-connector/src/main/yang/odl-sal-netconf-connector-cfg.yang b/opendaylight/md-sal/sal-netconf-connector/src/main/yang/odl-sal-netconf-connector-cfg.yang index 2331516877..b28e72eb80 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/main/yang/odl-sal-netconf-connector-cfg.yang +++ b/opendaylight/md-sal/sal-netconf-connector/src/main/yang/odl-sal-netconf-connector-cfg.yang @@ -88,6 +88,25 @@ module odl-sal-netconf-connector-cfg { } } } + + leaf connection-timeout-millis { + description "Specifies timeout in milliseconds after which connection must be established."; + type uint32; + default 5000; + } + + leaf max-connection-attempts { + description "Maximum number of connection retries. Non positive value or null is interpreted as infinity."; + type uint32; + default 0; // retry forever + } + + + leaf between-attempts-timeout-millis { + description "Timeout in milliseconds to wait between connection attempts."; + type uint16; + default 10000; + } } } } \ No newline at end of file diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java index 0a844b69ab..b1eb2fc720 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPusher.java @@ -47,23 +47,21 @@ public class ConfigPusher { private final EventLoopGroup nettyThreadgroup; // Default timeout for netconf becoming stable - public static final long DEFAULT_TIMEOUT = TimeUnit.MINUTES.toNanos(2); + public static final long DEFAULT_TIMEOUT_NANOS = TimeUnit.MINUTES.toNanos(2); + private static final long DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000; private final int delayMillis = 5000; private final long timeoutNanos; + private final long connectionTimeoutMillis; public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup) { - this(address, nettyThreadgroup, DEFAULT_TIMEOUT); + this(address, nettyThreadgroup, DEFAULT_TIMEOUT_NANOS, DEFAULT_CONNECTION_TIMEOUT_MILLIS); } - @Deprecated - public ConfigPusher(InetSocketAddress address, long timeoutMillis, EventLoopGroup nettyThreadgroup) { - this(address, nettyThreadgroup, TimeUnit.MILLISECONDS.toNanos(timeoutMillis)); - } - - public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup, long timeoutNanos) { + public ConfigPusher(InetSocketAddress address, EventLoopGroup nettyThreadgroup, long timeoutNanos, long connectionTimeoutMillis) { this.address = address; this.nettyThreadgroup = nettyThreadgroup; this.timeoutNanos = timeoutNanos; + this.connectionTimeoutMillis = connectionTimeoutMillis; } public synchronized NetconfClient init(List configs) throws InterruptedException { @@ -133,11 +131,11 @@ public class ConfigPusher { String additionalHeader = NetconfMessageAdditionalHeader.toString("unknown", address.getAddress().getHostAddress(), Integer.toString(address.getPort()), "tcp", Optional.of("persister")); - Set latestCapabilities = new HashSet<>(); + Set latestCapabilities = null; while (System.nanoTime() < deadline) { attempt++; NetconfClientDispatcher netconfClientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, - nettyThreadgroup, additionalHeader); + nettyThreadgroup, additionalHeader, connectionTimeoutMillis); NetconfClient netconfClient; try { netconfClient = new NetconfClient(this.toString(), address, delayMillis, netconfClientDispatcher); @@ -157,6 +155,10 @@ public class ConfigPusher { Util.closeClientAndDispatcher(netconfClient); Thread.sleep(delayMillis); } + if (latestCapabilities == null) { + logger.error("Could not connect to the server in {} ms", timeoutNanos / 1000); + throw new RuntimeException("Could not connect to netconf server"); + } Set allNotFound = new HashSet<>(expectedCaps); allNotFound.removeAll(latestCapabilities); logger.error("Netconf server did not provide required capabilities. Expected but not found: {}, all expected {}, current {}", diff --git a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java index 179c9681fc..a09c75b940 100644 --- a/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java +++ b/opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/osgi/ConfigPersisterActivator.java @@ -64,7 +64,7 @@ public class ConfigPersisterActivator implements BundleActivator { } String timeoutProperty = propertiesProvider.getProperty(PUSH_TIMEOUT); - long timeout = timeoutProperty == null ? ConfigPusher.DEFAULT_TIMEOUT : TimeUnit.SECONDS.toNanos(Integer.valueOf(timeoutProperty)); + long timeout = timeoutProperty == null ? ConfigPusher.DEFAULT_TIMEOUT_NANOS : TimeUnit.SECONDS.toNanos(Integer.valueOf(timeoutProperty)); final Pattern ignoredMissingCapabilityRegex = Pattern.compile(regex); nettyThreadgroup = new NioEventLoopGroup(); diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java index fc6f87db5d..1228a84a8a 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java @@ -8,15 +8,12 @@ package org.opendaylight.controller.netconf.client; +import com.google.common.base.Optional; import io.netty.channel.EventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.util.HashedWheelTimer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Promise; - -import java.io.Closeable; -import java.net.InetSocketAddress; - import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.api.NetconfSession; import org.opendaylight.controller.netconf.api.NetconfTerminationReason; @@ -28,7 +25,8 @@ import org.opendaylight.protocol.framework.SessionListenerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Optional; +import java.io.Closeable; +import java.net.InetSocketAddress; public class NetconfClientDispatcher extends AbstractDispatcher implements Closeable { @@ -37,16 +35,16 @@ public class NetconfClientDispatcher extends AbstractDispatcherabsent()); + this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.absent(), clientConnectionTimeoutMillis); } - public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String additionalHeader) { + public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String additionalHeader, long connectionTimeoutMillis) { super(bossGroup, workerGroup); timer = new HashedWheelTimer(); - this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader)); + this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), connectionTimeoutMillis); } public Future createClient(InetSocketAddress address, diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiator.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiator.java index dd8e3c9664..100b98c15a 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiator.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiator.java @@ -34,8 +34,9 @@ public class NetconfClientSessionNegotiator extends AbstractNetconfSessionNegotiator { protected NetconfClientSessionNegotiator(NetconfSessionPreferences sessionPreferences, - Promise promise, Channel channel, Timer timer, SessionListener sessionListener) { - super(sessionPreferences, promise, channel, timer, sessionListener); + Promise promise, Channel channel, Timer timer, SessionListener sessionListener, + long connectionTimeoutMillis) { + super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis); } private static Collection getCapabilities(Document doc) { diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java index abfbdd526c..db6c024e5a 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java @@ -8,13 +8,11 @@ package org.opendaylight.controller.netconf.client; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; import io.netty.channel.Channel; import io.netty.util.Timer; import io.netty.util.concurrent.Promise; - -import java.io.IOException; -import java.io.InputStream; - import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.api.NetconfSessionPreferences; import org.opendaylight.controller.netconf.util.xml.XmlUtil; @@ -23,18 +21,20 @@ import org.opendaylight.protocol.framework.SessionNegotiator; import org.opendaylight.protocol.framework.SessionNegotiatorFactory; import org.xml.sax.SAXException; -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.InputStream; public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory { private final Timer timer; private final Optional additionalHeader; + private final long connectionTimeoutMillis; - public NetconfClientSessionNegotiatorFactory(Timer timer, Optional additionalHeader) { + public NetconfClientSessionNegotiatorFactory(Timer timer, Optional additionalHeader, long connectionTimeoutMillis) { this.timer = timer; this.additionalHeader = additionalHeader; + this.connectionTimeoutMillis = connectionTimeoutMillis; } private static NetconfMessage loadHelloMessageTemplate() { @@ -57,7 +57,7 @@ public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorF } NetconfSessionPreferences proposal = new NetconfSessionPreferences(helloMessage); return new NetconfClientSessionNegotiator(proposal, promise, channel, timer, - sessionListenerFactory.getSessionListener()); + sessionListenerFactory.getSessionListener(), connectionTimeoutMillis); } } diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java index ee07b3949d..06239b2bfd 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java @@ -8,16 +8,13 @@ package org.opendaylight.controller.netconf.client; +import com.google.common.base.Optional; import io.netty.channel.ChannelHandler; import io.netty.channel.EventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.util.HashedWheelTimer; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.Promise; - -import java.io.IOException; -import java.net.InetSocketAddress; - import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.api.NetconfSession; import org.opendaylight.controller.netconf.api.NetconfTerminationReason; @@ -33,7 +30,8 @@ import org.opendaylight.protocol.framework.ReconnectStrategy; import org.opendaylight.protocol.framework.SessionListener; import org.opendaylight.protocol.framework.SessionListenerFactory; -import com.google.common.base.Optional; +import java.io.IOException; +import java.net.InetSocketAddress; public class NetconfSshClientDispatcher extends NetconfClientDispatcher { @@ -42,19 +40,19 @@ public class NetconfSshClientDispatcher extends NetconfClientDispatcher { private NetconfClientSessionNegotiatorFactory negotatorFactory; public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup, - EventLoopGroup workerGroup) { - super(bossGroup, workerGroup); + EventLoopGroup workerGroup, long connectionTimeoutMillis) { + super(bossGroup, workerGroup, connectionTimeoutMillis); this.authHandler = authHandler; this.timer = new HashedWheelTimer(); - this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.absent()); + this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.absent(), connectionTimeoutMillis); } public NetconfSshClientDispatcher(AuthenticationHandler authHandler, EventLoopGroup bossGroup, - EventLoopGroup workerGroup, String additionalHeader) { - super(bossGroup, workerGroup, additionalHeader); + EventLoopGroup workerGroup, String additionalHeader, long socketTimeoutMillis) { + super(bossGroup, workerGroup, additionalHeader, socketTimeoutMillis); this.authHandler = authHandler; this.timer = new HashedWheelTimer(); - this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader)); + this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader), socketTimeoutMillis); } public Future createClient(InetSocketAddress address, diff --git a/opendaylight/netconf/netconf-client/src/test/java/org/opendaylight/controller/netconf/client/SSHNetconfClientLiveTest.java b/opendaylight/netconf/netconf-client/src/test/java/org/opendaylight/controller/netconf/client/SSHNetconfClientLiveTest.java index 7f1d289c5b..a3cc10417e 100644 --- a/opendaylight/netconf/netconf-client/src/test/java/org/opendaylight/controller/netconf/client/SSHNetconfClientLiveTest.java +++ b/opendaylight/netconf/netconf-client/src/test/java/org/opendaylight/controller/netconf/client/SSHNetconfClientLiveTest.java @@ -29,7 +29,7 @@ public class SSHNetconfClientLiveTest { nettyThreadgroup = new NioEventLoopGroup(); netconfClientDispatcher = new NetconfSshClientDispatcher(new LoginPassword( System.getProperty("username"), System.getProperty("password")), - nettyThreadgroup, nettyThreadgroup); + nettyThreadgroup, nettyThreadgroup, 5000); } @Test diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiator.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiator.java index 8ba4cdc052..91555861dc 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiator.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiator.java @@ -8,12 +8,10 @@ package org.opendaylight.controller.netconf.impl; +import com.google.common.base.Optional; import io.netty.channel.Channel; import io.netty.util.Timer; import io.netty.util.concurrent.Promise; - -import java.net.InetSocketAddress; - import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences; import org.opendaylight.controller.netconf.impl.util.AdditionalHeaderUtil; @@ -22,7 +20,7 @@ import org.opendaylight.protocol.framework.SessionListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Optional; +import java.net.InetSocketAddress; public class NetconfServerSessionNegotiator extends AbstractNetconfSessionNegotiator { @@ -30,8 +28,9 @@ public class NetconfServerSessionNegotiator extends static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class); protected NetconfServerSessionNegotiator(NetconfServerSessionPreferences sessionPreferences, - Promise promise, Channel channel, Timer timer, SessionListener sessionListener) { - super(sessionPreferences, promise, channel, timer, sessionListener); + Promise promise, Channel channel, Timer timer, SessionListener sessionListener, + long connectionTimeoutMillis) { + super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis); } @Override diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java index 3c14d5124f..98462b8025 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java @@ -40,12 +40,14 @@ public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorF private static final Document helloMessageTemplate = loadHelloMessageTemplate(); private final SessionIdProvider idProvider; private final NetconfOperationServiceFactoryListener factoriesListener; + private final long connectionTimeoutMillis; public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener, - SessionIdProvider idProvider) { + SessionIdProvider idProvider, long connectionTimeoutMillis) { this.timer = timer; this.factoriesListener = factoriesListener; this.idProvider = idProvider; + this.connectionTimeoutMillis = connectionTimeoutMillis; } private static Document loadHelloMessageTemplate() { @@ -64,7 +66,7 @@ public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorF NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId), sessionId); return new NetconfServerSessionNegotiator(proposal, promise, channel, timer, - sessionListenerFactory.getSessionListener()); + sessionListenerFactory.getSessionListener(), connectionTimeoutMillis); } private static final XPathExpression sessionIdXPath = XMLNetconfUtil diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivator.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivator.java index abebacf974..95f7353600 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivator.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfImplActivator.java @@ -48,8 +48,9 @@ public class NetconfImplActivator implements BundleActivator { SessionIdProvider idProvider = new SessionIdProvider(); timer = new HashedWheelTimer(); + long connectionTimeoutMillis = NetconfConfigUtil.extractTimeoutMillis(context); NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - timer, factoriesListener, idProvider); + timer, factoriesListener, idProvider, connectionTimeoutMillis); commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java index 958ac42398..0ef2c285e4 100644 --- a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/ConcurrentClientsTest.java @@ -8,34 +8,14 @@ package org.opendaylight.controller.netconf.impl; -import static com.google.common.base.Preconditions.checkNotNull; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; +import com.google.common.base.Optional; +import com.google.common.collect.Sets; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.HashedWheelTimer; - -import java.io.DataOutputStream; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.management.ManagementFactory; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -import javax.management.ObjectName; - import org.apache.commons.io.IOUtils; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; @@ -63,15 +43,30 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; -import com.google.common.base.Optional; -import com.google.common.collect.Sets; +import javax.management.ObjectName; +import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; public class ConcurrentClientsTest { private static final int CONCURRENCY = 16; - private static EventLoopGroup nettyGroup = new NioEventLoopGroup(); - public static final NetconfClientDispatcher NETCONF_CLIENT_DISPATCHER = - new NetconfClientDispatcher( nettyGroup, nettyGroup); + private EventLoopGroup nettyGroup; + private NetconfClientDispatcher netconfClientDispatcher; @Mock private YangStoreService yangStoreService; @@ -88,6 +83,8 @@ public class ConcurrentClientsTest { @Mock private SessionMonitoringService monitoring; + HashedWheelTimer hashedWheelTimer; + @Before public void setUp() throws Exception { { // init mocks @@ -102,12 +99,16 @@ public class ConcurrentClientsTest { doReturn(Collections.emptySet()).when(jmxClient).lookupConfigBeans(); } + nettyGroup = new NioEventLoopGroup(); + netconfClientDispatcher = new NetconfClientDispatcher( nettyGroup, nettyGroup, 5000); + NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); factoriesListener.onAddNetconfOperationServiceFactory(mockOpF()); SessionIdProvider idProvider = new SessionIdProvider(); + hashedWheelTimer = new HashedWheelTimer(); NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider); + hashedWheelTimer, factoriesListener, idProvider, 5000); commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); @@ -123,8 +124,9 @@ public class ConcurrentClientsTest { s.await(); } - @AfterClass - public static void tearDownStatic() { + @After + public void tearDown(){ + hashedWheelTimer.stop(); nettyGroup.shutdownGracefully(); } @@ -285,7 +287,7 @@ public class ConcurrentClientsTest { @Override public void run() { try { - final NetconfClient netconfClient = new NetconfClient(clientId, netconfAddress, NETCONF_CLIENT_DISPATCHER); + final NetconfClient netconfClient = new NetconfClient(clientId, netconfAddress, netconfClientDispatcher); long sessionId = netconfClient.getSessionId(); logger.info("Client with sessionid {} hello exchanged", sessionId); diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfDispatcherImplTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfDispatcherImplTest.java index 0ecc1cb383..9835c2393b 100644 --- a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfDispatcherImplTest.java +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/NetconfDispatcherImplTest.java @@ -26,6 +26,7 @@ public class NetconfDispatcherImplTest { private EventLoopGroup nettyGroup; private NetconfServerDispatcher dispatch; private DefaultCommitNotificationProducer commitNot; + private HashedWheelTimer hashedWheelTimer; @Before public void setUp() throws Exception { @@ -36,8 +37,9 @@ public class NetconfDispatcherImplTest { NetconfOperationServiceFactoryListener factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); SessionIdProvider idProvider = new SessionIdProvider(); + hashedWheelTimer = new HashedWheelTimer(); NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(), factoriesListener, idProvider); + hashedWheelTimer, factoriesListener, idProvider, 5000); NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( factoriesListener, commitNot, idProvider, null); @@ -49,6 +51,7 @@ public class NetconfDispatcherImplTest { @After public void tearDown() throws Exception { + hashedWheelTimer.stop(); commitNot.close(); nettyGroup.shutdownGracefully(); } diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/AbstractNetconfConfigTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/AbstractNetconfConfigTest.java new file mode 100644 index 0000000000..b261218bf1 --- /dev/null +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/AbstractNetconfConfigTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.netconf.it; + +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.util.HashedWheelTimer; +import org.junit.After; +import org.junit.Before; +import org.opendaylight.controller.config.manager.impl.AbstractConfigTest; +import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; +import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; +import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory; +import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory; +import org.opendaylight.controller.netconf.impl.SessionIdProvider; +import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; +import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService; + +public class AbstractNetconfConfigTest extends AbstractConfigTest { + + protected EventLoopGroup nettyThreadgroup; + private HashedWheelTimer hashedWheelTimer; + + @Before + public void setUpAbstractNetconfConfigTest() { + nettyThreadgroup = new NioEventLoopGroup(); + hashedWheelTimer = new HashedWheelTimer(); + } + + + protected NetconfServerDispatcher createDispatcher( + NetconfOperationServiceFactoryListenerImpl factoriesListener, SessionMonitoringService sessionMonitoringService, + DefaultCommitNotificationProducer commitNotifier) { + SessionIdProvider idProvider = new SessionIdProvider(); + + NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( + hashedWheelTimer, factoriesListener, idProvider, 5000); + + NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( + factoriesListener, commitNotifier, idProvider, + sessionMonitoringService); + + NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer( + serverNegotiatorFactory, listenerFactory); + return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup); + } + + + @After + public void cleanUpTimer() { + hashedWheelTimer.stop(); + nettyThreadgroup.shutdownGracefully(); + } + +} diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java index 14f70d398c..8bfa0a5846 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java @@ -10,17 +10,14 @@ package org.opendaylight.controller.netconf.it; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import io.netty.channel.ChannelFuture; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; import org.apache.commons.lang3.StringUtils; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.matchers.JUnitMatchers; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import org.opendaylight.controller.config.manager.impl.AbstractConfigTest; import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import org.opendaylight.controller.config.persist.api.Persister; @@ -35,9 +32,6 @@ import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory; -import org.opendaylight.controller.netconf.impl.SessionIdProvider; import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; @@ -63,7 +57,6 @@ import java.net.InetSocketAddress; import java.util.Collection; import java.util.List; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import static junit.framework.Assert.assertEquals; @@ -74,16 +67,18 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -public class NetconfConfigPersisterITTest extends AbstractConfigTest { +public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest { private static final Logger logger = LoggerFactory.getLogger(NetconfConfigPersisterITTest.class); private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023); - private EventLoopGroup nettyThreadgroup; + private NetconfClientDispatcher clientDispatcher; + DefaultCommitNotificationProducer commitNotifier; + @Before public void setUp() throws Exception { super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(NetconfITTest.getModuleFactoriesS().toArray( @@ -97,13 +92,18 @@ public class NetconfConfigPersisterITTest extends AbstractConfigTest { .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory( new NetconfMonitoringOperationService(monitoringService))); - nettyThreadgroup = new NioEventLoopGroup(); - NetconfServerDispatcher dispatch = createDispatcher(factoriesListener); + commitNotifier = new DefaultCommitNotificationProducer(platformMBeanServer); + NetconfServerDispatcher dispatch = createDispatcher(factoriesListener, mockSessionMonitoringService(), commitNotifier); ChannelFuture s = dispatch.createServer(tcpAddress); s.await(); - clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup); + clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000); + } + + @After + public void cleanUp(){ + commitNotifier.close(); } private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException { @@ -111,27 +111,16 @@ public class NetconfConfigPersisterITTest extends AbstractConfigTest { return new HardcodedYangStoreService(yangDependencies); } - private NetconfServerDispatcher createDispatcher( - NetconfOperationServiceFactoryListenerImpl factoriesListener) { - SessionIdProvider idProvider = new SessionIdProvider(); - NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider); - - NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( - factoriesListener, new DefaultCommitNotificationProducer(platformMBeanServer), idProvider, mockSessionMonitoringService()); - NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer( - serverNegotiatorFactory, listenerFactory); - return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup); - } - - private SessionMonitoringService mockSessionMonitoringService() { + protected SessionMonitoringService mockSessionMonitoringService() { SessionMonitoringService mockedSessionMonitor = mock(SessionMonitoringService.class); doNothing().when(mockedSessionMonitor).onSessionUp(any(NetconfManagementSession.class)); doNothing().when(mockedSessionMonitor).onSessionDown(any(NetconfManagementSession.class)); return mockedSessionMonitor; } + + @Test public void testNetconfCommitNotifications() throws Exception { diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java index 793a756d54..44775fdf93 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java @@ -9,27 +9,9 @@ package org.opendaylight.controller.netconf.it; import io.netty.channel.ChannelFuture; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; -import java.io.IOException; -import java.io.InputStream; -import java.lang.management.ManagementFactory; -import java.net.InetSocketAddress; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.TimeUnit; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.config.manager.impl.AbstractConfigTest; import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver; import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.controller.config.yang.store.api.YangStoreException; @@ -39,18 +21,28 @@ import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory; -import org.opendaylight.controller.netconf.impl.SessionIdProvider; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; -public class NetconfITSecureTest extends AbstractConfigTest { +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import java.io.IOException; +import java.io.InputStream; +import java.lang.management.ManagementFactory; +import java.net.InetSocketAddress; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.Collection; +import java.util.List; + +public class NetconfITSecureTest extends AbstractNetconfConfigTest { private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024); private DefaultCommitNotificationProducer commitNot; private NetconfServerDispatcher dispatchS; - private EventLoopGroup nettyThreadgroup; @Before @@ -63,7 +55,6 @@ public class NetconfITSecureTest extends AbstractConfigTest { commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); - nettyThreadgroup = new NioEventLoopGroup(); dispatchS = createDispatcher(factoriesListener); ChannelFuture s = dispatchS.createServer(tlsAddress); @@ -71,22 +62,12 @@ public class NetconfITSecureTest extends AbstractConfigTest { } private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) { - SessionIdProvider idProvider = new SessionIdProvider(); - NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider); - - NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( - factoriesListener, commitNot, idProvider, NetconfITTest.getNetconfMonitoringListenerService()); - - NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer( - serverNegotiatorFactory, listenerFactory); - return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup); + return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot); } @After public void tearDown() throws Exception { commitNot.close(); - nettyThreadgroup.shutdownGracefully(); } private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, @@ -110,7 +91,7 @@ public class NetconfITSecureTest extends AbstractConfigTest { @Test public void testSecure() throws Exception { - NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup); + NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000); try (NetconfClient netconfClient = new NetconfClient("tls-client", tlsAddress, 4000, dispatch)) { } diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITTest.java index 5f4bcecff0..954da5f487 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITTest.java @@ -13,15 +13,11 @@ import ch.ethz.ssh2.Session; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import io.netty.channel.ChannelFuture; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.opendaylight.controller.config.manager.impl.AbstractConfigTest; import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver; import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.controller.config.util.ConfigTransactionJMXClient; @@ -38,9 +34,6 @@ import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory; -import org.opendaylight.controller.netconf.impl.SessionIdProvider; import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; @@ -71,7 +64,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Set; -import java.util.concurrent.TimeUnit; import static java.util.Collections.emptyList; import static junit.framework.Assert.assertEquals; @@ -81,7 +73,7 @@ import static org.mockito.Matchers.anyLong; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -public class NetconfITTest extends AbstractConfigTest { +public class NetconfITTest extends AbstractNetconfConfigTest { // TODO refactor, pull common code up to AbstractNetconfITTest @@ -96,7 +88,6 @@ public class NetconfITTest extends AbstractConfigTest { closeSession, startExi, stopExi; private DefaultCommitNotificationProducer commitNot; private NetconfServerDispatcher dispatch; - private EventLoopGroup nettyThreadgroup; private NetconfClientDispatcher clientDispatcher; @@ -110,7 +101,6 @@ public class NetconfITTest extends AbstractConfigTest { NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore())); - nettyThreadgroup = new NioEventLoopGroup(); commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); @@ -118,20 +108,11 @@ public class NetconfITTest extends AbstractConfigTest { ChannelFuture s = dispatch.createServer(tcpAddress); s.await(); - clientDispatcher = new NetconfClientDispatcher( nettyThreadgroup, nettyThreadgroup); + clientDispatcher = new NetconfClientDispatcher( nettyThreadgroup, nettyThreadgroup, 5000); } private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) { - SessionIdProvider idProvider = new SessionIdProvider(); - NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider); - - NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( - factoriesListener, commitNot, idProvider, getNetconfMonitoringListenerService()); - - NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer( - serverNegotiatorFactory, listenerFactory); - return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup); + return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(), commitNot); } static NetconfMonitoringServiceImpl getNetconfMonitoringListenerService() { @@ -145,7 +126,6 @@ public class NetconfITTest extends AbstractConfigTest { @After public void tearDown() throws Exception { commitNot.close(); - nettyThreadgroup.shutdownGracefully(); clientDispatcher.close(); } diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfMonitoringITTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfMonitoringITTest.java index b0821799b0..be2c32e34f 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfMonitoringITTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfMonitoringITTest.java @@ -11,15 +11,11 @@ import com.google.common.base.Charsets; import com.google.common.base.Optional; import com.google.common.collect.Sets; import io.netty.channel.ChannelFuture; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; -import io.netty.util.HashedWheelTimer; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; import org.junit.matchers.JUnitMatchers; import org.mockito.Mock; -import org.opendaylight.controller.config.manager.impl.AbstractConfigTest; import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver; import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.controller.config.yang.store.api.YangStoreException; @@ -31,9 +27,6 @@ import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory; -import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory; -import org.opendaylight.controller.netconf.impl.SessionIdProvider; import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener; import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; @@ -57,13 +50,12 @@ import java.net.Socket; import java.util.Collection; import java.util.List; import java.util.Set; -import java.util.concurrent.TimeUnit; import static org.mockito.Matchers.anyLong; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -public class NetconfMonitoringITTest extends AbstractConfigTest { +public class NetconfMonitoringITTest extends AbstractNetconfConfigTest { private static final Logger logger = LoggerFactory.getLogger(NetconfITTest.class); @@ -72,7 +64,6 @@ public class NetconfMonitoringITTest extends AbstractConfigTest { @Mock private DefaultCommitNotificationProducer commitNot; private NetconfServerDispatcher dispatch; - private EventLoopGroup nettyThreadgroup; private NetconfClientDispatcher clientDispatcher; @@ -91,13 +82,12 @@ public class NetconfMonitoringITTest extends AbstractConfigTest { .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory( new NetconfMonitoringOperationService(monitoringService))); - nettyThreadgroup = new NioEventLoopGroup(); dispatch = createDispatcher(factoriesListener); ChannelFuture s = dispatch.createServer(tcpAddress); s.await(); - clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup); + clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000); } private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException { @@ -107,16 +97,7 @@ public class NetconfMonitoringITTest extends AbstractConfigTest { private NetconfServerDispatcher createDispatcher( NetconfOperationServiceFactoryListenerImpl factoriesListener) { - SessionIdProvider idProvider = new SessionIdProvider(); - NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory( - new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider); - - NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory( - factoriesListener, commitNot, idProvider, getNetconfMonitoringListenerService(logger, monitoringService)); - - NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer( - serverNegotiatorFactory, listenerFactory); - return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup); + return super.createDispatcher(factoriesListener, getNetconfMonitoringListenerService(logger, monitoringService), commitNot); } static SessionMonitoringService getNetconfMonitoringListenerService(final Logger logger, final NetconfMonitoringServiceImpl monitor) { diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractNetconfSessionNegotiator.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractNetconfSessionNegotiator.java index 26ea7ceb79..8cfd177fce 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractNetconfSessionNegotiator.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractNetconfSessionNegotiator.java @@ -42,9 +42,6 @@ import java.util.concurrent.TimeUnit; public abstract class AbstractNetconfSessionNegotiator

extends AbstractSessionNegotiator { - // TODO Adjust wait time for negotiation, now is 1 minute ? - private static final long INITIAL_HOLDTIMER = 1; - private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfSessionNegotiator.class); public static final String NAME_OF_EXCEPTION_HANDLER = "lastExceptionHandler"; @@ -62,13 +59,15 @@ public abstract class AbstractNetconfSessionNegotiator

promise, Channel channel, Timer timer, - SessionListener sessionListener) { + SessionListener sessionListener, long connectionTimeoutMillis) { super(promise, channel); this.sessionPreferences = sessionPreferences; this.timer = timer; this.sessionListener = sessionListener; + this.connectionTimeoutMillis = connectionTimeoutMillis; } @Override @@ -120,6 +119,7 @@ public abstract class AbstractNetconfSessionNegotiator

inetSocketAddressOptional = extractSomeNetconfAddress(context, InfixProp.tcp, exceptionMessageIfNotFound, forClient); -- 2.36.6