Added support to Power Cycle OLT at the beginning of the functional test suite (as Cleanup) for ATT, DT and TT workflows

Change-Id: Ie2b618a2428210e58fcca68c179587c67fab7d18
diff --git a/libraries/utils.robot b/libraries/utils.robot
index defd154..30158ed 100755
--- a/libraries/utils.robot
+++ b/libraries/utils.robot
@@ -74,13 +74,15 @@
         ${serial_number}    Evaluate    ${olts}[${I}].get("serial")
         ${olt_ssh_ip}    Evaluate    ${olts}[${I}].get("sship")
         ${type}    Evaluate    ${olts}[${I}].get("type")
+        ${power_switch_port}    Evaluate    ${olts}[${I}].get("power_switch_port")
         ${orig_olt_port}    Evaluate    ${olts}[${I}].get("oltPort")
         ${port}=    Set Variable If    "${orig_olt_port}" == "None"    ${OLT_PORT}    ${orig_olt_port}
         ${onu_count}=    Get ONU Count For OLT    ${hosts.src}    ${serial_number}
         ${onu_list}=    Get ONU List For OLT    ${hosts.src}    ${serial_number}
         ${olt}    Create Dictionary    ip    ${ip}    user    ${user}    pass
         ...    ${pass}    sn    ${serial_number}   onucount   ${onu_count}    type    ${type}
-        ...    sship    ${olt_ssh_ip}    oltport    ${port}    onus    ${onu_list}
+        ...    sship    ${olt_ssh_ip}    oltport    ${port}    powerswitchport    ${power_switch_port}
+        ...    onus    ${onu_list}
         Append To List    ${list_olts}    ${olt}
     END
     ${num_all_onus}=    Get Length    ${hosts.src}
@@ -1656,3 +1658,42 @@
         ...    Set Tags    non-critical
         ...    AND    Exit For Loop
     END
+
+Perform Reboot ONUs and OLTs Physically
+    [Documentation]    This keyword reboots ONUs and OLTs physically
+    ...    It runs only on the PODs that are configured with PowerSwitch that
+    ...    controls the power off/on ONUs/OLT remotely (simulating a physical reboot)
+    [Arguments]    ${power_cycle_olt}=False
+    Power Switch Connection Suite    ${web_power_switch.ip}    ${web_power_switch.user}    ${web_power_switch.password}
+    @{onu_list}=    Create List
+    FOR    ${I}    IN RANGE    0    ${num_all_onus}
+        ${src}=    Set Variable    ${hosts.src[${I}]}
+        ${dst}=    Set Variable    ${hosts.dst[${I}]}
+        # If the power switch port is not specified, continue
+        Continue For Loop If    '${src["power_switch_port"]}' == '${None}'
+        # Skip if we have already handled this ONU
+        ${sn}=     Set Variable    ${src['onu']}
+        ${onu_id}=    Get Index From List    ${onu_list}   ${sn}
+        Continue For Loop If    -1 != ${onu_id}
+        Append To List    ${onu_list}    ${sn}
+        Disable Switch Outlet    ${src['power_switch_port']}
+        Sleep    10s
+        Enable Switch Outlet    ${src['power_switch_port']}
+    END
+    Pass Execution If    '${power_cycle_olt}'=='False'    Skipping OLT(s) Power Switch Reboot
+    # Waiting extra time for the ONUs to come up
+    Sleep    30s
+    FOR   ${I}    IN RANGE    0    ${olt_count}
+        ${olt_serial_number}=    Get From Dictionary    ${olt_ids}[${I}]    sn
+        ${power_switch_port}=    Get From Dictionary    ${list_olts}[${I}]    powerswitchport
+        ${olt_ssh_ip}=    Get From Dictionary    ${list_olts}[${I}]   sship
+        # If the power switch port is not specified, continue
+        Continue For Loop If    '${power_switch_port}' == '${None}'
+        Disable Switch Outlet    ${power_switch_port}
+        Sleep    10s
+        Enable Switch Outlet    ${power_switch_port}
+        Run Keyword If    ${has_dataplane}    Wait Until Keyword Succeeds    120s    10s
+        ...    Check Remote System Reachability    True    ${olt_ssh_ip}
+    END
+    # Waiting extra time for the ONUs to come up
+    Sleep    60s
diff --git a/tests/dt-workflow/Voltha_DT_PODTests.robot b/tests/dt-workflow/Voltha_DT_PODTests.robot
index c2126a5..11fc497 100755
--- a/tests/dt-workflow/Voltha_DT_PODTests.robot
+++ b/tests/dt-workflow/Voltha_DT_PODTests.robot
@@ -54,6 +54,8 @@
 ${has_dataplane}    True
 ${teardown_device}    True
 ${scripts}        ../../scripts
