*** 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 providing keywords for CRUD operations over VNF instances with OSM client.
Library   OperatingSystem
Library   String


*** Variables ***
${SUCCESS_RETURN_CODE}   0


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

Get VNF VIM ID
    [Documentation]   Get the identifier of the first VDU of a VNF instance. Useful to get the id when a VNF has a single VDU.
    [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[]."vim-id"'
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    RETURN   ${stdout}

Get VDU List From VNF
    [Documentation]   Return a list of the VDU instance ids 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 '.vdur[].id'
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    @{vdur_ids}=   Split String   ${stdout}
    RETURN   @{vdur_ids}

Get VDU VIM Id
    [Documentation]   Return the VIM VM ID for a VDU
    [Arguments]   ${vnf_id}   ${vdur_id}
    Should Not Be Empty   ${vnf_id}
    ${rc}   ${vm_vim_id}=   Run And Return Rc And Output   osm vnf-show ${vnf_id} --literal | yq '.vdur[] | select(.id == "'${vdur_id}'")' | yq -r '."vim-id"'
    Log   ${vm_vim_id}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${vm_vim_id}   values=False
    RETURN   ${vm_vim_id}

Get Vnf Vdur IPs
    [Documentation]   Return a list with the IP addresses 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[].interfaces[]."ip-address"'
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    @{ip}=   Split String   ${stdout}
    RETURN   @{ip}

Get Vdu Attribute
    [Documentation]   Return an attribute from VDU records selected by count_index of a VNF instance.
    [Arguments]   ${vnf_id}   ${attribute}   ${count_index}=0
    Should Not Be Empty   ${vnf_id}
    Should Not Be Empty   ${attribute}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnf-show ${vnf_id} --literal | yq '.vdur[] | select(."count-index" == ${count_index})' | yq -r '."${attribute}"'
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}   msg=${stdout}   values=False
    RETURN   ${stdout}
