Place VMs deterministically
[integration/test.git] / csit / libraries / OpenStackOperations.robot
1 *** Settings ***
2 Documentation     Openstack library. This library is useful for tests to create network, subnet, router and vm instances
3 Library           Collections
4 Library           OperatingSystem
5 Library           RequestsLibrary
6 Library           SSHLibrary
7 Resource          DataModels.robot
8 Resource          DevstackUtils.robot
9 Resource          L2GatewayOperations.robot
10 Resource          OVSDB.robot
11 Resource          SetupUtils.robot
12 Resource          SSHKeywords.robot
13 Resource          Utils.robot
14 Resource          ../variables/Variables.robot
15 Resource          ../variables/netvirt/Variables.robot
16 Variables         ../variables/netvirt/Modules.py
17
18 *** Keywords ***
19 Get Tenant ID From Security Group
20     [Documentation]    Returns tenant ID by reading it from existing default security-group.
21     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group show default | grep "| tenant_id" | awk '{print $4}'
22     Should Be True    '${rc}' == '0'
23     [Return]    ${output}
24
25 Get Tenant ID From Network
26     [Arguments]    ${network_uuid}
27     [Documentation]    Returns tenant ID by reading it from existing network.
28     ${resp}=    Get_From_Uri    uri=${CONFIG_API}/neutron:neutron/networks/network/${network_uuid}/    accept=${ACCEPT_EMPTY}    session=session
29     ${tenant_id}=    Utils.Extract Value From Content    ${resp}    /network/0/tenant-id    strip
30     [Return]    ${tenant_id}
31
32 Create Network
33     [Arguments]    ${network_name}    ${additional_args}=${EMPTY}    ${verbose}=TRUE
34     [Documentation]    Create Network with neutron request.
35     ${rc}    ${output}=    Run And Return Rc And Output    openstack network create ${network_name} ${additional_args}
36     Log    ${output}
37     Should Be True    '${rc}' == '0'
38     [Return]    ${output}
39
40 Update Network
41     [Arguments]    ${network_name}    ${additional_args}=${EMPTY}
42     [Documentation]    Update Network with neutron request.
43     ${cmd}=    Set Variable If    '${OPENSTACK_BRANCH}'=='stable/newton'    neutron -v net-update ${network_name} ${additional_args}    openstack network set ${network_name} ${additional_args}
44     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
45     Log    ${output}
46     Should Be True    '${rc}' == '0'
47     [Return]    ${output}
48
49 Show Network
50     [Arguments]    ${network_name}
51     [Documentation]    Show Network with neutron request.
52     ${rc}    ${output}=    Run And Return Rc And Output    openstack network show ${network_name}
53     Log    ${output}
54     Should Be True    '${rc}' == '0'
55     [Return]    ${output}
56
57 List Networks
58     [Documentation]    List networks and return output with neutron client.
59     ${rc}    ${output}=    Run And Return Rc And Output    openstack network list
60     Log    ${output}
61     Should Be True    '${rc}' == '0'
62     [Return]    ${output}
63
64 List Subnets
65     [Documentation]    List subnets and return output with neutron client.
66     ${rc}    ${output}=    Run And Return Rc And Output    openstack subnet list
67     Log    ${output}
68     Should Be True    '${rc}' == '0'
69     [Return]    ${output}
70
71 Delete Network
72     [Arguments]    ${network_name}
73     [Documentation]    Delete Network with neutron request.
74     ${rc}    ${output}=    Run And Return Rc And Output    openstack network delete ${network_name}
75     Log    ${output}
76     Should Be True    '${rc}' == '0'
77
78 Create SubNet
79     [Arguments]    ${network_name}    ${subnet}    ${range_ip}    ${additional_args}=${EMPTY}
80     [Documentation]    Create SubNet for the Network with neutron request.
81     ${rc}    ${output}=    Run And Return Rc And Output    openstack subnet create --network ${network_name} --subnet-range ${range_ip} ${subnet} ${additional_args}
82     Log    ${output}
83     Should Be True    '${rc}' == '0'
84
85 Update SubNet
86     [Arguments]    ${subnet_name}    ${additional_args}=${EMPTY}
87     [Documentation]    Update subnet with neutron request.
88     ${cmd}=    Set Variable If    '${OPENSTACK_BRANCH}'=='stable/newton'    neutron -v subnet-update ${subnet_name} ${additional_args}    openstack subnet set ${subnet_name} ${additional_args}
89     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
90     Log    ${output}
91     Should Be True    '${rc}' == '0'
92     [Return]    ${output}
93
94 Show SubNet
95     [Arguments]    ${subnet_name}
96     [Documentation]    Show subnet with neutron request.
97     ${rc}    ${output}=    Run And Return Rc And Output    openstack subnet show ${subnet_name}
98     Log    ${output}
99     Should Be True    '${rc}' == '0'
100     [Return]    ${output}
101
102 Create Port
103     [Arguments]    ${network_name}    ${port_name}    ${sg}=default    ${additional_args}=${EMPTY}    ${allowed_address_pairs}=${EMPTY}
104     [Documentation]    Create Port with neutron request.
105     # if allowed_address_pairs is not empty we need to create the arguments to pass to the port create command. They are
106     # in a different format with the neutron vs openstack cli.
107     ${address_pair_length}=    Get Length    ${allowed_address_pairs}
108     ${allowed_pairs_argv}=    Set Variable If    '${OPENSTACK_BRANCH}'=='stable/newton' and '${address_pair_length}'=='2'    --allowed-address-pairs type=dict list=true ip_address=@{allowed_address_pairs}[0] ip_address=@{allowed_address_pairs}[1]
109     ${allowed_pairs_argv}=    Set Variable If    '${OPENSTACK_BRANCH}'!='stable/newton' and '${address_pair_length}'=='2'    --allowed-address ip-address=@{allowed_address_pairs}[0] --allowed-address ip-address=@{allowed_address_pairs}[1]    ${allowed_pairs_argv}
110     ${allowed_pairs_argv}=    Set Variable If    '${address_pair_length}'=='0'    ${EMPTY}    ${allowed_pairs_argv}
111     ${cmd}=    Set Variable If    '${OPENSTACK_BRANCH}'=='stable/newton'    neutron -v port-create ${network_name} --name ${port_name} --security-group ${sg} ${additional_args} ${allowed_pairs_argv}    openstack port create --network ${network_name} ${port_name} --security-group ${sg} ${additional_args} ${allowed_pairs_argv}
112     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
113     Log    ${output}
114     Should Be True    '${rc}' == '0'
115
116 Update Port
117     [Arguments]    ${port_name}    ${additional_args}=${EMPTY}
118     [Documentation]    Update port with neutron request.
119     ${rc}    ${output}=    Run And Return Rc And Output    openstack port set ${port_name} ${additional_args}
120     Log    ${output}
121     Should Be True    '${rc}' == '0'
122     [Return]    ${output}
123
124 Show Port
125     [Arguments]    ${port_name}
126     [Documentation]    Show port with neutron request.
127     ${rc}    ${output}=    Run And Return Rc And Output    openstack port show ${port_name}
128     Log    ${output}
129     Should Be True    '${rc}' == '0'
130     [Return]    ${output}
131
132 Delete Port
133     [Arguments]    ${port_name}
134     [Documentation]    Delete Port with neutron request.
135     ${rc}    ${output}=    Run And Return Rc And Output    openstack port delete ${port_name}
136     Log    ${output}
137     Should Be True    '${rc}' == '0'
138
139 List Ports
140     [Documentation]    List ports and return output with neutron client.
141     ${rc}    ${output}=    Run And Return Rc And Output    openstack port list
142     Log    ${output}
143     Should Be True    '${rc}' == '0'
144     [Return]    ${output}
145
146 List Nova VMs
147     [Documentation]    List VMs and return output with nova client.
148     ${rc}    ${output}=    Run And Return Rc And Output    openstack server list --all-projects
149     Log    ${output}
150     Should Be True    '${rc}' == '0'
151     [Return]    ${output}
152
153 Create And Associate Floating IPs
154     [Arguments]    ${external_net}    @{vm_list}
155     [Documentation]    Create and associate floating IPs to VMs with nova request
156     ${ip_list}=    Create List    @{EMPTY}
157     : FOR    ${vm}    IN    @{vm_list}
158     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack floating ip create ${external_net}
159     \    Log    ${output}
160     \    Should Be True    '${rc}' == '0'
161     \    @{ip}    Get Regexp Matches    ${output}    [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}
162     \    ${ip_length}    Get Length    ${ip}
163     \    Run Keyword If    ${ip_length}>0    Append To List    ${ip_list}    @{ip}[0]
164     \    ...    ELSE    Append To List    ${ip_list}    None
165     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack server add floating ip ${vm} @{ip}[0]
166     \    Log    ${output}
167     \    Should Be True    '${rc}' == '0'
168     [Return]    ${ip_list}
169
170 Delete Floating IP
171     [Arguments]    ${fip}
172     [Documentation]    Delete floating ip with neutron request.
173     ${rc}    ${output}=    Run And Return Rc And Output    openstack floating ip delete ${fip}
174     Log    ${output}
175     Should Be True    '${rc}' == '0'
176
177 Verify Gateway Ips
178     [Documentation]    Verifies the Gateway Ips with dump flow.
179     ${output}=    Write Commands Until Prompt And Log    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
180     : FOR    ${GatewayIpElement}    IN    @{GATEWAY_IPS}
181     \    Should Contain    ${output}    ${GatewayIpElement}
182
183 Verify Dhcp Ips
184     [Documentation]    Verifies the Dhcp Ips with dump flow.
185     ${output}=    Write Commands Until Prompt And Log    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
186     : FOR    ${DhcpIpElement}    IN    @{DHCP_IPS}
187     \    Should Contain    ${output}    ${DhcpIpElement}
188
189 Verify No Dhcp Ips
190     [Documentation]    Verifies the Dhcp Ips with dump flow.
191     ${output}=    Write Commands Until Prompt And Log    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
192     : FOR    ${DhcpIpElement}    IN    @{DHCP_IPS}
193     \    Should Not Contain    ${output}    ${DhcpIpElement}
194
195 Delete SubNet
196     [Arguments]    ${subnet}
197     [Documentation]    Delete SubNet for the Network with neutron request.
198     Log    ${subnet}
199     ${rc}    ${output}=    Run And Return Rc And Output    openstack subnet delete ${subnet}
200     Should Be True    '${rc}' == '0'
201
202 Verify No Gateway Ips
203     [Documentation]    Verifies the Gateway Ips removed with dump flow.
204     ${output}=    Write Commands Until Prompt And Log    sudo ovs-ofctl -O OpenFlow13 dump-flows br-int
205     : FOR    ${GatewayIpElement}    IN    @{GATEWAY_IPS}
206     \    Should Not Contain    ${output}    ${GatewayIpElement}
207
208 Delete Vm Instance
209     [Arguments]    ${vm_name}
210     [Documentation]    Delete Vm instances using instance names.
211     ${rc}    ${output}=    Run And Return Rc And Output    openstack server delete ${vm_name}
212     Log    ${output}
213
214 Get Net Id
215     [Arguments]    ${network_name}
216     [Documentation]    Retrieve the net id for the given network name to create specific vm instance
217     ${rc}    ${output}=    Run And Return Rc And Output    openstack network list | grep "${network_name}" | awk '{print $2}'
218     Should Be True    '${rc}' == '0'
219     ${splitted_output}=    Split String    ${output}    ${EMPTY}
220     ${net_id}=    Get from List    ${splitted_output}    0
221     [Return]    ${net_id}
222
223 Get Subnet Id
224     [Arguments]    ${subnet_name}
225     [Documentation]    Retrieve the subnet id for the given subnet name
226     ${rc}    ${output}=    Run And Return Rc And Output    openstack subnet show "${subnet_name}" | grep " id " | awk '{print $4}'
227     Should Be True    '${rc}' == '0'
228     ${splitted_output}=    Split String    ${output}    ${EMPTY}
229     ${subnet_id}=    Get from List    ${splitted_output}    0
230     [Return]    ${subnet_id}
231
232 Get Port Id
233     [Arguments]    ${port_name}
234     [Documentation]    Retrieve the port id for the given port name to attach specific vm instance to a particular port
235     ${rc}    ${output}=    Run And Return Rc And Output    openstack port list | grep "${port_name}" | awk '{print $2}'
236     Should Be True    '${rc}' == '0'
237     ${splitted_output}=    Split String    ${output}    ${EMPTY}
238     ${port_id}=    Get from List    ${splitted_output}    0
239     [Return]    ${port_id}
240
241 Get Router Id
242     [Arguments]    ${router1}
243     [Documentation]    Retrieve the router id for the given router name
244     ${rc}    ${output}=    Run And Return Rc And Output    openstack router list -f table | grep "${router1}" | awk '{print $2}'
245     Should Be True    '${rc}' == '0'
246     ${splitted_output}=    Split String    ${output}    ${EMPTY}
247     ${router_id}=    Get from List    ${splitted_output}    0
248     [Return]    ${router_id}
249
250 Create Vm Instances
251     [Arguments]    ${net_name}    ${vm_instance_names}    ${image}=${EMPTY}    ${flavor}=m1.nano    ${sg}=default    ${min}=1
252     ...    ${max}=1
253     [Documentation]    Create X Vm Instance with the net id of the Netowrk.
254     ${image}    Set Variable If    "${image}"=="${EMPTY}"    ${CIRROS_${OPENSTACK_BRANCH}}    ${image}
255     ${net_id}=    Get Net Id    ${net_name}
256     : FOR    ${VmElement}    IN    @{vm_instance_names}
257     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack server create --image ${image} --flavor ${flavor} --nic net-id=${net_id} ${VmElement} --security-group ${sg} --min ${min} --max ${max}
258     \    Should Be True    '${rc}' == '0'
259     \    Log    ${output}
260
261 Create Vm Instance On Compute Node
262     [Arguments]    ${net_name}    ${vm_name}    ${node_hostname}    ${image}=${EMPTY}    ${flavor}=m1.nano    ${sg}=default
263     [Documentation]    Create a VM instance on a specific compute node.
264     ${image} =    Set Variable If    "${image}"=="${EMPTY}"    ${CIRROS_${OPENSTACK_BRANCH}}    ${image}
265     ${net_id} =    Get Net Id    ${net_name}
266     ${rc}    ${output} =    Run And Return Rc And Output    openstack server create ${vm_name} --image ${image} --flavor ${flavor} --nic net-id=${net_id} --security-group ${sg} --availability-zone nova:${node_hostname}
267     Should Not Be True    ${rc}
268     Log    ${output}
269
270 Create Vm Instance With Port
271     [Arguments]    ${port_name}    ${vm_instance_name}    ${image}=${EMPTY}    ${flavor}=m1.nano    ${sg}=default
272     [Documentation]    Create One VM instance using given ${port_name} and for given ${compute_node}
273     ${image}    Set Variable If    "${image}"=="${EMPTY}"    ${CIRROS_${OPENSTACK_BRANCH}}    ${image}
274     ${port_id}=    Get Port Id    ${port_name}
275     ${rc}    ${output}=    Run And Return Rc And Output    openstack server create --image ${image} --flavor ${flavor} --nic port-id=${port_id} ${vm_instance_name} --security-group ${sg}
276     Log    ${output}
277
278 Create Vm Instance With Ports
279     [Arguments]    ${port_name}    ${port2_name}    ${vm_instance_name}    ${image}=${EMPTY}    ${flavor}=m1.nano    ${sg}=default
280     [Documentation]    Create One VM instance using given ${port_name} and for given ${compute_node}
281     ${image}    Set Variable If    "${image}"=="${EMPTY}"    ${CIRROS_${OPENSTACK_BRANCH}}    ${image}
282     ${port_id}=    Get Port Id    ${port_name}
283     ${port2_id}=    Get Port Id    ${port2_name}
284     ${rc}    ${output}=    Run And Return Rc And Output    openstack server create --image ${image} --flavor ${flavor} --nic port-id=${port_id} --nic port-id=${port2_id} ${vm_instance_name} --security-group ${sg}
285     Log    ${output}
286     Should Be True    '${rc}' == '0'
287
288 Create Vm Instance With Port On Compute Node
289     [Arguments]    ${port_name}    ${vm_instance_name}    ${node_hostname}    ${image}=${EMPTY}    ${flavor}=m1.nano    ${sg}=default
290     [Documentation]    Create One VM instance using given ${port_name} and for given ${compute_node}
291     ${image}    Set Variable If    "${image}"=="${EMPTY}"    ${CIRROS_${OPENSTACK_BRANCH}}    ${image}
292     ${port_id}=    Get Port Id    ${port_name}
293     ${rc}    ${output}=    Run And Return Rc And Output    openstack server create --image ${image} --flavor ${flavor} --nic port-id=${port_id} --security-group ${sg} --availability-zone nova:${node_hostname} ${vm_instance_name}
294     Log    ${output}
295     Should Be True    '${rc}' == '0'
296
297 Get Hypervisor Hostname From IP
298     [Arguments]    ${hypervisor_ip}
299     [Documentation]    Returns the hostname found for the given IP address if it's listed in hypervisor list. For debuggability
300     ...    the full listing is logged first, then followed by a grep | cut to focus on the actual hostname to return
301     ${rc}    ${output}    Run And Return Rc And Output    openstack hypervisor list
302     Log    ${output}
303     ${rc}    ${hostname}=    Run And Return Rc And Output    openstack hypervisor list -f value | grep "${hypervisor_ip} " | cut -d" " -f 2
304     Log    ${hostname}
305     Should Be True    '${rc}' == '0'
306     [Return]    ${hostname}
307
308 Create Nano Flavor
309     [Documentation]    Create a nano flavor
310     ${rc}    ${output}=    Run And Return Rc And Output    openstack flavor create m1.nano --id auto --ram 64 --disk 0 --vcpus 1
311     Log    ${output}
312     Should Be True    '${rc}' == '0'
313
314 Verify VM Is ACTIVE
315     [Arguments]    ${vm_name}
316     [Documentation]    Run these commands to check whether the created vm instance is active or not.
317     ${rc}    ${output}=    Run And Return Rc And Output    openstack server show ${vm_name} | grep OS-EXT-STS:vm_state
318     Should Be True    '${rc}' == '0'
319     Should Contain    ${output}    active
320
321 Poll VM Is ACTIVE
322     [Arguments]    ${vm_name}    ${retry}=600s    ${retry_interval}=30s
323     [Documentation]    Run these commands to check whether the created vm instance is active or not.
324     Wait Until Keyword Succeeds    ${retry}    ${retry_interval}    Verify VM Is ACTIVE    ${vm_name}
325
326 Collect VM IP Addresses
327     [Arguments]    ${fail_on_none}    @{vm_list}
328     [Documentation]    Using the console-log on the provided ${vm_list} to search for the string "obtained" which
329     ...    correlates to the instance receiving it's IP address via DHCP. Also retrieved is the ip of the nameserver
330     ...    if available in the console-log output. The keyword will also return a list of the learned ips as it
331     ...    finds them in the console log output, and will have "None" for Vms that no ip was found.
332     ${ip_list}    Create List    @{EMPTY}
333     : FOR    ${vm}    IN    @{vm_list}
334     \    ${rc}    ${vm_ip_line}=    Run And Return Rc And Output    openstack console log show ${vm} | grep -i "obtained"
335     \    @{vm_ip}    Get Regexp Matches    ${vm_ip_line}    [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}
336     \    ${vm_ip_length}    Get Length    ${vm_ip}
337     \    Run Keyword If    ${vm_ip_length}>0    Append To List    ${ip_list}    @{vm_ip}[0]
338     \    ...    ELSE    Append To List    ${ip_list}    None
339     \    ${rc}    ${dhcp_ip_line}=    Run And Return Rc And Output    openstack console log show ${vm} | grep "^nameserver"
340     \    ${dhcp_ip}    Get Regexp Matches    ${dhcp_ip_line}    [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}
341     \    ${dhcp_ip_length}    Get Length    ${dhcp_ip}
342     \    Run Keyword If    ${dhcp_ip_length}<=0    Append To List    ${dhcp_ip}    None
343     \    ${vm_console_output}=    Run    openstack console log show ${vm}
344     \    Log    ${vm_console_output}
345     ${dhcp_length}    Get Length    ${dhcp_ip}
346     Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${ip_list}    None
347     Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${dhcp_ip}    None
348     Should Be True    ${dhcp_length} <= 1
349     Return From Keyword If    ${dhcp_length}==0    ${ip_list}    ${EMPTY}
350     [Return]    ${ip_list}    ${dhcp_ip}
351
352 Get Match
353     [Arguments]    ${text}    ${regexp}    ${index}=0
354     [Documentation]    Wrapper around Get Regexp Matches to return None if not found or the first match if found.
355     @{matches} =    String.Get Regexp Matches    ${text}    ${regexp}
356     ${matches_length} =    Get Length    ${matches}
357     BuiltIn.Set Suite Variable    ${OS_MATCH}    None
358     BuiltIn.Run Keyword If    ${matches_length} > ${index}    BuiltIn.Set Suite Variable    ${OS_MATCH}    @{matches}[${index}]
359     [Return]    ${OS_MATCH}
360
361 Get VM IP
362     [Arguments]    ${fail_on_none}    ${vm}
363     [Documentation]    Get the vm ip address and nameserver by scraping the vm's console log.
364     ...    Get VM IP returns three values: [0] the vm IP, [1] the DHCP IP and [2] the vm console log.
365     ${rc}    ${vm_console_output}=    Run And Return Rc And Output    openstack console log show ${vm}
366     ${vm_ip} =    Set Variable    None
367     ${dhcp_ip} =    Set Variable    None
368     ${match} =    Get Match    ${vm_console_output}    ${REGEX_OBTAINED}
369     ${vm_ip} =    Get Match    ${match}    ${REGEX_IPV4}    0
370     ${match} =    Get Match    ${vm_console_output}    ${REGEX_IPROUTE}
371     ${dhcp_ip} =    Get Match    ${match}    ${REGEX_IPV4}    1
372     BuiltIn.Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${vm_ip}    None
373     BuiltIn.Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${dhcp_ip}    None
374     [Return]    ${vm_ip}    ${dhcp_ip}    ${vm_console_output}
375
376 Get VM IPs
377     [Arguments]    @{vms}
378     [Documentation]    Get the instance IP addresses and nameserver address for the list of given vms.
379     ...    First poll for the vm instance to be in the active state, then poll for the vm ip address and nameserver.
380     ...    Get VM IPs returns two things: [0] a list of the ips for the vms passed to this keyword (may contain values
381     ...    of None) and [1] the dhcp ip address found in the last vm checked.
382     ...    TODO: there is a potential issue for a caller that passes in VMs belonging to different networks that
383     ...    may have different dhcp server addresses. Not sure what TODO about that, but noting it here for reference.
384     @{vm_ips}    BuiltIn.Create List    @{EMPTY}
385     : FOR    ${vm}    IN    @{vms}
386     \    Poll VM Is ACTIVE    ${vm}
387     \    ${status}    ${ips_and_console_log}    Run Keyword And Ignore Error    Wait Until Keyword Succeeds    180s    15s
388     \    ...    Get VM IP    true    ${vm}
389     \    # If there is trouble with Get VM IP, the status will be FAIL and the return value will be a string of what went
390     \    # wrong. We need to handle both the PASS and FAIL cases. In the FAIL case we know we wont have access to the
391     \    # console log, as it would not be returned; so we need to grab it again to log it. We also can append 'None' to
392     \    # the vm ip list if status is FAIL.
393     \    Run Keyword If    "${status}" == "PASS"    BuiltIn.Log    ${ips_and_console_log[2]}
394     \    BuiltIn.Run Keyword If    "${status}" == "PASS"    Collections.Append To List    ${vm_ips}    ${ips_and_console_log[0]}
395     \    BuiltIn.Run Keyword If    "${status}" == "FAIL"    Collections.Append To List    ${vm_ips}    None
396     \    ${rc}    ${vm_console_output}=    BuiltIn.Run Keyword If    "${status}" == "FAIL"    Run And Return Rc And Output    openstack console log show ${vm}
397     \    BuiltIn.Run Keyword If    "${status}" == "FAIL"    BuiltIn.Log    ${vm_console_output}
398     Copy DHCP Files From Control Node
399     [Return]    @{vm_ips}    ${ips_and_console_log[1]}
400
401 Collect VM IPv6 SLAAC Addresses
402     [Arguments]    ${fail_on_none}    ${vm_list}    ${network}    ${subnet}
403     [Documentation]    For each VM parse output of "openstack server show" to get its IPv6 address from Neutron DB.
404     ...    Then try to connect to each VM by SSH and execute there "ip -6 a" command. This double-check allows to
405     ...    obtain and compare IP info (Neutron DB vs dnsmasque/ODL DHCP) and to test L2 connectivity as well.
406     ...    Returns an empty list if no IPv6 addresses found or if SSH connection fails.
407     ...    Otherwise, returns a list of IPv6 addresses.
408     ${ipv6_list}    Create List    @{EMPTY}
409     : FOR    ${vm}    IN    @{vm_list}
410     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack server show ${vm} -f shell
411     \    Log    ${output}
412     \    Should Be True    '${rc}' == '0'
413     \    ${pattern}=    Replace String    ${subnet}    ::/64    (:[a-f0-9]{,4}){,4}
414     \    @{vm_ipv6}=    Get Regexp Matches    ${output}    ${pattern}
415     \    ${vm_ip_length}    Get Length    ${vm_ipv6}[0]
416     \    ${ipv6_data_from_vm}=    Run Keyword If    ${vm_ip_length}>0    Execute Command on VM Instance    ${network}    ${vm_ipv6[0]}
417     \    ...    ip -6 a
418     \    @{ipv6}=    Get Regexp Matches    ${ipv6_data_from_vm}    ${pattern}
419     \    ${ipv6_addr_list_length}    Get Length    @{ipv6}
420     \    Run Keyword If    ${ipv6_addr_list_length}>0    Append To List    ${ipv6_list}    ${ipv6[0]}
421     \    ...    ELSE    Append To List    ${ipv6_list}    None
422     [Return]    ${ipv6_list}
423
424 View Vm Console
425     [Arguments]    ${vm_instance_names}
426     [Documentation]    View Console log of the created vm instances using nova show.
427     : FOR    ${VmElement}    IN    @{vm_instance_names}
428     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack server show ${VmElement}
429     \    Log    ${output}
430     \    Should Be True    '${rc}' == '0'
431     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack console log show ${VmElement}
432     \    Log    ${output}
433     \    Should Be True    '${rc}' == '0'
434
435 Ping Vm From DHCP Namespace
436     [Arguments]    ${net_name}    ${vm_ip}
437     [Documentation]    Reach all Vm Instance with the net id of the Netowrk.
438     Get ControlNode Connection
439     ${net_id}=    Get Net Id    ${net_name}
440     ${output}=    Write Commands Until Prompt And Log    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
441     Should Contain    ${output}    64 bytes
442
443 Ping From DHCP Should Not Succeed
444     [Arguments]    ${net_name}    ${vm_ip}
445     [Documentation]    Should Not Reach Vm Instance with the net id of the Netowrk.
446     Return From Keyword If    "skip_if_${SECURITY_GROUP_MODE}" in @{TEST_TAGS}
447     Get ControlNode Connection
448     ${net_id}=    Get Net Id    ${net_name}
449     ${output}=    Write Commands Until Prompt And Log    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
450     Should Not Contain    ${output}    64 bytes
451
452 Ping Vm From Control Node
453     [Arguments]    ${vm_floating_ip}    ${additional_args}=${EMPTY}
454     [Documentation]    Ping VM floating IP from control node
455     Get ControlNode Connection
456     ${output}=    Write Commands Until Prompt And Log    ping ${additional_args} -c 3 ${vm_floating_ip}    20s
457     Should Contain    ${output}    64 bytes
458
459 Curl Metadata Server
460     [Documentation]    Ping to the expected destination ip.
461     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
462     Write Commands Until Prompt    exit
463     Should Contain    ${output}    200
464
465 Close Vm Instance
466     [Documentation]    Exit the vm instance.
467     ${output}=    Write Commands Until Prompt And Log    exit
468
469 Check If Console Is VmInstance
470     [Arguments]    ${console}=cirros
471     [Documentation]    Check if the session has been able to login to the VM instance
472     ${output}=    Write Commands Until Expected Prompt    id    ${OS_SYSTEM_PROMPT}
473     Should Contain    ${output}    ${console}
474
475 Exit From Vm Console
476     [Documentation]    Check if the session has been able to login to the VM instance and exit the instance
477     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance    cirros
478     Run Keyword If    ${rcode}    Write Commands Until Prompt    exit
479
480 Check Ping
481     [Arguments]    ${ip_address}    ${ttl}=64
482     [Documentation]    Run Ping command on the IP available as argument
483     ${ethertype}=    Get Regexp Matches    ${ip_address}    ${IP_REGEX}
484     ${output}=    Run Keyword If    ${ethertype}    Write Commands Until Expected Prompt    ping -t ${ttl} -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
485     ...    ELSE    Write Commands Until Expected Prompt    ping6 -t ${ttl} -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
486     Should Contain    ${output}    64 bytes
487
488 Check No Ping
489     [Arguments]    ${ip_address}    ${ttl}=64
490     [Documentation]    Run Ping command to the IP given as argument, executing 3 times and expecting NOT to see "64 bytes"
491     ${output}=    Write Commands Until Expected Prompt    ping -t ${ttl} -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
492     Should Not Contain    ${output}    64 bytes
493
494 Check Metadata Access
495     [Documentation]    Try curl on the Metadataurl and check if it is okay
496     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
497     Should Contain    ${output}    200
498
499 Execute Command on VM Instance
500     [Arguments]    ${net_name}    ${vm_ip}    ${cmd}    ${user}=cirros    ${password}=cubswin:)
501     [Documentation]    Login to the vm instance using ssh in the network, executes a command inside the VM and returns the ouput.
502     Get ControlNode Connection
503     ${net_id} =    Get Net Id    ${net_name}
504     ${output} =    Write Commands Until Expected Prompt    sudo ip netns exec qdhcp-${net_id} ssh ${user}@${vm_ip} -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null    password:
505     ${output} =    Write Commands Until Expected Prompt    ${password}    ${OS_SYSTEM_PROMPT}
506     ${rcode} =    Run Keyword And Return Status    Check If Console Is VmInstance
507     ${output} =    Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ${cmd}    ${OS_SYSTEM_PROMPT}
508     [Teardown]    Exit From Vm Console
509     [Return]    ${output}
510
511 Test Operations From Vm Instance
512     [Arguments]    ${net_name}    ${src_ip}    ${dest_ips}    ${user}=cirros    ${password}=cubswin:)    ${ttl}=64
513     ...    ${ping_should_succeed}=True    ${check_metadata}=True
514     [Documentation]    Login to the vm instance using ssh in the network.
515     Get ControlNode Connection
516     ${net_id}=    Get Net Id    ${net_name}
517     ${output}=    Write Commands Until Expected Prompt    sudo ip netns exec qdhcp-${net_id} ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no ${user}@${src_ip} -o UserKnownHostsFile=/dev/null    password:
518     ${output}=    Write Commands Until Expected Prompt    ${password}    ${OS_SYSTEM_PROMPT}
519     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
520     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ifconfig    ${OS_SYSTEM_PROMPT}
521     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    route -n    ${OS_SYSTEM_PROMPT}
522     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    route -A inet6    ${OS_SYSTEM_PROMPT}
523     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    arp -an    ${OS_SYSTEM_PROMPT}
524     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ip -f inet6 neigh show    ${OS_SYSTEM_PROMPT}
525     : FOR    ${dest_ip}    IN    @{dest_ips}
526     \    ${string_empty}=    Run Keyword And Return Status    Should Be Empty    ${dest_ip}
527     \    Run Keyword If    ${string_empty}    Continue For Loop
528     \    Run Keyword If    ${rcode} and "${ping_should_succeed}" == "True"    Check Ping    ${dest_ip}    ttl=${ttl}
529     \    ...    ELSE    Check No Ping    ${dest_ip}    ttl=${ttl}
530     ${ethertype}=    Get Regexp Matches    ${src_ip}    ${IP_REGEX}
531     Run Keyword If    ${rcode} and "${check_metadata}" and ${ethertype} == "True"    Check Metadata Access
532     [Teardown]    Exit From Vm Console
533
534 Test Netcat Operations From Vm Instance
535     [Arguments]    ${net_name}    ${vm_ip}    ${dest_ip}    ${additional_args}=${EMPTY}    ${port}=12345    ${user}=cirros
536     ...    ${password}=cubswin:)
537     [Documentation]    Use Netcat to test TCP/UDP connections to the controller
538     ${client_data}    Set Variable    Test Client Data
539     ${server_data}    Set Variable    Test Server Data
540     Get ControlNode Connection
541     ${output}=    Write Commands Until Prompt And Log    ( ( echo "${server_data}" | sudo timeout 60 nc -l ${additional_args} ${port} ) & )
542     ${output}=    Write Commands Until Prompt And Log    sudo netstat -nlap | grep ${port}
543     ${nc_output}=    Execute Command on VM Instance    ${net_name}    ${vm_ip}    sudo echo "${client_data}" | nc -v -w 5 ${additional_args} ${dest_ip} ${port}
544     ${output}=    Execute Command on VM Instance    ${net_name}    ${vm_ip}    sudo route -n
545     Log    ${output}
546     ${output}=    Execute Command on VM Instance    ${net_name}    ${vm_ip}    sudo arp -an
547     Log    ${output}
548     Should Match Regexp    ${nc_output}    ${server_data}
549
550 Ping Other Instances
551     [Arguments]    ${list_of_external_dst_ips}
552     [Documentation]    Check reachability with other network's instances.
553     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
554     : FOR    ${dest_ip}    IN    @{list_of_external_dst_ips}
555     \    Check Ping    ${dest_ip}
556
557 Create Router
558     [Arguments]    ${router_name}
559     [Documentation]    Create Router and Add Interface to the subnets.
560     ${rc}    ${output}=    Run And Return Rc And Output    openstack router create ${router_name}
561     Should Be True    '${rc}' == '0'
562
563 List Routers
564     [Documentation]    List Routers and return output with neutron client.
565     ${rc}    ${output}=    Run And Return Rc And Output    openstack router list -f value
566     Log    ${output}
567     Should Be True    '${rc}' == '0'
568     [Return]    ${output}
569
570 Add Router Interface
571     [Arguments]    ${router_name}    ${interface_name}
572     ${rc}    ${output}=    Run And Return Rc And Output    openstack router add subnet ${router_name} ${interface_name}
573     Should Be True    '${rc}' == '0'
574
575 Show Router Interface
576     [Arguments]    ${router_name}
577     [Documentation]    List Routers interface associated with given Router and return output with neutron client.
578     ${rc}    ${output}=    Run And Return Rc And Output    openstack port list --router ${router_name} -f value
579     Should Be True    '${rc}' == '0'
580     [Return]    ${output}
581
582 Add Router Gateway
583     [Arguments]    ${router_name}    ${external_network_name}
584     ${cmd}=    Set Variable If    '${OPENSTACK_BRANCH}'=='stable/newton'    neutron -v router-gateway-set ${router_name} ${external_network_name}    openstack router set ${router_name} --external-gateway ${external_network_name}
585     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
586     Should Be True    '${rc}' == '0'
587
588 Remove Interface
589     [Arguments]    ${router_name}    ${interface_name}
590     [Documentation]    Remove Interface to the subnets.
591     ${rc}    ${output}=    Run And Return Rc And Output    openstack router remove subnet ${router_name} ${interface_name}
592     Should Be True    '${rc}' == '0'
593
594 Remove Gateway
595     [Arguments]    ${router_name}
596     [Documentation]    Remove external gateway from the router.
597     BuiltIn.Log    openstack router unset ${router_name} --external-gateway
598
599 Update Router
600     [Arguments]    ${router_name}    ${cmd}
601     [Documentation]    Update the router with the command. Router name and command should be passed as argument.
602     ${rc}    ${output} =    Run And Return Rc And Output    openstack router set ${router_name} ${cmd}
603     Should Be True    '${rc}' == '0'
604
605 Show Router
606     [Arguments]    ${router_name}    ${options}
607     [Documentation]    Show information of a given router. Router name and optional fields should be sent as arguments.
608     ${rc}    ${output} =    Run And Return Rc And Output    openstack router show ${router_name}
609     Log    ${output}
610
611 Delete Router
612     [Arguments]    ${router_name}
613     [Documentation]    Delete Router and Interface to the subnets.
614     ${rc}    ${output}=    Run And Return Rc And Output    openstack router delete ${router_name}
615     Log    ${output}
616     Should Be True    '${rc}' == '0'
617
618 Get DumpFlows And Ovsconfig
619     [Arguments]    ${conn_id}
620     [Documentation]    Get the OvsConfig and Flow entries from OVS from the Openstack Node
621     SSHLibrary.Switch Connection    ${conn_id}
622     Write Commands Until Expected Prompt    ip -o link    ${DEFAULT_LINUX_PROMPT_STRICT}
623     Write Commands Until Expected Prompt    ip -o addr    ${DEFAULT_LINUX_PROMPT_STRICT}
624     Write Commands Until Expected Prompt    ip route    ${DEFAULT_LINUX_PROMPT_STRICT}
625     Write Commands Until Expected Prompt    arp -an    ${DEFAULT_LINUX_PROMPT_STRICT}
626     ${nslist}=    Write Commands Until Expected Prompt    ip netns list | awk '{print $1}'    ${DEFAULT_LINUX_PROMPT_STRICT}
627     @{lines}    Split To Lines    ${nslist}    end=-1
628     : FOR    ${line}    IN    @{lines}
629     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip -o link    ${DEFAULT_LINUX_PROMPT_STRICT}
630     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip -o addr    ${DEFAULT_LINUX_PROMPT_STRICT}
631     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip route    ${DEFAULT_LINUX_PROMPT_STRICT}
632     Write Commands Until Expected Prompt    sudo ovs-vsctl show    ${DEFAULT_LINUX_PROMPT_STRICT}
633     Write Commands Until Expected Prompt    sudo ovs-vsctl list Open_vSwitch    ${DEFAULT_LINUX_PROMPT_STRICT}
634     Write Commands Until Expected Prompt    sudo ovs-ofctl show br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
635     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-flows br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
636     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-groups br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
637     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-group-stats br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
638
639 Get Karaf Log Type From Test Start
640     [Arguments]    ${ip}    ${test_name}    ${type}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
641     ...    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
642     ${cmd}    Set Variable    sed '1,/ROBOT MESSAGE: Starting test ${test_name}/d' ${log_file} | grep '${type}'
643     ${output}    Run Command On Controller    ${ip}    ${cmd}    ${user}    ${password}    ${prompt}
644     [Return]    ${output}
645
646 Get Karaf Log Types From Test Start
647     [Arguments]    ${ip}    ${test_name}    ${types}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
648     ...    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
649     : FOR    ${type}    IN    @{types}
650     \    Get Karaf Log Type From Test Start    ${ip}    ${test_name}    ${type}    ${user}    ${password}
651     \    ...    ${prompt}    ${log_file}
652
653 Get Karaf Log Events From Test Start
654     [Arguments]    ${test_name}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
655     ${log_types} =    Create List    ERROR    WARN    Exception
656     Run Keyword If    0 < ${NUM_ODL_SYSTEM}    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_IP}    ${test_name}    ${log_types}
657     Run Keyword If    1 < ${NUM_ODL_SYSTEM}    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_2_IP}    ${test_name}    ${log_types}
658     Run Keyword If    2 < ${NUM_ODL_SYSTEM}    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_3_IP}    ${test_name}    ${log_types}
659
660 Get ControlNode Connection
661     SSHLibrary.Switch Connection    ${OS_CNTL_CONN_ID}
662     [Return]    ${OS_CNTL_CONN_ID}
663
664 Get OvsDebugInfo
665     [Documentation]    Get the OvsConfig and Flow entries from all Openstack nodes
666     Run Keyword If    0 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_CNTL_CONN_ID}
667     Run Keyword If    1 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_CMP1_CONN_ID}
668     Run Keyword If    2 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_CMP2_CONN_ID}
669
670 Get Test Teardown Debugs
671     [Arguments]    ${test_name}=${TEST_NAME}
672     Get OvsDebugInfo
673     Run Keyword And Ignore Error    Get Model Dump    ${HA_PROXY_IP}    ${netvirt_data_models}
674     Get Karaf Log Events From Test Start    ${test_name}
675
676 Get Test Teardown Debugs For SFC
677     [Arguments]    ${test_name}=${TEST_NAME}
678     Run Keyword And Ignore Error    Get Model Dump    ${HA_PROXY_IP}    ${netvirt_sfc_data_models}
679
680 Show Debugs
681     [Arguments]    @{vm_indices}
682     [Documentation]    Run these commands for debugging, it can list state of VM instances and ip information in control node
683     Get ControlNode Connection
684     ${output}=    Write Commands Until Prompt And Log    sudo ip netns list
685     : FOR    ${index}    IN    @{vm_indices}
686     \    ${rc}    ${output}=    Run And Return Rc And Output    nova show ${index}
687     \    Log    ${output}
688     List Nova VMs
689     List Routers
690     List Networks
691     List Subnets
692     List Ports
693     List Security Groups
694
695 List Security Groups
696     [Documentation]    Logging keyword to display all security groups using the openstack cli. Assumes openstack
697     ...    credentials are already sourced
698     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group list
699     Log    ${output}
700     Should Be True    '${rc}' == '0'
701     [Return]    ${output}
702
703 Neutron Security Group Show
704     [Arguments]    ${SecurityGroupRuleName}
705     [Documentation]    Displays the neutron security group configurations that belongs to a given neutron security group name
706     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group show ${SecurityGroupRuleName}
707     Log    ${output}
708     Should Be True    '${rc}' == '0'
709     [Return]    ${output}
710
711 Neutron Port Show
712     [Arguments]    ${PortName}
713     [Documentation]    Display the port configuration that belong to a given neutron port
714     ${rc}    ${output}=    Run And Return Rc And Output    openstack port show ${PortName}
715     Log    ${output}
716     Should Be True    '${rc}' == '0'
717     [Return]    ${output}
718
719 Neutron Security Group Create
720     [Arguments]    ${SecurityGroupName}    ${additional_args}=${EMPTY}
721     [Documentation]    Create a security group with specified name ,description & protocol value according to security group template
722     Get ControlNode Connection
723     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group create ${SecurityGroupName} ${additional_args}
724     Log    ${output}
725     Should Be True    '${rc}' == '0'
726     ${sgp_id}=    Should Match Regexp    ${output}    [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
727     Log    ${sgp_id}
728     [Return]    ${output}    ${sgp_id}
729
730 Neutron Security Group Update
731     [Arguments]    ${SecurityGroupName}    ${additional_args}=${EMPTY}
732     [Documentation]    Updating security groups
733     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group set ${SecurityGroupName} ${additional_args}
734     Log    ${output}
735     Should Be True    '${rc}' == '0'
736     [Return]    ${output}
737
738 Delete SecurityGroup
739     [Arguments]    ${sg_name}
740     [Documentation]    Delete Security group
741     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group delete ${sg_name}
742     Log    ${output}
743     Should Be True    '${rc}' == '0'
744
745 Neutron Security Group Rule Create
746     [Arguments]    ${Security_group_name}    &{Kwargs}
747     [Documentation]    Creates neutron security rule with Openstack CLI with or without optional params, here security group name is mandatory args, rule with optional params can be created by passing the optional args values ex: direction=${INGRESS_EGRESS}, Then these optional params are catenated with mandatory args, example of usage: "Neutron Security Group Rule Create ${SGP_SSH} direction=${RULE_PARAMS[0]} ethertype=${RULE_PARAMS[1]} ..."
748     Run Keyword If    ${Kwargs}    Log    ${Kwargs}
749     ${description}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    description    default=${None}
750     ${direction}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    direction    default=${None}
751     ${ethertype}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    ethertype    default=${None}
752     ${port_range_max}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_max    default=${None}
753     ${port_range_min}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_min    default=${None}
754     ${protocol}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    protocol    default=${None}
755     ${remote_group_id}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_group_id    default=${None}
756     ${remote_ip_prefix}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_ip_prefix    default=${None}
757     ${cmd}=    Set Variable    openstack security group rule create ${Security_group_name}
758     ${cmd}=    Run Keyword If    '${description}'!='None'    Catenate    ${cmd}    --description ${description}
759     ...    ELSE    Catenate    ${cmd}
760     ${cmd}=    Run Keyword If    '${direction}'!='None'    Catenate    ${cmd}    --${direction}
761     ...    ELSE    Catenate    ${cmd}
762     ${cmd}=    Run Keyword If    '${ethertype}'!='None'    Catenate    ${cmd}    --ethertype ${ethertype}
763     ...    ELSE    Catenate    ${cmd}
764     ${cmd}=    Run Keyword If    '${port_range_min}'!='None' and '${port_range_max}'!='None'    Catenate    ${cmd}    --dst-port ${port_range_min}:${port_range_max}
765     ...    ELSE IF    '${port_range_max}'!='None'    Catenate    ${cmd}    --dst-port ${port_range_max}
766     ...    ELSE IF    '${port_range_min}'!='None'    Catenate    ${cmd}    --dst-port ${port_range_min}
767     ...    ELSE    Catenate    ${cmd}
768     ${cmd}=    Run Keyword If    '${protocol}'!='None'    Catenate    ${cmd}    --protocol ${protocol}
769     ...    ELSE    Catenate    ${cmd}
770     ${cmd}=    Run Keyword If    '${remote_group_id}'!='None'    Catenate    ${cmd}    --remote-group ${remote_group_id}
771     ...    ELSE    Catenate    ${cmd}
772     ${cmd}=    Run Keyword If    '${remote_ip_prefix}'!='None'    Catenate    ${cmd}    --src-ip ${remote_ip_prefix}
773     ...    ELSE    Catenate    ${cmd}
774     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
775     ${rule_id}=    Should Match Regexp    ${output}    [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
776     Log    ${rule_id}
777     Should Be True    '${rc}' == '0'
778     [Return]    ${output}    ${rule_id}
779
780 Neutron Security Group Rule Create Legacy Cli
781     [Arguments]    ${Security_group_name}    &{Kwargs}
782     [Documentation]    Creates neutron security rule with neutron request with or without optional params, here security group name is mandatory args, rule with optional params can be created by passing the optional args values ex: direction=${INGRESS_EGRESS}, Then these optional params are catenated with mandatory args, example of usage: "Neutron Security Group Rule Create ${SGP_SSH} direction=${RULE_PARAMS[0]} ethertype=${RULE_PARAMS[1]} ..."
783     Run Keyword If    ${Kwargs}    Log    ${Kwargs}
784     ${description}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    description    default=${None}
785     ${direction}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    direction    default=${None}
786     ${ethertype}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    ethertype    default=${None}
787     ${port_range_max}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_max    default=${None}
788     ${port_range_min}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_min    default=${None}
789     ${protocol}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    protocol    default=${None}
790     ${remote_group_id}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_group_id    default=${None}
791     ${remote_ip_prefix}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_ip_prefix    default=${None}
792     ${cmd}=    Set Variable    neutron security-group-rule-create ${Security_group_name}
793     ${cmd}=    Run Keyword If    '${description}'!='None'    Catenate    ${cmd}    --description ${description}
794     ...    ELSE    Catenate    ${cmd}
795     ${cmd}=    Run Keyword If    '${direction}'!='None'    Catenate    ${cmd}    --direction ${direction}
796     ...    ELSE    Catenate    ${cmd}
797     ${cmd}=    Run Keyword If    '${ethertype}'!='None'    Catenate    ${cmd}    --ethertype ${ethertype}
798     ...    ELSE    Catenate    ${cmd}
799     ${cmd}=    Run Keyword If    '${port_range_max}'!='None'    Catenate    ${cmd}    --port_range_max ${port_range_max}
800     ...    ELSE    Catenate    ${cmd}
801     ${cmd}=    Run Keyword If    '${port_range_min}'!='None'    Catenate    ${cmd}    --port_range_min ${port_range_min}
802     ...    ELSE    Catenate    ${cmd}
803     ${cmd}=    Run Keyword If    '${protocol}'!='None'    Catenate    ${cmd}    --protocol ${protocol}
804     ...    ELSE    Catenate    ${cmd}
805     ${cmd}=    Run Keyword If    '${remote_group_id}'!='None'    Catenate    ${cmd}    --remote_group_id ${remote_group_id}
806     ...    ELSE    Catenate    ${cmd}
807     ${cmd}=    Run Keyword If    '${remote_ip_prefix}'!='None'    Catenate    ${cmd}    --remote_ip_prefix ${remote_ip_prefix}
808     ...    ELSE    Catenate    ${cmd}
809     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
810     ${rule_id}=    Should Match Regexp    ${output}    [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
811     Log    ${rule_id}
812     Should Be True    '${rc}' == '0'
813     [Return]    ${output}    ${rule_id}
814
815 Security Group Create Without Default Security Rules
816     [Arguments]    ${sg_name}    ${additional_args}=${EMPTY}
817     [Documentation]    Create Neutron Security Group with no default rules, using specified name and optional arguments.
818     Neutron Security Group Create    ${sg_name}    ${additional_args}
819     Delete All Security Group Rules    ${sg_name}
820
821 Delete All Security Group Rules
822     [Arguments]    ${sg_name}
823     [Documentation]    Delete all security rules from a specified security group
824     ${rc}    ${sg_rules_output}=    Run And Return Rc And Output    openstack security group rule list ${sg_name} -cID -fvalue
825     Log    ${sg_rules_output}
826     Should Be True    '${rc}' == '0'
827     @{sg_rules}=    Split String    ${sg_rules_output}    \n
828     : FOR    ${rule}    IN    @{sg_rules}
829     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack security group rule delete ${rule}
830     \    Log    ${output}
831     \    Should Be True    '${rc}' == '0'
832
833 Create Allow All SecurityGroup
834     [Arguments]    ${sg_name}    ${ether_type}=IPv4
835     [Documentation]    Allow all TCP/UDP/ICMP packets for this suite
836     Neutron Security Group Create    ${sg_name}
837     Neutron Security Group Rule Create    ${sg_name}    direction=ingress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=tcp
838     Neutron Security Group Rule Create    ${sg_name}    direction=egress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=tcp
839     Neutron Security Group Rule Create    ${sg_name}    direction=ingress    ethertype=${ether_type}    protocol=icmp
840     Neutron Security Group Rule Create    ${sg_name}    direction=egress    ethertype=${ether_type}    protocol=icmp
841     Neutron Security Group Rule Create    ${sg_name}    direction=ingress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=udp
842     Neutron Security Group Rule Create    ${sg_name}    direction=egress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=udp
843
844 Create Neutron Port With Additional Params
845     [Arguments]    ${network_name}    ${port_name}    ${additional_args}=${EMPTY}
846     [Documentation]    Create Port With given additional parameters
847     ${rc}    ${output}=    Run And Return Rc And Output    neutron -v port-create ${network_name} --name ${port_name} ${additional_args}
848     Log    ${output}
849     Should Be True    '${rc}' == '0'
850     ${port_id}=    Should Match Regexp    ${OUTPUT}    [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
851     Log    ${port_id}
852     [Return]    ${OUTPUT}    ${port_id}
853
854 Get Ports MacAddr
855     [Arguments]    ${portName_list}
856     [Documentation]    Retrieve the port MacAddr for the given list of port name and return the MAC address list.
857     ${MacAddr-list}    Create List
858     : FOR    ${portName}    IN    @{portName_list}
859     \    ${macAddr}=    OpenStackOperations.Get Port Mac    ${portName}
860     \    Append To List    ${MacAddr-list}    ${macAddr}
861     [Return]    ${MacAddr-list}
862
863 Get Port Ip
864     [Arguments]    ${port_name}
865     [Documentation]    Keyword would return the IP of the ${port_name} received.
866     ${rc}    ${output}=    Run And Return Rc And Output    openstack port list | grep "${port_name}" | awk -F\\' '{print $2}'
867     ${splitted_output}=    Split String    ${output}    ${EMPTY}
868     ${port_ip}=    Get from List    ${splitted_output}    0
869     Should Be True    '${rc}' == '0'
870     [Return]    ${port_ip}
871
872 Get Port Mac
873     [Arguments]    ${port_name}
874     [Documentation]    Keyword would return the MAC ID of the ${port_name} received.
875     ${rc}    ${output}=    Run And Return Rc And Output    openstack port show ${port_name} | grep mac_address | awk '{print $4}'
876     ${splitted_output}=    Split String    ${output}    ${EMPTY}
877     ${port_mac}=    Get from List    ${splitted_output}    0
878     Should Be True    '${rc}' == '0'
879     [Return]    ${port_mac}
880
881 Create L2Gateway
882     [Arguments]    ${bridge_name}    ${intf_name}    ${gw_name}
883     [Documentation]    Keyword to create an L2 Gateway ${gw_name} for bridge ${bridge_name} connected to interface ${intf_name} (Using Neutron CLI).
884     ${rc}    ${l2gw_output}=    Run And Return Rc And Output    ${L2GW_CREATE} name=${bridge_name},interface_names=${intf_name} ${gw_name}
885     Log    ${l2gw_output}
886     [Return]    ${l2gw_output}
887
888 Create L2Gateway Connection
889     [Arguments]    ${gw_name}    ${net_name}
890     [Documentation]    Keyword would create a new L2 Gateway Connection for ${gw_name} to ${net_name} (Using Neutron CLI).
891     ${rc}    ${l2gw_output}=    Run And Return Rc And Output    ${L2GW_CONN_CREATE} ${gw_name} ${net_name}
892     Log    ${l2gw_output}
893     Should Be True    '${rc}' == '0'
894     [Return]    ${l2gw_output}
895
896 Get All L2Gateway
897     [Documentation]    Keyword to return all the L2 Gateways available (Using Neutron CLI).
898     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET_YAML}
899     Should Be True    '${rc}' == '0'
900     [Return]    ${output}
901
902 Get All L2Gateway Connection
903     [Documentation]    Keyword to return all the L2 Gateway connections available (Using Neutron CLI).
904     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET_CONN_YAML}
905     Should Be True    '${rc}' == '0'
906     [Return]    ${output}
907
908 Get L2Gateway
909     [Arguments]    ${gw_id}
910     [Documentation]    Keyword to check if the ${gw_id} is available in the L2 Gateway list (Using Neutron CLI).
911     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_SHOW} ${gw_id}
912     Log    ${output}
913     Should Be True    '${rc}' == '0'
914     [Return]    ${output}
915
916 Get L2gw Id
917     [Arguments]    ${l2gw_name}
918     [Documentation]    Keyword to retrieve the L2 Gateway ID for the ${l2gw_name} (Using Neutron CLI).
919     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET} | grep "${l2gw_name}" | awk '{print $2}'
920     Log    ${output}
921     Should Be True    '${rc}' == '0'
922     ${splitted_output}=    Split String    ${output}    ${EMPTY}
923     ${l2gw_id}=    Get from List    ${splitted_output}    0
924     [Return]    ${l2gw_id}
925
926 Get L2gw Connection Id
927     [Arguments]    ${l2gw_name}
928     [Documentation]    Keyword to retrieve the L2 Gateway Connection ID for the ${l2gw_name} (Using Neutron CLI).
929     ${l2gw_id}=    OpenStackOperations.Get L2gw Id    ${l2gw_name}
930     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET_CONN} | grep "${l2gw_id}" | awk '{print $2}'
931     Should Be True    '${rc}' == '0'
932     ${splitted_output}=    Split String    ${output}    ${EMPTY}
933     ${splitted_output}=    Split String    ${output}    ${EMPTY}
934     ${l2gw_conn_id}=    Get from List    ${splitted_output}    0
935     [Return]    ${l2gw_conn_id}
936
937 Neutron Port List Rest
938     [Documentation]    Keyword to get all ports details in Neutron (Using REST).
939     ${resp} =    RequestsLibrary.Get Request    session    ${PORT_URL}
940     Log    ${resp.content}
941     Should Be Equal As Strings    ${resp.status_code}    200
942     [Return]    ${resp.content}
943
944 Get Neutron Port Rest
945     [Arguments]    ${port_id}
946     [Documentation]    Keyword to get the specific port details in Neutron (Using REST).
947     ${resp} =    RequestsLibrary.Get Request    session    ${CONFIG_API}/${GET_PORT_URL}/${port_id}
948     Log    ${resp.content}
949     Should Be Equal As Strings    ${resp.status_code}    200
950     [Return]    ${resp.content}
951
952 Update Port Rest
953     [Arguments]    ${port_id}    ${json_data}
954     [Documentation]    Keyword to update ${port_id} with json data received in ${json_data} (Using REST).
955     Log    ${json_data}
956     ${resp} =    RequestsLibrary.Put Request    session    ${CONFIG_API}/${GET_PORT_URL}/${port_id}    ${json_data}
957     Log    ${resp.content}
958     Should Be Equal As Strings    ${resp.status_code}    200
959     [Return]    ${resp.content}
960
961 Create And Configure Security Group
962     [Arguments]    ${sg-name}
963     [Documentation]    Create Security Group with given name, and default allow rules for TCP/UDP/ICMP protocols.
964     Neutron Security Group Create    ${sg-name}
965     Neutron Security Group Rule Create    ${sg-name}    direction=ingress    port_range_max=65535    port_range_min=1    protocol=tcp    remote_ip_prefix=0.0.0.0/0
966     Neutron Security Group Rule Create    ${sg-name}    direction=egress    port_range_max=65535    port_range_min=1    protocol=tcp    remote_ip_prefix=0.0.0.0/0
967     Neutron Security Group Rule Create    ${sg-name}    direction=ingress    protocol=icmp    remote_ip_prefix=0.0.0.0/0
968     Neutron Security Group Rule Create    ${sg-name}    direction=egress    protocol=icmp    remote_ip_prefix=0.0.0.0/0
969     Neutron Security Group Rule Create    ${sg-name}    direction=ingress    port_range_max=65535    port_range_min=1    protocol=udp    remote_ip_prefix=0.0.0.0/0
970     Neutron Security Group Rule Create    ${sg-name}    direction=egress    port_range_max=65535    port_range_min=1    protocol=udp    remote_ip_prefix=0.0.0.0/0
971
972 Add Security Group To VM
973     [Arguments]    ${vm}    ${sg}
974     [Documentation]    Add the security group provided to the given VM.
975     ${rc}    ${output}=    Run And Return Rc And Output    openstack server add security group ${vm} ${sg}
976     Log    ${output}
977     Should Be True    '${rc}' == '0'
978
979 Remove Security Group From VM
980     [Arguments]    ${vm}    ${sg}
981     [Documentation]    Remove the security group provided to the given VM.
982     Get ControlNode Connection
983     ${output}=    Write Commands Until Prompt And Log    openstack server remove security group ${vm} ${sg}
984
985 Create SFC Flow Classifier
986     [Arguments]    ${name}    ${src_ip}    ${dest_ip}    ${protocol}    ${dest_port}    ${neutron_src_port}
987     [Documentation]    Create a flow classifier for SFC
988     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc flow classifier create --ethertype IPv4 --source-ip-prefix ${src_ip}/32 --destination-ip-prefix ${dest_ip}/32 --protocol ${protocol} --destination-port ${dest_port}:${dest_port} --logical-source-port ${neutron_src_port} ${name}
989     Log    ${output}
990     Should Be True    '${rc}' == '0'
991     Should Contain    ${output}    ${name}
992     [Return]    ${output}
993
994 Delete SFC Flow Classifier
995     [Arguments]    ${name}
996     [Documentation]    Delete a SFC flow classifier
997     Get ControlNode Connection
998     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc flow classifier delete ${name}
999     Log    ${output}
1000     Should Be True    '${rc}' == '0'
1001     [Return]    ${output}
1002
1003 Create SFC Port Pair
1004     [Arguments]    ${name}    ${port_in}    ${port_out}
1005     [Documentation]    Creates a neutron port pair for SFC
1006     Get ControlNode Connection
1007     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair create --ingress=${port_in} --egress=${port_out} ${name}
1008     Log    ${output}
1009     Should Be True    '${rc}' == '0'
1010     Should Contain    ${output}    ${name}
1011     [Return]    ${output}
1012
1013 Delete SFC Port Pair
1014     [Arguments]    ${name}
1015     [Documentation]    Delete a SFC port pair
1016     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair delete ${name}
1017     Log    ${output}
1018     Should Be True    '${rc}' == '0'
1019     [Return]    ${output}
1020
1021 Create SFC Port Pair Group
1022     [Arguments]    ${name}    ${port_pair}
1023     [Documentation]    Creates a port pair group with a single port pair for SFC
1024     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair group create --port-pair ${port_pair} ${name}
1025     Log    ${output}
1026     Should Be True    '${rc}' == '0'
1027     Should Contain    ${output}    ${name}
1028     [Return]    ${output}
1029
1030 Create SFC Port Pair Group With Two Pairs
1031     [Arguments]    ${name}    ${port_pair1}    ${port_pair2}
1032     [Documentation]    Creates a port pair group with two port pairs for SFC
1033     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair group create --port-pair ${port_pair1} --port-pair ${port_pair2} ${name}
1034     Log    ${output}
1035     Should Be True    '${rc}' == '0'
1036     Should Contain    ${output}    ${name}
1037     [Return]    ${output}
1038
1039 Delete SFC Port Pair Group
1040     [Arguments]    ${name}
1041     [Documentation]    Delete a SFC port pair group
1042     Get ControlNode Connection
1043     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair group delete ${name}
1044     Log    ${output}
1045     Should Be True    '${rc}' == '0'
1046     [Return]    ${output}
1047
1048 Create SFC Port Chain
1049     [Arguments]    ${name}    ${pg1}    ${pg2}    ${fc}
1050     [Documentation]    Creates a port pair chain with two port groups and a singel classifier.
1051     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port chain create --port-pair-group ${pg1} --port-pair-group ${pg2} --flow-classifier ${fc} ${name}
1052     Log    ${output}
1053     Should Be True    '${rc}' == '0'
1054     Should Contain    ${output}    ${name}
1055     [Return]    ${output}
1056
1057 Delete SFC Port Chain
1058     [Arguments]    ${name}
1059     [Documentation]    Delete a SFC port chain
1060     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port chain delete ${name}
1061     Log    ${output}
1062     Should Be True    '${rc}' == '0'
1063     [Return]    ${output}
1064
1065 Reboot Nova VM
1066     [Arguments]    ${vm_name}
1067     [Documentation]    Reboot NOVA VM
1068     ${rc}    ${output}=    Run And Return Rc And Output    openstack server reboot --wait ${vm_name}
1069     Log    ${output}
1070     Should Be True    '${rc}' == '0'
1071     Wait Until Keyword Succeeds    35s    10s    Verify VM Is ACTIVE    ${vm_name}
1072
1073 Remove RSA Key From KnownHosts
1074     [Arguments]    ${vm_ip}
1075     [Documentation]    Remove RSA
1076     Get ControlNode Connection
1077     ${output}=    Write Commands Until Prompt And Log    sudo cat /root/.ssh/known_hosts    30s
1078     ${output}=    Write Commands Until Prompt And Log    sudo ssh-keygen -f "/root/.ssh/known_hosts" -R ${vm_ip}    30s
1079     ${output}=    Write Commands Until Prompt    sudo cat "/root/.ssh/known_hosts"    30s
1080
1081 Wait For Routes To Propogate
1082     [Arguments]    ${networks}    ${subnets}
1083     [Documentation]    Check propagated routes
1084     Get ControlNode Connection
1085     : FOR    ${INDEX}    IN RANGE    0    1
1086     \    ${net_id}=    Get Net Id    @{networks}[${INDEX}]
1087     \    ${is_ipv6}=    Get Regexp Matches    @{subnets}[${INDEX}]    ${IP6_REGEX}
1088     \    ${length}=    Get Length    ${is_ipv6}
1089     \    ${cmd}=    Set Variable If    ${length} == 0    ip route    ip -6 route
1090     \    ${output}=    Write Commands Until Expected Prompt    sudo ip netns exec qdhcp-${net_id} ${cmd}    ]>
1091     \    Should Contain    ${output}    @{subnets}[${INDEX}]
1092
1093 Neutron Cleanup
1094     [Arguments]    ${vms}=@{EMPTY}    ${networks}=@{EMPTY}    ${subnets}=@{EMPTY}    ${ports}=@{EMPTY}    ${sgs}=@{EMPTY}
1095     : FOR    ${vm}    IN    @{vms}
1096     \    BuiltIn.Run Keyword And Ignore Error    Delete Vm Instance    ${vm}
1097     : FOR    ${port}    IN    @{ports}
1098     \    BuiltIn.Run Keyword And Ignore Error    Delete Port    ${port}
1099     : FOR    ${subnet}    IN    @{subnets}
1100     \    BuiltIn.Run Keyword And Ignore Error    Delete SubNet    ${subnet}
1101     : FOR    ${network}    IN    @{networks}
1102     \    BuiltIn.Run Keyword And Ignore Error    Delete Network    ${network}
1103     : FOR    ${sg}    IN    @{sgs}
1104     \    BuiltIn.Run Keyword And Ignore Error    Delete SecurityGroup    ${sg}
1105
1106 OpenStack List All
1107     [Documentation]    Get a list of different OpenStack resources that might be in use.
1108     @{modules} =    BuiltIn.Create List    floating ip    server    router    port    network
1109     ...    subnet    security group    security group rule
1110     : FOR    ${module}    IN    @{modules}
1111     \    OpenStack CLI    openstack ${module} list
1112
1113 OpenStack CLI Get List
1114     [Arguments]    ${cmd}
1115     [Documentation]    Return a json list from the output of an OpenStack command.
1116     ${json} =    OpenStack CLI    ${cmd}
1117     @{list} =    RequestsLibrary.To Json    ${json}
1118     BuiltIn.Log    ${list}
1119     [Return]    @{list}
1120
1121 OpenStack CLI
1122     [Arguments]    ${cmd}
1123     [Documentation]    Run the given OpenStack ${cmd}.
1124     ${rc}    ${output} =    OperatingSystem.Run And Return Rc And Output    ${cmd}
1125     BuiltIn.Log    ${output}
1126     Should Be True    '${rc}' == '0'
1127     [Return]    ${output}
1128
1129 OpenStack Cleanup All
1130     [Documentation]    Cleanup all Openstack resources with best effort. The keyword will query for all resources
1131     ...    in use and then attempt to delete them. Errors are ignored to allow the cleanup to continue.
1132     @{fips} =    OpenStack CLI Get List    openstack floating ip list -f json
1133     : FOR    ${fip}    IN    @{fips}
1134     \    BuiltIn.Run Keyword And Ignore Error    Delete Floating IP    ${fip['ID']}
1135     @{vms} =    OpenStack CLI Get List    openstack server list -f json
1136     : FOR    ${vm}    IN    @{vms}
1137     \    BuiltIn.Run Keyword And Ignore Error    Delete Vm Instance    ${vm['ID']}
1138     @{routers} =    OpenStack CLI Get List    openstack router list -f json
1139     : FOR    ${router}    IN    @{routers}
1140     \    BuiltIn.Run Keyword And Ignore Error    Cleanup Router    ${router['ID']}
1141     @{ports} =    OpenStack CLI Get List    openstack port list -f json
1142     : FOR    ${port}    IN    @{ports}
1143     \    BuiltIn.Run Keyword And Ignore Error    Delete Port    ${port['ID']}
1144     @{networks} =    OpenStack CLI Get List    openstack network list -f json
1145     : FOR    ${network}    IN    @{networks}
1146     \    BuiltIn.Run Keyword And Ignore Error    Delete Subnet    ${network['Subnets']}
1147     \    BuiltIn.Run Keyword And Ignore Error    Delete Network    ${network['ID']}
1148     @{security_groups} =    OpenStack CLI Get List    openstack security group list -f json
1149     : FOR    ${security_group}    IN    @{security_groups}
1150     \    BuiltIn.Run Keyword If    "${security_group['Name']}" != "default"    BuiltIn.Run Keyword And Ignore Error    Delete SecurityGroup    ${security_group['ID']}
1151     OpenStack List All
1152
1153 Cleanup Router
1154     [Arguments]    ${id}
1155     [Documentation]    Delete a router, but first remove any interfaces or gateways so that the delete will be successful.
1156     @{ports} =    OpenStack CLI Get List    openstack port list --router ${id} -f json --long
1157     : FOR    ${port}    IN    @{ports}
1158     \    ${subnet_id} =    Get Match    ${port['Fixed IP Addresses']}    [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}    0
1159     \    BuiltIn.Run Keyword If    "${port['Device Owner']}" == "network:router_gateway"    BuiltIn.Run Keyword And Ignore Error    Remove Gateway    ${id}
1160     \    BuiltIn.Run Keyword If    "${port['Device Owner']}" == "network:router_interface"    BuiltIn.Run Keyword And Ignore Error    Remove Interface    ${id}    ${subnet_id}
1161     BuiltIn.Run Keyword And Ignore Error    Delete Router    ${id}
1162
1163 OpenStack Suite Setup
1164     [Documentation]    Wrapper teardown keyword that can be used in any suite running in an openstack environement
1165     SetupUtils.Setup_Utils_For_Setup_And_Teardown
1166     Run Keyword If    "${PRE_CLEAN_OPENSTACK_ALL}"=="True"    OpenStack Cleanup All
1167     DevstackUtils.Devstack Suite Setup
1168     Add OVS Logging On All OpenStack Nodes
1169
1170 OpenStack Suite Teardown
1171     [Documentation]    Wrapper teardown keyword that can be used in any suite running in an openstack environement
1172     ...    to clean up all openstack resources. For example, all instances, networks, ports, etc will be listed and
1173     ...    and deleted. As other global cleanup tasks are needed, they can be added here and the suites will all
1174     ...    benefit automatically.
1175     OpenStack Cleanup All
1176     SSHLibrary.Close All Connections
1177
1178 Copy DHCP Files From Control Node
1179     [Documentation]    Copy the current DHCP files to the robot vm. The keyword must be called
1180     ...    after the subnet(s) are created and before the subnet(s) are deleted.
1181     ${suite_} =    BuiltIn.Evaluate    """${SUITE_NAME}""".replace(" ","_").replace("/","_").replace(".","_")
1182     ${dstdir} =    Set Variable    /tmp/qdhcp/${suite_}
1183     OperatingSystem.Create Directory    ${dstdir}
1184     Get ControlNode Connection
1185     BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Get Directory    /opt/stack/data/neutron/dhcp    ${dstdir}    recursive=True
1186
1187 Is Feature Installed
1188     [Arguments]    ${features}=none
1189     : FOR    ${feature}    IN    ${features}
1190     \    ${status}    ${output}    Run Keyword And Ignore Error    Builtin.Should Contain    ${CONTROLLERFEATURES}    ${feature}
1191     \    Return From Keyword If    "${status}" == "PASS"    True
1192     [Return]    False
1193
1194 Add OVS Logging On All OpenStack Nodes
1195     [Documentation]    Add higher levels of OVS logging to all the OpenStack nodes
1196     Run Keyword If    0 < ${NUM_OS_SYSTEM}    OVSDB.Add OVS Logging    ${OS_CNTL_CONN_ID}
1197     Run Keyword If    1 < ${NUM_OS_SYSTEM}    OVSDB.Add OVS Logging    ${OS_CMP1_CONN_ID}
1198     Run Keyword If    2 < ${NUM_OS_SYSTEM}    OVSDB.Add OVS Logging    ${OS_CMP2_CONN_ID}
1199
1200 Reset OVS Logging On All OpenStack Nodes
1201     [Documentation]    Reset the OVS logging to all the OpenStack nodes
1202     Run Keyword If    0 < ${NUM_OS_SYSTEM}    OVSDB.Reset OVS Logging    ${OS_CNTL_CONN_ID}
1203     Run Keyword If    1 < ${NUM_OS_SYSTEM}    OVSDB.Reset OVS Logging    ${OS_CMP1_CONN_ID}
1204     Run Keyword If    2 < ${NUM_OS_SYSTEM}    OVSDB.Reset OVS Logging    ${OS_CMP2_CONN_ID}