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