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