*** Comments ***
#   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 ***
Documentation   Library to deploy and delete NS, and run operations on them.

Library   Collections
Library   DateTime
Library   OperatingSystem
Library   String


*** Variables ***
${SUCCESS_RETURN_CODE}   0
${NS_ACTION_MAX_WAIT_TIME}   1min
${NS_ACTION_POL_TIME}   15sec
${VNF_SCALE_POL_TIME}   15sec
${HEALING_POL_TIME}   15sec
${VIM_TIMEOUT_MULTIPLIER}   %{OSM_VIM_MULTIPLIER_TIMEOUT=1.0}


*** Keywords ***
Update Network Service
    [Documentation]   Run an update operation over a NS instance, and return the operation id.
    [Arguments]   ${ns_id}   ${update_type}   ${ns_update_config}   ${ns_update_timeout}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-update ${ns_id} --updatetype ${update_type} --config ${ns_update_config} --timeout ${ns_update_timeout} --wait
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Execute NS Action
    [Documentation]   Execute an action over the desired NS.
    ...               Parameters are given to this function in key=value format (one argument per key/value pair).
    ...               Return the ID of the operation associated to the executed action.
    ...               Examples of execution:
    ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}
    ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}   \${param1}=\${value1}   \${param2}=\${value2}

    [Arguments]   ${ns_name}   ${ns_action}   ${vnf_member_index}   @{action_params}
    ${params}=   Set Variable   ${EMPTY}
    FOR   ${param}   IN   @{action_params}
        ${match}   ${param_name}   ${param_value}=   Should Match Regexp   ${param}   (.+)=(.+)   msg=Syntax error in parameters
        Log   ${match},${param_name},${param_value}
        ${params}=   Catenate   SEPARATOR=   ${params}   "${param_name}":"${param_value}",
    END
    ${osm_ns_action_command}=   Set Variable   osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index}
    IF   '${params}' != '${EMPTY}'
        ${osm_ns_action_command}=   Catenate   ${osm_ns_action_command}   --params '{${params}}'
    ELSE
        ${osm_ns_action_command}=   Set Variable   ${osm_ns_action_command}
    END
    ${osm_ns_action_command}=   Catenate   ${osm_ns_action_command}   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   ${osm_ns_action_command}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    Wait Until Keyword Succeeds   ${NS_ACTION_MAX_WAIT_TIME}   ${NS_ACTION_POL_TIME}   Check For NS Operation Ended   ${stdout}
    Check For NS Operation Completed   ${stdout}
    RETURN   ${stdout}

Execute NS K8s Action
    [Documentation]   Execute an action over the desired K8s NS.
    ...               Parameters are given to this function in key=value format (one argument per key/value pair).
    ...               Return the ID of the operation associated to the executed action.
    ...               Examples of execution:
    ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}
    ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}   \${param1}=\${value1}   \${param2}=\${value2}

    [Arguments]   ${ns_name}   ${ns_action}   ${vnf_member_index}   ${kdu_name}   @{action_params}

    ${params}=   Set Variable   ${EMPTY}
    FOR   ${param}   IN   @{action_params}
        ${match}   ${param_name}   ${param_value}=   Should Match Regexp   ${param}   (.+)=(.+)   msg=Syntax error in parameters
        Log   ${match},${param_name},${param_value}
        ${params}=   Catenate   SEPARATOR=   ${params}   "${param_name}":"${param_value}",
    END
    ${osm_ns_action_command}=   Set Variable   osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index} --kdu_name ${kdu_name}
    IF   '${params}' != '${EMPTY}'
        ${osm_ns_action_command}=   Catenate   ${osm_ns_action_command}   --params '{${params}}'
    ELSE
        ${osm_ns_action_command}=   Set Variable   ${osm_ns_action_command}
    END
    ${osm_ns_action_command}=   Catenate   ${osm_ns_action_command}   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   ${osm_ns_action_command}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    Wait Until Keyword Succeeds   ${NS_ACTION_MAX_WAIT_TIME}   ${NS_ACTION_POL_TIME}   Check For NS Operation Ended   ${stdout}
    Check For NS Operation Completed   ${stdout}
    RETURN   ${stdout}

