*** 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_LAUNCH_POL_TIME}   30sec
${NS_DELETE_POL_TIME}   15sec
${VIM_TIMEOUT_MULTIPLIER}   %{OSM_VIM_MULTIPLIER_TIMEOUT=1.0}


*** Keywords ***
Create Network Service
    [Documentation]   Launch the instantation of a NS and verify in a loop that the NS instance is successfully created in a given time, and return the NS instance id.
    [Arguments]   ${nsd}   ${vim_name}   ${ns_name}   ${ns_config}   ${publickey}   ${ns_launch_max_wait_time}=5min   ${config_file}=${EMPTY}
    ${ns_launch_max_wait_time}=   Convert Time   ${ns_launch_max_wait_time}   result_format=number
    ${ns_launch_max_wait_time}=   Evaluate   ${ns_launch_max_wait_time} * ${VIM_TIMEOUT_MULTIPLIER}
    Log   ${ns_launch_max_wait_time}
    ${config_attr}=   Set Variable If   '${ns_config}'!='${EMPTY}'   --config '${ns_config}'   ${EMPTY}
    ${sshkeys_attr}=   Set Variable If   '${publickey}'!='${EMPTY}'   --ssh_keys ${publickey}   ${EMPTY}
    ${config_file_attr}=   Set Variable If   '${config_file}'!='${EMPTY}'   --config_file '${config_file}'   ${EMPTY}
    ${ns_id}=   Instantiate Network Service   ${ns_name}   ${nsd}   ${vim_name}   ${config_attr} ${sshkeys_attr} ${config_file_attr}
    Log   ${ns_id}
    Wait Until Keyword Succeeds   ${ns_launch_max_wait_time}   ${NS_LAUNCH_POL_TIME}   Check For NS Instance To Configured   ${ns_name}
    Check For NS Instance For Failure   ${ns_name}
    RETURN   ${ns_id}

Instantiate Network Service
    [Documentation]   Launch the instantation of a NS, and return the NS instance id.
    [Arguments]   ${ns_name}   ${nsd}   ${vim_name}   ${ns_extra_args}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-create --ns_name ${ns_name} --nsd_name ${nsd} --vim_account ${vim_name} ${ns_extra_args}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Get Vnf Management Ip Address
    [Documentation]   Get the mgmt IP address of a member VNF in a NS instance, and return it.
    [Arguments]   ${ns_id}   ${vnf_member_index}
    Should Not Be Empty   ${ns_id}
    Should Not Be Empty   ${vnf_member_index}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnf-list --filter member-vnf-index-ref=${vnf_member_index} | grep ${ns_id} | awk '{print $14}' 2>&1
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Get Vnf Id
    [Documentation]   Get the VNF instance ID of a member VNF in a NS instance, and return it.
    [Arguments]   ${ns_id}   ${vnf_member_index}
    Should Not Be Empty   ${ns_id}
    Should Not Be Empty   ${vnf_member_index}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnf-list --filter member-vnf-index-ref=${vnf_member_index} | grep ${ns_id} | awk '{print $2}' 2>&1
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Get Ns Id
    [Documentation]   Get the NS instance ID from a NS instance name passed as argument, and return it.
    [Arguments]   ${ns_name}
    Should Not Be Empty   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-list | grep ${ns_name} | awk '{print $4}' 2>&1
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Not Be Empty   ${stdout}
    RETURN   ${stdout}

Get Ns List
    [Documentation]   Get the list of NS instances and return it.
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-list 2>&1
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Get Ns Vnf List
    [Documentation]   Get the list of VNF instances of a given NS instance id, and return it.
    [Arguments]   ${ns_id}
    Should Not Be Empty   ${ns_id}
    @{vnf_list_string}=   Run And Return Rc And Output   osm vnf-list --ns ${ns_id} | grep ${ns_id} | awk '{print $2}' 2>&1
    # Returns a String of vnf_id and needs to be converted into a list
    @{vnf_list}=   Split String   ${vnf_list_string}[1]
    Log List   ${vnf_list}
    RETURN   @{vnf_list}

