*** 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 manage VIM Targets.

Library   String
Library   Collections
Library   OperatingSystem

Resource   ../lib/prometheus_lib.resource


*** Variables ***
${SUCCESS_RETURN_CODE}   0
${VIM_DELETE_MAX_WAIT_TIME}   1min
${VIM_DELETE_POL_TIME}   15sec
${VIM_STATUS_MAX_WAIT_TIME}   3min
${VIM_STATUS_POL_TIME}   15sec
${VIM_OPSTATE_MAX_WAIT_TIME}   40sec
${VIM_OPSTATE_POL_TIME}   10sec


*** Keywords ***
Create VIM Target
    [Documentation]   Register a VIM account in OSM and return the id.
    ...                 The optional parameters (such as 'config' or 'sdn_controller') are given to this function in name=value format. These parameters will be appended to the 'osm vim-create' command with the next syntax: --param_name=param_value
    ...                 Return the ID of the created VIM Target.
    ...                 Example of execution:
    ...                     \${vim_account_id}=   Create VIM Target   \${vim_name}   \${vim_user}   \${vim_password}   \${vim_auth_url}   \${vim_tenant}   \${vim_account_type}   config='{...}'
    [Arguments]   ${vim_name}   ${vim_user}   ${vim_password}   ${vim_auth_url}   ${vim_tenant}   ${vim_account_type}   @{optional_parameters}
    ${osm_vim_create_command}=   Set Variable   osm vim-create --name ${vim_name} --user ${vim_user} --password ${vim_password} --auth_url ${vim_auth_url} --tenant ${vim_tenant} --account_type ${vim_account_type}
    FOR   ${param}   IN   @{optional_parameters}
        ${match}   ${param_name}   ${param_value}=   Should Match Regexp   ${param}   (.+)=(.+)   msg=Syntax error in optional parameters
        Log   ${match},${param_name},${param_value}
        ${osm_vim_create_command}=   Catenate   ${osm_vim_create_command}   --${param_name}=${param_value}
    END
    ${rc}   ${stdout}=   Run And Return Rc And Output   ${osm_vim_create_command}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Delete VIM Target
    [Documentation]   Unregister/delete a VIM account from OSM.
    [Arguments]   ${vim_name}
    ${rc}   ${stdout}=   Run Keyword And Continue On Failure   Run And Return Rc And Output   osm vim-delete ${vim_name}
    Log   ${rc},${stdout}
    Wait Until Keyword Succeeds   ${VIM_DELETE_MAX_WAIT_TIME}   ${VIM_DELETE_POL_TIME}   Check For VIM Target   ${vim_name}

Get VIM Targets
    [Documentation]   Get the list of VIM accounts in OSM.
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vim-list
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}

Check For VIM Target
    [Documentation]   Check if a VIM account has been registered in OSM.
    [Arguments]   ${vim_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vim-list | awk '{print $2}' | grep ${vim_name}
    Log   ${rc},${stdout}
    Should Not Be Equal As Strings   ${stdout}   ${vim_name}

Check For VIM Target Metric
    [Documentation]   Check in a loop if the metric of osm_vim_status is present in Prometheus for the VIM account in OSM.
    [Arguments]   ${vim_name}   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}
    ${vim_account_id}=   Get VIM Target ID   ${vim_name}
    Wait Until Keyword Succeeds   ${VIM_STATUS_MAX_WAIT_TIME}   ${VIM_STATUS_POL_TIME}   Check If VIM Target Has Metric   ${vim_account_id}   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}

Check For VIM Target Status
    [Documentation]   Check in a loop if the value of the metric osm_vim_status in Prometheus for the VIM account is 1 (reachable).
    [Arguments]   ${vim_name}   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}
    ${vim_account_id}=   Get VIM Target ID   ${vim_name}
    Wait Until Keyword Succeeds   ${VIM_STATUS_MAX_WAIT_TIME}   ${VIM_STATUS_POL_TIME}   Check If VIM Target Is Available   ${vim_account_id}   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}

Get VIM Target ID
    [Documentation]   Get from OSM the VIM account id associated to the VIM account name passed as parameter.
    [Arguments]   ${vim_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vim-list | grep " ${vim_name} " | awk '{print $4}'
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Not Be Equal As Strings   ${stdout}   ${EMPTY}   msg=VIM Target '${vim_name}' not found   values=false
    RETURN   ${stdout}

Check VIM Target Operational State
    [Documentation]   Check if the VIM account in OSM is enabled.
    [Arguments]   ${vim_name}
    Wait Until Keyword Succeeds   ${VIM_OPSTATE_MAX_WAIT_TIME}   ${VIM_OPSTATE_POL_TIME}   Check If VIM Target Is Enabled   ${vim_name}

Check If VIM Target Is Enabled
    [Documentation]   Check if the VIM account passed as argument is enabled in OSM (meaning that the RO could reach the VIM account).
    [Arguments]   ${vim_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vim-list | grep " ${vim_name} " | awk '{print $6}'
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Be Equal As Strings   ${stdout}   ENABLED   msg=VIM Target '${vim_name}' is not enabled   values=false

Check If VIM Target Is Available
    [Documentation]   Check if the value of the metric osm_vim_status in Prometheus for the VIM account is 1 (reachable).
    [Arguments]   ${vim_account_id}   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}
    Log   ${prometheus_url}
    Log   ${prometheus_user}
    Log   ${prometheus_password}
    Log   ${vim_account_id}
    ${metric}=   Get Metric   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}   osm_vim_status   vim_account_id=${vim_account_id}
    Should Be Equal As Integers   ${metric}   1   msg=VIM Target '${vim_account_id}' is not active   values=false

Check If VIM Target Has Metric
    [Documentation]   Check if the metric of osm_vim_status is present in Prometheus for the VIM account in OSM.
    [Arguments]   ${vim_account_id}   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}
    ${metric}=   Get Metric   ${prometheus_url}   ${prometheus_user}   ${prometheus_password}   osm_vim_status   vim_account_id=${vim_account_id}
    Should Be True   ${metric} <2   msg=VIM Target '${vim_account_id}' has no metric
