From: Robert Varga Date: Wed, 1 Jun 2022 18:01:31 +0000 (+0200) Subject: Clean up NetconfServerSessionNegotiator X-Git-Tag: v4.0.0~55 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;ds=sidebyside;h=refs%2Fchanges%2F17%2F101417%2F1;p=netconf.git Clean up NetconfServerSessionNegotiator We have quite outdated and verbose formatting, adjust to JDK11 capabilities. Change-Id: Iefa1ed5b7e86f9cc1bb97a564cd36dfbe89d1bdf Signed-off-by: Robert Varga --- diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionNegotiator.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionNegotiator.java index 1a975fc79f..b1f60731a1 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionNegotiator.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionNegotiator.java @@ -14,9 +14,8 @@ import io.netty.util.Timer; import io.netty.util.concurrent.Promise; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.util.AbstractMap; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map; -import java.util.Optional; import org.opendaylight.netconf.api.NetconfDocumentedException; import org.opendaylight.netconf.api.NetconfServerSessionPreferences; import org.opendaylight.netconf.api.messages.NetconfHelloMessage; @@ -25,26 +24,20 @@ import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class NetconfServerSessionNegotiator +public final class NetconfServerSessionNegotiator extends AbstractNetconfSessionNegotiator { - private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class); - private static final String UNKNOWN = "unknown"; - protected NetconfServerSessionNegotiator( - NetconfServerSessionPreferences sessionPreferences, - Promise promise, Channel channel, - Timer timer, NetconfServerSessionListener sessionListener, - long connectionTimeoutMillis) { - super(sessionPreferences, promise, channel, timer, sessionListener, - connectionTimeoutMillis); + protected NetconfServerSessionNegotiator(final NetconfServerSessionPreferences sessionPreferences, + final Promise promise, final Channel channel, final Timer timer, + final NetconfServerSessionListener sessionListener, final long connectionTimeoutMillis) { + super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis); } @Override - protected void handleMessage(NetconfHelloMessage netconfMessage) - throws NetconfDocumentedException { + protected void handleMessage(final NetconfHelloMessage netconfMessage) throws NetconfDocumentedException { NetconfServerSession session = getSessionForHelloMessage(netconfMessage); replaceHelloMessageInboundHandler(session); // Negotiation successful after all non hello messages were processed @@ -52,30 +45,19 @@ public class NetconfServerSessionNegotiator } @Override - @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE") - protected NetconfServerSession getSession( - NetconfServerSessionListener sessionListener, Channel channel, - NetconfHelloMessage message) { - Optional additionalHeader = message - .getAdditionalHeader(); - - NetconfHelloMessageAdditionalHeader parsedHeader; - if (additionalHeader.isPresent()) { - parsedHeader = additionalHeader.get(); - } else { - - parsedHeader = new NetconfHelloMessageAdditionalHeader(UNKNOWN, - getHostName(channel.localAddress()).getValue(), - getHostName(channel.localAddress()).getKey(), "tcp", - "client"); - - } - - LOG.debug("Additional header from hello parsed as {} from {}", - parsedHeader, additionalHeader); - - return new NetconfServerSession(sessionListener, channel, - getSessionPreferences().getSessionId(), parsedHeader); + @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", + justification = "SpotBugs does not grok generic return of getSessionPreferences()") + protected NetconfServerSession getSession(final NetconfServerSessionListener sessionListener, final Channel channel, + final NetconfHelloMessage message) { + final var additionalHeader = message.getAdditionalHeader(); + final var parsedHeader = additionalHeader.orElseGet(() -> { + final var hostName = getHostName(channel.localAddress()); + return new NetconfHelloMessageAdditionalHeader(UNKNOWN, hostName.getValue(), hostName.getKey(), "tcp", + "client"); + }); + + LOG.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader); + return new NetconfServerSession(sessionListener, channel, getSessionPreferences().getSessionId(), parsedHeader); } /** @@ -85,25 +67,15 @@ public class NetconfServerSessionNegotiator * InetSocketAddress, for others returns unknown * @return Two values - port and host of socket address */ - protected static Map.Entry getHostName( - SocketAddress socketAddress) { - + protected static Map.Entry getHostName(final SocketAddress socketAddress) { if (socketAddress instanceof InetSocketAddress) { - - InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; - - return new AbstractMap.SimpleImmutableEntry<>( - Integer.toString(inetSocketAddress.getPort()), + final var inetSocketAddress = (InetSocketAddress) socketAddress; + return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()), inetSocketAddress.getHostString()); - } else if (socketAddress instanceof LocalAddress) { - - return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, - ((LocalAddress) socketAddress).id()); - + return new SimpleImmutableEntry<>(UNKNOWN, ((LocalAddress) socketAddress).id()); + } else { + return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN); } - return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN); - } - }