Feature 10908: Update charm in running VNF instance
[osm/tests.git] / robot-systest / lib / ns_lib.robot
index 6a8c84b..a7c6cb9 100644 (file)
 #   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   DateTime
+
+
 *** Variables ***
 ${success_return_code}   0
 ${ns_launch_pol_time}   30sec
-${ns_delete_max_wait_time}   1min
 ${ns_delete_pol_time}   15sec
 ${ns_action_max_wait_time}   1min
 ${ns_action_pol_time}   15sec
-${vnf_scale_max_wait_time}   5min
-${vnf_scale_pol_time}   30sec
+${vnf_scale_pol_time}   15sec
+${vim_timeout_multiplier}   %{OSM_VIM_MULTIPLIER_TIMEOUT=1.0}
 
 
 *** Keywords ***
 Create Network Service
-    [Arguments]   ${nsd}   ${vim_name}   ${ns_name}   ${ns_config}   ${publickey}   ${ns_launch_max_wait_time}=5min
+    [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}'   \
     ${sshkeys_attr}   Set Variable If   '${publickey}'!='${EMPTY}'   --ssh_keys ${publickey}   \
+    ${config_file_attr}   Set Variable If   '${config_file}'!='${EMPTY}'   --config_file '${config_file}'   \
 
-    ${ns_id}=   Instantiate Network Service   ${ns_name}   ${nsd}   ${vim_name}   ${config_attr} ${sshkeys_attr}
+    ${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}
@@ -45,6 +54,15 @@ Instantiate Network Service
     [Return]  ${stdout}
 
 
+Update Network Service
+    [Arguments]   ${ns_id}   ${update_type}   ${ns_update_config}   ${ns_update_timeout}
+
+    ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-update ${ns_id} --updatetype ${update_type} --config ${ns_update_config} --timeout ${ns_update_timeout} --wait
+    log   ${stdout}
+    Should Be Equal As Integers   ${rc}   ${success_return_code}
+    [Return]  ${stdout}
+
+
 Get Vnf Management Ip Address
     [Arguments]   ${ns_id}   ${vnf_member_index}
 
@@ -56,6 +74,28 @@ Get Vnf Management Ip Address
     [Return]  ${stdout}
 
 
