[ONOS-2794] Enable sending emails from main.stop()

    -Read email configuration from teston.cfg file
    -Make smtp parameters configurable

Change-Id: I7d8b412251a1a6f3e1a5eed39d0d9cd6937330d1
diff --git a/TestON/config/teston.cfg b/TestON/config/teston.cfg
index b255f45..aba93d1 100644
--- a/TestON/config/teston.cfg
+++ b/TestON/config/teston.cfg
@@ -5,6 +5,9 @@
         <class>xmlparser</class>
     </parser>
     <mail_to>hari@onlab.us</mail_to>
+    <mail_from></mail_from>
+    <mail_pass></mail_pass>
+    <mail_server></mail_server>
 
     <logger>
         <file>core/logger.py</file>
diff --git a/TestON/core/logger.py b/TestON/core/logger.py
index c65fa49..05693ba 100644
--- a/TestON/core/logger.py
+++ b/TestON/core/logger.py
@@ -120,6 +120,7 @@
         main.WikiFileName = main.logdir + "/" + main.TEST + "Wiki.txt"
         main.SummaryFileName = main.logdir + "/" + main.TEST + "Summary.txt"
         main.JenkinsCSV = main.logdir + "/" + main.TEST + ".csv"
+        main.TOTAL_TC_SUCCESS = 0
 
         #### Add log-level - Report
         logging.addLevelName(9, "REPORT")
diff --git a/TestON/core/teston.py b/TestON/core/teston.py
index bc23be0..4bbdc3c 100644
--- a/TestON/core/teston.py
+++ b/TestON/core/teston.py
@@ -97,7 +97,7 @@
         self.cleanupLock = threading.Lock()
         self.initiated = False
 
-        self.configparser()
+        self.config = self.configparser()
         verifyOptions( options )
         load_logger()
         self.componentDictionary = {}
@@ -742,11 +742,16 @@
         """
         Stop the test until Ctrl-D is entered.
         Ctrl-C will kill the test
+
+        Optional arguments:
+        email can either be a bool, or you can specify the email address
+        to send the email to
         """
         try:
             if email:
-                # FIXME: implement this
-                raise NotImplementedError
+                if '@' in email:
+                    main.mail = email
+                utilities.send_warning_email()
             self.log.error( "Test execution suspended. Press Ctrl-D to "
                             "resume or Ctrl-C to exit the test" )
             # NOTE: Ctrl-D needs to be entered on a new line
@@ -801,11 +806,20 @@
         main.logdir = main.FALSE
 
 def verifyMail( options ):
-    # Checking the mailing list
-    if options.mail:
+    # Mail-To: field
+    if options.mail:  # Test run specific
         main.mail = options.mail
-    else:
-        main.mail = main.params.get( 'mail', 'paxweb@paxterrasolutions.com' )
+    elif main.params.get('mail'):  # Test suite specific
+        main.mail = main.params.get( 'mail' )
+    else:  # TestON specific
+        main.mail = main.config['config'].get( 'mail_to' )
+    # Mail-From: field
+    main.sender = main.config['config'].get( 'mail_from' )
+    # Mail smtp server
+    main.smtp = main.config['config'].get( 'mail_server' )
+    # Mail-From account password
+    main.senderPwd = main.config['config'].get( 'mail_pass' )
+
 
 def verifyTestCases( options ):
     # Getting Test cases list
diff --git a/TestON/core/utilities.py b/TestON/core/utilities.py
index d5a5c0f..cdf6c1a 100644
--- a/TestON/core/utilities.py
+++ b/TestON/core/utilities.py
@@ -215,37 +215,67 @@
         msg = email.mime.Multipart.MIMEMultipart()
         try :
             if main.test_target:
-                sub = "Result summary of \""+main.TEST+"\" run on component \""+main.test_target+"\" Version \""+vars(main)[main.test_target].get_version()+"\": "+str(main.TOTAL_TC_SUCCESS)+"% Passed"
+                sub = "Result summary of \"" + main.TEST + "\" run on component \"" +\
+                      main.test_target + "\" Version \"" +\
+                      vars( main )[main.test_target].get_version() + "\": " +\
+                      str( main.TOTAL_TC_SUCCESS ) + "% Passed"
             else :
-                sub = "Result summary of \""+main.TEST+"\": "+str(main.TOTAL_TC_SUCCESS)+"% Passed"
+                sub = "Result summary of \"" + main.TEST + "\": " +\
+                      str( main.TOTAL_TC_SUCCESS ) + "% Passed"
         except ( KeyError, AttributeError ):
-            sub = "Result summary of \""+main.TEST+"\": "+str(main.TOTAL_TC_SUCCESS)+"% Passed"
+            sub = "Result summary of \"" + main.TEST + "\": " +\
+                  str( main.TOTAL_TC_SUCCESS ) + "% Passed"
 
         msg['Subject'] = sub
-        msg['From'] = 'paxweb@paxterrasolutions.com'
+        msg['From'] = main.sender
         msg['To'] = main.mail
-        #msg['Cc'] = 'paxweb@paxterrasolutions.com'
 
         # The main body is just another attachment
-        body = email.mime.Text.MIMEText(main.logHeader+"\n"+main.testResult)
-        msg.attach(body)
+        body = email.mime.Text.MIMEText( main.logHeader + "\n" +
+                                         main.testResult)
+        msg.attach( body )
 
-        # Attachment
-        for filename in os.listdir(main.logdir):
-            filepath = main.logdir+"/"+filename
-            fp=open(filepath,'rb')
-            att = email.mime.application.MIMEApplication(fp.read(),_subtype="")
+        # Attachments
+        for filename in os.listdir( main.logdir ):
+            filepath = main.logdir + "/" + filename
+            fp = open( filepath, 'rb' )
+            att = email.mime.application.MIMEApplication( fp.read(),
+                                                          _subtype="" )
             fp.close()
-            att.add_header('Content-Disposition','attachment',filename=filename)
-            msg.attach(att)
-
-        smtp = smtplib.SMTP('198.57.211.46')
-        smtp.starttls()
-        smtp.login('paxweb@paxterrasolutions.com','pax@peace')
-        smtp.sendmail(msg['From'],[msg['To']], msg.as_string())
-        smtp.quit()
+            att.add_header( 'Content-Disposition',
+                            'attachment',
+                            filename=filename )
+            msg.attach( att )
+        try:
+            smtp = smtplib.SMTP( main.smtp )
+            smtp.starttls()
+            smtp.login( main.sender, main.senderPwd )
+            smtp.sendmail( msg['From'], [msg['To']], msg.as_string() )
+            smtp.quit()
+        except Exception:
+            main.log.exception( "Error sending email" )
         return main.TRUE
 
+    def send_warning_email( self, subject=None ):
+        try:
+            if not subject:
+                subject = main.TEST + " PAUSED!"
+            # Create a text/plain message
+            msg = email.mime.Multipart.MIMEMultipart()
+
+            msg['Subject'] = subject
+            msg['From'] = main.sender
+            msg['To'] = main.mail
+
+            smtp = smtplib.SMTP( main.smtp )
+            smtp.starttls()
+            smtp.login( main.sender, main.senderPwd )
+            smtp.sendmail( msg['From'], [msg['To']], msg.as_string() )
+            smtp.quit()
+        except Exception:
+            main.log.exception( "" )
+            return main.FALSE
+        return main.TRUE
 
     def parse(self,fileName):
         '''