correct check_for_null for l2gw delete
[integration/test.git] / csit / libraries / L2GatewayOperations.robot
1 *** Settings ***
2 Documentation     L2Gateway Operations Library. This library has useful keywords for various actions on Hwvtep and Ovs connectivity. Most of the keywords expects that ovs_conn_id,ovs2_conn_id, hwvtep_conn_id, hwvtep2_conn_id and OS_CNTL_CONN_ID are available.
3 Library           SSHLibrary
4 Library           Collections
5 Library           RequestsLibrary
6 Resource          OVSDB.robot
7 Resource          Utils.robot
8 Resource          MininetKeywords.robot
9 Resource          VpnOperations.robot
10 Resource          OpenStackOperations.robot
11 Resource          DevstackUtils.robot
12 Resource          ../variables/l2gw/Variables.robot
13 Resource          ../variables/netvirt/Variables.robot
14
15 *** Variables ***
16 ${L2GW_VAR_BASE}    ${CURDIR}/../variables/l2gw
17
18 *** Keywords ***
19 Add Ovs Bridge Manager Controller And Verify
20     [Arguments]    ${conn_id}=${OS_CMP1_CONN_ID}    ${hwvtep_bridge}=${HWVTEP_BRIDGE}
21     [Documentation]    Keyword to set OVS manager and controller to ${ODL_IP} for the OVS IP connected in ${conn_id} and verify the entries in OVSDB NETWORK TOPOLOGY and NETSTAT results.
22     ${output}=    Exec Command    ${conn_id}    ${OVS_RESTART}
23     ${output}=    Exec Command    ${conn_id}    ${OVS_DEL_MGR}
24     ${output}=    Exec Command    ${conn_id}    ${OVS_DEL_CTRLR} ${INTEGRATION_BRIDGE}
25     ${output}=    Exec Command    ${conn_id}    ${DEL_OVS_BRIDGE} ${INTEGRATION_BRIDGE}
26     ${output}=    Exec Command    ${conn_id}    ${OVS_SHOW}
27     Should Not Contain    ${output}    Manager
28     Should Not Contain    ${output}    Controller
29     ${output}=    Exec Command    ${conn_id}    ${CREATE_OVS_BRIDGE} ${INTEGRATION_BRIDGE}
30     ${output}=    Exec Command    ${conn_id}    ${SET_FAIL_MODE} ${INTEGRATION_BRIDGE} secure
31     ${output}=    Exec Command    ${conn_id}    ${OVS_SET_MGR}:${ODL_IP}:${OVSDBPORT}
32     ${output}=    Exec Command    ${conn_id}    ${OVS_SET_CTRLR} ${INTEGRATION_BRIDGE} tcp:${ODL_IP}:${ODL_OF_PORT}
33     Wait Until Keyword Succeeds    60s    2s    Verify Strings In Command Output    ${conn_id}    ${OVS_SHOW}    Manager "tcp:${ODL_IP}:${OVSDBPORT}"
34     ...    Controller "tcp:${ODL_IP}:${ODL_OF_PORT}"
35     ${output}=    Exec Command    ${conn_id}    ${NETSTAT}
36     Wait Until Keyword Succeeds    30s    2s    Validate Regexp In String    ${output}    ${NETSTAT_OVSDB_REGEX}
37     Wait Until Keyword Succeeds    30s    2s    Validate Regexp In String    ${output}    ${NETSTAT_OF_REGEX}
38     @{list_to_check}=    Create List    bridge/${INTEGRATION_BRIDGE}    bridge/${hwvtep_bridge}
39     Wait Until Keyword Succeeds    30s    2s    Check For Elements At URI    ${OVSDB_NETWORK_TOPOLOGY}    ${list_to_check}    session
40
41 Create Itm Tunnel Between Hwvtep and Ovs
42     [Arguments]    ${ovs_id}    ${ovs_ip}
43     [Documentation]    Keyword to create ITM Tunnel Between HWVTEP and OVS connection in ${ovs_id}.
44     ${dpn_id}=    Get Dpnid Decimal    ${ovs_id}
45     ${first_two_octets}    ${third_octet}    ${last_octet}=    Split String From Right    ${ovs_ip}    .    2
46     ${prefix} =    Set Variable    ${first_two_octets}.0.0/24
47     TemplatedRequests.Post_As_Json_Templated    folder=${L2GW_VAR_BASE}/itm_create    mapping={"dpnid":"${dpn_id}","ip":"${ovs_ip}","prefix":"${prefix}"}    session=session
48     ${output}=    ITM Get Tunnels
49     Log    ${output}
50
51 Add Vtep Manager And Verify
52     [Arguments]    ${odl_ip}    ${conn_id}=${hwvtep_conn_id}
53     [Documentation]    Keyword to add vtep manager for HWVTEP connected in ${conn_id} as ${odl_ip} received in argument and verify the entries in NETSTAT and HWVTEP NETWORK TOPOLOGY.
54     ${set_manager_command}=    Set Variable    ${VTEP_ADD_MGR}:${odl_ip}:${OVSDBPORT}
55     ${output}=    Exec Command    ${conn_id}    ${set_manager_command}
56     Log    ${output}
57     @{list_to_verify}=    Create List    ${odl_ip}    state=ACTIVE
58     Wait Until Keyword Succeeds    60s    2s    Verify Vtep List    ${conn_id}    ${MANAGER_TABLE}    @{list_to_verify}
59     ${output}=    Exec Command    ${conn_id}    ${NETSTAT}
60     Should Contain    ${output}    ${OVSDBPORT}
61     @{list_to_check}=    Create List    ${odl_ip}
62     Wait Until Keyword Succeeds    30s    2s    Utils.Check For Elements At URI    ${HWVTEP_NETWORK_TOPOLOGY}    ${list_to_check}    session
63
64 Create Verify L2Gateway
65     [Arguments]    ${bridge_name}    ${intf_name}    ${gw_name}
66     [Documentation]    Keyword to create an L2 Gateway ${gw_name} for bridge ${bridge_name} connected to interface ${intf_name} (Using Neutron CLI).
67     ${l2gw_output}=    OpenStackOperations.Create L2Gateway    ${bridge_name}    ${intf_name}    ${gw_name}
68     Log    ${l2gw_output}
69     ${output}=    OpenStackOperations.Get All L2Gateway
70     Log    ${output}
71     Should Contain    ${output}    ${gw_name}
72     @{list_to_check}=    Create List    ${gw_name}
73     Utils.Check For Elements At URI    ${L2GW_LIST_REST_URL}    ${list_to_check}    session
74     [Return]    ${l2gw_output}
75
76 Update And Verify L2Gateway
77     [Arguments]    ${bridge_name}    ${gw_name}    ${intf_name_1}    ${intf_name_2}
78     [Documentation]    Keyword to add interface {intf_name_2} to an existing L2 Gateway ${gw_name} for bridge ${bridge_name} (Using Neutron CLI).
79     ${l2gw_output}=    OpenStackOperations.Update L2Gateway    ${bridge_name}    ${gw_name}    ${intf_name_1}    ${intf_name_2}
80     ${output}=    OpenStackOperations.Get All L2Gateway
81     Log    ${output}
82     Should Contain    ${output}    ${gw_name}
83     Should Contain    ${output}    ${intf_name_1}
84     Should Contain    ${output}    ${intf_name_2}
85     @{list_to_check}=    Create List    ${gw_name}
86     Utils.Check For Elements At URI    ${L2GW_LIST_REST_URL}    ${list_to_check}    session
87     [Return]    ${l2gw_output}
88
89 Delete L2Gateway
90     [Arguments]    ${gw_name}    ${check_for_null}=False
91     [Documentation]    Keyword to delete the L2 Gateway ${gw_name} received in argument.
92     ...    If ${check_for_null} is True return of 404 is treated as empty list. From Neon onwards,
93     ...    an empty list is always returned as null, giving 404 on rest call.
94     ${output}=    Exec Command    ${OS_CNTL_CONN_ID}    ${L2GW_DELETE} ${gw_name}
95     Log    ${output}
96     @{list_to_check}=    Create List    ${gw_name}
97     BuiltIn.Wait Until Keyword Succeeds    5s    1s    Utils.Check For Elements Not At URI    ${L2GW_LIST_REST_URL}    ${list_to_check}    session
98     ...    check_for_null=${check_for_null}
99
100 Create Verify L2Gateway Connection
101     [Arguments]    ${gw_name}    ${net_name}
102     [Documentation]    Keyword to create a new L2 Gateway Connection for ${gw_name} to ${net_name} (Using Neutron CLI).
103     ${l2gw_output}=    OpenStackOperations.Create L2Gateway Connection    ${gw_name}    ${net_name}
104     Log    ${l2gw_output}
105     ${l2gw_id}=    OpenStackOperations.Get L2gw Id    ${gw_name}
106     ${output}=    OpenStackOperations.Get All L2Gateway Connection
107     Log    ${output}
108     Should Contain    ${output}    ${l2gw_id}
109     @{list_to_check}=    Create List    ${l2gw_id}
110     Utils.Check For Elements At URI    ${L2GW_CONN_LIST_REST_URL}    ${list_to_check}    session
111     [Return]    ${l2gw_output}
112
113 Verify L2Gateway Connection
114     [Arguments]    ${gw_name}    ${net_name}
115     [Documentation]    Keyword to verify existing L2 Gateway Connection for ${gw_name} to ${net_name} $(Using Neutron CLI).
116     ${l2gw_id}=    OpenStackOperations.Get L2gw Id    ${gw_name}
117     ${output}=    OpenStackOperations.Get All L2Gateway Connection
118     Log    ${output}
119     Should Contain    ${output}    ${l2gw_id}
120     @{list_to_check}=    Create List    ${l2gw_id}
121     Utils.Check For Elements At URI    ${L2GW_CONN_LIST_REST_URL}    ${list_to_check}    session
122
123 Delete L2Gateway Connection
124     [Arguments]    ${gw_name}    ${check_for_null}=False
125     [Documentation]    Delete the L2 Gateway connection existing for Gateway ${gw_name} received in argument (Using Neutron CLI).
126     ...    If ${check_for_null} is True return of 404 is treated as empty list. From Neon onwards, an empty list is always
127     ...    returned as null, giving 404 on rest call.
128     ${l2gw_conn_id}=    OpenStackOperations.Get L2gw Connection Id    ${gw_name}
129     ${output}=    Exec Command    ${OS_CNTL_CONN_ID}    ${L2GW_CONN_DELETE} ${l2gw_conn_id}
130     Log    ${output}
131     @{list_to_check}=    Create List    ${l2gw_conn_id}
132     BuiltIn.Wait Until Keyword Succeeds    5s    1s    Utils.Check For Elements Not At URI    ${L2GW_CONN_LIST_REST_URL}    ${list_to_check}    session
133     ...    check_for_null=${check_for_null}
134
135 Update Port For Hwvtep
136     [Arguments]    ${port_name}
137     [Documentation]    Keyword to update the Neutron Ports for specific configuration required to connect to HWVTEP (Using REST).
138     ${port_id}=    Get Port Id    ${port_name}
139     Log    ${port_id}
140     ${json_data}=    Get Neutron Port Rest    ${port_id}
141     Should Contain    ${json_data}    ${STR_VIF_TYPE}
142     Should Contain    ${json_data}    ${STR_VNIC_TYPE}
143     ${json_data}=    Replace String    ${json_data}    ${STR_VIF_TYPE}    ${STR_VIF_REPLACE}
144     ${json_data}=    Replace String    ${json_data}    ${STR_VNIC_TYPE}    ${STR_VNIC_REPLACE}
145     ${return}=    OpenStackOperations.Update Port Rest    ${port_id}    ${json_data}
146     ${output}=    OpenStackOperations.Get Neutron Port Rest    ${port_id}
147     Log    ${output}
148     Should Contain    ${output}    ${STR_VIF_REPLACE}
149     Should Contain    ${output}    ${STR_VNIC_REPLACE}
150     Should Not Contain    ${output}    ${STR_VIF_TYPE}
151     Should Not Contain    ${output}    ${STR_VNIC_TYPE}
152     [Return]    ${return}
153
154 Attach Port To Hwvtep Namespace
155     [Arguments]    ${port_mac}    ${ns_name}    ${tap_name}    ${conn_id}=${hwvtep_conn_id}
156     [Documentation]    Keyword to assign the ${port_mac} to the tap port ${tap_name} in namespace ${ns_name}
157     Exec Command    ${conn_id}    ${NETNS_EXEC} ${ns_name} ${IFCONF} ${tap_name} ${HW_ETHER} ${port_mac}
158     ${output}=    Exec Command    ${conn_id}    ${NETNS_EXEC} ${ns_name} ${IFCONF}
159     Should Contain    ${output}    ${port_mac}
160
161 Namespace Dhclient Verify
162     [Arguments]    ${ns_name}    ${ns_tap}    ${ns_port_ip}    ${conn_id}=${hwvtep_conn_id}    ${hwvtep_ip}=${HWVTEP_IP}
163     [Documentation]    Keyword to run dhclient for the tap port ${ns_tap} and verify if it has got assigned with ${ns_port_ip}.
164     Start Command In Hwvtep    ${NETNS_EXEC} ${ns_name} dhclient ${ns_tap}    ${hwvtep_ip}
165     Wait Until Keyword Succeeds    60s    2s    Verify Strings In Command Output    ${conn_id}    ${NETNS_EXEC} ${ns_name} ${IFCONF}    ${ns_port_ip}
166
167 Verify Strings In Command Output
168     [Arguments]    ${conn_id}    ${command}    @{string_list}
169     [Documentation]    Keyword to run the ${command} in ${conn_id} and verify if the output contains the list @{string_list}.
170     ${output}=    Exec Command    ${conn_id}    ${command}
171     : FOR    ${item}    IN    @{string_list}
172     \    Should Contain    ${output}    ${item}
173
174 Verify Ping In Namespace Extra Timeout
175     [Arguments]    ${ns_name}    ${ns_port_mac}    ${vm_ip}    ${conn_id}=${hwvtep_conn_id}    ${hwvtep_ip}=${HWVTEP_IP}
176     [Documentation]    Keyword to ping the IP ${vm_ip} from ${ns_name} and verify MCAS Local Table contains ${ns_port_mac}.
177     ${output}=    Exec Command    ${conn_id}    ${NETNS_EXEC} ${ns_name} ${IFCONF}
178     Log    ${output}
179     ${output}=    Exec Command    ${conn_id}    ${NETNS_EXEC} ${ns_name} ping -c3 ${vm_ip}    30s
180     Log    ${output}
181     Should Not Contain    ${output}    ${PACKET_LOSS}
182     Wait Until Keyword Succeeds    30s    2s    Verify Macs Local Table While Ping    ${ns_port_mac}    ${conn_id}
183
184 Verify Ping Fails In Namespace
185     [Arguments]    ${ns_name}    ${ns_port_mac}    ${vm_ip}    ${conn_id}=${hwvtep_conn_id}    ${hwvtep_ip}=${HWVTEP_IP}
186     [Documentation]    Keyword to ping the IP ${vm_ip} from ${ns_name} and should verify that it fails
187     ${output}=    Exec Command    ${conn_id}    ${NETNS_EXEC} ${ns_name} ping -c3 ${vm_ip}    30s
188     Log    ${output}
189     Should Contain    ${output}    ${PACKET_LOSS}
190
191 Verify Macs Local Table While Ping
192     [Arguments]    ${mac}    ${conn_id}
193     [Documentation]    Keyword to check if ${mac} is available under UCAST_MACS_LOCALE_TABLE of HWVTEP dump table.
194     Verify Vtep List    ${conn_id}    ${UCAST_MACS_LOCALE_TABLE}    ${mac}
195
196 Verify Nova VM IP
197     [Arguments]    ${vm_name}
198     [Documentation]    Keyword to verify if the VM has received IP, and to verify it is not null.
199     @{vm_ip}    ${dhcp_ip} =    Get VM IPs    ${vm_name}
200     Should Not Contain    ${vm_ip}    None
201     Should Not Contain    ${dhcp_ip}    None
202     [Return]    @{vm_ip}[0]
203
204 Get L2gw Debug Info
205     [Documentation]    Keyword to collect the general debug information required for HWVTEP Test Suite.
206     Exec Command    ${hwvtep_conn_id}    ${OVSDB_CLIENT_DUMP}
207     OpenStackOperations.Get Test Teardown Debugs
208     ${resp} =    RequestsLibrary.Get Request    session    ${CONFIG_API}/itm-state:external-tunnel-list/
209     Log    ${resp.content}
210     ${resp} =    RequestsLibrary.Get Request    session    ${CONFIG_API}/network-topology:network-topology/topology/hwvtep:1
211     Log    ${resp.content}
212     ${resp} =    RequestsLibrary.Get Request    session    ${OPERATIONAL_API}/network-topology:network-topology/topology/hwvtep:1
213     Log    ${resp.content}
214     Exec Command    ${OS_CNTL_CONN_ID}    cat /etc/neutron/neutron.conf
215     Exec Command    ${OS_CNTL_CONN_ID}    cat /etc/neutron/l2gw_plugin.ini
216     Exec Command    ${OS_CNTL_CONN_ID}    ps -ef | grep neutron-server
217
218 Start Command In Hwvtep
219     [Arguments]    ${command}    ${hwvtep_ip}
220     [Documentation]    Keyword to execute Start Command in HWVTEP IP.
221     ${conn_id}=    SSHLibrary.Open Connection    ${hwvtep_ip}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=30s
222     Log    ${conn_id}
223     Flexible SSH Login    ${DEFAULT_USER}    ${DEFAULT_PASSWORD}
224     Start Command    ${command}
225     ${output}=    Exec Command    ${conn_id}    sudo ovs-ofctl dump-flows ${INTEGRATION_BRIDGE} -O Openflow13
226     Log    ${output}
227     close connection
228
229 Verify Vtep List
230     [Arguments]    ${conn_id}    ${table_name}    @{list}
231     [Documentation]    Keyword to run vtep-ctl list for the table ${table_name} and verify the list @{list} contents exists in output.
232     ${output}=    Exec Command    ${conn_id}    ${VTEP LIST} ${table_name}
233     : FOR    ${item}    IN    @{list}
234     \    Should Contain    ${output}    ${item}
235
236 Get Vtep List
237     [Arguments]    ${table_name}    ${conn_id}=${hwvtep_conn_id}
238     [Documentation]    Keyword to return the contents of vtep-ctl list for table ${table_name}.
239     ${output}=    Exec Command    ${conn_id}    ${VTEP LIST} ${table_name}
240     [Return]    ${output}
241
242 Get Dpnid Decimal
243     [Arguments]    ${conn_id}
244     [Documentation]    Keyword to return DPN ID in decimal for the br-int in IP connected via ${conn_id}.
245     ${output}=    Exec Command    ${conn_id}    ${GET_DPNID}
246     Log    ${output}
247     ${splitted_output}=    Split String    ${output}    ${EMPTY}
248     ${dpn_id}=    Get from List    ${splitted_output}    0
249     Log    ${dpn_id}
250     [Return]    ${dpn_id}
251
252 Verify Ovs Tunnel
253     [Arguments]    ${hwvtep_ip}    ${ovs_ip}    ${seg_id}=${NET_1_SEGID}    ${conn_id}=${hwvtep_conn_id}
254     [Documentation]    Keyword to verify that the OVS tunnel entries are configured for OVS and HWVTEP.
255     ${output}=    Exec Command    ${conn_id}    ${OVS_SHOW}
256     Log    ${output}
257     Should Contain    ${output}    key="${seg_id}", remote_ip="${ovs_ip}"
258     ${output}=    Exec Command    ${OS_CMP1_CONN_ID}    ${OVS_SHOW}
259     Log    ${output}
260     Should Contain    ${output}    key=flow, local_ip="${ovs_ip}", remote_ip="${hwvtep_ip}"
261
262 Get Vtep Field Values From Table
263     [Arguments]    ${table_name}    ${column_name}    ${conn_id}=${hwvtep_conn_id}
264     [Documentation]    Keyword to return specific field value received in ${column_name} from the vtep-ctl list for ${table_name}.
265     ${output}=    Exec Command    ${conn_id}    ${VTEP_LIST_COLUMN}${column_name} list ${table_name} | awk '{print $3}'
266     Log    ${output}
267     @{keys}=    Split String    ${output}
268     Log    ${keys}
269     [Return]    ${keys}
270
271 Validate Regexp In String
272     [Arguments]    ${string}    ${regexp}    ${verify_count}=1
273     @{occr}=    Get Regexp Matches    ${string}    ${regexp}
274     ${count}=    Get Length    ${occr}
275     Should Be Equal As Integers    ${count}    ${verify_count}
276
277 Exec Command
278     [Arguments]    ${conn_id}    ${command}    ${timeout}=10s
279     Switch Connection    ${conn_id}
280     ${output}=    DevstackUtils.Write Commands Until Prompt    ${command}    ${timeout}
281     Log    ${output}
282     [Return]    ${output}
283
284 Verify Elan Flow Entries
285     [Arguments]    ${ip}    ${srcMacAddrs}    ${destMacAddrs}
286     [Documentation]    Verify Flows Are Present For ELAN service
287     ${flow_output} =    Run Command On Remote System    ${ip}    sudo ovs-ofctl -O OpenFlow13 dump-flows ${INTEGRATION_BRIDGE}
288     Log    ${flow_output}
289     Should Contain    ${flow_output}    table=50
290     ${sMac_output} =    Get Lines Containing String    ${flow_output}    table=50
291     Log    ${sMac_output}
292     : FOR    ${sMacAddr}    IN    @{srcMacAddrs}
293     \    ${resp}=    Should Contain    ${sMac_output}    dl_src=${sMacAddr}
294     Should Contain    ${flow_output}    table=51
295     ${dMac_output} =    Get Lines Containing String    ${flow_output}    table=51
296     Log    ${dMac_output}
297     : FOR    ${dMacAddr}    IN    @{destMacAddrs}
298     \    ${resp}=    Should Contain    ${dMac_output}    dl_dst=${dMacAddr}
299     Should Contain    ${flow_output}    table=52
300     ${sMac_output} =    Get Lines Containing String    ${flow_output}    table=52
301     Log    ${sMac_output}
302
303 Cleanup L2GW Optional Resources
304     [Documentation]    Cleanup resources that are only allocated on certain combos...
305     OpenStackOperations.Delete Port    ${HWVTEP_PORT_3}