[VOL-1129] Refactor Voltha Test Automation Robot
Add missing License Header to files
Change-Id: I04311b17ad2f4c7af77c8bfd9539970915fbfea5
diff --git a/tests/atests/common/auto_test.py b/tests/atests/common/auto_test.py
new file mode 100755
index 0000000..d0bd327
--- /dev/null
+++ b/tests/atests/common/auto_test.py
@@ -0,0 +1,81 @@
+#!/usr/bin/python2
+
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""
+vOLT-HA Automated Testing module
+"""
+import os
+import time
+import argparse
+import volthaMngr
+import preprovisioningTest
+
+DEFAULT_LOG_DIR = '/tmp/voltha_test_results'
+
+def runOnos():
+    os.system('docker-compose -f compose/docker-compose-auth-test.yml'
+              ' up -d onos freeradius' + ' > /dev/null 2>&1')
+
+
+def dirInit(logDir=DEFAULT_LOG_DIR,
+         volthaDir=os.environ['VOLTHA_BASE']):
+    print(__file__)
+    """
+    Init automated testing environment and return three directories: root dir,
+    voltha sources dir and log dir
+    """
+
+    rootDir = os.path.abspath(os.path.dirname(__file__))
+
+    currentTime = time.strftime("%Y-%m-%d-%H-%M-%S")
+
+    # In future in order to keep the history of jobs, the run time should be
+    # added to the log directory name
+    # logDir += '_' + currentTime
+
+    os.system('mkdir -p ' + logDir + ' > /dev/null 2>&1')
+    os.system('rm -rf %s/*' + logDir)
+    print('Start Provisioning Test at: %s\nRoot Directory: %s\n'
+          'VOLTHA Directory: %s\nLog Directory: %s' %
+          (currentTime, rootDir, volthaDir, logDir))
+
+    return rootDir, volthaDir, logDir
+
+
+#
+# MAIN
+#
+if __name__ == "__main__":
+    """
+    Main entry point of the automated testing when executed directly
+    """
+
+    parser = argparse.ArgumentParser(description='VOLTHA Automated Testing')
+    parser.add_argument('-l', dest='logDir', default=DEFAULT_LOG_DIR,
+                        help='log directory (default: %s).' % DEFAULT_LOG_DIR)
+    args = parser.parse_args()
+
+    ROOT_DIR, VOLTHA_DIR, LOG_DIR = dirInit(args.logDir)
+
+    volthaMngr.voltha_Initialize(ROOT_DIR, VOLTHA_DIR, LOG_DIR)
+
+    runOnos()
+
+    preprovisioningTest.runTest('172.17.0.1', 50060, LOG_DIR)
+
+    time.sleep(5)
diff --git a/tests/atests/common/enable_bridge.sh b/tests/atests/common/enable_bridge.sh
new file mode 100755
index 0000000..9d17ae5
--- /dev/null
+++ b/tests/atests/common/enable_bridge.sh
@@ -0,0 +1,17 @@
+#!/bin/sh +x
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+echo 'Enable Bridge'
+echo 8 > /sys/class/net/ponmgmt/bridge/group_fwd_mask
diff --git a/tests/atests/common/preprovisioningTest.py b/tests/atests/common/preprovisioningTest.py
new file mode 100755
index 0000000..a8bbe3e
--- /dev/null
+++ b/tests/atests/common/preprovisioningTest.py
@@ -0,0 +1,105 @@
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""
+vOLT-HA Pre-provisioning Test module
+"""
+
+import time
+import os
+import commands
+
+
+class preprovisioningTest(object):
+
+    """
+    This class implements voltha pre-provisioning test
+    """
+    
+    def __init__(self):
+        self.__oltIpAddress = None        
+        self.__oltPort = None
+        self.__logDir = None
+        self.__oltDeviceId = None
+        self.__statusLine = ""
+        self.__fields = []
+        
+    def configure(self, oltIpAddress, oltPort, logDir):
+        self.__oltIpAddress = oltIpAddress        
+        self.__oltPort = oltPort
+        self.__logDir = logDir    
+
+
+    def preprovisionOlt(self):
+        print('Do PROVISIONING')
+        self.send_command_to_voltha_cli(
+            'preprovision_olt -t ponsim_olt -H %s:%s' %
+            (self.__oltIpAddress, self.__oltPort),
+            'voltha_preprovision_olt.log')
+        time.sleep(5)
+   
+    def query_devices_before_enable(self):
+        self.send_command_to_voltha_cli('devices',
+                                        'voltha_devices_before_enable.log')
+        time.sleep(5)
+        grepCommand =\
+            "grep PREPROVISIONED %s/voltha_devices_before_enable.log " % self.__logDir
+        self.__statusLine = commands.getstatusoutput(grepCommand)[1]
+        self.__fields = self.parseFields(self.__statusLine)
+        self.__oltDeviceId = self.__fields[1].strip()
+        print ("OLT device id = %s" % self.__oltDeviceId)
+        
+    def enable(self):
+        print('Enable %s OLT device' % self.__oltDeviceId)
+        self.send_command_to_voltha_cli('enable ' + self.__oltDeviceId,
+                                        'voltha_enable.log')
+    def query_devices_after_enable(self):
+        self.send_command_to_voltha_cli('devices',
+                                        'voltha_devices_after_enable.log')
+
+    def send_command_to_voltha_cli(self, cmd, logFile):
+        # os.system("docker exec -i -t compose_cli_1 sh -c 'echo \"" + cmd +
+        #           "\" > /voltha_tmp_command.txt'")
+        os.system("docker exec compose_cli_1 sh -c 'echo \"" + cmd +
+                  "\" > /voltha_tmp_command.txt'")
+        os.system("docker exec compose_cli_1 sh -c '/cli/cli/main.py -C "
+                  "vconsul:8500 -L < /voltha_tmp_command.txt' > " +
+                  self.__logDir + '/' + logFile)
+
+    def send_command_to_onos_cli(self, cmd, logFile):
+        os.system(
+            "sshpass -p karaf ssh -o StrictHostKeyChecking=no -p 8101 "
+            "karaf@localhost " + cmd + " 2>/dev/null > " +
+            self.__logDir + '/' + logFile)
+            
+    def parseFields(self, statusLine):
+        statusList = statusLine.split("|")
+        return statusList
+
+            
+ 
+def runTest(oltIpAddress, oltPort, logDir):
+    preprovisioning = preprovisioningTest()
+    preprovisioning.configure(str(oltIpAddress), int(oltPort),
+                                          str(logDir))
+    preprovisioning.preprovisionOlt()
+    preprovisioning.query_devices_before_enable()
+    preprovisioning.enable()
+    preprovisioning.query_devices_after_enable()
+    
+                                          
+                                          
+
diff --git a/tests/atests/common/run_robot.sh b/tests/atests/common/run_robot.sh
new file mode 100755
index 0000000..fa60b99
--- /dev/null
+++ b/tests/atests/common/run_robot.sh
@@ -0,0 +1,22 @@
+#!/bin/bash +x
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+SRC_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+VOLTHA_DIR="$SRC_DIR/../../.."
+
+echo "Run Robot Framework TEST. Log: $1"
+cd $VOLTHA_DIR
+source env.sh
+robot -d $1 -v LOG_DIR:$1/voltha ./tests/atests/robot/auto_testing.robot
diff --git a/tests/atests/common/volthaMngr.py b/tests/atests/common/volthaMngr.py
new file mode 100755
index 0000000..553f7c0
--- /dev/null
+++ b/tests/atests/common/volthaMngr.py
@@ -0,0 +1,132 @@
+#
+# Copyright 2018 the original author or authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""
+vOLT-HA Start/Stop module
+"""
+
+
+import os
+import time
+import subprocess
+import paramiko
+import spur
+
+class volthaMngr(object):
+
+    """
+    This class implements voltha startup/shutdown callable helper functions
+    """
+    def __init__(self):
+        self.__rootDir = None
+        self.__volthaDir = None
+        self.__logDir = None
+        self.__rootSsh = None
+
+    def configDir(self, rootDir, volthaDir, logDir):
+        self.__rootDir = rootDir
+        self.__volthaDir = volthaDir
+        self.__logDir = logDir
+        
+        os.chdir(volthaDir)
+
+    def openRootSsh(self):
+        shell = spur.SshShell(hostname='localhost', username='root',
+                              password='root',
+                              missing_host_key=spur.ssh.MissingHostKey.accept)
+        return shell
+
+    def getAllRunningContainers(self):
+        allContainers = []
+        proc1 = subprocess.Popen(['docker', 'ps', '-a'],
+                                 stdout=subprocess.PIPE,
+                                 stderr=subprocess.PIPE)
+        proc2 = subprocess.Popen(['grep', '-v', 'CONT'], stdin=proc1.stdout,
+                                 stdout=subprocess.PIPE,
+                                 stderr=subprocess.PIPE)
+        proc1.stdout.close
+        out, err = proc2.communicate()
+        if out:
+            for line in out.split('\n'):
+                items = line.split()
+                if len(items):
+                    allContainers.append(items)
+        return allContainers
+
+    def stopPonsim(self):
+        command = "for pid in $(ps -ef | grep ponsim | grep -v grep | " \
+                  "awk '{print $2}'); do echo $pid; done"
+        client = paramiko.SSHClient()
+        client.load_system_host_keys()
+        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        client.connect('localhost', username='root', password='root')
+        transport = client.get_transport()
+        channel = transport.open_session()
+
+        channel.exec_command(command)
+        procIds = channel.recv(4096).replace('\n', ' ')
+        channel = transport.open_session()
+        channel.exec_command('sudo kill -9 %s' % procIds)
+
+    def removeExistingContainers(self):
+        allContainers = self.getAllRunningContainers()
+        for container in allContainers:
+            procID = container[0]
+            os.system('docker rm -f %s > /dev/null 2>&1' % procID)
+
+    def startVolthaContainers(self):
+        print('Start VOLTHA containers')
+        # Bring up all the containers required for VOLTHA (total 15)
+        os.system(
+            'docker-compose -f compose/docker-compose-system-test.yml '
+            'up -d > %s/start_voltha_containers.log 2>&1' %
+            self.__logDir)
+
+    def collectAllLogs(self):
+        print('Collect all VOLTHA container logs')
+        allContainers = self.getAllRunningContainers()
+        for container in allContainers:
+            containerName = container[-1]
+            os.system('docker logs --since 0m -f %s > %s/%s.log 2>&1 &' %
+                      (containerName, self.__logDir, containerName))
+
+    def enableBridge(self):
+        self.__rootSsh = self.openRootSsh()
+        result = self.__rootSsh.run([self.__rootDir + '/enable_bridge.sh'])
+        print(result.output)
+
+    def startPonsim(self, onusAmount=1):
+        command = 'source env.sh ; ./ponsim/main.py -v'
+        if onusAmount > 1:
+            command += ' -o %s' % onusAmount
+        ponsimLog = open('%s/ponsim.log' % self.__logDir, 'w')
+        process = self.__rootSsh.spawn(['bash', '-c', command],
+                                       cwd=self.__volthaDir, store_pid=True,
+                                       stdout=ponsimLog)
+        return process.pid
+
+
+def voltha_Initialize(rootDir, volthaDir, logDir):
+
+    voltha = volthaMngr()
+    voltha.configDir(rootDir, volthaDir, logDir)
+    voltha.stopPonsim()
+    voltha.removeExistingContainers()
+    voltha.startVolthaContainers()
+    voltha.collectAllLogs()
+    voltha.enableBridge()
+    voltha.startPonsim(3)
+    
diff --git a/tests/atests/robot/voltha_automated_test_suite.robot b/tests/atests/robot/voltha_automated_test_suite.robot
new file mode 100755
index 0000000..9c9f37b
--- /dev/null
+++ b/tests/atests/robot/voltha_automated_test_suite.robot
@@ -0,0 +1,70 @@
+# Copyright 2017-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+*** Settings ***
+Library           Process
+Library           ../common/auto_test.py
+Library           ../common/volthaMngr.py
+Library           ../common/preprovisioningTest.py
+
+Test Setup        Start Voltha      
+Test Teardown     Stop Voltha
+
+
+*** Variables ***
+${LOG_DIR}        /tmp/voltha_test_results
+${ROOT_DIR}       ${EMPTY}
+${VOLTHA_DIR}     ${EMPTY}
+${PONSIM_PID}     ${EMPTY}
+${ONUS}           3
+${ONOS_SSH_PORT}  8101
+${OLT_IP_ADDR}    "172.17.0.1"
+${OLT_PORT_ID}    50060
+
+
+*** Test Cases ***
+Provisioning
+    [Documentation]     VOLTHA Pre-provisioning Test
+    ...                 This test deploys an OLT port and a number of ONU ports 
+    ...                 Then it verifies that all the physical and logical devices are up 
+    Configure   ${OLT_IP_ADDR}    ${OLT_PORT_ID}    ${LOG_DIR}
+    Preprovision Olt
+    Query Devices Before Enable
+    Enable
+    Query Devices After Enable
+
+
+*** Keywords ***
+Start Voltha
+    [Documentation]     Start Voltha infrastructure to run test(s). This includes starting all 
+    ...                 Docker containers for Voltha and Onos as well as Ponsim. It then start 
+    ...                 Voltha and Onos Cli
+    ${ROOT_DIR}    ${VOLTHA_DIR}    ${LOG_DIR}  Dir Init    ${LOG_DIR}
+    Config Dir  ${ROOT_DIR}    ${VOLTHA_DIR}    ${LOG_DIR}
+    Stop Voltha
+    Start Voltha Containers
+    Collect All Logs
+    Enable Bridge
+    ${PONSIM_PID}   Start Ponsim  ${ONUS}
+    Run Onos
+    
+    
+Stop Voltha
+    [Documentation]     Stop Voltha infrastucture. This includes stopping all Docker containers 
+    ...                 for Voltha and Onos as well stopping Ponsim process.
+    Stop Ponsim
+    Remove Existing Containers
+    
+    
+