Add ping test in aclrecovery suite
[integration/test.git] / csit / libraries / OvsManager.robot
1 *** Settings ***
2 Documentation     Library to provide ovsdb access to mininet topologies
3 Library           SSHLibrary
4 Library           ${CURDIR}/VsctlListParser.py
5 Library           Collections
6
7 *** Variables ***
8 ${SH_BR_CMD}      ovs-vsctl list Bridge
9 ${SH_CNTL_CMD}    ovs-vsctl list Controller
10 ${SHOW_OVS_VERSION}    sudo ovs-vsctl show | grep version
11 ${GET_LOCAL_IP}    sudo ovs-vsctl list Open_vSwitch | grep local_ip=
12 ${ovs_switch_data}    ${None}
13 ${lprompt}        mininet>
14 ${lcmd_prefix}    sh
15
16 *** Keywords ***
17 Initialize If Shell Used
18     [Arguments]    ${prompt}    ${cmd_prefix}
19     BuiltIn.Set Suite variable    ${lprompt}    ${prompt}
20     BuiltIn.Set Suite variable    ${lcmd_prefix}    ${cmd_prefix}
21
22 Get Ovsdb Data
23     [Arguments]    ${prompt}=mininet>
24     [Documentation]    Gets ovs data and parse them.
25     SSHLibrary.Write    ${lcmd_prefix} ${SH_BR_CMD}
26     ${brstdout}=    SSHLibrary.Read_Until    ${lprompt}
27     Log    ${brstdout}
28     SSHLibrary.Write    ${lcmd_prefix} ${SH_CNTL_CMD}
29     ${cntlstdout}=    SSHLibrary.Read_Until    ${lprompt}
30     Log    ${cntlstdout}
31     ${data}    ${bridegs}    ${controllers}=    VsctlListParser.Parse    ${brstdout}    ${cntlstdout}
32     BuiltIn.Log    ${data}
33     BuiltIn.Set Suite Variable    ${ovs_switch_data}    ${data}
34     BuiltIn.Return From Keyword    ${data}
35
36 Get Controllers Uuid
37     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
38     [Documentation]    Returns controllers uuid
39     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
40     ${bridge}=    Collections.Get From Dictionary    ${ovs_switch_data}    ${switch}
41     ${cntls}=    Collections.Get From Dictionary    ${bridge}    controller
42     ${cntl}=    Collections.Get From Dictionary    ${cntls}    ${controller}
43     ${uuid}=    Collections.Get From Dictionary    ${cntl}    _uuid
44     BuiltIn.Return From Keyword    ${uuid}
45
46 Execute OvsVsctl Show Command
47     [Documentation]    Executes ovs-vsctl show command and returns stdout, no check nor change is performed
48     SSHLibrary.Write    ${lcmd_prefix} ovs-vsctl show
49     ${output}=    SSHLibrary.Read_Until    ${lprompt}
50     Log    ${output}
51
52 Set Bridge Controllers
53     [Arguments]    ${bridge}    ${controllers}    ${ofversion}=13    ${disconnected}=${False}
54     [Documentation]    Adds controller to the bridge
55     ${cmd}=    BuiltIn.Set Variable    ${lcmd_prefix} ovs-vsctl set bridge ${bridge} protocols=OpenFlow${ofversion}
56     SSHLibrary.Write    ${cmd}
57     ${output}=    SSHLibrary.Read_Until    ${lprompt}
58     Log    ${output}
59     ${cmd}=    BuiltIn.Set Variable    ${lcmd_prefix} ovs-vsctl set-controller ${bridge}
60     : FOR    ${cntl}    IN    @{controllers}
61     \    ${cmd}=    BuiltIn.Set Variable If    ${disconnected}==${False}    ${cmd} tcp:${cntl}:6653    ${cmd} tcp:${cntl}:6654
62     BuiltIn.Log    ${cmd}
63     SSHLibrary.Write    ${cmd}
64     ${output}=    SSHLibrary.Read_Until    ${lprompt}
65     Log    ${output}
66
67 Disconnect Switch From Controller And Verify Disconnected
68     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}    ${verify_disconnected}=${True}
69     [Documentation]    Disconnects the switch from the controller by setting the incorrect port
70     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
71     ${uuid}=    Get Controllers Uuid    ${switch}    ${controller}
72     ${cmd}=    BuiltIn.Set Variable    ${lcmd_prefix} ovs-vsctl set Controller ${uuid} target="tcp\\:${controller}\\:6654"
73     SSHLibrary.Write    ${cmd}
74     ${output}=    SSHLibrary.Read_Until    ${lprompt}
75     Log    ${output}
76     Return From Keyword If    ${verify_disconnected}==${False}
77     BuiltIn.Wait Until Keyword Succeeds    5x    2s    Should Be Disconnected    ${switch}    ${controller}    update_data=${True}
78     [Teardown]    Execute OvsVsctl Show Command
79
80 Reconnect Switch To Controller And Verify Connected
81     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}    ${verify_connected}=${True}
82     [Documentation]    Reconnects the switch back to the controller by setting the correct port
83     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
84     ${uuid}=    Get Controllers Uuid    ${switch}    ${controller}
85     ${cmd}=    BuiltIn.Set Variable    ${lcmd_prefix} ovs-vsctl set Controller ${uuid} target="tcp\\:${controller}\\:6653"
86     SSHLibrary.Write    ${cmd}
87     ${output}=    SSHLibrary.Read_Until    ${lprompt}
88     Log    ${output}
89     Return From Keyword If    ${verify_connected}==${False}
90     BuiltIn.Wait Until Keyword Succeeds    5x    2s    Should Be Connected    ${switch}    ${controller}    update_data=${True}
91     [Teardown]    Execute OvsVsctl Show Command
92
93 Should Be Connected
94     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
95     [Documentation]    Check if the switch is connected
96     ${connected}=    OvsManager__Is_Connected    ${switch}    ${controller}    update_data=${update_data}
97     BuiltIn.Should Be True    ${connected}
98
99 Should Be Disconnected
100     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
101     [Documentation]    Check if the switch is disconnected
102     ${connected}=    OvsManager__Is_Connected    ${switch}    ${controller}    update_data=${update_data}
103     BuiltIn.Should Be Equal    ${connected}    ${False}
104
105 OvsManager__Is_Connected
106     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
107     [Documentation]    Return is_connected boolean value
108     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
109     ${bridge}=    Collections.Get From Dictionary    ${ovs_switch_data}    ${switch}
110     ${cntls}=    Collections.Get From Dictionary    ${bridge}    controller
111     ${cntl}=    Collections.Get From Dictionary    ${cntls}    ${controller}
112     ${connected}=    Collections.Get From Dictionary    ${cntl}    is_connected
113     [Teardown]    Execute OvsVsctl Show Command
114     [Return]    ${connected}
115
116 Should Be Master
117     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
118     [Documentation]    Verifies the master role
119     ${role}    Get Node Role    ${switch}    ${controller}    update_data=${update_data}
120     BuiltIn.Should Be Equal    ${role}    master
121
122 Should Be Slave
123     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
124     [Documentation]    Verifies the slave role
125     ${role}    Get Node Role    ${switch}    ${controller}    update_data=${update_data}
126     BuiltIn.Should Be Equal    ${role}    slave
127
128 Get Node Role
129     [Arguments]    ${switch}    ${controller}    ${update_data}=${False}
130     [Documentation]    Returns the controllers role
131     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
132     ${bridge}=    Collections.Get From Dictionary    ${ovs_switch_data}    ${switch}
133     ${cntls}=    Collections.Get From Dictionary    ${bridge}    controller
134     ${cntl}=    Collections.Get From Dictionary    ${cntls}    ${controller}
135     ${role}=    Collections.Get From Dictionary    ${cntl}    role
136     Return From Keyword    ${role}
137
138 Get Master Node
139     [Arguments]    ${switch}    ${update_data}=${False}
140     [Documentation]    Gets controller which is a master
141     ${master}=    BuiltIn.Set Variable    ${None}
142     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
143     ${bridge}=    Collections.Get From Dictionary    ${ovs_switch_data}    ${switch}
144     ${cntls_dict}=    Collections.Get From Dictionary    ${bridge}    controller
145     ${cntls_items}=    Collections.Get Dictionary Items    ${cntls_dict}
146     : FOR    ${key}    ${value}    IN    @{cntls_items}
147     \    Log    ${key} : ${value}
148     \    ${role}=    Collections.Get From Dictionary    ${value}    role
149     \    Run Keyword If    "${role}"=="master"    BuiltIn.Should Be Equal    ${master}    ${None}
150     \    ${master}=    BuiltIn.Set Variable If    "${role}"=="master"    ${key}    ${master}
151     BuiltIn.Should Not Be Equal    ${master}    ${None}
152     Return From Keyword    ${master}
153
154 Get Slave Nodes
155     [Arguments]    ${switch}    ${update_data}=${False}
156     [Documentation]    Returns a list of ips of slave nodes for particular switch
157     ${slaves}=    BuiltIn.Create List
158     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
159     ${bridge}=    Collections.Get From Dictionary    ${ovs_switch_data}    ${switch}
160     ${cntls_dict}=    Collections.Get From Dictionary    ${bridge}    controller
161     ${cntls_items}=    Collections.Get Dictionary Items    ${cntls_dict}
162     : FOR    ${key}    ${value}    IN    @{cntls_items}
163     \    Log    ${key} : ${value}
164     \    ${role}=    Collections.Get From Dictionary    ${value}    role
165     \    Run Keyword If    "${role}"=="slave"    Collections.Append To List    ${slaves}    ${key}
166     Return From Keyword    ${slaves}
167
168 Setup Clustered Controller For Switches
169     [Arguments]    ${switches}    ${controller_ips}    ${verify_connected}=${False}
170     [Documentation]    The idea of this keyword is to setup clustered controller and to be more or less sure that the role is filled correctly. The problem is when
171     ...    more controllers are being set up at once, the role shown in Controller ovsdb table is not the same as we can see from wireshark traces.
172     ...    Now we set disconnected controllers and we will connect them expecting that the first connected controller will be master.
173     : FOR    ${switch_name}    IN    @{switches}
174     \    Set Bridge Controllers    ${switch_name}    ${controller_ips}    disconnected=${True}
175     # now we need to enable one node which will be master
176     OvsManager.Get Ovsdb Data
177     : FOR    ${switch_name}    IN    @{switches}
178     \    ${own}=    Collections.Get From List    ${controller_ips}    0
179     \    Reconnect Switch To Controller And Verify Connected    ${switch_name}    ${own}    verify_connected=${False}
180     # now we need to wait till master controllers are connected
181     BuiltIn.Wait Until Keyword Succeeds    5x    2s    OvsManager__Verify_Masters_Connected    ${switches}    update_data=${True}
182     # now we can enable slaves
183     OvsManager__Enable_Slaves    ${switches}    verify_connected=${verify_connected}
184
185 OvsManager__Verify_Masters_Connected
186     [Arguments]    ${switches}    ${update_data}=${False}
187     [Documentation]    Private keyword, the existence of master means it is verified
188     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
189     : FOR    ${switch_name}    IN    @{switches}
190     \    Get Master Node    ${switch_name}
191
192 OvsManager__Enable_Slaves
193     [Arguments]    ${switches}    ${update_data}=${False}    ${verify_connected}=${False}
194     [Documentation]    This is a private keyword to enable diconnected controllers
195     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
196     : FOR    ${switch_name}    IN    @{switches}
197     \    OvsManager__Enable_Slaves_For_Switch    ${switch_name}    verify_connected=${verify_connected}
198
199 OvsManager__Enable_Slaves_For_Switch
200     [Arguments]    ${switch}    ${update_data}=${False}    ${verify_connected}=${False}
201     [Documentation]    This is a private keyword, verification is not reliable yet, enables disconnected controllers
202     Run Keyword If    ${update_data}==${True}    Get Ovsdb Data
203     ${bridge}=    Collections.Get From Dictionary    ${ovs_switch_data}    ${switch}
204     ${cntls_dict}=    Collections.Get From Dictionary    ${bridge}    controller
205     ${cntls_items}=    Collections.Get Dictionary Items    ${cntls_dict}
206     : FOR    ${cntl_id}    ${cntl_value}    IN    @{cntls_items}
207     \    Log    ${cntl_id} : ${cntl_value}
208     \    ${role}=    Collections.Get From Dictionary    ${cntl_value}    role
209     \    ${connected}=    Collections.Get From Dictionary    ${cntl_value}    is_connected
210     \    Run Keyword If    ${connected}==${False}    Reconnect Switch To Controller And Verify Connected    ${switch}    ${cntl_id}    verify_connected=${verify_connected}
211
212 Get Dump Flows Count
213     [Arguments]    ${conn_id}    ${acl_sr_table_id}    ${port_mac}=""
214     [Documentation]    Count the number of dump flows for a given table id
215     ...    and grep with port_mac if provided
216     ${cmd} =    BuiltIn.Set Variable    sudo ovs-ofctl dump-flows br-int -OOpenFlow13 | grep table=${acl_sr_table_id} | grep ${port_mac} | wc -l
217     SSHLibrary.Switch Connection    ${conn_id}
218     ${output} =    Utils.Write Commands Until Expected Prompt    ${cmd}    ${DEFAULT_LINUX_PROMPT_STRICT}
219     @{list} =    String.Split String    ${output}
220     [Return]    ${list[0]}
221
222 Get Packet Count From Table
223     [Arguments]    ${system_ip}    ${br_name}    ${table_no}    ${addtioanal_args}=${EMPTY}
224     [Documentation]    Return packet count for the specific table no.
225     ${flow_output} =    Utils.Run Command On Remote System    ${system_ip}    sudo ovs-ofctl dump-flows -O Openflow13 ${br_name} | grep ${table_no} ${addtioanal_args}
226     @{output} =    String.Split String    ${flow_output}    \r\n
227     ${flow} =    Collections.Get From List    ${output}    0
228     ${packetcountlist} =    String.Get Regexp Matches    ${flow}    n_packets=([0-9]+),    1
229     ${packetcount} =    Collections.Get From List    ${packetcountlist}    0
230     [Return]    ${packetcount}
231
232 Get Packet Count In Table For IP
233     [Arguments]    ${os_compute_ip}    ${table_no}    ${ip_address}    ${additional_args}=${EMPTY}
234     [Documentation]    Capture packetcount for IP in Table
235     ${cmd} =    BuiltIn.Set Variable    sudo ovs-ofctl dump-flows br-int -OOpenFlow13 | grep table=${table_no} | grep ${ip_address} ${additional_args}
236     ${output} =    Utils.Run Command On Remote System And Log    ${os_compute_ip}    ${cmd}
237     @{output_list} =    String.Split String    ${output}    \r\n
238     ${flow} =    Collections.Get From List    ${output_list}    0
239     ${packetcount_list} =    String.Get Regexp Matches    ${flow}    n_packets=([0-9]+)    1
240     ${count} =    Collections.Get From List    ${packetcount_list}    0
241     [Return]    ${count}
242
243 Verify Ovs Version Greater Than Or Equal To
244     [Arguments]    ${ovs_version}    @{nodes}
245     [Documentation]    Get ovs version and verify greater than required version
246     : FOR    ${ip}    IN    @{nodes}
247     \    ${output} =    Utils.Run Command On Remote System    ${ip}    ${SHOW_OVS_VERSION}
248     \    ${version} =    String.Get Regexp Matches    ${output}    \[0-9].\[0-9]
249     \    ${result} =    BuiltIn.Convert To Number    ${version[0]}
250     \    BuiltIn.Should Be True    ${result} >= ${ovs_version}
251
252 Get OVS Local Ip
253     [Arguments]    ${ip}
254     [Documentation]    Get local ip of compute node ovsdb
255     ${cmd_output} =    Utils.Run Command On Remote System    ${ip}    ${GET_LOCAL_IP}
256     ${localip} =    String.Get Regexp Matches    ${cmd_output}    (\[0-9]+\.\[0-9]+\.\[0-9]+\.\[0-9]+)
257     [Return]    @{localip}[0]