Execute Manual VNF Scale
    [Documentation]   Execute a manual VNF Scale action.
    ...               The parameter 'scale_type' must be SCALE_IN or SCALE_OUT.
    ...               Return the ID of the operation associated to the executed scale action.
    [Arguments]   ${ns_name}   ${vnf_member_index}   ${scaling_group}   ${scale_type}   ${vnf_scale_max_wait_time}=2min
    ${vnf_scale_max_wait_time}=   Convert Time   ${vnf_scale_max_wait_time}   result_format=number
    ${vnf_scale_max_wait_time}=   Evaluate   ${vnf_scale_max_wait_time} * ${VIM_TIMEOUT_MULTIPLIER}
    Should Contain Any   ${scale_type}   SCALE_IN   SCALE_OUT   msg=Unknown scale type: ${scale_type}   values=False
    ${osm_vnf_scale_command}=   Set Variable   osm vnf-scale --scaling-group ${scaling_group}
    IF   '${scale_type}'=='SCALE_IN'
        ${osm_vnf_scale_command}=   Catenate   ${osm_vnf_scale_command}   --scale-in
    ELSE
        ${osm_vnf_scale_command}=   Catenate   ${osm_vnf_scale_command}   --scale-out
    END
    ${osm_vnf_scale_command}=   Catenate   ${osm_vnf_scale_command}   ${ns_name}   ${vnf_member_index}
    ${rc}   ${stdout}=   Run And Return Rc And Output   ${osm_vnf_scale_command}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    Wait Until Keyword Succeeds   ${vnf_scale_max_wait_time}   ${VNF_SCALE_POL_TIME}   Check For NS Operation Ended   ${stdout}
    Check For NS Operation Completed   ${stdout}
    RETURN   ${stdout}

Heal Network Service
    [Documentation]   Execute healing operation over one NS.
    ...               Return the ID of the operation associated to the executed healing action.
    [Arguments]   ${ns_name}   ${params}   ${healing_max_wait_time}=10m
    Should Not Be Empty   ${ns_name}
    Should Not Be Empty   ${params}
    ${healing_max_wait_time}=   Convert Time   ${healing_max_wait_time}   result_format=number
    ${healing_max_wait_time}=   Evaluate   ${healing_max_wait_time} * ${VIM_TIMEOUT_MULTIPLIER}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-heal ${ns_name} ${params}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    Wait Until Keyword Succeeds   ${healing_max_wait_time}   ${HEALING_POL_TIME}   Check For NS Operation Ended   ${stdout}
    Check For NS Operation Completed   ${stdout}
    RETURN   ${stdout}

Get Operations List
    [Documentation]   Get the list of operations of a given NS instance.
    [Arguments]   ${ns_name}
    Should Not Be Empty   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-op-list ${ns_name}
    Log   ${stdout}
    Log   ${rc}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}

Check For NS Operation Completed
    [Documentation]   Check wheter the status of the desired operation is "COMPLETED" or not.
    [Arguments]   ${ns_operation_id}
    Should Not Be Empty   ${ns_operation_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-op-show ${ns_operation_id} --literal | yq -r .operationState
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Contain   ${stdout}   COMPLETED   msg=The ns-action with id ${ns_operation_id} was not completed   values=False

Check For NS Operation Failed
    [Documentation]   Check wheter the status of the desired operation is "FAILED" or not.
    [Arguments]   ${ns_operation_id}
    Should Not Be Empty   ${ns_operation_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-op-show ${ns_operation_id} --literal | yq -r .operationState
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Contain   ${stdout}   FAILED   msg=Timeout waiting for ns-action with id ${ns_operation_id}   values=False

Check For NS Operation Ended
    [Documentation]   Check wheter the status of the desired operation is "FAILED" or "COMPLETED".
    [Arguments]   ${ns_operation_id}
    Should Not Be Empty   ${ns_operation_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-op-show ${ns_operation_id} --literal | yq -r .operationState
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Contain Any   ${stdout}   FAILED   COMPLETED   msg=Timeout waiting for ns-action with id ${ns_operation_id}   values=False

Check For NS Operation Cancelled
    [Documentation]   Check whether the operation was cancelled or not.
    [Arguments]   ${ns_operation_id}
    Should Not Be Empty   ${ns_operation_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-op-show ${ns_operation_id} --literal | yq -r '.operationState, .isCancelPending'
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Contain   ${stdout}   FAILED_TEMP\nfalse   msg=Timeout waiting for ns-action with id ${ns_operation_id}   values=False

Get Operations By Type
    [Documentation]   Keyword to get the operation by type
    [Arguments]   ${ns_id}   ${type}
    Should Not Be Empty   ${ns_id}
    Should Not Be Empty   ${type}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-op-list ${ns_id} | grep ${type} | awk '{print $2}' 2>&1
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Not Be Empty   ${stdout}
    RETURN   ${stdout}

Cancel Operation By Id
    [Documentation]   Cancels an ongoing operation by operation ID
    [Arguments]   ${op_id}   ${cancel_mode}=GRACEFUL

    Should Not Be Empty   ${op_id}
    ${rc}   ${stdout}=   Run And Return RC And Output   osm ns-op-cancel ${op_id} --cancel_mode ${cancel_mode} --wait
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${success_return_code}
    RETURN   ${stdout}
