*** 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 with sol003 keywords and variables .

Library   String
Library   OperatingSystem
Library   RequestsLibrary
Library   JsonValidator
Library   yaml
Library   JSONLibrary

Resource   ../lib/vim_lib.resource


*** Variables ***
@{SUCCESS_STATUS_CODE_LIST}   200   201   202   204
${AUTH_TOKEN_URI}   /osm/admin/v1/tokens
${REST_API_HOST}   ${EMPTY}
${OSM_USER}   %{OSM_USER=admin}
${OSM_PASSWORD}   %{OSM_PASSWORD=admin}
${OSM_PROJECT}   %{OSM_PROJECT=admin}


*** Keywords ***
Get Auth Token
    [Documentation]   Send a REST POST message to OSM to get an authentication token,
    ...               and store the authentication token in a suite variable to be used later on.
    Set REST API Host
    Create Session   osmhit   ${REST_API_HOST}   disable_warnings=1
    &{headers}=   Create Dictionary   Content-Type=application/json   Accept=application/json
    &{data}=   Create Dictionary   username=${OSM_USER}   password=${OSM_PASSWORD}   project-id=${OSM_PROJECT}
    ${resp}=   Post On Session   osmhit   ${AUTH_TOKEN_URI}   json=${data}   headers=${headers}
    Log   ${resp}
    Pass Execution If   ${resp.status_code} in ${SUCCESS_STATUS_CODE_LIST}   Get Auth Token completed
    Set Suite Variable   ${TOKEN_STATUS_CODE}   ${resp.status_code}
    ${access_token}=   Get Value From Json   ${resp.json()}   $..id
    Set Test Variable   ${ACCESS_TOKEN}   ${access_token[0]}
    Set Test Variable   ${TOKEN_RESPONSE}   ${resp}
    Sleep   2s

Set Dockerized Host
    [Documentation]   Set the suite variable REST_API_HOST to be used in this library and the testsuites using it.
    ...               This method must be used if the tests are run from a container in the same K8s cluster where OSM is running.
    [Arguments]   ${env_host}
    Set Suite Variable   ${REST_API_HOST}   https://${env_host}

Set Standalone Host
    [Documentation]   Set a suite variable REST_API_HOST to be used in this library and the testsuites using it.
    ...               This method must be used if the tests are run from any place outside the K8s cluster where OSM is running.
    [Arguments]   ${env_host}
    Set Suite Variable   ${REST_API_HOST}   https://${env_host}:9999

Read Directory
    [Documentation]   Read Current Directory and return it
    ${Directory}=   Replace String   ${CURDIR}   lib   resources
    ${json_path}=   Set Variable   ${Directory}
    RETURN   ${json_path}

Post Api Request
    [Documentation]   Send to OSM a POST API message to OSM to the given uri with the given json data,
    ...               and save response and status code in suite variables to be used later on.
    [Arguments]   ${PostApi}   ${json_data}
    Create Session   APISession   ${REST_API_HOST}
    &{headers}=   Create Dictionary   Content-Type=application/json   Accept=application/json   Authorization=Bearer ${ACCESS_TOKEN}
    ${request_response}=   Post On Session   APISession   ${PostApi}   json=${json_data}   headers=${headers}
    Set Suite Variable   ${REQUEST_RESPONSE}   ${request_response}
    ${response_statuscode}=   Convert To String   ${request_response.status_code}
    Set Suite Variable   ${RESPONSE_STATUSCODE}   ${response_statuscode}

Set REST API Host
    [Documentation]   Set the REST_API_HOST suite variable from OSM_HOSTNAME env variable
    ${nbi_host}=   Get Environment Variable   OSM_HOSTNAME
    ${passed}=   Run Keyword And Return Status   Should Contain   ${nbi_host}   :
    IF   ${passed}
        Set Dockerized Host   ${nbi_host}
    ELSE
        Set Standalone Host   ${nbi_host}
    END

Get ID
    [Documentation]   Examine a REQUEST_RESPONSE dictionary as JSON and return the requested key passed as argument.
    [Arguments]   ${Key}
    Pass Execution If   ${REQUEST_RESPONSE.status_code} in ${SUCCESS_STATUS_CODE_LIST}   Get Auth Token completed
    ${id}=   Get Value From Json   ${REQUEST_RESPONSE.json()}   $..${Key}
    RETURN   ${id[0]}

Get Api Request
    [Documentation]   Send to OSM a GET API message to OSM to the given uri,
    ...               and save response and status code in suite variables to be used later on.
    [Arguments]   ${uri}
    Create Session   Session   ${REST_API_HOST}
    &{headers}=   Create Dictionary   Content-Type=application/json   Accept=application/json   Authorization=Bearer ${ACCESS_TOKEN}
    ${request_response}=   Get On Session   Session   ${uri}   headers=${headers}
    Set Suite Variable   ${REQUEST_RESPONSE}   ${request_response}
    ${response_statuscode}=   Convert To String   ${request_response.status_code}
    Set Suite Variable   ${RESPONSE_STATUSCODE}   ${response_statuscode}