Get Ns Ip List
    [Documentation]   Obtain the list of IP addresses of all VM in a NS, and return it.
    [Arguments]   @{vnf_list}
    Should Not Be Empty   @{vnf_list}
    @{temp_list}=   Create List
    FOR   ${vnf_id}   IN   @{vnf_list}
        Log   ${vnf_id}
        @{vnf_ip_list}=   Get Vnf Ip List   ${vnf_id}
        @{temp_list}=   Combine Lists   ${temp_list}   ${vnf_ip_list}
    END
    Should Not Be Empty   ${temp_list}
    RETURN   @{temp_list}

Get Vnf Ip List
    [Documentation]   Obtain the list of IP addresses of all VM in a VNF, and return it.
    [Arguments]   ${vnf_id}
    Should Not Be Empty   ${vnf_id}
    @{vnf_ip_list_string}=   Run And Return Rc And Output   osm vnf-list --filter id=${vnf_id} | grep -o '[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}\\.[0-9]\\{1,3\\}' | sort -t: -u -k1,1 2>&1
    # returns a string of ip addresses and needs to be converted into a list
    Should Not Be Empty   ${vnf_ip_list_string}[1]
    @{vnf_ip_list}=   Split String   ${vnf_ip_list_string}[1]
    Log Many   ${vnf_ip_list}
    Should Not Be Empty   ${vnf_ip_list}
    RETURN   @{vnf_ip_list}

