*** Comments ***
# Copyright 2019 Tech Mahindra Limited
#
# All Rights Reserved.
#
# 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 VNFD / VNF packages with OSM client.
Library   OperatingSystem
Library   String


*** Variables ***
${SUCCESS_RETURN_CODE}   0
${FAILURE_RETURN_CODE}   1
${VNFD_DELETE_MAX_WAIT_TIME}   1min
${VNFD_DELETE_POL_TIME}   15sec


*** Keywords ***
Get VNFDs List
    [Documentation]   Get the list of VNF packages and log it.
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-list
    Log   ${stdout}
    Log   ${rc}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}

Create VNFD
    [Documentation]   Onboards ("creates") a NF Package into OSM.
    ...               - Parameters:
    ...                 - vnfd_pkg: Name (and location) of the NF Package
    ...                 - overrides (optional): String with options to override the EPA and/or interface properties of the Package.
    ...                                        This is very useful to allow to deploy e.g. non-EPA packages in EPA VIMs (or vice-versa).
    ...                                        Valid strings are the same as in the command. E.g.:
    ...                                        - `--override-epa`: adds EPA attributes to all VDUs.
    ...                                        - `--override-nonepa`: removes all EPA attributes from all VDUs.
    ...                                        - `--override-paravirt`: converts all interfaces to `PARAVIRT`. This one can be combined with
    ...                                           the others above (e.g. '--override-nonepa --override-paravirt').
    ...               - Relevant environment variables:
    ...                  - OVERRIDES: If the environment variable "OVERRIDES" exists, it prevails over the value in the argument.
    ...                               This is often more convenient to enforce the same behaviour for every test run in a given VIM.
    [Arguments]   ${vnfd_pkg}   ${overrides}=${EMPTY}
    # If env variable "OVERRIDES" exists, it prevails over the value in the argument
    ${overrides}=   Get Environment Variable   OVERRIDES   default=${overrides}
    # Proceeds with the onboarding with the appropriate arguments
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-create ${overrides} ${vnfd_pkg}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    ${lines}=   Get Line Count   ${stdout}
    ${last}=   Evaluate   ${lines} - 1
    ${id}=   Get Line   ${stdout}   ${last}
    RETURN   ${id}

Update VNFD
    [Documentation]   Onboard ("Update") a NF Package into OSM.
    ...               - Parameters:
    ...                 - vnfd_pkg: Name (and location) of the NF Package
    ...                 - vnfd_name: Name of the existing NF Package
    [Arguments]   ${vnfd_pkg}   ${vnfd_name}
    # Proceeds with the onboarding with the appropriate arguments
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-update --content ${vnfd_pkg} ${vnfd_name}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    ${lines}=   Get Line Count   ${stdout}
    ${last}=   Evaluate   ${lines} - 1
    ${id}=   Get Line   ${stdout}   ${last}
    RETURN   ${id}

Create VNFD Overriding Fields
    [Documentation]   Onboards ("creates") a NF Package into OSM.
    ...               - Parameters:
    ...                 - vnfd_pkg: Name (and location) of the NF Package
    ...                 - override_fields: String with options to override fields in descriptor, format: "key1.key2...=value[;key3...=value;...]"
    ...                 - overrides (optional): String with options to override the EPA and/or interface properties of the Package.
    ...                                        This is very useful to allow to deploy e.g. non-EPA packages in EPA VIMs (or vice-versa).
    ...                                        Valid strings are the same as in the command. E.g.:
    ...                                        - `--override-epa`: adds EPA attributes to all VDUs.
    ...                                        - `--override-nonepa`: removes all EPA attributes from all VDUs.
    ...                                        - `--override-paravirt`: converts all interfaces to `PARAVIRT`. This one can be combined with
    ...                                           the others above (e.g. '--override-nonepa --override-paravirt').
    ...               - Relevant environment variables:
    ...                  - OVERRIDES: If the environment variable "OVERRIDES" exists, it prevails over the value in the argument.
    ...                               This is often more convenient to enforce the same behaviour for every test run in a given VIM.
    [Arguments]   ${vnfd_pkg}   ${override_fields}   ${overrides}=${EMPTY}
    # If env variable "OVERRIDES" exists, it prevails over the value in the argument
    ${overrides}=   Get Environment Variable   OVERRIDES   default=${overrides}
    # Proceeds with the onboarding with the appropriate arguments
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-create ${overrides} ${vnfd_pkg} --override '${override_fields}'
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    ${lines}=   Get Line Count   ${stdout}
    ${last}=   Evaluate   ${lines} - 1
    ${id}=   Get Line   ${stdout}   ${last}
    RETURN   ${id}

Delete VNFD
    [Documentation]   Delete a VNF package from OSM.
    [Arguments]   ${vnfd_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-delete ${vnfd_id}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${SUCCESS_RETURN_CODE}
    Wait Until Keyword Succeeds   ${VNFD_DELETE_MAX_WAIT_TIME}   ${VNFD_DELETE_POL_TIME}   Check For VNFD   ${vnfd_id}

Assert Failure Delete VNFD
    [Documentation]   Deletes a NF Package that cannot be deleted and asserts the failure
    [Arguments]   ${vnfd_id}
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-delete ${vnfd_id}
    Log   ${stdout}
    Should Be Equal As Integers   ${rc}   ${FAILURE_RETURN_CODE}
    Should Contain   ${stdout}   409   msg=Expected Conflict   values=False
    Wait Until Keyword Succeeds   ${VNFD_DELETE_MAX_WAIT_TIME}   ${VNFD_DELETE_POL_TIME}   Check For VNFD   ${vnfd_id}   True

Check For VNFD
    [Documentation]   Check that a VNF package exists in OSM.
    [Arguments]   ${vnfd_id}   ${exists}=False
    ${rc}   ${stdout}=   Run And Return Rc And Output   osm vnfpkg-list | awk '{print $2}' | grep ${vnfd_id}
    Log   ${rc},${stdout}
    IF   ${exists}
        Should Be Equal As Strings   ${stdout}   ${vnfd_id}
    ELSE
        Should Not Be Equal As Strings   ${stdout}   ${vnfd_id}
    END
