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=4af12fe78dad387b68207121602530bab13cc2bc;hb=7a5d09a57cedf0ad1259b72dc918f5925e248683;hp=e3fd704663afa7a3d591dee80a3b6b2a8bc14aaf;hpb=53646a063c2ec4251bb4f9d02a4ff488956a0dcc;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 e3fd704663..4af12fe78d 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,56 +45,35 @@ 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); } /** - * @param socketAddress - * type of socket address LocalAddress, or - * InetSocketAddress, for others returns unknown + * Get a name of the host. + * + * @param socketAddress type of socket address LocalAddress, or + * 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); - } - }