*** 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 to read objects from Juju using juju client.
Library   String
Resource   ssh_lib.resource


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


*** Keywords ***
Get Scale Number
    [Documentation]   Get from Juju the number of units (scale) of a juju application and return it.
    [Arguments]   ${endpoint}   ${username}   ${password}   ${privatekey}   ${application_name}   ${model_name}
    Should Not Be Empty   ${application_name}   ${model_name}
    ${host}=   Remove String Using Regexp   ${endpoint}   :([0-9]+)$
    ${stdout}=   Execute Remote Command Check Rc Return Output   ${host}   ${username}   ${password}   ${privatekey}   juju show-status ${application_name} --format yaml -m ${model_name} | grep scale | awk -F ': ' '{print $2}'
    Log   ${stdout}
    RETURN   ${stdout}

Get Model Name
    [Documentation]   Get from Juju the model from a KDU name and a NS id.
    [Arguments]   ${endpoint}   ${username}   ${password}   ${privatekey}   ${ns_id}   ${kdu_name}
    Should Not Be Empty   ${ns_id}   ${kdu_name}
    ${host}=   Remove String Using Regexp   ${endpoint}   :([0-9]+)$
    ${stdout}=   Execute Remote Command Check Rc Return Output   ${host}   ${username}   ${password}   ${privatekey}   juju models | grep -i ${kdu_name} | grep -i ${ns_id} | cut -f1 -d " "
    Log   ${stdout}
    ${model_name}=   Set Variable   ${kdu_name}-${ns_id}
    Should Be Equal As Strings   ${model_name}   ${stdout}
    RETURN   ${stdout}

Get Application Name VDU Level Charm
    [Documentation]   Get from Juju the application name matching a model and a VDU profile id passed as arguments. The application name must contain the EE passed as argument.
    [Arguments]   ${endpoint}   ${username}   ${password}   ${privatekey}   ${model_name}   ${vdu_profile_id}   ${ee_name}
    Should Not Be Empty   ${model_name}
    ${host}=   Remove String Using Regexp   ${endpoint}   :([0-9]+)$
    ${stdout}=   Execute Remote Command Check Rc Return Output   ${host}   ${username}   ${password}   ${privatekey}   juju status -m ${model_name} | grep "\\-vdu" | grep -v "*" | grep -i ${vdu_profile_id} | cut -f1 -d " "
    Log   ${stdout}
    Should Not Be Empty   ${stdout}
    Should Contain   ${stdout}   ${ee_name}
    RETURN   ${stdout}

Get Application Name NS Level Charm
    [Documentation]   Get from Juju the application name matching a model and a NS charm passed as arguments.
    [Arguments]   ${endpoint}   ${username}   ${password}   ${privatekey}   ${model_name}   ${charm_name}
    Should Not Be Empty   ${model_name}
    ${host}=   Remove String Using Regexp   ${endpoint}   :([0-9]+)$
    ${stdout}=   Execute Remote Command Check Rc Return Output   ${host}   ${username}   ${password}   ${privatekey}   juju status -m ${model_name} | grep "\\-ns" | grep -v "*" | grep -i ${charm_name} | cut -f1 -d " "
    Log   ${stdout}
    Should Not Be Empty   ${stdout}
    RETURN   ${stdout}

Get Application Name VNF Level Charm
    [Documentation]   Get from Juju the application name matching a model and a VNF profile id passed as arguments. The application name must contain the EE passed as argument.
    [Arguments]   ${endpoint}   ${username}   ${password}   ${privatekey}   ${model_name}   ${vnf_profile_id}   ${ee_name}
    Should Not Be Empty   ${model_name}
    ${host}=   Remove String Using Regexp   ${endpoint}   :([0-9]+)$
    ${stdout}=   Execute Remote Command Check Rc Return Output   ${host}   ${username}   ${password}   ${privatekey}   juju status -m ${model_name} | grep "\\-vnf" | grep -v "*" | grep -i ${vnf_profile_id} | cut -f1 -d " "
    Log   ${stdout}
    Should Not Be Empty   ${stdout}
    Should Contain   ${stdout}   ${ee_name}
    RETURN   ${stdout}