+Get Vnf Id
+    [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
+    [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 Vnf List
     [Arguments]   ${ns_id}
 
@@ -85,7 +125,7 @@ Get Vnf Ip List
     [arguments]   ${vnf_id}
 
     should not be empty   ${vnf_id}
-    @{vnf_ip_list_string}=   run and return rc and output   osm vnf-show ${vnf_id} --filter vdur --literal | 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
+    @{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]
@@ -97,6 +137,8 @@ Get Vnf Ip List
 Check For Ns Instance To Configured
     [arguments]  ${ns_name}
 
+    ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
+    log   ${stdout}
     ${rc}   ${stdout}=   run and return rc and output   osm ns-list --filter name="${ns_name}"
     log   ${stdout}
     Should Be Equal As Integers   ${rc}   ${success_return_code}
@@ -105,6 +147,8 @@ Check For Ns Instance To Configured
 Check For NS Instance For Failure
     [Arguments]  ${ns_name}
 
+    ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
+    log   ${stdout}
     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list --filter name="${ns_name}"
     log   ${stdout}
     Should Be Equal As Integers   ${rc}   ${success_return_code}
@@ -113,15 +157,20 @@ Check For NS Instance For Failure
 Check For NS Instance To Be Deleted
     [Arguments]  ${ns}
 
+    ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
+    log   ${stdout}
     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | awk '{print $2}' | grep ${ns}
     Should Not Be Equal As Strings   ${stdout}   ${ns}
 
 Delete NS
-    [Documentation]  Delete ns
-    [Arguments]  ${ns}
+    [Documentation]   Delete ns
+    [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}
+    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}
@@ -151,13 +200,40 @@ Execute NS Action
     [Return]  ${stdout}
 
 
+Execute NS K8s Action
+    [Documentation]     Execute an action over the desired K8s NS.
+    ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
+    ...                 Return the ID of the operation associated to the executed action.
+    ...                 Examples of execution:
+    ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
+    ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
+
+    [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  ${kdu_name}  @{action_params}
+
+    ${params}=  Set Variable  ${EMPTY}
+    FOR  ${param}  IN  @{action_params}
+        ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
+        ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
+    END
+    ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index} --kdu_name ${kdu_name}
+    ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
+    ...  ELSE  Set Variable  ${osm_ns_action_command}
+    ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
+    ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
+    Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
+    Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
+    [Return]  ${stdout}
+
+
 Execute Manual VNF Scale
     [Documentation]     Execute a manual VNF Scale action.
     ...                 The parameter 'scale_type' must be SCALE_IN or SCALE_OUT.
     ...                 Return the ID of the operation associated to the executed scale action.
 
-    [Arguments]  ${ns_name}  ${vnf_member_index}  ${scaling_group}  ${scale_type}
+    [Arguments]   ${ns_name}   ${vnf_member_index}   ${scaling_group}   ${scale_type}   ${vnf_scale_max_wait_time}=2min
 
+    ${vnf_scale_max_wait_time}=   Convert Time   ${vnf_scale_max_wait_time}   result_format=number
+    ${vnf_scale_max_wait_time}=   Evaluate   ${vnf_scale_max_wait_time} * ${vim_timeout_multiplier}
     Should Contain Any  ${scale_type}  SCALE_IN  SCALE_OUT  msg=Unknown scale type: ${scale_type}  values=False
     ${osm_vnf_scale_command}=  Set Variable  osm vnf-scale --scaling-group ${scaling_group}
     ${osm_vnf_scale_command}=  Run Keyword If  '${scale_type}'=='SCALE_IN'  Catenate  ${osm_vnf_scale_command}  --scale-in
@@ -165,7 +241,7 @@ Execute Manual VNF Scale
     ${osm_vnf_scale_command}=  Catenate  ${osm_vnf_scale_command}  ${ns_name}  ${vnf_member_index}
     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_vnf_scale_command}
     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
-    Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
+    Wait Until Keyword Succeeds  ${vnf_scale_max_wait_time}  ${vnf_scale_pol_time}  Check For NS Operation Completed  ${stdout}
     [Return]  ${stdout}
 
 
@@ -183,7 +259,7 @@ Check For NS Operation Completed
 
     [Arguments]  ${ns_operation_id}
 
-    ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq r - operationState
+    ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
     log  ${stdout}
     Should Be Equal As Integers  ${rc}  ${success_return_code}
     Should Contain  ${stdout}  COMPLETED  msg=Timeout waiting for ns-action with id ${ns_operation_id}  values=False
@@ -205,8 +281,55 @@ Get Vnf Vdur Names
 
     [Arguments]  ${vnf_id}
 
-    ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --literal | yq r - vdur.*.name
+    ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --literal | yq .vdur[].name | tr -d \\"
     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
     @{vdur} =  Split String  ${stdout}
     [Return]  @{vdur}
 
+
+Get Vnf Kdu Replica Count
+    [Documentation]     Return the number of KDU replicas (empty if none) of a VNF instance.
+
+    [Arguments]   ${vnf_id}   ${kdu_name}
+
+    ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --kdu ${kdu_name} | yq .config.replicaCount | tr -d \\"
+    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}
+
+    ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-show ${ns_name} --literal | yq ._admin.deployed.VCA[].application | tr -d \\"
+    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 '."affinity-or-anti-affinity-group-id"[0]' | tr -d \\" 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 '.name' | tr -d \\" 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 Vnf Namespace
+    [Documentation]    Return the KDU's namespace
+
+    [Arguments]   ${vnf_id}
+
+    ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --literal | yq '.kdur[]."k8s-namespace"' | tr -d \\"
+    Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
+    Should Not Be Empty   ${stdout}
+    log  namespace: ${stdout}
+    [Return]  ${stdout}