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