Check For Ns Instance To Configured
    [Documentation]   Check if a given NS instance has completed the instantiation, no matter if it succeeded (the status should be READY or BROKEN).
    [Arguments]   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server list
    Log   ${rc},${stdout}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-list --filter name="${ns_name}"
    Log   ${rc},${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Contain Any   ${stdout}   READY   BROKEN

Check For NS Instance For Failure
    [Documentation]   Check if a given NS instance has not failed in the instantiation (the status should not be BROKEN).
    [Arguments]   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server list
    Log   ${rc},${stdout}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-list --filter name="${ns_name}"
    Log   ${rc},${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Should Not Contain   ${stdout}   BROKEN

Check For NS Instance To Be Deleted
    [Documentation]   Check if a given NS instance is not present in OSM.
    [Arguments]   ${ns}
    ${rc}   ${stdout}=   Run And Return Rc And Output   openstack server list
    Log   ${rc},${stdout}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-list | awk '{print $2}' | grep ${ns}
    Log   ${rc},${stdout}
    Should Not Be Equal As Strings   ${stdout}   ${ns}

Delete NS
    [Documentation]   Delete a NS instance.
    [Arguments]   ${ns}   ${ns_delete_max_wait_time}=4min
    ${ns_delete_max_wait_time}=   Convert Time   ${ns_delete_max_wait_time}   result_format=number
    ${ns_delete_max_wait_time}=   Evaluate   ${ns_delete_max_wait_time} * ${VIM_TIMEOUT_MULTIPLIER}
    Log   ${ns_delete_max_wait_time}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-delete ${ns}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Wait Until Keyword Succeeds   ${ns_delete_max_wait_time}   ${NS_DELETE_POL_TIME}   Check For NS Instance To Be Deleted   ${ns}

Get Ns Vnfr Ids
    [Documentation]   Return a list with the IDs of the VNF records of a NS instance.
    [Arguments]   ${ns_id}
    Should Not Be Empty   ${ns_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnf-list --ns ${ns_id} | grep ${ns_id} | awk '{print $2}' 2>&1
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    @{vnfr}=   Split String   ${stdout}
    RETURN   @{vnfr}

Get Vnf Vdur Names
    [Documentation]   Return a list with the names of the VDU records of a VNF instance.
    [Arguments]   ${vnf_id}
    Should Not Be Empty   ${vnf_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnf-show ${vnf_id} --literal | yq -r .vdur[].name
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    @{vdur}=   Split String   ${stdout}
    RETURN   @{vdur}

Check Kdu Labels
    [Documentation]   Return the labels of the deployed Kubernetes objects associated with a NS instance.
    [Arguments]   ${ns_id}   ${vnf_id}   ${kdu_name}
    Should Not Be Empty   ${ns_id}
    Should Not Be Empty   ${vnf_id}
    Should Not Be Empty   ${kdu_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-show ${ns_id} --literal | yq -r '._admin.deployed.K8s[0]."detailed-status"' | yq -r '.manifest[0].metadata.labels'
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    ${labels}=   Evaluate   json.loads('''${stdout}''')   json
    Should Be Equal As Strings   ${labels["managed-by"]}   osm.etsi.org   msg=${labels}
    Should Be Equal As Strings   ${labels["osm.etsi.org/ns-id"]}   ${ns_id}   msg=${labels}
    Should Be Equal As Strings   ${labels["osm.etsi.org/vnf-id"]}   ${vnf_id}   msg=${labels}
    Should Be Equal As Strings   ${labels["osm.etsi.org/kdu-name"]}   ${kdu_name}   msg=${labels}
    RETURN   ${labels}

Get Vnf Kdu Replica Count
    [Documentation]   Return the number of KDU replicas (empty if none) of a VNF instance.
    [Arguments]   ${vnf_id}   ${kdu_name}
    Should Not Be Empty   ${vnf_id}
    Should Not Be Empty   ${kdu_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnf-show ${vnf_id} --kdu ${kdu_name} | yq -r .config.replicaCount
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    ${return}=   Set Variable If   '${stdout}' == 'null'   ${EMPTY}   ${stdout}
    RETURN   ${return}

Get Application Names
    [Documentation]   Return the list of the application of a VNF instance.
    [Arguments]   ${ns_name}
    Should Not Be Empty   ${ns_name}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm ns-show ${ns_name} --literal | yq -r ._admin.deployed.VCA[].application
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    @{app_names}=   Split String   ${stdout}
    RETURN   ${app_names}

Get VDU Affinity Group Name
    [Documentation]   Return the affinity group name for a NF
    [Arguments]   ${ns_name}   ${vnf_id}   ${vdur_id}
    ${rc}   ${affinity_group_id}=   Run And Return Rc And Output   osm vnf-show ${vnf_id} --literal | yq '.vdur[] | select(.id == "'${vdur_id}'")' | yq -r '."affinity-or-anti-affinity-group-id"[0]' 2>&1
    Log   ${affinity_group_id}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${affinity_group_id}   values=False
    ${rc}   ${affinity_group_name}=   Run And Return Rc And Output   osm ns-show ${ns_name} --literal | yq '."affinity-or-anti-affinity-group"[] | select(.id == "'${affinity_group_id}'")' | yq -r '.name' 2>&1
    Log   ${affinity_group_name}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${affinity_group_name}   values=False
    RETURN   ${affinity_group_name}

Get Dual Ip
    [Documentation]   Get dual ip from the ns list
    [Arguments]   ${ns_id}
    Should Not Be Empty   ${ns_id}
    ${rc}   ${stdout}=   Run And Return RC And Output   osm ns-show ${ns_id} --literal | grep -A2 ip-address | grep -v "ip-address:" | awk '{print $2}' | tr -d ',-'
    Log   ${rc},${stdout}
    RETURN   ${stdout}

Get NSD Id From The NS
    [Documentation]   Get the nsd id from the ns
    [Arguments]   ${ns_id}
    Should Not Be Empty   ${ns_id}
    ${stdout}=   Run   osm ns-show ${ns_id} --literal | grep nsd-id | awk '{print $2}'
    Log   ${stdout}
    RETURN   ${stdout}

Get Additional Params And Values From Kdu
    [Documentation]   Return the value of additional params (empty if none) of a KNF instance.
    [Arguments]   ${ns_name}   ${param_name}
    Should Not Be Empty   ${ns_name}
    Should Not Be Empty   ${param_name}
    ${rc0}   ${stdout0}=   Run And Return Rc And Output   osm ns-show ${ns_name} --literal | yq -e '._admin.deployed' | yq '.K8s[]."detailed-status"'
    Should Be Equal As Integers   ${rc0}   ${SUCCESS_RETURN_CODE}   msg=${stdout0}   values=False
    ${rc}   ${stdout}=   Run And Return Rc And Output   echo ${stdout0} | yq '.config.${param_name}'
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    ${return}=   Set Variable If   '${stdout}' == 'null'   ${EMPTY}   ${stdout}
    RETURN   ${return}
