X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fimpl%2FNetconfServerSessionNegotiator.java;h=75ca014aac9877666ed21e478b21ee3241711bd2;hb=a33c5a6dce59e7fde138a873500861b06116b3bc;hp=b3e263a92beadc5f17597abc0eb46340556f3ff2;hpb=20e9c932c289ba5aa0e0949a47b7a02a7c56eb77;p=netconf.git 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 b3e263a92b..75ca014aac 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 @@ -5,46 +5,39 @@ * 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.impl; -import com.google.common.base.Optional; import io.netty.channel.Channel; import io.netty.channel.local.LocalAddress; 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 org.opendaylight.netconf.api.NetconfDocumentedException; -import org.opendaylight.netconf.api.NetconfServerSessionPreferences; import org.opendaylight.netconf.api.messages.NetconfHelloMessage; import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader; import org.opendaylight.netconf.nettyutil.AbstractNetconfSessionNegotiator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class NetconfServerSessionNegotiator - extends AbstractNetconfSessionNegotiator { - +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); + private final long sessionId; + + protected NetconfServerSessionNegotiator(final NetconfHelloMessage hello, final long sessionId, + final Promise promise, final Channel channel, final Timer timer, + final NetconfServerSessionListener sessionListener, final long connectionTimeoutMillis) { + super(hello, promise, channel, timer, sessionListener, connectionTimeoutMillis); + this.sessionId = sessionId; } @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,29 +45,17 @@ public class NetconfServerSessionNegotiator } @Override - 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); + 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, sessionId, parsedHeader); } /** @@ -84,25 +65,14 @@ public class NetconfServerSessionNegotiator * InetSocketAddress, for others returns unknown * @return Two values - port and host of socket address */ - protected static Map.Entry getHostName( - SocketAddress socketAddress) { - - if (socketAddress instanceof InetSocketAddress) { - - InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress; - - return new AbstractMap.SimpleImmutableEntry<>( - Integer.toString(inetSocketAddress.getPort()), + protected static Map.Entry getHostName(final SocketAddress socketAddress) { + if (socketAddress instanceof InetSocketAddress inetSocketAddress) { + return new SimpleImmutableEntry<>(Integer.toString(inetSocketAddress.getPort()), inetSocketAddress.getHostString()); - - } else if (socketAddress instanceof LocalAddress) { - - return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, - ((LocalAddress) socketAddress).id()); - + } else if (socketAddress instanceof LocalAddress localAddress) { + return new SimpleImmutableEntry<>(UNKNOWN, localAddress.id()); + } else { + return new SimpleImmutableEntry<>(UNKNOWN, UNKNOWN); } - return new AbstractMap.SimpleImmutableEntry<>(UNKNOWN, UNKNOWN); - } - }