8acdb46a03891856f77a59110692fedc3dddf80d
[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 *** Variables ***
14 ${success_return_code}   0
15 ${ns_launch_pol_time}   30sec
16 ${ns_delete_max_wait_time}   4min
17 ${ns_delete_pol_time}   15sec
18 ${ns_action_max_wait_time}   1min
19 ${ns_action_pol_time}   15sec
20 ${vnf_scale_pol_time}   15sec
21
22
23 *** Keywords ***
24 Create Network Service
25     [Arguments]   ${nsd}   ${vim_name}   ${ns_name}   ${ns_config}   ${publickey}  ${ns_launch_max_wait_time}=5min  ${config_file}=${EMPTY}
26
27     ${config_attr}   Set Variable If   '${ns_config}'!='${EMPTY}'   --config '${ns_config}'   \
28     ${sshkeys_attr}   Set Variable If   '${publickey}'!='${EMPTY}'   --ssh_keys ${publickey}   \
29     ${config_file_attr}   Set Variable If   '${config_file}'!='${EMPTY}'   --config_file '${config_file}'   \
30
31     ${ns_id}=   Instantiate Network Service   ${ns_name}   ${nsd}   ${vim_name}   ${config_attr} ${sshkeys_attr} ${config_file_attr}
32     log   ${ns_id}
33
34     WAIT UNTIL KEYWORD SUCCEEDS   ${ns_launch_max_wait_time}   ${ns_launch_pol_time}   Check For NS Instance To Configured   ${ns_name}
35     Check For NS Instance For Failure   ${ns_name}
36     [Return]  ${ns_id}
37
38
39 Instantiate Network Service
40     [Arguments]   ${ns_name}   ${nsd}   ${vim_name}   ${ns_extra_args}
41
42     ${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}
43     log   ${stdout}
44     Should Be Equal As Integers   ${rc}   ${success_return_code}
45     [Return]  ${stdout}
46
47
48 Get Vnf Management Ip Address
49     [Arguments]   ${ns_id}   ${vnf_member_index}
50
51     Should Not Be Empty   ${ns_id}
52     Should Not Be Empty   ${vnf_member_index}
53     ${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
54     log   ${stdout}
55     Should Be Equal As Integers   ${rc}   ${success_return_code}
56     [Return]  ${stdout}
57
58
59 Get Vnf Id
60     [Arguments]   ${ns_id}   ${vnf_member_index}
61
62     Should Not Be Empty   ${ns_id}
63     Should Not Be Empty   ${vnf_member_index}
64     ${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
65     log   ${stdout}
66     Should Be Equal As Integers   ${rc}   ${success_return_code}
67     [Return]  ${stdout}
68
69
70 Get Ns Vnf List
71     [Arguments]   ${ns_id}
72
73     Should Not Be Empty   ${ns_id}
74     @{vnf_list_string}=   Run and Return RC and Output   osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
75     # Returns a String of vnf_id and needs to be converted into a list
76     @{vnf_list} =  Split String    ${vnf_list_string}[1]
77     Log List    ${vnf_list}
78     [Return]  @{vnf_list}
79
80
81 Get Ns Ip List
82     [Arguments]   @{vnf_list}
83
84     should not be empty   @{vnf_list}
85     @{temp_list}=    Create List
86     FOR   ${vnf_id}   IN   @{vnf_list}
87         log   ${vnf_id}
88         @{vnf_ip_list}   Get Vnf Ip List   ${vnf_id}
89         @{temp_list}=   Combine Lists   ${temp_list}    ${vnf_ip_list}
90     END
91     should not be empty   ${temp_list}
92     [return]  @{temp_list}
93
94
95 Get Vnf Ip List
96     [arguments]   ${vnf_id}
97
98     should not be empty   ${vnf_id}
99     @{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
100     # returns a string of ip addresses and needs to be converted into a list
101     should not be empty   ${vnf_ip_list_string}[1]
102     @{vnf_ip_list} =  split string    ${vnf_ip_list_string}[1]
103     log list    ${vnf_ip_list}
104     should not be empty   ${vnf_ip_list}
105     [return]  @{vnf_ip_list}
106
107
108 Check For Ns Instance To Configured
109     [arguments]  ${ns_name}
110
111     ${rc}   ${stdout}=   run and return rc and output   osm ns-list --filter name="${ns_name}"
112     log   ${stdout}
113     Should Be Equal As Integers   ${rc}   ${success_return_code}
114     Should Contain Any   ${stdout}   READY   BROKEN
115
116 Check For NS Instance For Failure
117     [Arguments]  ${ns_name}
118
119     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list --filter name="${ns_name}"
120     log   ${stdout}
121     Should Be Equal As Integers   ${rc}   ${success_return_code}
122     Should Not Contain   ${stdout}   BROKEN
123
124 Check For NS Instance To Be Deleted
125     [Arguments]  ${ns}
126
127     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-list | awk '{print $2}' | grep ${ns}
128     Should Not Be Equal As Strings   ${stdout}   ${ns}
129
130 Delete NS
131     [Documentation]  Delete ns
132     [Arguments]  ${ns}
133
134     ${rc}   ${stdout}=   Run and Return RC and Output   osm ns-delete ${ns}
135     log   ${stdout}
136     Should Be Equal As Integers   ${rc}   ${success_return_code}
137
138     WAIT UNTIL KEYWORD SUCCEEDS  ${ns_delete_max_wait_time}   ${ns_delete_pol_time}   Check For NS Instance To Be Deleted   ${ns}
139
140 Execute NS Action
141     [Documentation]     Execute an action over the desired NS.
142     ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
143     ...                 Return the ID of the operation associated to the executed action.
144     ...                 Examples of execution:
145     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
146     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
147
148     [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  @{action_params}
149
150     ${params}=  Set Variable  ${EMPTY}
151     FOR  ${param}  IN  @{action_params}
152         ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
153         ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
154     END
155     ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index}
156     ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
157     ...  ELSE  Set Variable  ${osm_ns_action_command}
158     ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
159     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
160     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
161     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
162     [Return]  ${stdout}
163
164
165 Execute NS K8s Action
166     [Documentation]     Execute an action over the desired K8s NS.
167     ...                 Parameters are given to this function in key=value format (one argument per key/value pair).
168     ...                 Return the ID of the operation associated to the executed action.
169     ...                 Examples of execution:
170     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}
171     ...                     \${ns_op_id}=  Execute NS Action  \${ns_name}  \${ns_action}  \${vnf_member_index}  \${param1}=\${value1}  \${param2}=\${value2}
172
173     [Arguments]  ${ns_name}  ${ns_action}  ${vnf_member_index}  ${kdu_name}  @{action_params}
174
175     ${params}=  Set Variable  ${EMPTY}
176     FOR  ${param}  IN  @{action_params}
177         ${match}  ${param_name}  ${param_value} =  Should Match Regexp  ${param}  (.+)=(.+)  msg=Syntax error in parameters
178         ${params}=  Catenate  SEPARATOR=  ${params}  "${param_name}":"${param_value}",
179     END
180     ${osm_ns_action_command}=  Set Variable  osm ns-action --action_name ${ns_action} --vnf_name ${vnf_member_index} --kdu_name ${kdu_name}
181     ${osm_ns_action_command}=  Run Keyword If  '${params}'!='${EMPTY}'  Catenate  ${osm_ns_action_command}  --params '{${params}}'
182     ...  ELSE  Set Variable  ${osm_ns_action_command}
183     ${osm_ns_action_command}=  Catenate  ${osm_ns_action_command}  ${ns_name}
184     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_ns_action_command}
185     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
186     Wait Until Keyword Succeeds  ${ns_action_max_wait_time}  ${ns_action_pol_time}  Check For NS Operation Completed  ${stdout}
187     [Return]  ${stdout}
188
189
190 Execute Manual VNF Scale
191     [Documentation]     Execute a manual VNF Scale action.
192     ...                 The parameter 'scale_type' must be SCALE_IN or SCALE_OUT.
193     ...                 Return the ID of the operation associated to the executed scale action.
194
195     [Arguments]   ${ns_name}   ${vnf_member_index}   ${scaling_group}   ${scale_type}   ${vnf_scale_max_wait_time}=2min
196
197     ${vnf_scale_max_wait_time}=   Convert Time   ${vnf_scale_max_wait_time}   result_format=number
198     ${vnf_scale_max_wait_time}=   Evaluate   ${vnf_scale_max_wait_time} * ${vim_timeout_multiplier}
199     Should Contain Any  ${scale_type}  SCALE_IN  SCALE_OUT  msg=Unknown scale type: ${scale_type}  values=False
200     ${osm_vnf_scale_command}=  Set Variable  osm vnf-scale --scaling-group ${scaling_group}
201     ${osm_vnf_scale_command}=  Run Keyword If  '${scale_type}'=='SCALE_IN'  Catenate  ${osm_vnf_scale_command}  --scale-in
202     ...  ELSE  Catenate  ${osm_vnf_scale_command}  --scale-out
203     ${osm_vnf_scale_command}=  Catenate  ${osm_vnf_scale_command}  ${ns_name}  ${vnf_member_index}
204     ${rc}  ${stdout}=  Run and Return RC and Output  ${osm_vnf_scale_command}
205     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
206     Wait Until Keyword Succeeds  ${vnf_scale_max_wait_time}  ${vnf_scale_pol_time}  Check For NS Operation Completed  ${stdout}
207     [Return]  ${stdout}
208
209
210 Get Operations List
211     [Arguments]  ${ns_name}
212
213     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-list ${ns_name}
214     log  ${stdout}
215     log  ${rc}
216     Should Be Equal As Integers  ${rc}  ${success_return_code}
217
218
219 Check For NS Operation Completed
220     [Documentation]     Check wheter the status of the desired operation is "COMPLETED" or not.
221
222     [Arguments]  ${ns_operation_id}
223
224     ${rc}  ${stdout}=  Run and Return RC and Output  osm ns-op-show ${ns_operation_id} --literal | yq r - operationState
225     log  ${stdout}
226     Should Be Equal As Integers  ${rc}  ${success_return_code}
227     Should Contain  ${stdout}  COMPLETED  msg=Timeout waiting for ns-action with id ${ns_operation_id}  values=False
228
229
230 Get Ns Vnfr Ids
231     [Documentation]     Return a list with the IDs of the VNF records of a NS instance.
232
233     [Arguments]  ${ns_id}
234
235     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-list | grep ${ns_id} | awk '{print $2}' 2>&1
236     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
237     @{vdur} =  Split String  ${stdout}
238     [Return]  @{vdur}
239
240
241 Get Vnf Vdur Names
242     [Documentation]     Return a list with the names of the VDU records of a VNF instance.
243
244     [Arguments]  ${vnf_id}
245
246     ${rc}  ${stdout}=  Run and Return RC and Output  osm vnf-show ${vnf_id} --literal | yq r - vdur.*.name
247     Should Be Equal As Integers  ${rc}  ${success_return_code}  msg=${stdout}  values=False
248     @{vdur} =  Split String  ${stdout}
249     [Return]  @{vdur}
250