Use instanceof pattern in netconf-impl
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerSessionNegotiator.java
index b3e263a92beadc5f17597abc0eb46340556f3ff2..75ca014aac9877666ed21e478b21ee3241711bd2 100644 (file)
@@ -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<NetconfServerSessionPreferences, NetconfServerSession,
-                NetconfServerSessionListener> {
-
+public final class NetconfServerSessionNegotiator
+        extends AbstractNetconfSessionNegotiator<NetconfServerSession, NetconfServerSessionListener> {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiator.class);
-
     private static final String UNKNOWN = "unknown";
 
-    protected NetconfServerSessionNegotiator(
-            NetconfServerSessionPreferences sessionPreferences,
-            Promise<NetconfServerSession> 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<NetconfServerSession> 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<NetconfHelloMessageAdditionalHeader> 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<String, String> getHostName(
-            SocketAddress socketAddress) {
-
-        if (socketAddress instanceof InetSocketAddress) {
-
-            InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
-
-            return new AbstractMap.SimpleImmutableEntry<>(
-                    Integer.toString(inetSocketAddress.getPort()),
+    protected static Map.Entry<String, String> 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);
-
     }
-
 }