[VOL-2698] - Refactor the power switch library to support two vendors

Change-Id: I489e7ce881d934577f962f5892f0516d8587f3ec
diff --git a/libraries/power_switch.robot b/libraries/power_switch.robot
index 6d9c118..19c5974 100644
--- a/libraries/power_switch.robot
+++ b/libraries/power_switch.robot
@@ -13,45 +13,117 @@
 # limitations under the License.
 
 *** Settings ***
-Documentation     Library for Digital Loggers Web Power Switch
-                  ...    Official Document: https://www.digital-loggers.com/rest.html
+Documentation     Library for Web Power Switch, support DLI(Digital Loggers)
+                  ...    and EPC(Expert Power Control)
 Library           Collections
 Library           RequestsLibrary
 
 *** Variables ***
 ${timeout}       60s
 ${alias_name}    Switch Outlet
-${restapi_uri}   restapi/relay/outlets/
 
 *** Keywords ***
 Power Switch Connection Suite
     [Arguments]    ${ip}    ${username}    ${password}
     [Documentation]    Setup The HTTP Session To Web Power Switch
+    Variable Should Exist    ${powerswitch_type}
+    ...    'Miss Global Variable powerswitch_type, available options are EPC, DLI'
+    Run Keyword IF    '${powerswitch_type}' == 'DLI'    Setup DLI Power Switch    ${ip}    ${username}    ${password}
+    ...    ELSE IF    '${powerswitch_type}' == 'EPC'    Setup EPC Power Switch    ${ip}    ${username}    ${password}
+    ...    ELSE    Fail    'The Power Switch Type unsupported: ${powerswitch_type}'
+
+Enable Switch Outlet
+    [Arguments]    ${outlet_number}
+    [Documentation]    Enable specific outlet of the Web Power Switch
+    Variable Should Exist    ${powerswitch_type}
+    ...    'Miss Global Variable powerswitch_type, available options are EPC, DLI'
+    Run Keyword IF    '${powerswitch_type}' == 'DLI'    Enable DLI Switch Outlet   ${outlet_number}
+    ...    ELSE IF    '${powerswitch_type}' == 'EPC'    Enable EPC Switch Outlet   ${outlet_number}
+    ...    ELSE    Fail    'The Power Switch Type unsupported: ${powerswitch_type}'
+
+
+Disable Switch Outlet
+    [Arguments]    ${outlet_number}
+    [Documentation]    Disable specific outlet of the web Power Switch
+    Variable Should Exist    ${powerswitch_type}
+    ...    'Miss Global Variable powerswitch_type, available options are EPC, DLI'
+    Run Keyword IF    '${powerswitch_type}' == 'DLI'    Disable DLI Switch Outlet   ${outlet_number}
+    ...    ELSE IF    '${powerswitch_type}' == 'EPC'    Disable EPC Switch Outlet   ${outlet_number}
+    ...    ELSE    Fail    'The Power Switch Type unsupported: ${powerswitch_type}'
+
+Check Expected Switch Outlet Status
+    [Arguments]    ${outlet_number}    ${status}
+    [Documentation]    Succeeds if the status of the desired switch outlet is expected
+    Variable Should Exist    ${powerswitch_type}
+    ...    'Miss Global Variable powerswitch_type, available options are EPC, DLI'
+    Run Keyword IF    '${powerswitch_type}' == 'DLI'    Check Expected DLI Switch Outlet Status    ${outlet_number}
+    ...    ELSE IF    '${powerswitch_type}' == 'EPC'    Check Expected EPC Switch Outlet Status    ${outlet_number}
+    ...    ELSE    Fail    'The Power Switch Type unsupported: ${powerswitch_type}'
+
+#Intenal Use Only
+Setup DLI Power Switch
+    [Arguments]    ${ip}    ${username}    ${password}
+    [Documentation]    Setup The HTTP Session To Web Power Switch
     ${auth}=    Create List    ${username}    ${password}
     ${headers}=    Create Dictionary
     Set To Dictionary    ${headers}    X-CSRF    x
     Set To Dictionary    ${headers}    Content-Type    application/x-www-form-urlencoded
-    Create Digest Session    alias=${alias_name}    url=http://${ip}/${restapi_uri}
+    Create Digest Session    alias=${alias_name}    url=http://${ip}/restapi/relay/outlets/
     ...    auth=${auth}    headers=${headers}
 
