From: Robert Varga Date: Sun, 25 Feb 2024 10:06:22 +0000 (+0100) Subject: Use a switch expression in createClient() X-Git-Tag: v7.0.0~16 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=02e8961f3c112efd08b27022797afa67245225fc;p=netconf.git Use a switch expression in createClient() Using a switch expression ensures we are handling each case and makes it clear we are not doing anything with the returned future. JIRA: NETCONF-590 Change-Id: I534bdee682d86abc8cb93787ef52183cb2b57a42 Signed-off-by: Robert Varga --- diff --git a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java index 0ad6ae59fe..73244e8011 100644 --- a/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java +++ b/protocol/netconf-client/src/main/java/org/opendaylight/netconf/client/NetconfClientFactoryImpl.java @@ -8,9 +8,6 @@ package org.opendaylight.netconf.client; import static java.util.Objects.requireNonNull; -import static org.opendaylight.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol.SSH; -import static org.opendaylight.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol.TCP; -import static org.opendaylight.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol.TLS; import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.ListenableFuture; @@ -33,10 +30,14 @@ import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Singleton @Component(service = NetconfClientFactory.class) public final class NetconfClientFactoryImpl implements NetconfClientFactory { + private static final Logger LOG = LoggerFactory.getLogger(NetconfClientFactoryImpl.class); + private final SSHTransportStackFactory factory; private final NetconfTimer timer; @@ -62,28 +63,28 @@ public final class NetconfClientFactoryImpl implements NetconfClientFactory { @Override public ListenableFuture createClient(final NetconfClientConfiguration configuration) throws UnsupportedConfigurationException { - final var protocol = configuration.getProtocol(); final var future = SettableFuture.create(); final var channelInitializer = new ClientChannelInitializer(createNegotiatorFactory(configuration), () -> configuration.getSessionListener()); - final var bootstrap = factory.newBootstrap(); - if (TCP.equals(protocol)) { - TCPClient.connect(new ClientTransportChannelListener(future, channelInitializer), bootstrap, - configuration.getTcpParameters()); - } else if (TLS.equals(protocol)) { - if (configuration.getTlsParameters() != null) { - TLSClient.connect(new ClientTransportChannelListener(future, channelInitializer), bootstrap, - configuration.getTcpParameters(), new FixedSslHandlerFactory(configuration.getTlsParameters())); - } else { - TLSClient.connect(new ClientTransportChannelListener(future, channelInitializer), bootstrap, - configuration.getTcpParameters(), configuration.getSslHandlerFactory()); - } - } else if (SSH.equals(protocol)) { - factory.connectClient(TransportConstants.SSH_SUBSYSTEM, + // FIXME: do not ignore this future + final var stackFuture = switch (configuration.getProtocol()) { + case SSH -> factory.connectClient(TransportConstants.SSH_SUBSYSTEM, new ClientTransportChannelListener(future, channelInitializer), configuration.getTcpParameters(), configuration.getSshParameters(), configuration.getSshConfigurator()); - } + case TCP -> TCPClient.connect(new ClientTransportChannelListener(future, channelInitializer), + factory.newBootstrap(), configuration.getTcpParameters()); + case TLS -> { + var handlerFactory = configuration.getSslHandlerFactory(); + if (handlerFactory == null) { + handlerFactory = new FixedSslHandlerFactory(configuration.getTlsParameters()); + } + yield TLSClient.connect(new ClientTransportChannelListener(future, channelInitializer), + factory.newBootstrap(), configuration.getTcpParameters(), handlerFactory); + } + }; + LOG.trace("Future stack is {}", stackFuture); + return future; }