+# flag to reboot OLT through Power Switch
+${power_cycle_olt}    False
 
 # For dataplane bandwidth testing
 ${upper_margin_pct}      105     # Allow 5% over the limit
@@ -73,24 +75,15 @@
 ${bbsim_port}    50060
 
 *** Test Cases ***
-Reboot DT ONUs Physically
-    [Documentation]   This test reboots ONUs physically before execution all the tests
+Reboot DT ONUs and OLTs Physically
+    [Documentation]   This test reboots ONUs and OLTs physically before execution all the tests
     ...    Test case runs only on the PODs that are configured with PowerSwitch that
     ...    controls the power off/on ONUs/OLT remotely (simulating a physical reboot)
-    [Tags]    functionalDt   PowerSwitch    RebootAllDTONUs    soak
-    [Setup]    Start Logging    RebootAllDTONUs
+    [Tags]    functionalDt   PowerSwitch    RebootAllDTONUsOLTs    soak
+    [Setup]    Start Logging    RebootAllDTONUsOLTs
     [Teardown]    Run Keywords    Run Keyword If    ${logging}    Collect Logs
-    ...           AND             Stop Logging    RebootAllDTONUs
-    Power Switch Connection Suite    ${web_power_switch.ip}    ${web_power_switch.user}    ${web_power_switch.password}
-    FOR    ${I}    IN RANGE    0    ${num_all_onus}
-        ${src}=    Set Variable    ${hosts.src[${I}]}
-        ${dst}=    Set Variable    ${hosts.dst[${I}]}
-        # If the power switch port is not specified, continue
-        Continue For Loop If    '${src["power_switch_port"]}' == '${None}'
-        Disable Switch Outlet    ${src['power_switch_port']}
-        Sleep    10s
-        Enable Switch Outlet    ${src['power_switch_port']}
-    END
+    ...           AND             Stop Logging    RebootAllDTONUsOLTs
+    Perform Reboot ONUs and OLTs Physically    ${power_cycle_olt}
 
 Create Soak BBSim Device
     [Documentation]    This creates and enables the BBSim device as required by the soak testing
diff --git a/tests/functional/Voltha_PODTests.robot b/tests/functional/Voltha_PODTests.robot
index 091c479..f57406c 100755
--- a/tests/functional/Voltha_PODTests.robot
+++ b/tests/functional/Voltha_PODTests.robot
@@ -54,6 +54,8 @@
 ${has_dataplane}    True
 ${teardown_device}    False
 ${scripts}        ../../scripts
+# flag to reboot OLT through Power Switch
+${power_cycle_olt}    False
 
 # For dataplane bandwidth testing
 ${upper_margin_pct}      105     # Allow 5% over the limit
@@ -69,24 +71,15 @@
 ${logging}    True
 
 *** Test Cases ***
-Reboot ONUs Physically
-    [Documentation]   This test reboots ONUs physically before execution all the tests
+Reboot ONUs and OLTs Physically
+    [Documentation]   This test reboots ONUs and OLTs physically before execution all the tests
     ...    Test case runs only on the PODs that are configured with PowerSwitch that
     ...    controls the power off/on ONUs/OLT remotely (simulating a physical reboot)
