Use instanceof pattern in netconf-impl
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerSession.java
index dc314c05e0fe49a198ce33cce94925faa305af44..a9cb3eb4f773321d2180581ae2ceeb1280b7fab1 100644 (file)
@@ -5,14 +5,14 @@
  * 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.Preconditions;
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verify;
+
 import com.google.common.net.InetAddresses;
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelFuture;
-import io.netty.channel.ChannelFutureListener;
 import io.netty.handler.codec.ByteToMessageDecoder;
 import io.netty.handler.codec.MessageToByteEncoder;
 import java.net.Inet4Address;
@@ -21,7 +21,6 @@ import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
@@ -35,7 +34,6 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.NetconfTcp;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.Session1;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.extension.rev131210.Session1Builder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfSsh;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Transport;
@@ -44,6 +42,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.mon
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionKey;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.ZeroBasedCounter32;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -52,7 +51,14 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSession.class);
     private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
-    private static final String DATE_TIME_PATTERN_STRING = DateAndTime.PATTERN_CONSTANTS.get(0);
+
+    private static final String DATE_TIME_PATTERN_STRING;
+
+    static {
+        verify(DateAndTime.PATTERN_CONSTANTS.size() == 1);
+        DATE_TIME_PATTERN_STRING = DateAndTime.PATTERN_CONSTANTS.get(0);
+    }
+
     private static final Pattern DATE_TIME_PATTERN = Pattern.compile(DATE_TIME_PATTERN_STRING);
 
     private final NetconfHelloMessageAdditionalHeader header;
@@ -70,13 +76,13 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
         super(sessionListener, channel, sessionId);
         this.header = header;
         this.sessionListener = sessionListener;
-        LOG.debug("Session {} created", toString());
+        LOG.debug("Session {} created", this);
     }
 
     @Override
     protected void sessionUp() {
-        Preconditions.checkState(loginTime == null, "Session is already up");
-        this.loginTime = Instant.now().atZone(ZoneId.systemDefault());
+        checkState(loginTime == null, "Session is already up");
+        loginTime = Instant.now().atZone(ZoneId.systemDefault());
         super.sessionUp();
     }
 
@@ -85,24 +91,19 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
      * Suitable for close rpc that needs to send ok response before the session is closed.
      */
     public void delayedClose() {
-        this.delayedClose = true;
+        delayedClose = true;
     }
 
     @Override
     public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
         final ChannelFuture channelFuture = super.sendMessage(netconfMessage);
-        if (netconfMessage instanceof NetconfNotification) {
+        if (netconfMessage instanceof NetconfNotification notification) {
             outNotification++;
-            sessionListener.onNotification(this, (NetconfNotification) netconfMessage);
+            sessionListener.onNotification(this, notification);
         }
         // delayed close was set, close after the message was sent
         if (delayedClose) {
-            channelFuture.addListener(new ChannelFutureListener() {
-                @Override
-                public void operationComplete(final ChannelFuture future) throws Exception {
-                    close();
-                }
-            });
+            channelFuture.addListener(future -> close());
         }
         return channelFuture;
     }
@@ -121,11 +122,10 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
 
     @Override
     public Session toManagementSession() {
-        SessionBuilder builder = new SessionBuilder();
-
-        builder.setSessionId(getSessionId());
-        IpAddress address;
-        InetAddress address1 = InetAddresses.forString(header.getAddress());
+        final SessionBuilder builder = new SessionBuilder()
+                .withKey(new SessionKey(Uint32.valueOf(getSessionId())));
+        final InetAddress address1 = InetAddresses.forString(header.getAddress());
+        final IpAddress address;
         if (address1 instanceof Inet4Address) {
             address = new IpAddress(new Ipv4Address(header.getAddress()));
         } else {
@@ -133,41 +133,28 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
         }
         builder.setSourceHost(new Host(address));
 
-        Preconditions.checkState(DateAndTime.PATTERN_CONSTANTS.size() == 1);
-        String formattedDateTime = DATE_FORMATTER.format(loginTime);
-
-        Matcher matcher = DATE_TIME_PATTERN.matcher(formattedDateTime);
-        Preconditions.checkState(matcher.matches(), "Formatted datetime %s does not match pattern %s",
-                formattedDateTime, DATE_TIME_PATTERN);
-        builder.setLoginTime(new DateAndTime(formattedDateTime));
-
-        builder.setInBadRpcs(new ZeroBasedCounter32(inRpcFail));
-        builder.setInRpcs(new ZeroBasedCounter32(inRpcSuccess));
-        builder.setOutRpcErrors(new ZeroBasedCounter32(outRpcError));
-
-        builder.setUsername(header.getUserName());
-        builder.setTransport(getTransportForString(header.getTransport()));
-
-        builder.setOutNotifications(new ZeroBasedCounter32(outNotification));
-
-        builder.setKey(new SessionKey(getSessionId()));
-
-        Session1Builder builder1 = new Session1Builder();
-        builder1.setSessionIdentifier(header.getSessionIdentifier());
-        builder.addAugmentation(Session1.class, builder1.build());
-
-        return builder.build();
+        final String formattedDateTime = DATE_FORMATTER.format(loginTime);
+        checkState(DATE_TIME_PATTERN.matcher(formattedDateTime).matches(),
+            "Formatted datetime %s does not match pattern %s", formattedDateTime, DATE_TIME_PATTERN);
+
+        return builder
+                .setLoginTime(new DateAndTime(formattedDateTime))
+                .setInBadRpcs(new ZeroBasedCounter32(Uint32.valueOf(inRpcFail)))
+                .setInRpcs(new ZeroBasedCounter32(Uint32.valueOf(inRpcSuccess)))
+                .setOutRpcErrors(new ZeroBasedCounter32(Uint32.valueOf(outRpcError)))
+                .setUsername(header.getUserName())
+                .setTransport(getTransportForString(header.getTransport()))
+                .setOutNotifications(new ZeroBasedCounter32(Uint32.valueOf(outNotification)))
+                .addAugmentation(new Session1Builder().setSessionIdentifier(header.getSessionIdentifier()).build())
+                .build();
     }
 
-    private static Class<? extends Transport> getTransportForString(final String transport) {
-        switch (transport) {
-            case "ssh":
-                return NetconfSsh.class;
-            case "tcp":
-                return NetconfTcp.class;
-            default:
-                throw new IllegalArgumentException("Unknown transport type " + transport);
-        }
+    private static Transport getTransportForString(final String transport) {
+        return switch (transport) {
+            case "ssh" -> NetconfSsh.VALUE;
+            case "tcp" -> NetconfTcp.VALUE;
+            default -> throw new IllegalArgumentException("Unknown transport type " + transport);
+        };
     }
 
     @Override