-Enable Switch Outlet
+Setup EPC Power Switch
+    [Arguments]    ${ip}    ${username}    ${password}
+    [Documentation]    Setup The HTTP Session To Web Power Switch
+    ${auth}=    Create List    ${username}    ${password}
+    ${headers}=    Create Dictionary
+    Set To Dictionary    ${headers}    X-CSRF    x
+    Set To Dictionary    ${headers}    Content-Type    application/x-www-form-urlencoded
+    Create Digest Session    alias=${alias_name}    url=http://${ip}
+    ...    auth=${auth}    headers=${headers}
+
+
+Enable DLI Switch Outlet
     [Arguments]    ${outlet_number}
-    [Documentation]    Enable specific outlet of the Web Power Switch
+    [Documentation]    Enable specific outlet of DLI power switch
     ${resp}=    Put Request    alias=${alias_name}    uri==${outlet_number}/state/    data=value=true
     Should Be Equal As Strings  ${resp.status_code}  207
     Wait Until Keyword Succeeds    ${timeout}    2s
-    ...    Check Expected Switch Outlet Status    ${outlet_number}    true
+    ...    Check Expected DLI Switch Outlet Status    ${outlet_number}    true
 
-Disable Switch Outlet
+Enable EPC Switch Outlet
     [Arguments]    ${outlet_number}
-    [Documentation]    Disable specific outlet of the Web Power Switch
+    [Documentation]    Enable specific outlet of EPC power switch
+    ${resp}=    Get Request    alias=${alias_name}    uri=ov.html?cmd=1&p=${outlet_number}&s=1
+    Wait Until Keyword Succeeds    ${timeout}    2s
+    ...    Check Expected EPC Switch Outlet Status    ${outlet_number}    1
+
+Disable DLI Switch Outlet
+    [Arguments]    ${outlet_number}
+    [Documentation]    Disable specific outlet of DLI Power Switch
     ${resp}=    Put Request    alias=${alias_name}    uri==${outlet_number}/state/    data=value=false
     Should Be Equal As Strings  ${resp.status_code}  207
     Wait Until Keyword Succeeds    ${timeout}    2s
-    ...    Check Expected Switch Outlet Status    ${outlet_number}    false
+    ...    Check Expected DLI Switch Outlet Status    ${outlet_number}    false
 
-Check Expected Switch Outlet Status
+Disable EPC Switch Outlet
+    [Arguments]    ${outlet_number}
+    [Documentation]    Disable specific outlet of EPC Power Switch
+    ${resp}=    Get Request    alias=${alias_name}    uri=ov.html?cmd=1&p=${outlet_number}&s=0
+    Wait Until Keyword Succeeds    ${timeout}    2s
+    ...    Check Expected EPC Switch Outlet Status    ${outlet_number}    0
+
+Check Expected DLI Switch Outlet Status
     [Arguments]    ${outlet_number}    ${status}
-    [Documentation]    Succeeds if the status of the desired switch outlet is expected
+    [Documentation]    Succeeds if the status of the desired DLI switch outlet is expected
     ${resp}=    Get Request    alias=${alias_name}    uri==${outlet_number}/state/
     Should Be Equal As Strings  ${resp.text}  [${status}]
+
+Check Expected EPC Switch Outlet Status
+    [Arguments]    ${outlet_number}    ${status}
+    [Documentation]    Succeeds if the status of the desired EPC switch outlet is expected
+    ${resp}=    Get Request    alias=${alias_name}    uri=statusjsn.js?components=1
+    ${outlet_number}=  Convert To Number  ${outlet_number}
+    ${rc}    ${outlet_status}    Run and Return Rc And Output
+    ...    echo '${resp.text}' | jq -r .outputs[${outlet_number - 1}].state
+    Should Be Equal As Integers    0    ${rc}
+    Should Be Equal As Strings  ${outlet_status}  ${status}
diff --git a/tests/functional/Voltha_FailureScenarios.robot b/tests/functional/Voltha_FailureScenarios.robot
index 32ae0dc..45026fa 100644
--- a/tests/functional/Voltha_FailureScenarios.robot
+++ b/tests/functional/Voltha_FailureScenarios.robot
@@ -14,7 +14,7 @@
 
 *** Settings ***
 Documentation     Test various failure scenarios
-Suite Setup       Common Test Suite Setup
+Suite Setup       Setup Suite
 Test Setup        Setup
 Test Teardown     Teardown
 #Suite Teardown    Teardown Suite
@@ -424,6 +424,8 @@
 Setup Suite
     [Documentation]    Set up the test suite
     Common Test Suite Setup
+    #power_switch.robot needs it to support different vendor's power switch
+    Set Global Variable    ${powerswitch_type}    ${web_power_switch.type}
 
 Clear All Devices Then Create New Device
     [Documentation]    Remove any devices from VOLTHA and ONOS