Merge "Bug 509: Improve logging in InMemoryDataStore."
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSession.java
index b6921794296e3f04d02d6e36ef45762a8888ecec..280375d918fc487f98b29777a276fbdef9123cec 100644 (file)
@@ -8,13 +8,24 @@
 
 package org.opendaylight.controller.netconf.impl;
 
-import com.google.common.base.Preconditions;
-import io.netty.channel.Channel;
-import org.opendaylight.controller.netconf.api.NetconfSession;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
-import org.opendaylight.protocol.framework.SessionListener;
+import org.opendaylight.controller.netconf.util.AbstractNetconfSession;
+import org.opendaylight.controller.netconf.util.handler.NetconfEXICodec;
+import org.opendaylight.controller.netconf.util.handler.NetconfEXIToMessageDecoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfMessageToEXIEncoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
+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;
@@ -25,22 +36,20 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import com.google.common.base.Preconditions;
+import io.netty.channel.Channel;
 
-public class NetconfServerSession extends NetconfSession implements NetconfManagementSession {
+public final class NetconfServerSession extends AbstractNetconfSession<NetconfServerSession, NetconfServerSessionListener> implements NetconfManagementSession {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfServerSession.class);
 
-    private final NetconfServerSessionNegotiator.AdditionalHeader header;
+    private final NetconfHelloMessageAdditionalHeader header;
 
     private Date loginTime;
     private long inRpcSuccess, inRpcFail, outRpcError;
 
-    public NetconfServerSession(SessionListener sessionListener, Channel channel, long sessionId,
-            NetconfServerSessionNegotiator.AdditionalHeader header) {
+    public NetconfServerSession(NetconfServerSessionListener sessionListener, Channel channel, long sessionId,
+            NetconfHelloMessageAdditionalHeader header) {
         super(sessionListener, channel, sessionId);
         this.header = header;
         logger.debug("Session {} created", toString());
@@ -67,6 +76,9 @@ public class NetconfServerSession extends NetconfSession implements NetconfManag
 
     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
     public Session toManagementSession() {
         SessionBuilder builder = new SessionBuilder();
@@ -76,30 +88,34 @@ public class NetconfServerSession extends NetconfSession implements NetconfManag
 
         Preconditions.checkState(DateAndTime.PATTERN_CONSTANTS.size() == 1);
         String formattedDateTime = formatDateTime(loginTime);
-        String pattern = DateAndTime.PATTERN_CONSTANTS.get(0);
-        Matcher matcher = Pattern.compile(pattern).matcher(formattedDateTime);
-        Preconditions.checkState(matcher.matches(), "Formatted datetime %s does not match pattern %s", formattedDateTime, pattern);
+
+        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.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();
     }
 
     private Class<? extends Transport> getTransportForString(String transport) {
         switch(transport) {
-            case "ssh" : return NetconfSsh.class;
-            // TODO what about tcp
-            case "tcp" : return NetconfSsh.class;
-            default: throw new IllegalArgumentException("Unknown transport type " + transport);
+        case "ssh" : return NetconfSsh.class;
+        case "tcp" : return NetconfTcp.class;
+        default: throw new IllegalArgumentException("Unknown transport type " + transport);
         }
     }
 
@@ -108,4 +124,20 @@ public class NetconfServerSession extends NetconfSession implements NetconfManag
         return dateFormat.format(loginTime);
     }
 
+    @Override
+    protected NetconfServerSession thisInstance() {
+        return this;
+    }
+
+    @Override
+    protected void addExiHandlers(NetconfEXICodec exiCodec) {
+        replaceMessageDecoder(new NetconfEXIToMessageDecoder(exiCodec));
+        replaceMessageEncoderAfterNextMessage(new NetconfMessageToEXIEncoder(exiCodec));
+    }
+
+    @Override
+    public void stopExiCommunication() {
+        replaceMessageDecoder(new NetconfXMLToMessageDecoder());
+        replaceMessageEncoderAfterNextMessage(new NetconfMessageToXMLEncoder());
+    }
 }