36ecd574b63b8ad02d5048a1922a7cae4020644a
[osm/tests.git] / robot-systest / lib / ns_lib.robot
1 #   Licensed under the Apache License, Version 2.0 (the "License");
2 #   you may not use this file except in compliance with the License.
3 #   You may obtain a copy of the License at
4 #
5 #       http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #   Unless required by applicable law or agreed to in writing, software
8 #   distributed under the License is distributed on an "AS IS" BASIS,
9 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 #   See the License for the specific language governing permissions and
11 #   limitations under the License.
12
13 *** Settings ***
14 Documentation   Library to deploy and delete NS, and run operations on them.
15
16 Library   DateTime
17
18
19 *** Variables ***
20 ${success_return_code}   0
21 ${ns_launch_pol_time}   30sec
22 ${ns_delete_pol_time}   15sec
23 ${ns_action_max_wait_time}   1min
24 ${ns_action_pol_time}   15sec
25 ${vnf_scale_pol_time}   15sec
26 ${healing_pol_time}   15sec
27 ${vim_timeout_multiplier}   %{OSM_VIM_MULTIPLIER_TIMEOUT=1.0}
28
29
30 *** Keywords ***
31 Create Network Service
32     [Arguments]   ${nsd}   ${vim_name}   ${ns_name}   ${ns_config}   ${publickey}   ${ns_launch_max_wait_time}=5min   ${config_file}=${EMPTY}
33
34     ${ns_launch_max_wait_time}=   Convert Time   ${ns_launch_max_wait_time}   result_format=number
35     ${ns_launch_max_wait_time}=   Evaluate   ${ns_launch_max_wait_time} * ${vim_timeout_multiplier}
36     Log   ${ns_launch_max_wait_time}
37     ${config_attr}   Set Variable If   '${ns_config}'!='${EMPTY}'   --config '${ns_config}'   \
38     ${sshkeys_attr}   Set Variable If   '${publickey}'!='${EMPTY}'   --ssh_keys ${publickey}   \
39     ${config_file_attr}   Set Variable If   '${config_file}'!='${EMPTY}'   --config_file '${config_file}'   \
40
41     ${ns_id}=   Instantiate Network Service   ${ns_name}   ${nsd}   ${vim_name}   ${config_attr} ${sshkeys_attr} ${config_file_attr}
42     Log   ${ns_id}
43
44     WAIT UNTIL KEYWORD SUCCEEDS   ${ns_launch_max_wait_time}   ${ns_launch_pol_time}   Check For NS Instance To Configured   ${ns_name}
45     Check For NS Instance For Failure   ${ns_name}
46     [Return]   ${ns_id}
47
48
49 Instantiate Network Service
50     [Arguments]   ${ns_name}   ${nsd}   ${vim_name}   ${ns_extra_args}
51
52     ${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}
53     Log   ${stdout}
54     Should Be Equal As Integers   ${rc}   ${success_return_code}
55     [Return]   ${stdout}
56
57
58 Update Network Service
59     [Arguments]   ${ns_id}   ${update_type}   ${ns_update_config}   ${ns_update_timeout}
60
61     ${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
62     Log   ${stdout}
63     Should Be Equal As Integers   ${rc}   ${success_return_code}
64     [Return]   ${stdout}
65
66
67 Get Vnf Management Ip Address
68     [Arguments]   ${ns_id}   ${vnf_member_index}
69
70     Should Not Be Empty   ${ns_id}
71     Should Not Be Empty   ${vnf_member_index}
72     ${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
73     Log   ${stdout}
74     Should Be Equal As Integers   ${rc}   ${success_return_code}
75     [Return]   ${stdout}
76
77
78 Get Vnf Id
79     [Arguments]   ${ns_id}   ${vnf_member_index}
80
81     Should Not Be Empty   ${ns_id}
82     Should Not Be Empty   ${vnf_member_index}
83     ${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
84     Log   ${stdout}
85     Should Be Equal As Integers   ${rc}   ${success_return_code}
86     [Return]   ${stdout}
87
88
89 Get Ns Id
90     [Arguments]   ${ns_name}
91
92     Should Not Be Empty   ${ns_name}
93     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | grep ${ns_name} | awk '{print $4}' 2>&1
94     Log   ${stdout}
95     Should Be Equal As Integers   ${rc}   ${success_return_code}
96     Should Not Be Empty   ${stdout}
97     [Return]   ${stdout}
98
99
100 Get Ns List
101     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list 2>&1
102     Log   ${stdout}
103     Should Be Equal As Integers   ${rc}   ${success_return_code}
104     [Return]   ${stdout}
105
106
107 Get Ns Vnf List
108     [Arguments]   ${ns_id}
109
110     Should Not Be Empty   ${ns_id}
111     @{vnf_list_string}=   Run and Return RC and Output   osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
112     # Returns a String of vnf_id and needs to be converted into a list
113     @{vnf_list} =   Split String   ${vnf_list_string}[1]
114     Log List   ${vnf_list}
115     [Return]   @{vnf_list}
116
117
118 Get Ns Ip List
119     [Arguments]   @{vnf_list}
120
121     should not be empty   @{vnf_list}
122     @{temp_list}=   Create List
123     FOR   ${vnf_id}   IN   @{vnf_list}
124         Log   ${vnf_id}
125         @{vnf_ip_list}   Get Vnf Ip List   ${vnf_id}
126         @{temp_list}=   Combine Lists   ${temp_list}   ${vnf_ip_list}
127     END
128     should not be empty   ${temp_list}
129     [return]   @{temp_list}
130
131
132 Get Vnf Ip List
133     [arguments]   ${vnf_id}
134
135     should not be empty   ${vnf_id}
136     @{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
137     # returns a string of ip addresses and needs to be converted into a list
138     should not be empty   ${vnf_ip_list_string}[1]
139     @{vnf_ip_list} =   split string   ${vnf_ip_list_string}[1]
140     Log Many   ${vnf_ip_list}
141     should not be empty   ${vnf_ip_list}
142     [return]   @{vnf_ip_list}
143
144
145 Check For Ns Instance To Configured
146     [arguments]   ${ns_name}
147
148     ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
149     Log   ${stdout}
150     ${rc}   ${stdout}=   run and return rc and output   osm ns-list --filter name="${ns_name}"
151     Log   ${stdout}
152     Should Be Equal As Integers   ${rc}   ${success_return_code}
153     Should Contain Any   ${stdout}   READY   BROKEN
154
155 Check For NS Instance For Failure
156     [Arguments]   ${ns_name}
157
158     ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
159     Log   ${stdout}
160     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list --filter name="${ns_name}"
161     Log   ${stdout}
162     Should Be Equal As Integers   ${rc}   ${success_return_code}
163     Should Not Contain   ${stdout}   BROKEN
164
165 Check For NS Instance To Be Deleted
166     [Arguments]   ${ns}
167
168     ${rc}   ${stdout}=   Run and Return RC and Output   openstack server list
169     Log   ${stdout}
170     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | awk '{print $2}' | grep ${ns}
171     Should Not Be Equal As Strings   ${stdout}   ${ns}
172
173 Delete NS
174     [Documentation]   Delete ns
175     [Arguments]   ${ns}   ${ns_delete_max_wait_time}=4min
176
177     ${ns_delete_max_wait_time}=   Convert Time   ${ns_delete_max_wait_time}   result_format=number
178     ${ns_delete_max_wait_time}=   Evaluate   ${ns_delete_max_wait_time} * ${vim_timeout_multiplier}
179     Log   ${ns_delete_max_wait_time}
180     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-delete ${ns}
181     Log   ${stdout}
182     Should Be Equal As Integers   ${rc}   ${success_return_code}
183
184     WAIT UNTIL KEYWORD SUCCEEDS   ${ns_delete_max_wait_time}   ${ns_delete_pol_time}   Check For NS Instance To Be Deleted   ${ns}
185
186 Execute NS Action
187     [Documentation]   Execute an action over the desired NS.
188     ...               Parameters are given to this function in key=value format (one argument per key/value pair).
189     ...               Return the ID of the operation associated to the executed action.
190     ...               Examples of execution:
191     ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}
192     ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}   \${param1}=\${value1}   \${param2}=\${value2}
193
194     [Arguments]   ${ns_name}   ${ns_action}   ${vnf_member_index}   @{action_params}
195
196     ${params}=   Set Variable   ${EMPTY}
197     FOR   ${param}   IN   @{action_params}
198         ${match}   ${param_name}   ${param_value} =   Should Match Regexp   ${param}   (.+)=(.+)   msg=Syntax error in parameters
199         ${params}=   Catenate   SEPARATOR=   ${params}   "${param_name}":"${param_value}",
200     END
201     ${osm_ns_action_command}=   Set Variable   osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index}
202     ${osm_ns_action_command}=   Run Keyword If   '${params}'!='${EMPTY}'   Catenate   ${osm_ns_action_command}   --params '{${params}}'
203     ...   ELSE   Set Variable   ${osm_ns_action_command}
204     ${osm_ns_action_command}=   Catenate   ${osm_ns_action_command}   ${ns_name}
205     ${rc}   ${stdout}=   Run and Return RC and Output   ${osm_ns_action_command}
206     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
207     Wait Until Keyword Succeeds   ${ns_action_max_wait_time}   ${ns_action_pol_time}   Check For NS Operation Ended   ${stdout}
208     Check For NS Operation Completed   ${stdout}
209     [Return]   ${stdout}
210
211
212 Execute NS K8s Action
213     [Documentation]   Execute an action over the desired K8s NS.
214     ...               Parameters are given to this function in key=value format (one argument per key/value pair).
215     ...               Return the ID of the operation associated to the executed action.
216     ...               Examples of execution:
217     ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}
218     ...                   \${ns_op_id}=   Execute NS Action   \${ns_name}   \${ns_action}   \${vnf_member_index}   \${param1}=\${value1}   \${param2}=\${value2}
219
220     [Arguments]   ${ns_name}   ${ns_action}   ${vnf_member_index}   ${kdu_name}   @{action_params}
221
222     ${params}=   Set Variable   ${EMPTY}
223     FOR   ${param}   IN   @{action_params}
224         ${match}   ${param_name}   ${param_value} =   Should Match Regexp   ${param}   (.+)=(.+)   msg=Syntax error in parameters
225         ${params}=   Catenate   SEPARATOR=   ${params}   "${param_name}":"${param_value}",
226     END
227     ${osm_ns_action_command}=   Set Variable   osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index} --kdu_name ${kdu_name}
228     ${osm_ns_action_command}=   Run Keyword If   '${params}'!='${EMPTY}'   Catenate   ${osm_ns_action_command}   --params '{${params}}'
229     ...   ELSE   Set Variable   ${osm_ns_action_command}
230     ${osm_ns_action_command}=   Catenate   ${osm_ns_action_command}   ${ns_name}
231     ${rc}   ${stdout}=   Run and Return RC and Output   ${osm_ns_action_command}
232     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
233     Wait Until Keyword Succeeds   ${ns_action_max_wait_time}   ${ns_action_pol_time}   Check For NS Operation Ended   ${stdout}
234     Check For NS Operation Completed   ${stdout}
235     [Return]   ${stdout}
236
237
238 Execute Manual VNF Scale
239     [Documentation]   Execute a manual VNF Scale action.
240     ...               The parameter 'scale_type' must be SCALE_IN or SCALE_OUT.
241     ...               Return the ID of the operation associated to the executed scale action.
242
243     [Arguments]   ${ns_name}   ${vnf_member_index}   ${scaling_group}   ${scale_type}   ${vnf_scale_max_wait_time}=2min
244
245     ${vnf_scale_max_wait_time}=   Convert Time   ${vnf_scale_max_wait_time}   result_format=number
246     ${vnf_scale_max_wait_time}=   Evaluate   ${vnf_scale_max_wait_time} * ${vim_timeout_multiplier}
247     Should Contain Any   ${scale_type}   SCALE_IN   SCALE_OUT   msg=Unknown scale type: ${scale_type}   values=False
248     ${osm_vnf_scale_command}=   Set Variable   osm vnf-scale --scaling-group ${scaling_group}
249     ${osm_vnf_scale_command}=   Run Keyword If   '${scale_type}'=='SCALE_IN'   Catenate   ${osm_vnf_scale_command}   --scale-in
250     ...   ELSE   Catenate   ${osm_vnf_scale_command}   --scale-out
251     ${osm_vnf_scale_command}=   Catenate   ${osm_vnf_scale_command}   ${ns_name}   ${vnf_member_index}
252     ${rc}   ${stdout}=   Run and Return RC and Output   ${osm_vnf_scale_command}
253     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
254     Wait Until Keyword Succeeds   ${vnf_scale_max_wait_time}   ${vnf_scale_pol_time}   Check For NS Operation Ended   ${stdout}
255     Check For NS Operation Completed   ${stdout}
256     [Return]   ${stdout}
257
258
259 Heal Network Service
260     [Documentation]   Execute healing operation over one NS.
261     ...               Return the ID of the operation associated to the executed healing action.
262
263     [Arguments]   ${ns_name}   ${params}   ${healing_max_wait_time}=10m
264
265     Should Not Be Empty   ${ns_name}
266     Should Not Be Empty   ${params}
267     ${healing_max_wait_time}=   Convert Time   ${healing_max_wait_time}   result_format=number
268     ${healing_max_wait_time}=   Evaluate   ${healing_max_wait_time} * ${vim_timeout_multiplier}
269     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-heal ${ns_name} ${params}
270     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
271     Wait Until Keyword Succeeds   ${healing_max_wait_time}   ${healing_pol_time}   Check For NS Operation Ended   ${stdout}
272     Check For NS Operation Completed   ${stdout}
273     [Return]   ${stdout}
274
275
276 Get Operations List
277     [Arguments]   ${ns_name}
278
279     Should Not Be Empty   ${ns_name}
280     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-op-list ${ns_name}
281     Log   ${stdout}
282     Log   ${rc}
283     Should Be Equal As Integers   ${rc}   ${success_return_code}
284
285
286 Check For NS Operation Completed
287     [Documentation]   Check wheter the status of the desired operation is "COMPLETED" or not.
288
289     [Arguments]   ${ns_operation_id}
290
291     Should Not Be Empty   ${ns_operation_id}
292     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
293     Log   ${stdout}
294     Should Be Equal As Integers   ${rc}   ${success_return_code}
295     Should Contain   ${stdout}   COMPLETED   msg=The ns-action with id ${ns_operation_id} was not completed   values=False
296
297
298 Check For NS Operation Failed
299     [Documentation]   Check wheter the status of the desired operation is "FAILED" or not.
300
301     [Arguments]   ${ns_operation_id}
302
303     Should Not Be Empty   ${ns_operation_id}
304     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
305     Log   ${stdout}
306     Should Be Equal As Integers   ${rc}   ${success_return_code}
307     Should Contain   ${stdout}   FAILED   msg=Timeout waiting for ns-action with id ${ns_operation_id}   values=False
308
309
310 Check For NS Operation Ended
311     [Documentation]   Check wheter the status of the desired operation is "FAILED" or "COMPLETED".
312
313     [Arguments]   ${ns_operation_id}
314
315     Should Not Be Empty   ${ns_operation_id}
316     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-op-show ${ns_operation_id} --literal | yq .operationState | tr -d \\"
317     Log   ${stdout}
318     Should Be Equal As Integers   ${rc}   ${success_return_code}
319     Should Contain Any   ${stdout}   FAILED   COMPLETED   msg=Timeout waiting for ns-action with id ${ns_operation_id}   values=False
320
321
322 Get Ns Vnfr Ids
323     [Documentation]   Return a list with the IDs of the VNF records of a NS instance.
324
325     [Arguments]   ${ns_id}
326
327     Should Not Be Empty   ${ns_id}
328     ${rc}   ${stdout}=   Run and Return RC and Output   osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
329     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
330     @{vdur} =   Split String   ${stdout}
331     [Return]   @{vdur}
332
333
334 Get Vnf Vdur Names
335     [Documentation]   Return a list with the names of the VDU records of a VNF instance.
336
337     [Arguments]   ${vnf_id}
338
339     Should Not Be Empty   ${vnf_id}
340     ${rc}   ${stdout}=   Run and Return RC and Output   osm vnf-show ${vnf_id} --literal | yq .vdur[].name | tr -d \\"
341     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
342     @{vdur} =   Split String   ${stdout}
343     [Return]   @{vdur}
344
345
346 Get Vnf Kdu Replica Count
347     [Documentation]   Return the number of KDU replicas (empty if none) of a VNF instance.
348
349     [Arguments]   ${vnf_id}   ${kdu_name}
350
351     Should Not Be Empty   ${vnf_id}
352     Should Not Be Empty   ${kdu_name}
353     ${rc}   ${stdout}=   Run and Return RC and Output   osm vnf-show ${vnf_id} --kdu ${kdu_name} | yq .config.replicaCount | tr -d \\"
354     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
355     ${return} =   Set Variable If   '${stdout}' == 'null'   ${EMPTY}   ${stdout}
356     [Return]   ${return}
357
358
359 Get Application Names
360     [Documentation]   Return the list of the application of a VNF instance.
361
362     [Arguments]   ${ns_name}
363
364     Should Not Be Empty   ${ns_name}
365     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-show ${ns_name} --literal | yq ._admin.deployed.VCA[].application | tr -d \\"
366     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${stdout}   values=False
367     @{app_names} =   Split String   ${stdout}
368     [Return]   ${app_names}
369
370
371 Get VDU Affinity Group Name
372     [Documentation]   Return the affinity group name for a NF
373
374     [Arguments]   ${ns_name}   ${vnf_id}   ${vdur_id}
375
376     ${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
377     Log   ${affinity_group_id}
378     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${affinity_group_id}   values=False
379     ${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
380     Log   ${affinity_group_name}
381     Should Be Equal As Integers   ${rc}   ${success_return_code}   msg=${affinity_group_name}   values=False
382     [Return]   ${affinity_group_name}
383
384 Get Operations By Type
385     [Documentation]   Keyword to get the operation by type
386
387     [Arguments]   ${ns_id}   ${type}
388
389     Should Not Be Empty   ${ns_id}
390     Should Not Be Empty   ${type}
391     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-op-list ${ns_id} | grep ${type} | awk '{print $2}' 2>&1
392     Log   ${stdout}
393     Should Be Equal As Integers   ${rc}   ${success_return_code}
394     Should Not Be Empty   ${stdout}
395     [Return]   ${stdout}