-    [Tags]    functional   PowerSwitch    RebootAllONUs
-    [Setup]    Start Logging    RebootAllONUs
+    [Tags]    functional   PowerSwitch    RebootAllONUsOLTs
+    [Setup]    Start Logging    RebootAllONUsOLTs
     [Teardown]    Run Keywords    Collect Logs
-    ...           AND             Stop Logging    RebootAllONUs
-    Power Switch Connection Suite    ${web_power_switch.ip}    ${web_power_switch.user}    ${web_power_switch.password}
-    FOR    ${I}    IN RANGE    0    ${num_all_onus}
-        ${src}=    Set Variable    ${hosts.src[${I}]}
-        ${dst}=    Set Variable    ${hosts.dst[${I}]}
-        # If the power switch port is not specified, continue
-        Continue For Loop If    '${src["power_switch_port"]}' == '${None}'
-        Disable Switch Outlet    ${src['power_switch_port']}
-        Sleep    10s
-        Enable Switch Outlet    ${src['power_switch_port']}
-    END
+    ...           AND             Stop Logging    RebootAllONUsOLTs
+    Perform Reboot ONUs and OLTs Physically    ${power_cycle_olt}
 
 Sanity E2E Test for OLT/ONU on POD
     [Documentation]    Validates E2E Ping Connectivity and object states for the given scenario:
diff --git a/tests/tt-workflow/Voltha_TT_PODTests.robot b/tests/tt-workflow/Voltha_TT_PODTests.robot
index 483ca6e..28d4e5f 100755
--- a/tests/tt-workflow/Voltha_TT_PODTests.robot
+++ b/tests/tt-workflow/Voltha_TT_PODTests.robot
@@ -54,6 +54,8 @@
 ${teardown_device}    True
 ${scripts}        ../../scripts
 ${data_dir}    ../data
+# flag to reboot OLT through Power Switch
+${power_cycle_olt}    False
 
 # Per-test logging on failure is turned off by default; set this variable to enable
 ${container_log_dir}    ${None}
@@ -75,24 +77,15 @@
 ${unitag_sub}    False
 
 *** Test Cases ***
-Reboot TT ONUs Physically - Clean Up
-    [Documentation]   This test reboots ONUs physically before execution all the tests
+Reboot TT ONUs and OLTs Physically - Clean Up
+    [Documentation]   This test reboots ONUs and OLTs physically before execution all the tests
     ...    Test case runs only on the PODs that are configured with PowerSwitch that
     ...    controls the power off/on ONUs/OLT remotely (simulating a physical reboot)
-    [Tags]    functionalTT   PowerSwitch    RebootAllTTONUs
-    [Setup]    Start Logging    RebootAllTTONUs
+    [Tags]    functionalTT   PowerSwitch    RebootAllTTONUsOLTs
+    [Setup]    Start Logging    RebootAllTTONUsOLTs
     [Teardown]    Run Keywords    Collect Logs
-    ...           AND             Stop Logging    RebootAllTTONUs
-    Power Switch Connection Suite    ${web_power_switch.ip}    ${web_power_switch.user}    ${web_power_switch.password}
-    FOR    ${I}    IN RANGE    0    ${num_all_onus}
-        ${src}=    Set Variable    ${hosts.src[${I}]}
-        ${dst}=    Set Variable    ${hosts.dst[${I}]}
-        # If the power switch port is not specified, continue
-        Continue For Loop If    '${src["power_switch_port"]}' == '${None}'
-        Disable Switch Outlet    ${src['power_switch_port']}
-        Sleep    10s
-        Enable Switch Outlet    ${src['power_switch_port']}
-    END
+    ...           AND             Stop Logging    RebootAllTTONUsOLTs
+    Perform Reboot ONUs and OLTs Physically    ${power_cycle_olt}
 
 Sanity E2E Test for TT (HSIA, VoD, VoIP)
     [Documentation]    Validates E2E Ping Connectivity and object states for the given scenario: