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