Reduce logging in collect ips
[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     ${rc}    ${vm_console_output} =    Run And Return Rc And Output    openstack console log show ${vm}
346     # TODO Add hooks to only tail the console log on subsequent runs, e.g. look for "info: initramfs:".
347     # This would drop repeatedly logging the kernel messages which are long.
348     # Also add flag to log or not
349     BuiltIn.Log    ${vm_console_output}
350     ${match} =    Get Match    ${vm_console_output}    ${REGEX_OBTAINED}
351     ${OSO_VM_IP} =    Get Match    ${match}    ${REGEX_IPV4}
352     ${match} =    Get Match    ${vm_console_output}    ${REGEX_NAMESERVER}
353     ${OSO_DHCP_IP} =    Get Match    ${match}    ${REGEX_IPV4}
354     BuiltIn.Set Test Variable    ${OSO_VM_IP}
355     BuiltIn.Set Test Variable    ${OSO_DHCP_IP}
356     BuiltIn.Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${OSO_VM_IP}    None
357     BuiltIn.Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${OSO_DHCP_IP}    None
358
359 Get VM IPs
360     [Arguments]    @{vms}
361     [Documentation]    Get the instance IP addresses and nameserver address for the list of given vms.
362     ...    First poll for the vm instance to be in the active state, then poll for the vm ip address and nameserver.
363     ...    ${OSO_VM_IP} and ${OSO_DHCP_IP} are test variables shared with Get VM IP.
364     @{OSO_VM_IPS}    BuiltIn.Create List    @{EMPTY}
365     : FOR    ${vm}    IN    @{vms}
366     \    BuiltIn.Set Test Variable    ${OSO_VM_IP}    None
367     \    BuiltIn.Set Test Variable    ${OSO_DHCP_IP}    None
368     \    Poll VM Is ACTIVE    ${vm}
369     \    ${status}    ${message}    Run Keyword And Ignore Error    Wait Until Keyword Succeeds    60s    15s
370     \    Get VM IP    true    ${vm}
371     \    BuiltIn.Run Keyword If    '${OSO_VM_IP}' != 'None'    Collections.Append To List    ${OSO_VM_IPS}    ${OSO_VM_IP}
372     [Return]    @{OSO_VM_IPS}    ${OSO_DHCP_IP}
373
374 Collect VM IPv6 SLAAC Addresses
375     [Arguments]    ${fail_on_none}    ${prefix}    @{vm_list}
376     [Documentation]    Using the console-log on the provided ${vm_list} to search for the string "inet6" which
377     ...    correlates to the instance generated IPv6 address, based on the ${prefix} received from ODL (SLAAC mode).
378     ${ip_list}    Create List    @{EMPTY}
379     : FOR    ${vm}    IN    @{vm_list}
380     \    Log    ${vm}
381     \    ${rc}    ${vm_ip_line}=    Run And Return Rc And Output    openstack console log show ${vm} | grep -i "inet6"
382     \    Log    ${vm_ip_line}
383     \    Log    ${rc}
384     \    @{vm_ip_list}    Get Regexp Matches    ${vm_ip_line}    ${prefix}
385     \    ${vm_ip_length}    Get Length    ${vm_ip_list}
386     \    Run Keyword If    ${vm_ip_length}>0    Append To List    ${ip_list}    @{vm_ip_list}[0]
387     \    ...    ELSE    Append To List    ${ip_list}    None
388     \    Log    ${ip_list}
389     Run Keyword If    '${fail_on_none}' == 'true'    Should Not Contain    ${ip_list}    None
390     Log    ${ip_list}
391     [Return]    ${ip_list}
392
393 View Vm Console
394     [Arguments]    ${vm_instance_names}
395     [Documentation]    View Console log of the created vm instances using nova show.
396     : FOR    ${VmElement}    IN    @{vm_instance_names}
397     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack server show ${VmElement}
398     \    Log    ${output}
399     \    Should Not Be True    ${rc}
400     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack console log show ${VmElement}
401     \    Log    ${output}
402     \    Should Not Be True    ${rc}
403
404 Ping Vm From DHCP Namespace
405     [Arguments]    ${net_name}    ${vm_ip}
406     [Documentation]    Reach all Vm Instance with the net id of the Netowrk.
407     Log    ${vm_ip}
408     ${devstack_conn_id}=    Get ControlNode Connection
409     Switch Connection    ${devstack_conn_id}
410     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
411     Log    ${net_id}
412     ${output}=    Write Commands Until Prompt And Log    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
413     Close Connection
414     Should Contain    ${output}    64 bytes
415
416 Ping From DHCP Should Not Succeed
417     [Arguments]    ${net_name}    ${vm_ip}
418     [Documentation]    Should Not Reach Vm Instance with the net id of the Netowrk.
419     Return From Keyword If    "skip_if_${SECURITY_GROUP_MODE}" in @{TEST_TAGS}
420     Log    ${vm_ip}
421     ${devstack_conn_id}=    Get ControlNode Connection
422     Switch Connection    ${devstack_conn_id}
423     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
424     Log    ${net_id}
425     ${output}=    Write Commands Until Prompt    sudo ip netns exec qdhcp-${net_id} ping -c 3 ${vm_ip}    20s
426     Close Connection
427     Log    ${output}
428     Should Not Contain    ${output}    64 bytes
429
430 Ping Vm From Control Node
431     [Arguments]    ${vm_floating_ip}    ${additional_args}=${EMPTY}
432     [Documentation]    Ping VM floating IP from control node
433     Log    ${vm_floating_ip}
434     ${devstack_conn_id}=    Get ControlNode Connection
435     Switch Connection    ${devstack_conn_id}
436     ${output}=    Write Commands Until Prompt And Log    ping ${additional_args} -c 3 ${vm_floating_ip}    20s
437     Close Connection
438     Should Contain    ${output}    64 bytes
439
440 Ping From Instance
441     [Arguments]    ${dest_vm_ip}
442     [Documentation]    Ping to the expected destination ip.
443     ${output}=    Write Commands Until Expected Prompt    ping -c 3 ${dest_vm_ip}    ${OS_SYSTEM_PROMPT}
444     Log    ${output}
445     [Return]    ${output}
446
447 Curl Metadata Server
448     [Documentation]    Ping to the expected destination ip.
449     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
450     Write Commands Until Prompt    exit
451     Should Contain    ${output}    200
452
453 Close Vm Instance
454     [Documentation]    Exit the vm instance.
455     ${output}=    Write Commands Until Prompt And Log    exit
456
457 Check If Console Is VmInstance
458     [Arguments]    ${console}=cirros
459     [Documentation]    Check if the session has been able to login to the VM instance
460     ${output}=    Write Commands Until Expected Prompt    id    ${OS_SYSTEM_PROMPT}
461     Should Contain    ${output}    ${console}
462
463 Exit From Vm Console
464     [Documentation]    Check if the session has been able to login to the VM instance and exit the instance
465     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance    cirros
466     Run Keyword If    ${rcode}    Write Commands Until Prompt    exit
467     Close Connection
468
469 Check Ping
470     [Arguments]    ${ip_address}    ${ttl}=64
471     [Documentation]    Run Ping command on the IP available as argument
472     ${ethertype}=    Get Regexp Matches    ${ip_address}    ${IP_REGEX}
473     ${output}=    Run Keyword If    ${ethertype}    Write Commands Until Expected Prompt    ping -t ${ttl} -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
474     ...    ELSE    Write Commands Until Expected Prompt    ping6 -t ${ttl} -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
475     Should Contain    ${output}    64 bytes
476
477 Check No Ping
478     [Arguments]    ${ip_address}    ${ttl}=64
479     [Documentation]    Run Ping command to the IP given as argument, executing 3 times and expecting NOT to see "64 bytes"
480     ${output}=    Write Commands Until Expected Prompt    ping -t ${ttl} -c 3 ${ip_address}    ${OS_SYSTEM_PROMPT}
481     Should Not Contain    ${output}    64 bytes
482
483 Check Metadata Access
484     [Documentation]    Try curl on the Metadataurl and check if it is okay
485     ${output}=    Write Commands Until Expected Prompt    curl -i http://169.254.169.254    ${OS_SYSTEM_PROMPT}
486     Should Contain    ${output}    200
487
488 Execute Command on VM Instance
489     [Arguments]    ${net_name}    ${vm_ip}    ${cmd}    ${user}=cirros    ${password}=cubswin:)
490     [Documentation]    Login to the vm instance using ssh in the network, executes a command inside the VM and returns the ouput.
491     ${devstack_conn_id} =    Get ControlNode Connection
492     Switch Connection    ${devstack_conn_id}
493     ${net_id} =    Get Net Id    ${net_name}    ${devstack_conn_id}
494     Log    ${vm_ip}
495     ${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:
496     Log    ${output}
497     ${output} =    Write Commands Until Expected Prompt    ${password}    ${OS_SYSTEM_PROMPT}
498     Log    ${output}
499     ${rcode} =    Run Keyword And Return Status    Check If Console Is VmInstance
500     ${output} =    Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ${cmd}    ${OS_SYSTEM_PROMPT}
501     [Teardown]    Exit From Vm Console
502     [Return]    ${output}
503
504 Test Operations From Vm Instance
505     [Arguments]    ${net_name}    ${src_ip}    ${dest_ips}    ${user}=cirros    ${password}=cubswin:)    ${ttl}=64
506     ...    ${ping_should_succeed}=True    ${check_metadata}=True
507     [Documentation]    Login to the vm instance using ssh in the network.
508     ${devstack_conn_id}=    Get ControlNode Connection
509     Switch Connection    ${devstack_conn_id}
510     Log    ${src_ip}
511     ${net_id}=    Get Net Id    ${net_name}    ${devstack_conn_id}
512     ${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:
513     Log    ${output}
514     ${output}=    Write Commands Until Expected Prompt    ${password}    ${OS_SYSTEM_PROMPT}
515     Log    ${output}
516     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
517     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ifconfig    ${OS_SYSTEM_PROMPT}
518     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    route -n    ${OS_SYSTEM_PROMPT}
519     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    route -A inet6    ${OS_SYSTEM_PROMPT}
520     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    arp -an    ${OS_SYSTEM_PROMPT}
521     Run Keyword If    ${rcode}    Write Commands Until Expected Prompt    ip -6 neigh    ${OS_SYSTEM_PROMPT}
522     : FOR    ${dest_ip}    IN    @{dest_ips}
523     \    Log    ${dest_ip}
524     \    ${string_empty}=    Run Keyword And Return Status    Should Be Empty    ${dest_ip}
525     \    Run Keyword If    ${string_empty}    Continue For Loop
526     \    Run Keyword If    ${rcode} and "${ping_should_succeed}" == "True"    Check Ping    ${dest_ip}    ttl=${ttl}
527     \    ...    ELSE    Check No Ping    ${dest_ip}    ttl=${ttl}
528     ${ethertype}=    Get Regexp Matches    ${src_ip}    ${IP_REGEX}
529     Run Keyword If    ${rcode} and "${check_metadata}" and ${ethertype} == "True"    Check Metadata Access
530     [Teardown]    Exit From Vm Console
531
532 Test Netcat Operations From Vm Instance
533     [Arguments]    ${net_name}    ${vm_ip}    ${dest_ip}    ${additional_args}=${EMPTY}    ${port}=12345    ${user}=cirros
534     ...    ${password}=cubswin:)
535     [Documentation]    Use Netcat to test TCP/UDP connections to the controller
536     ${client_data}    Set Variable    Test Client Data
537     ${server_data}    Set Variable    Test Server Data
538     ${devstack_conn_id}=    Get ControlNode Connection
539     Switch Connection    ${devstack_conn_id}
540     Log    ${vm_ip}
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     Log    ${nc_output}
545     ${output}=    Execute Command on VM Instance    ${net_name}    ${vm_ip}    sudo route -n
546     Log    ${output}
547     ${output}=    Execute Command on VM Instance    ${net_name}    ${vm_ip}    sudo arp -an
548     Log    ${output}
549     Should Match Regexp    ${nc_output}    ${server_data}
550
551 Ping Other Instances
552     [Arguments]    ${list_of_external_dst_ips}
553     [Documentation]    Check reachability with other network's instances.
554     ${rcode}=    Run Keyword And Return Status    Check If Console Is VmInstance
555     : FOR    ${dest_ip}    IN    @{list_of_external_dst_ips}
556     \    Log    ${dest_ip}
557     \    Check Ping    ${dest_ip}
558
559 Create Router
560     [Arguments]    ${router_name}
561     [Documentation]    Create Router and Add Interface to the subnets.
562     ${rc}    ${output}=    Run And Return Rc And Output    openstack router create ${router_name}
563     Should Not Be True    ${rc}
564
565 List Router
566     [Documentation]    List Router and return output with neutron client.
567     ${rc}    ${output}=    Run And Return Rc And Output    openstack router list -f value
568     Log    ${output}
569     Should Not Be True    ${rc}
570     [Return]    ${output}
571
572 Add Router Interface
573     [Arguments]    ${router_name}    ${interface_name}
574     ${rc}    ${output}=    Run And Return Rc And Output    openstack router add subnet ${router_name} ${interface_name}
575     Should Not Be True    ${rc}
576
577 Show Router Interface
578     [Arguments]    ${router_name}
579     [Documentation]    List Router interface associated with given Router and return output with neutron client.
580     ${rc}    ${output}=    Run And Return Rc And Output    openstack port list --router ${router_name} -f value
581     Should Not Be True    ${rc}
582     [Return]    ${output}
583
584 Add Router Gateway
585     [Arguments]    ${router_name}    ${external_network_name}
586     ${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}
587     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
588     Should Not Be True    ${rc}
589
590 Remove Interface
591     [Arguments]    ${router_name}    ${interface_name}
592     [Documentation]    Remove Interface to the subnets.
593     ${rc}    ${output}=    Run And Return Rc And Output    openstack router remove subnet ${router_name} ${interface_name}
594     Should Not Be True    ${rc}
595
596 Update Router
597     [Arguments]    ${router_name}    ${cmd}
598     [Documentation]    Update the router with the command. Router name and command should be passed as argument.
599     ${rc}    ${output} =    Run And Return Rc And Output    openstack router set ${router_name} ${cmd}
600     Should Not Be True    ${rc}
601
602 Show Router
603     [Arguments]    ${router_name}    ${options}
604     [Documentation]    Show information of a given router. Router name and optional fields should be sent as arguments.
605     ${rc}    ${output} =    Run And Return Rc And Output    openstack router show ${router_name}
606     Log    ${output}
607
608 Delete Router
609     [Arguments]    ${router_name}
610     [Documentation]    Delete Router and Interface to the subnets.
611     ${rc}    ${output}=    Run And Return Rc And Output    openstack router delete ${router_name}
612     Should Not Be True    ${rc}
613
614 Get DumpFlows And Ovsconfig
615     [Arguments]    ${openstack_node_ip}
616     [Documentation]    Get the OvsConfig and Flow entries from OVS from the Openstack Node
617     Log    ${openstack_node_ip}
618     SSHLibrary.Open Connection    ${openstack_node_ip}    prompt=${DEFAULT_LINUX_PROMPT}
619     SSHKeywords.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
620     SSHLibrary.Set Client Configuration    timeout=${default_devstack_prompt_timeout}
621     Write Commands Until Expected Prompt    ip -o link    ${DEFAULT_LINUX_PROMPT_STRICT}
622     Write Commands Until Expected Prompt    ip -o addr    ${DEFAULT_LINUX_PROMPT_STRICT}
623     Write Commands Until Expected Prompt    ip route    ${DEFAULT_LINUX_PROMPT_STRICT}
624     Write Commands Until Expected Prompt    arp -an    ${DEFAULT_LINUX_PROMPT_STRICT}
625     ${nslist}=    Write Commands Until Expected Prompt    ip netns list | awk '{print $1}'    ${DEFAULT_LINUX_PROMPT_STRICT}
626     @{lines}    Split To Lines    ${nslist}    end=-1
627     : FOR    ${line}    IN    @{lines}
628     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip -o link    ${DEFAULT_LINUX_PROMPT_STRICT}
629     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip -o addr    ${DEFAULT_LINUX_PROMPT_STRICT}
630     \    Write Commands Until Expected Prompt    sudo ip netns exec ${line} ip route    ${DEFAULT_LINUX_PROMPT_STRICT}
631     Write Commands Until Expected Prompt    sudo ovs-vsctl show    ${DEFAULT_LINUX_PROMPT_STRICT}
632     Write Commands Until Expected Prompt    sudo ovs-vsctl list Open_vSwitch    ${DEFAULT_LINUX_PROMPT_STRICT}
633     Write Commands Until Expected Prompt    sudo ovs-ofctl show br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
634     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-flows br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
635     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-groups br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
636     Write Commands Until Expected Prompt    sudo ovs-ofctl dump-group-stats br-int -OOpenFlow13    ${DEFAULT_LINUX_PROMPT_STRICT}
637
638 Get Karaf Log Type From Test Start
639     [Arguments]    ${ip}    ${test_name}    ${type}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
640     ...    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
641     ${cmd}    Set Variable    sed '1,/ROBOT MESSAGE: Starting test ${test_name}/d' ${log_file} | grep '${type}'
642     ${output}    Run Command On Controller    ${ip}    ${cmd}    ${user}    ${password}    ${prompt}
643     [Return]    ${output}
644
645 Get Karaf Log Types From Test Start
646     [Arguments]    ${ip}    ${test_name}    ${types}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
647     ...    ${log_file}=${WORKSPACE}/${BUNDLEFOLDER}/data/log/karaf.log
648     : FOR    ${type}    IN    @{types}
649     \    Get Karaf Log Type From Test Start    ${ip}    ${test_name}    ${type}    ${user}    ${password}
650     \    ...    ${prompt}    ${log_file}
651
652 Get Karaf Log Events From Test Start
653     [Arguments]    ${test_name}    ${user}=${ODL_SYSTEM_USER}    ${password}=${ODL_SYSTEM_PASSWORD}    ${prompt}=${ODL_SYSTEM_PROMPT}
654     ${log_types} =    Create List    ERROR    WARN    Exception
655     Run Keyword If    0 < ${NUM_ODL_SYSTEM}    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_IP}    ${test_name}    ${log_types}
656     Run Keyword If    1 < ${NUM_ODL_SYSTEM}    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_2_IP}    ${test_name}    ${log_types}
657     Run Keyword If    2 < ${NUM_ODL_SYSTEM}    Get Karaf Log Types From Test Start    ${ODL_SYSTEM_3_IP}    ${test_name}    ${log_types}
658
659 Get ControlNode Connection
660     ${control_conn_id}=    SSHLibrary.Open Connection    ${OS_CONTROL_NODE_IP}    prompt=${DEFAULT_LINUX_PROMPT_STRICT}
661     SSHKeywords.Flexible SSH Login    ${OS_USER}    ${DEVSTACK_SYSTEM_PASSWORD}
662     SSHLibrary.Set Client Configuration    timeout=30s
663     [Return]    ${control_conn_id}
664
665 Get OvsDebugInfo
666     [Documentation]    Get the OvsConfig and Flow entries from all Openstack nodes
667     Run Keyword If    0 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_CONTROL_NODE_IP}
668     Run Keyword If    1 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_COMPUTE_1_IP}
669     Run Keyword If    2 < ${NUM_OS_SYSTEM}    Get DumpFlows And Ovsconfig    ${OS_COMPUTE_2_IP}
670
671 Get Test Teardown Debugs
672     [Arguments]    ${test_name}=${TEST_NAME}
673     Get OvsDebugInfo
674     Run Keyword And Ignore Error    Get Model Dump    ${HA_PROXY_IP}    ${netvirt_data_models}
675     Get Karaf Log Events From Test Start    ${test_name}
676
677 Get Test Teardown Debugs For SFC
678     [Arguments]    ${test_name}=${TEST_NAME}
679     Run Keyword And Ignore Error    Get Model Dump    ${HA_PROXY_IP}    ${netvirt_sfc_data_models}
680
681 Show Debugs
682     [Arguments]    @{vm_indices}
683     [Documentation]    Run these commands for debugging, it can list state of VM instances and ip information in control node
684     ${devstack_conn_id}=    Get ControlNode Connection
685     Switch Connection    ${devstack_conn_id}
686     ${output}=    Write Commands Until Prompt And Log    sudo ip netns list
687     Close Connection
688     : FOR    ${index}    IN    @{vm_indices}
689     \    ${rc}    ${output}=    Run And Return Rc And Output    nova show ${index}
690     \    Log    ${output}
691     List Nova VMs
692     List Networks
693     List Subnets
694     List Ports
695
696 Neutron Security Group Show
697     [Arguments]    ${SecurityGroupRuleName}
698     [Documentation]    Displays the neutron security group configurations that belongs to a given neutron security group name
699     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group show ${SecurityGroupRuleName}
700     Log    ${output}
701     Should Not Be True    ${rc}
702     [Return]    ${output}
703
704 Neutron Port Show
705     [Arguments]    ${PortName}
706     [Documentation]    Display the port configuration that belong to a given neutron port
707     ${rc}    ${output}=    Run And Return Rc And Output    openstack port show ${PortName}
708     Log    ${output}
709     Should Not Be True    ${rc}
710     [Return]    ${output}
711
712 Neutron Security Group Create
713     [Arguments]    ${SecurityGroupName}    ${additional_args}=${EMPTY}
714     [Documentation]    Create a security group with specified name ,description & protocol value according to security group template
715     ${devstack_conn_id}=    Get ControlNode Connection
716     Switch Connection    ${devstack_conn_id}
717     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group create ${SecurityGroupName} ${additional_args}
718     Log    ${output}
719     Should Not Be True    ${rc}
720     ${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}
721     Log    ${sgp_id}
722     [Return]    ${output}    ${sgp_id}
723
724 Neutron Security Group Update
725     [Arguments]    ${SecurityGroupName}    ${additional_args}=${EMPTY}
726     [Documentation]    Updating security groups
727     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group set ${SecurityGroupName} ${additional_args}
728     Log    ${output}
729     Should Not Be True    ${rc}
730     [Return]    ${output}
731
732 Delete SecurityGroup
733     [Arguments]    ${sg_name}
734     [Documentation]    Delete Security group
735     ${rc}    ${output}=    Run And Return Rc And Output    openstack security group delete ${sg_name}
736     Log    ${output}
737     Should Not Be True    ${rc}
738
739 Neutron Security Group Rule Create
740     [Arguments]    ${Security_group_name}    &{Kwargs}
741     [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]} ..."
742     Run Keyword If    ${Kwargs}    Log    ${Kwargs}
743     ${description}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    description    default=${None}
744     ${direction}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    direction    default=${None}
745     ${ethertype}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    ethertype    default=${None}
746     ${port_range_max}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_max    default=${None}
747     ${port_range_min}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_min    default=${None}
748     ${protocol}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    protocol    default=${None}
749     ${remote_group_id}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_group_id    default=${None}
750     ${remote_ip_prefix}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_ip_prefix    default=${None}
751     ${cmd}=    Set Variable    openstack security group rule create ${Security_group_name}
752     ${cmd}=    Run Keyword If    '${description}'!='None'    Catenate    ${cmd}    --description ${description}
753     ...    ELSE    Catenate    ${cmd}
754     ${cmd}=    Run Keyword If    '${direction}'!='None'    Catenate    ${cmd}    --${direction}
755     ...    ELSE    Catenate    ${cmd}
756     ${cmd}=    Run Keyword If    '${ethertype}'!='None'    Catenate    ${cmd}    --ethertype ${ethertype}
757     ...    ELSE    Catenate    ${cmd}
758     ${cmd}=    Run Keyword If    '${port_range_min}'!='None' and '${port_range_max}'!='None'    Catenate    ${cmd}    --dst-port ${port_range_min}:${port_range_max}
759     ...    ELSE IF    '${port_range_max}'!='None'    Catenate    ${cmd}    --dst-port ${port_range_max}
760     ...    ELSE IF    '${port_range_min}'!='None'    Catenate    ${cmd}    --dst-port ${port_range_min}
761     ...    ELSE    Catenate    ${cmd}
762     ${cmd}=    Run Keyword If    '${protocol}'!='None'    Catenate    ${cmd}    --protocol ${protocol}
763     ...    ELSE    Catenate    ${cmd}
764     ${cmd}=    Run Keyword If    '${remote_group_id}'!='None'    Catenate    ${cmd}    --remote-group ${remote_group_id}
765     ...    ELSE    Catenate    ${cmd}
766     ${cmd}=    Run Keyword If    '${remote_ip_prefix}'!='None'    Catenate    ${cmd}    --src-ip ${remote_ip_prefix}
767     ...    ELSE    Catenate    ${cmd}
768     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
769     ${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}
770     Log    ${rule_id}
771     Should Not Be True    ${rc}
772     [Return]    ${output}    ${rule_id}
773
774 Neutron Security Group Rule Create Legacy Cli
775     [Arguments]    ${Security_group_name}    &{Kwargs}
776     [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]} ..."
777     Run Keyword If    ${Kwargs}    Log    ${Kwargs}
778     ${description}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    description    default=${None}
779     ${direction}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    direction    default=${None}
780     ${ethertype}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    ethertype    default=${None}
781     ${port_range_max}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_max    default=${None}
782     ${port_range_min}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    port_range_min    default=${None}
783     ${protocol}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    protocol    default=${None}
784     ${remote_group_id}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_group_id    default=${None}
785     ${remote_ip_prefix}    Run Keyword If    ${Kwargs}    Pop From Dictionary    ${Kwargs}    remote_ip_prefix    default=${None}
786     ${cmd}=    Set Variable    neutron security-group-rule-create ${Security_group_name}
787     ${cmd}=    Run Keyword If    '${description}'!='None'    Catenate    ${cmd}    --description ${description}
788     ...    ELSE    Catenate    ${cmd}
789     ${cmd}=    Run Keyword If    '${direction}'!='None'    Catenate    ${cmd}    --direction ${direction}
790     ...    ELSE    Catenate    ${cmd}
791     ${cmd}=    Run Keyword If    '${ethertype}'!='None'    Catenate    ${cmd}    --ethertype ${ethertype}
792     ...    ELSE    Catenate    ${cmd}
793     ${cmd}=    Run Keyword If    '${port_range_max}'!='None'    Catenate    ${cmd}    --port_range_max ${port_range_max}
794     ...    ELSE    Catenate    ${cmd}
795     ${cmd}=    Run Keyword If    '${port_range_min}'!='None'    Catenate    ${cmd}    --port_range_min ${port_range_min}
796     ...    ELSE    Catenate    ${cmd}
797     ${cmd}=    Run Keyword If    '${protocol}'!='None'    Catenate    ${cmd}    --protocol ${protocol}
798     ...    ELSE    Catenate    ${cmd}
799     ${cmd}=    Run Keyword If    '${remote_group_id}'!='None'    Catenate    ${cmd}    --remote_group_id ${remote_group_id}
800     ...    ELSE    Catenate    ${cmd}
801     ${cmd}=    Run Keyword If    '${remote_ip_prefix}'!='None'    Catenate    ${cmd}    --remote_ip_prefix ${remote_ip_prefix}
802     ...    ELSE    Catenate    ${cmd}
803     ${rc}    ${output}=    Run And Return Rc And Output    ${cmd}
804     ${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}
805     Log    ${rule_id}
806     Should Not Be True    ${rc}
807     [Return]    ${output}    ${rule_id}
808
809 Security Group Create Without Default Security Rules
810     [Arguments]    ${sg_name}    ${additional_args}=${EMPTY}
811     [Documentation]    Create Neutron Security Group with no default rules, using specified name and optional arguments.
812     Neutron Security Group Create    ${sg_name}    ${additional_args}
813     Delete All Security Group Rules    ${sg_name}
814
815 Delete All Security Group Rules
816     [Arguments]    ${sg_name}
817     [Documentation]    Delete all security rules from a specified security group
818     ${rc}    ${sg_rules_output}=    Run And Return Rc And Output    openstack security group rule list ${sg_name} -cID -fvalue
819     Log    ${sg_rules_output}
820     Should Not Be True    ${rc}
821     @{sg_rules}=    Split String    ${sg_rules_output}    \n
822     : FOR    ${rule}    IN    @{sg_rules}
823     \    ${rc}    ${output}=    Run And Return Rc And Output    openstack security group rule delete ${rule}
824     \    Log    ${output}
825     \    Should Not Be True    ${rc}
826
827 Create Allow All SecurityGroup
828     [Arguments]    ${sg_name}    ${ether_type}=IPv4
829     [Documentation]    Allow all TCP/UDP/ICMP packets for this suite
830     Neutron Security Group Create    ${sg_name}
831     Neutron Security Group Rule Create    ${sg_name}    direction=ingress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=tcp
832     Neutron Security Group Rule Create    ${sg_name}    direction=egress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=tcp
833     Neutron Security Group Rule Create    ${sg_name}    direction=ingress    ethertype=${ether_type}    protocol=icmp
834     Neutron Security Group Rule Create    ${sg_name}    direction=egress    ethertype=${ether_type}    protocol=icmp
835     Neutron Security Group Rule Create    ${sg_name}    direction=ingress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=udp
836     Neutron Security Group Rule Create    ${sg_name}    direction=egress    ethertype=${ether_type}    port_range_max=65535    port_range_min=1    protocol=udp
837
838 Create Neutron Port With Additional Params
839     [Arguments]    ${network_name}    ${port_name}    ${additional_args}=${EMPTY}
840     [Documentation]    Create Port With given additional parameters
841     ${rc}    ${output}=    Run And Return Rc And Output    neutron -v port-create ${network_name} --name ${port_name} ${additional_args}
842     Log    ${output}
843     Should Not Be True    ${rc}
844     ${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}
845     Log    ${port_id}
846     [Return]    ${OUTPUT}    ${port_id}
847
848 Get Ports MacAddr
849     [Arguments]    ${portName_list}
850     [Documentation]    Retrieve the port MacAddr for the given list of port name and return the MAC address list.
851     ${MacAddr-list}    Create List
852     : FOR    ${portName}    IN    @{portName_list}
853     \    ${macAddr}=    OpenStackOperations.Get Port Mac    ${portName}    ${devstack_conn_id}
854     \    Append To List    ${MacAddr-list}    ${macAddr}
855     [Return]    ${MacAddr-list}
856
857 Get Port Ip
858     [Arguments]    ${port_name}
859     [Documentation]    Keyword would return the IP of the ${port_name} received.
860     ${rc}    ${output}=    Run And Return Rc And Output    openstack port list | grep "${port_name}" | awk -F\\' '{print $2}'
861     ${splitted_output}=    Split String    ${output}    ${EMPTY}
862     ${port_ip}=    Get from List    ${splitted_output}    0
863     Should Not Be True    ${rc}
864     [Return]    ${port_ip}
865
866 Get Port Mac
867     [Arguments]    ${port_name}    ${conn_id}=${devstack_conn_id}
868     [Documentation]    Keyword would return the MAC ID of the ${port_name} received.
869     ${rc}    ${output}=    Run And Return Rc And Output    openstack port show ${port_name} | grep mac_address | awk '{print $4}'
870     ${splitted_output}=    Split String    ${output}    ${EMPTY}
871     ${port_mac}=    Get from List    ${splitted_output}    0
872     Should Not Be True    ${rc}
873     [Return]    ${port_mac}
874
875 Create L2Gateway
876     [Arguments]    ${bridge_name}    ${intf_name}    ${gw_name}
877     [Documentation]    Keyword to create an L2 Gateway ${gw_name} for bridge ${bridge_name} connected to interface ${intf_name} (Using Neutron CLI).
878     ${rc}    ${l2gw_output}=    Run And Return Rc And Output    ${L2GW_CREATE} name=${bridge_name},interface_names=${intf_name} ${gw_name}
879     Log    ${l2gw_output}
880     [Return]    ${l2gw_output}
881
882 Create L2Gateway Connection
883     [Arguments]    ${gw_name}    ${net_name}
884     [Documentation]    Keyword would create a new L2 Gateway Connection for ${gw_name} to ${net_name} (Using Neutron CLI).
885     ${rc}    ${l2gw_output}=    Run And Return Rc And Output    ${L2GW_CONN_CREATE} ${gw_name} ${net_name}
886     Log    ${l2gw_output}
887     Should Not Be True    ${rc}
888     [Return]    ${l2gw_output}
889
890 Get All L2Gateway
891     [Documentation]    Keyword to return all the L2 Gateways available (Using Neutron CLI).
892     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET_YAML}
893     Should Not Be True    ${rc}
894     [Return]    ${output}
895
896 Get All L2Gateway Connection
897     [Documentation]    Keyword to return all the L2 Gateway connections available (Using Neutron CLI).
898     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET_CONN_YAML}
899     Should Not Be True    ${rc}
900     [Return]    ${output}
901
902 Get L2Gateway
903     [Arguments]    ${gw_id}
904     [Documentation]    Keyword to check if the ${gw_id} is available in the L2 Gateway list (Using Neutron CLI).
905     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_SHOW} ${gw_id}
906     Log    ${output}
907     Should Not Be True    ${rc}
908     [Return]    ${output}
909
910 Get L2gw Id
911     [Arguments]    ${l2gw_name}
912     [Documentation]    Keyword to retrieve the L2 Gateway ID for the ${l2gw_name} (Using Neutron CLI).
913     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET} | grep "${l2gw_name}" | awk '{print $2}'
914     Log    ${output}
915     Should Not Be True    ${rc}
916     ${splitted_output}=    Split String    ${output}    ${EMPTY}
917     ${l2gw_id}=    Get from List    ${splitted_output}    0
918     [Return]    ${l2gw_id}
919
920 Get L2gw Connection Id
921     [Arguments]    ${l2gw_name}
922     [Documentation]    Keyword to retrieve the L2 Gateway Connection ID for the ${l2gw_name} (Using Neutron CLI).
923     ${l2gw_id}=    OpenStackOperations.Get L2gw Id    ${l2gw_name}
924     ${rc}    ${output}=    Run And Return Rc And Output    ${L2GW_GET_CONN} | grep "${l2gw_id}" | awk '{print $2}'
925     Should Not Be True    ${rc}
926     ${splitted_output}=    Split String    ${output}    ${EMPTY}
927     ${splitted_output}=    Split String    ${output}    ${EMPTY}
928     ${l2gw_conn_id}=    Get from List    ${splitted_output}    0
929     [Return]    ${l2gw_conn_id}
930
931 Neutron Port List Rest
932     [Documentation]    Keyword to get all ports details in Neutron (Using REST).
933     ${resp} =    RequestsLibrary.Get Request    session    ${PORT_URL}
934     Log    ${resp.content}
935     Should Be Equal As Strings    ${resp.status_code}    200
936     [Return]    ${resp.content}
937
938 Get Neutron Port Rest
939     [Arguments]    ${port_id}
940     [Documentation]    Keyword to get the specific port details in Neutron (Using REST).
941     ${resp} =    RequestsLibrary.Get Request    session    ${CONFIG_API}/${GET_PORT_URL}/${port_id}
942     Log    ${resp.content}
943     Should Be Equal As Strings    ${resp.status_code}    200
944     [Return]    ${resp.content}
945
946 Update Port Rest
947     [Arguments]    ${port_id}    ${json_data}
948     [Documentation]    Keyword to update ${port_id} with json data received in ${json_data} (Using REST).
949     Log    ${json_data}
950     ${resp} =    RequestsLibrary.Put Request    session    ${CONFIG_API}/${GET_PORT_URL}/${port_id}    ${json_data}
951     Log    ${resp.content}
952     Should Be Equal As Strings    ${resp.status_code}    200
953     [Return]    ${resp.content}
954
955 Create And Configure Security Group
956     [Arguments]    ${sg-name}
957     [Documentation]    Create Security Group with given name, and default allow rules for TCP/UDP/ICMP protocols.
958     Neutron Security Group Create    ${sg-name}
959     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
960     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
961     Neutron Security Group Rule Create    ${sg-name}    direction=ingress    protocol=icmp    remote_ip_prefix=0.0.0.0/0
962     Neutron Security Group Rule Create    ${sg-name}    direction=egress    protocol=icmp    remote_ip_prefix=0.0.0.0/0
963     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
964     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
965
966 Add Security Group To VM
967     [Arguments]    ${vm}    ${sg}
968     [Documentation]    Add the security group provided to the given VM.
969     ${rc}    ${output}=    Run And Return Rc And Output    openstack server add security group ${vm} ${sg}
970     Log    ${output}
971     Should Not Be True    ${rc}
972
973 Remove Security Group From VM
974     [Arguments]    ${vm}    ${sg}
975     [Documentation]    Remove the security group provided to the given VM.
976     ${devstack_conn_id}=    Get ControlNode Connection
977     Switch Connection    ${devstack_conn_id}
978     ${output}=    Write Commands Until Prompt And Log    openstack server remove security group ${vm} ${sg}
979     Close Connection
980
981 Create SFC Flow Classifier
982     [Arguments]    ${name}    ${src_ip}    ${dest_ip}    ${protocol}    ${dest_port}    ${neutron_src_port}
983     [Documentation]    Create a flow classifier for SFC
984     ${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}
985     Log    ${output}
986     Should Not Be True    ${rc}
987     Should Contain    ${output}    ${name}
988     [Return]    ${output}
989
990 Delete SFC Flow Classifier
991     [Arguments]    ${name}
992     [Documentation]    Delete a SFC flow classifier
993     ${devstack_conn_id}=    Get ControlNode Connection
994     Switch Connection    ${devstack_conn_id}
995     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc flow classifier delete ${name}
996     Log    ${output}
997     Should Not Be True    ${rc}
998     [Return]    ${output}
999
1000 Create SFC Port Pair
1001     [Arguments]    ${name}    ${port_in}    ${port_out}
1002     [Documentation]    Creates a neutron port pair for SFC
1003     ${devstack_conn_id}=    Get ControlNode Connection
1004     Switch Connection    ${devstack_conn_id}
1005     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair create --ingress=${port_in} --egress=${port_out} ${name}
1006     Log    ${output}
1007     Should Not Be True    ${rc}
1008     Should Contain    ${output}    ${name}
1009     [Return]    ${output}
1010
1011 Delete SFC Port Pair
1012     [Arguments]    ${name}
1013     [Documentation]    Delete a SFC port pair
1014     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair delete ${name}
1015     Log    ${output}
1016     Should Not Be True    ${rc}
1017     [Return]    ${output}
1018
1019 Create SFC Port Pair Group
1020     [Arguments]    ${name}    ${port_pair}
1021     [Documentation]    Creates a port pair group with a single port pair for SFC
1022     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair group create --port-pair ${port_pair} ${name}
1023     Log    ${output}
1024     Should Not Be True    ${rc}
1025     Should Contain    ${output}    ${name}
1026     [Return]    ${output}
1027
1028 Create SFC Port Pair Group With Two Pairs
1029     [Arguments]    ${name}    ${port_pair1}    ${port_pair2}
1030     [Documentation]    Creates a port pair group with two port pairs for SFC
1031     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair group create --port-pair ${port_pair1} --port-pair ${port_pair2} ${name}
1032     Log    ${output}
1033     Should Not Be True    ${rc}
1034     Should Contain    ${output}    ${name}
1035     [Return]    ${output}
1036
1037 Delete SFC Port Pair Group
1038     [Arguments]    ${name}
1039     [Documentation]    Delete a SFC port pair group
1040     ${devstack_conn_id}=    Get ControlNode Connection
1041     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port pair group delete ${name}
1042     Log    ${output}
1043     Should Not Be True    ${rc}
1044     [Return]    ${output}
1045
1046 Create SFC Port Chain
1047     [Arguments]    ${name}    ${pg1}    ${pg2}    ${fc}
1048     [Documentation]    Creates a port pair chain with two port groups and a singel classifier.
1049     ${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}
1050     Log    ${output}
1051     Should Not Be True    ${rc}
1052     Should Contain    ${output}    ${name}
1053     [Return]    ${output}
1054
1055 Delete SFC Port Chain
1056     [Arguments]    ${name}
1057     [Documentation]    Delete a SFC port chain
1058     ${rc}    ${output}=    Run And Return Rc And Output    openstack sfc port chain delete ${name}
1059     Log    ${output}
1060     Should Not Be True    ${rc}
1061     [Return]    ${output}
1062
1063 Reboot Nova VM
1064     [Arguments]    ${vm_name}
1065     [Documentation]    Reboot NOVA VM
1066     ${rc}    ${output}=    Run And Return Rc And Output    openstack server reboot --wait ${vm_name}
1067     Log    ${output}
1068     Should Not Be True    ${rc}
1069     Wait Until Keyword Succeeds    35s    10s    Verify VM Is ACTIVE    ${vm_name}
1070
1071 Remove RSA Key From KnowHosts
1072     [Arguments]    ${vm_ip}
1073     [Documentation]    Remove RSA
1074     ${devstack_conn_id}=    Get ControlNode Connection
1075     Switch Connection    ${devstack_conn_id}
1076     ${output}=    Write Commands Until Prompt And Log    sudo cat /root/.ssh/known_hosts    30s
1077     ${output}=    Write Commands Until Prompt And Log    sudo ssh-keygen -f "/root/.ssh/known_hosts" -R ${vm_ip}    30s
1078     ${output}=    Write Commands Until Prompt    sudo cat "/root/.ssh/known_hosts"    30s
1079
1080 Wait For Routes To Propogate
1081     [Arguments]    ${networks}    ${subnets}
1082     [Documentation]    Check propagated routes
1083     ${devstack_conn_id} =    Get ControlNode Connection
1084     Switch Connection    ${devstack_conn_id}
1085     : FOR    ${INDEX}    IN RANGE    0    1
1086     \    ${net_id}=    Get Net Id    @{networks}[${INDEX}]    ${devstack_conn_id}
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}]