--- /dev/null
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.netconf.client;
+
+import static java.util.Objects.requireNonNull;
+
+import io.netty.channel.Channel;
+import io.netty.util.concurrent.Promise;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
+
+/**
+ * Abstract base class for {@link NetconfClientSession} initializers.
+ */
+abstract class AbstractClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
+ private final @NonNull NetconfClientSessionNegotiatorFactory negotiatorFactory;
+ private final @NonNull NetconfClientSessionListener sessionListener;
+
+ AbstractClientChannelInitializer(final NetconfClientSessionNegotiatorFactory negotiatorFactory,
+ final NetconfClientSessionListener sessionListener) {
+ this.negotiatorFactory = requireNonNull(negotiatorFactory);
+ this.sessionListener = requireNonNull(sessionListener);
+ }
+
+ @Override
+ protected final void initializeSessionNegotiator(final Channel ch, final Promise<NetconfClientSession> promise) {
+ ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, NETCONF_SESSION_NEGOTIATOR,
+ negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
+ ch.config().setConnectTimeoutMillis((int) negotiatorFactory.getConnectionTimeoutMillis());
+ }
+}
import io.netty.channel.Channel;
import io.netty.util.concurrent.Promise;
import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
import org.opendaylight.netconf.nettyutil.handler.ssh.client.NetconfSshClient;
-final class SshClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
+final class SshClientChannelInitializer extends AbstractClientChannelInitializer {
private final AuthenticationHandler authenticationHandler;
- private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
- private final NetconfClientSessionListener sessionListener;
private final NetconfSshClient sshClient;
SshClientChannelInitializer(final AuthenticationHandler authHandler,
final NetconfClientSessionNegotiatorFactory negotiatorFactory,
final NetconfClientSessionListener sessionListener, @Nullable final NetconfSshClient sshClient) {
- this.authenticationHandler = authHandler;
- this.negotiatorFactory = negotiatorFactory;
- this.sessionListener = sessionListener;
+ super(negotiatorFactory, sessionListener);
+ authenticationHandler = authHandler;
this.sshClient = sshClient;
}
ch.pipeline().addFirst(AsyncSshHandler.createForNetconfSubsystem(authenticationHandler, promise, sshClient));
super.initialize(ch, promise);
}
-
- @Override
- protected void initializeSessionNegotiator(final Channel ch,
- final Promise<NetconfClientSession> promise) {
- ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
- negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
- ch.config().setConnectTimeoutMillis((int)negotiatorFactory.getConnectionTimeoutMillis());
- }
}
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import java.net.SocketAddress;
-import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
-
-class TcpClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
-
- private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
- private final NetconfClientSessionListener sessionListener;
+final class TcpClientChannelInitializer extends AbstractClientChannelInitializer {
TcpClientChannelInitializer(final NetconfClientSessionNegotiatorFactory negotiatorFactory,
- final NetconfClientSessionListener sessionListener) {
- this.negotiatorFactory = negotiatorFactory;
- this.sessionListener = sessionListener;
+ final NetconfClientSessionListener sessionListener) {
+ super(negotiatorFactory, sessionListener);
}
@Override
super.initialize(ch, promise);
}
-
- @Override
- protected void initializeSessionNegotiator(final Channel ch, final Promise<NetconfClientSession> promise) {
- ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
- negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
- }
}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.Promise;
-import org.opendaylight.netconf.nettyutil.AbstractChannelInitializer;
-public final class TlsClientChannelInitializer extends AbstractChannelInitializer<NetconfClientSession> {
+public final class TlsClientChannelInitializer extends AbstractClientChannelInitializer {
public static final String CHANNEL_ACTIVE_SENTRY = "channelActiveSentry";
private final SslHandlerFactory sslHandlerFactory;
- private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
- private final NetconfClientSessionListener sessionListener;
public TlsClientChannelInitializer(final SslHandlerFactory sslHandlerFactory,
final NetconfClientSessionNegotiatorFactory negotiatorFactory,
final NetconfClientSessionListener sessionListener) {
+ super(negotiatorFactory, sessionListener);
this.sslHandlerFactory = sslHandlerFactory;
- this.negotiatorFactory = negotiatorFactory;
- this.sessionListener = sessionListener;
}
@Override
- public void initialize(Channel ch, Promise<NetconfClientSession> promise) {
+ public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) {
// When ssl handshake fails due to the certificate mismatch, the connection will try again,
// then we have a chance to create a new SslHandler using the latest certificates with the
// help of the sentry. We will replace the sentry with the new SslHandler once the channel
super.initialize(ch, promise);
}
- @Override
- protected void initializeSessionNegotiator(Channel ch, Promise<NetconfClientSession> promise) {
- ch.pipeline().addAfter(NETCONF_MESSAGE_DECODER, AbstractChannelInitializer.NETCONF_SESSION_NEGOTIATOR,
- negotiatorFactory.getSessionNegotiator(() -> sessionListener, ch, promise));
- }
-
private static final class ChannelActiveSentry extends ChannelInboundHandlerAdapter {
private final SslHandlerFactory sslHandlerFactory;
}
@Override
- public void channelActive(ChannelHandlerContext ctx) {
- ctx.pipeline().replace(this, "sslHandler", sslHandlerFactory.createSslHandler())
- .fireChannelActive();
+ public void channelActive(final ChannelHandlerContext ctx) {
+ final var sslHandler = sslHandlerFactory.createSslHandler();
+ ctx.pipeline().replace(this, "sslHandler", sslHandler).fireChannelActive();
}
}
}
import static org.mockito.Mockito.verify;
import io.netty.channel.Channel;
+import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.util.concurrent.Promise;
doReturn(pipeline).when(channel).pipeline();
doReturn("").when(channel).toString();
+ ChannelConfig channelConfig = mock(ChannelConfig.class);
+ doReturn(channelConfig).when(channel).config();
+ doReturn(1L).when(factory).getConnectionTimeoutMillis();
+ doReturn(channelConfig).when(channelConfig).setConnectTimeoutMillis(1);
+
Promise<NetconfClientSession> promise = mock(Promise.class);
doReturn("").when(promise).toString();
import static org.mockito.Mockito.verify;
import io.netty.channel.Channel;
+import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import io.netty.util.concurrent.Promise;
doReturn(pipeline).when(pipeline).addFirst(anyString(), any(ChannelHandler.class));
doReturn(pipeline).when(pipeline).addLast(anyString(), any(ChannelHandler.class));
+ ChannelConfig channelConfig = mock(ChannelConfig.class);
+ doReturn(channelConfig).when(channel).config();
+ doReturn(1L).when(negotiatorFactory).getConnectionTimeoutMillis();
+ doReturn(channelConfig).when(channelConfig).setConnectTimeoutMillis(1);
+
Promise<NetconfClientSession> promise = mock(Promise.class);
TlsClientChannelInitializer initializer = new TlsClientChannelInitializer(sslHandlerFactory,