Bump mdsal to 5.0.2
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / netconf / impl / NetconfServerSession.java
index 16e9285d704e04b3d6a481fc70615a2d2029ba27..97729f99ad8fe7c4177bad17d7628527bf24d049 100644 (file)
@@ -9,11 +9,13 @@
 package org.opendaylight.netconf.impl;
 
 import com.google.common.base.Preconditions;
+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;
+import java.net.InetAddress;
 import java.time.Instant;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
@@ -26,8 +28,11 @@ import org.opendaylight.netconf.api.monitoring.NetconfManagementSession;
 import org.opendaylight.netconf.nettyutil.AbstractNetconfSession;
 import org.opendaylight.netconf.nettyutil.handler.NetconfMessageToXMLEncoder;
 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToMessageDecoder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Host;
+import org.opendaylight.netconf.notifications.NetconfNotification;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+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;
@@ -36,26 +41,35 @@ 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.Session;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.SessionBuilder;
 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.rev100924.DateAndTime;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.ZeroBasedCounter32;
+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;
 
-public final class NetconfServerSession extends AbstractNetconfSession<NetconfServerSession, NetconfServerSessionListener> implements NetconfManagementSession {
+public final class NetconfServerSession extends AbstractNetconfSession<NetconfServerSession,
+        NetconfServerSessionListener> implements NetconfManagementSession {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSession.class);
-    private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
+    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 Pattern DATE_TIME_PATTERN = Pattern.compile(DATE_TIME_PATTERN_STRING);
 
     private final NetconfHelloMessageAdditionalHeader header;
+    private final NetconfServerSessionListener sessionListener;
 
     private ZonedDateTime loginTime;
-    private long inRpcSuccess, inRpcFail, outRpcError;
+    private long inRpcSuccess;
+    private long inRpcFail;
+    private long outRpcError;
+    private long outNotification;
     private volatile boolean delayedClose;
 
-    public NetconfServerSession(final NetconfServerSessionListener sessionListener, final Channel channel, final long sessionId,
-            final NetconfHelloMessageAdditionalHeader header) {
+    public NetconfServerSession(final NetconfServerSessionListener sessionListener, final Channel channel,
+                                final long sessionId, final NetconfHelloMessageAdditionalHeader header) {
         super(sessionListener, channel, sessionId);
         this.header = header;
+        this.sessionListener = sessionListener;
         LOG.debug("Session {} created", toString());
     }
 
@@ -77,14 +91,13 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
     @Override
     public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
         final ChannelFuture channelFuture = super.sendMessage(netconfMessage);
+        if (netconfMessage instanceof NetconfNotification) {
+            outNotification++;
+            sessionListener.onNotification(this, (NetconfNotification) netconfMessage);
+        }
         // 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();
-                }
-            });
+        if (delayedClose) {
+            channelFuture.addListener(future -> close());
         }
         return channelFuture;
     }
@@ -101,21 +114,26 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
         outRpcError++;
     }
 
-    private static final String dateTimePatternString = DateAndTime.PATTERN_CONSTANTS.get(0);
-    private static final Pattern dateTimePattern = Pattern.compile(dateTimePatternString);
-
     @Override
     public Session toManagementSession() {
         SessionBuilder builder = new SessionBuilder();
 
         builder.setSessionId(getSessionId());
-        builder.setSourceHost(new Host(new DomainName(header.getAddress())));
+        IpAddress address;
+        InetAddress address1 = InetAddresses.forString(header.getAddress());
+        if (address1 instanceof Inet4Address) {
+            address = new IpAddress(new Ipv4Address(header.getAddress()));
+        } else {
+            address = new IpAddress(new Ipv6Address(header.getAddress()));
+        }
+        builder.setSourceHost(new Host(address));
 
         Preconditions.checkState(DateAndTime.PATTERN_CONSTANTS.size() == 1);
-        String formattedDateTime = dateFormatter.format(loginTime);
+        String formattedDateTime = DATE_FORMATTER.format(loginTime);
 
-        Matcher matcher = dateTimePattern.matcher(formattedDateTime);
-        Preconditions.checkState(matcher.matches(), "Formatted datetime %s does not match pattern %s", formattedDateTime, dateTimePattern);
+        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));
@@ -125,9 +143,9 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
         builder.setUsername(header.getUserName());
         builder.setTransport(getTransportForString(header.getTransport()));
 
-        builder.setOutNotifications(new ZeroBasedCounter32(0L));
+        builder.setOutNotifications(new ZeroBasedCounter32(outNotification));
 
-        builder.setKey(new SessionKey(getSessionId()));
+        builder.withKey(new SessionKey(Uint32.valueOf(getSessionId())));
 
         Session1Builder builder1 = new Session1Builder();
         builder1.setSessionIdentifier(header.getSessionIdentifier());
@@ -137,13 +155,13 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
     }
 
     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);
+        switch (transport) {
+            case "ssh":
+                return NetconfSsh.class;
+            case "tcp":
+                return NetconfTcp.class;
+            default:
+                throw new IllegalArgumentException("Unknown transport type " + transport);
         }
     }
 
@@ -153,7 +171,8 @@ public final class NetconfServerSession extends AbstractNetconfSession<NetconfSe
     }
 
     @Override
-    protected void addExiHandlers(final ByteToMessageDecoder decoder, final MessageToByteEncoder<NetconfMessage> encoder) {
+    protected void addExiHandlers(final ByteToMessageDecoder decoder,
+                                  final MessageToByteEncoder<NetconfMessage> encoder) {
         replaceMessageDecoder(decoder);
         replaceMessageEncoderAfterNextMessage(encoder);
     }