Bug 591: Minimize the use of Xtend in sal-binding-broker-impl
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSession.java
index 1ae3fabbb9a31bf5275b63f1bc27da15e8a72d88..9ddc64f1a16934b95c6a3bd69410eef554e4313b 100644 (file)
 package org.opendaylight.controller.netconf.impl;
 
 import io.netty.channel.Channel;
-import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.api.NetconfSession;
-import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.opendaylight.protocol.framework.SessionListener;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.opendaylight.controller.netconf.api.AbstractNetconfSession;
+import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
+import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
+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.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;
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.IOException;
-
-public class NetconfServerSession extends NetconfSession {
+import com.google.common.base.Preconditions;
 
-    private final SessionListener sessionListener;
-    private final Channel channel;
+public final class NetconfServerSession extends AbstractNetconfSession<NetconfServerSession, NetconfServerSessionListener> implements NetconfManagementSession {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfServerSession.class);
-    private final long sessionId;
-    private boolean up = false;
 
-    public NetconfServerSession(SessionListener sessionListener, Channel channel, long sessionId) {
-        this.sessionListener = sessionListener;
-        this.channel = channel;
-        this.sessionId = sessionId;
+    private final NetconfHelloMessageAdditionalHeader header;
+
+    private Date loginTime;
+    private long inRpcSuccess, inRpcFail, outRpcError;
+
+    public NetconfServerSession(NetconfServerSessionListener sessionListener, Channel channel, long sessionId,
+            NetconfHelloMessageAdditionalHeader header) {
+        super(sessionListener, channel, sessionId);
+        this.header = header;
         logger.debug("Session {} created", toString());
     }
 
     @Override
-    public void close() {
-        channel.close();
-        sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
+    protected void sessionUp() {
+        super.sessionUp();
+        Preconditions.checkState(loginTime == null, "Session is already up");
+        this.loginTime = new Date();
     }
 
-    @Override
-    protected void handleMessage(NetconfMessage netconfMessage) {
-        logger.debug("Session {} received message {}", toString(), XmlUtil.toString(netconfMessage.getDocument()));
-        sessionListener.onMessage(this, netconfMessage);
+    public void onIncommingRpcSuccess() {
+        inRpcSuccess++;
     }
 
-    public void sendMessage(NetconfMessage netconfMessage) {
-        channel.writeAndFlush(netconfMessage);
+    public void onIncommingRpcFail() {
+        inRpcFail++;
     }
 
-    @Override
-    protected void endOfInput() {
-        logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
-                : "initialized");
-        if (isUp()) {
-            this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
-        }
+    public void onOutgoingRpcError() {
+        outRpcError++;
     }
 
+    public static final String ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
+
+    private static final String dateTimePatternString = DateAndTime.PATTERN_CONSTANTS.get(0);
+    private static final Pattern dateTimePattern = Pattern.compile(dateTimePatternString);
+
     @Override
-    protected void sessionUp() {
-        logger.debug("Session {} up", toString());
-        sessionListener.onSessionUp(this);
-        this.up = true;
+    public Session toManagementSession() {
+        SessionBuilder builder = new SessionBuilder();
+
+        builder.setSessionId(getSessionId());
+        builder.setSourceHost(new Host(new DomainName(header.getAddress())));
+
+        Preconditions.checkState(DateAndTime.PATTERN_CONSTANTS.size() == 1);
+        String formattedDateTime = formatDateTime(loginTime);
+
+        Matcher matcher = dateTimePattern.matcher(formattedDateTime);
+        Preconditions.checkState(matcher.matches(), "Formatted datetime %s does not match pattern %s", formattedDateTime, dateTimePattern);
+        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(0L));
+
+        builder.setKey(new SessionKey(getSessionId()));
+
+        Session1Builder builder1 = new Session1Builder();
+        builder1.setSessionIdentifier(header.getSessionIdentifier());
+        builder.addAugmentation(Session1.class, builder1.build());
+
+        return builder.build();
     }
 
-    @Override
-    public String toString() {
-        final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
-        sb.append("sessionId=").append(sessionId);
-        sb.append('}');
-        return sb.toString();
+    private Class<? extends Transport> getTransportForString(String transport) {
+        switch(transport) {
+        case "ssh" : return NetconfSsh.class;
+        case "tcp" : return NetconfTcp.class;
+        default: throw new IllegalArgumentException("Unknown transport type " + transport);
+        }
     }
 
-    public boolean isUp() {
-        return up;
+    private String formatDateTime(Date loginTime) {
+        SimpleDateFormat dateFormat = new SimpleDateFormat(ISO_DATE_FORMAT);
+        return dateFormat.format(loginTime);
     }
 
-    public long getSessionId() {
-        return sessionId;
+    @Override
+    protected NetconfServerSession thisInstance() {
+        return this;
     }
 }