Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / MininetKeywords.robot
1 *** Settings ***
2 Documentation     Mininet library. This library is useful for tests using mininet tool to simulate devices.
3 Library           SSHLibrary
4 Resource          SSHKeywords.robot
5 Resource          Utils.robot
6 Resource          OVSDB.robot
7 Resource          ClusterManagement.robot
8 Variables         ../variables/Variables.py
9
10 *** Variables ***
11 ${switch_private_key}    switch.key
12 ${switch_certificate}    switch.crt
13 ${controller_ca_list}    cachain.crt
14 ${topology_file}    create_fullymesh.py
15 ${topology_file_path}    MininetTopo/${topology_file}
16
17 *** Keywords ***
18 Start Mininet Single Controller
19     [Arguments]    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${options}=--topo tree,1    ${custom}=${EMPTY}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}
20     ...    ${timeout}=${DEFAULT_TIMEOUT}
21     [Documentation]    Start Mininet with custom topology and connect to controller.
22     Log    Clear any existing mininet
23     Utils.Clean Mininet System    ${mininet}
24     ${mininet_conn_id}=    SSHKeywords.Open_Connection_To_Tools_System    ip_address=${mininet}    timeout=${timeout}
25     Set Suite Variable    ${mininet_conn_id}
26     Run Keyword If    '${custom}' != '${EMPTY}'    Put File    ${custom}
27     Log    Start mininet ${options} to ${controller}
28     SSHLibrary.Write    sudo mn --controller 'remote,ip=${controller},port=${ofport}' ${options} --switch ovsk,protocols=OpenFlow${ofversion}
29     SSHLibrary.Read Until    mininet>
30     Log    Check OVS configuratiom
31     SSHLibrary.Write    sh ovs-vsctl show
32     SSHLibrary.Read Until    mininet>
33     [Return]    ${mininet_conn_id}
34
35 Start Mininet Multiple Controllers
36     [Arguments]    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller_index_list}=${EMPTY}    ${options}=--topo tree,1    ${custom}=${EMPTY}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}
37     ...    ${protocol}=tcp    ${timeout}=${DEFAULT_TIMEOUT}
38     [Documentation]    Start Mininet with custom topology and connect to list of controllers in ${controller_index_list} or all if no list is provided.
39     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${controller_index_list}
40     Log    Clear any existing mininet
41     Utils.Clean Mininet System    ${mininet}
42     ${mininet_conn_id}=    SSHKeywords.Open_Connection_To_Tools_System    ip_address=${mininet}    timeout=${timeout}
43     Set Suite Variable    ${mininet_conn_id}
44     Run Keyword If    '${custom}' != '${EMPTY}'    Put File    ${custom}
45     Run Keyword If    '${protocol}' == 'ssl'    Install Certificates In Mininet
46     Log    Start mininet ${options}
47     SSHLibrary.Write    sudo mn ${options}
48     SSHLibrary.Read Until    mininet>
49     Log    Create controller configuration
50     ${controller_opt}=    Set Variable
51     FOR    ${index}    IN    @{index_list}
52         ${controller_opt}=    Catenate    ${controller_opt}    ${SPACE}${protocol}:${ODL_SYSTEM_${index}_IP}:${ofport}
53         Log    ${controller_opt}
54     END
55     Log    Open extra SSH connection to configure the OVS bridges
56     SSHKeywords.Open_Connection_To_Tools_System    ip_address=${mininet}    timeout=${timeout}
57     ${num_bridges}    SSHLibrary.Execute Command    sudo ovs-vsctl show | grep Bridge | wc -l
58     ${num_bridges}=    Convert To Integer    ${num_bridges}
59     ${bridges}=    Create List
60     FOR    ${i}    IN RANGE    1    ${num_bridges+1}
61         ${bridge}=    SSHLibrary.Execute Command    sudo ovs-vsctl show | grep Bridge | cut -c 12- | sort | head -${i} | tail -1
62         SSHLibrary.Execute Command    sudo ovs-vsctl del-controller ${bridge} && sudo ovs-vsctl set bridge ${bridge} protocols=OpenFlow${ofversion}
63         Collections.Append To List    ${bridges}    ${bridge}
64     END
65     Log    Configure OVS controllers ${controller_opt} in all bridges
66     FOR    ${bridge}    IN    @{bridges}
67         SSHLibrary.Execute Command    sudo ovs-vsctl set-controller ${bridge} ${controller_opt}
68     END
69     Log    Check OVS configuratiom
70     ${output}=    SSHLibrary.Execute Command    sudo ovs-vsctl show
71     Log    ${output}
72     SSHLibrary.Close Connection
73     [Return]    ${mininet_conn_id}
74
75 Start Mininet Multiple Hosts
76     [Arguments]    ${hosts}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
77     [Documentation]    Start mininet 1 switch with ${hosts} hosts attached.
78     Log    Start Mininet Linear
79     MininetKeywords.StartMininet Single Controller    options=--topo single,${hosts} --mac    ofversion=${ofversion}    ofport=${ofport}    timeout=${mininet_timeout}
80
81 Start Mininet Linear
82     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
83     [Documentation]    Start mininet linear topology with ${switches} nodes.
84     Log    Start Mininet Linear
85     MininetKeywords.StartMininet Single Controller    options=--topo linear,${switches}    ofversion=${ofversion}    ofport=${ofport}    timeout=${mininet_timeout}
86
87 Start Mininet Full Mesh
88     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${ofversion}=13    ${ofport}=${ODL_OF_PORT}    ${hosts}=0
89     ...    ${mininet_timeout}=${DEFAULT_TIMEOUT}
90     [Documentation]    Start a custom mininet topology.
91     ${mininet_conn_id}=    SSHLibrary.Open Connection    ${mininet}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${mininet_timeout}
92     Set Suite Variable    ${mininet_conn_id}
93     SSHKeywords.Flexible_Mininet_Login
94     Log    Copying ${topology_file_path} file to Mininet VM and Creating Full Mesh topology
95     SSHLibrary.Put File    ${CURDIR}/${topology_file_path}
96     SSHLibrary.Write    python ${topology_file} ${switches} ${hosts} 00:00:00:00:00:00 10.0.0.0
97     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
98     Log    Start Mininet Full Mesh
99     SSHLibrary.Write    sudo mn --controller=remote,ip=${controller},port=${ofport} --custom switch.py --topo demotopo --switch ovsk,protocols=OpenFlow${ofversion}
100     SSHLibrary.Read Until    mininet>
101     Log    Check OVS configuratiom
102     SSHLibrary.Write    sh ovs-vsctl show
103     ${output}=    Read Until    mininet>
104     # Ovsdb connection is sometimes lost after mininet is started. Checking if the connection is alive before proceeding.
105     Should Not Contain    ${output}    database connection failed
106
107 Send Mininet Command
108     [Arguments]    ${mininet_conn}=${EMPTY}    ${cmd}=help
109     [Documentation]    Sends Command ${cmd} to Mininet session ${mininet_conn} and returns read buffer response.
110     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
111     SSHLibrary.Write    ${cmd}
112     ${output}=    SSHLibrary.Read Until    mininet>
113     [Return]    ${output}
114
115 Send Mininet Command Multiple Sessions
116     [Arguments]    ${mininet_conn_list}    ${cmd}=help
117     [Documentation]    Sends Command ${cmd} to Mininet sessions in ${mininet_conn_list} and returns list of read buffer responses.
118     ${output_list}=    Create List
119     FOR    ${mininet_conn}    IN    @{mininet_conn_list}
120         ${output}=    Utils.Send Mininet Command    ${mininet_conn}    ${cmd}
121         Collections.Append To List    ${output_list}    ${output}
122     END
123     [Return]    ${output_list}
124
125 Stop Mininet And Exit
126     [Arguments]    ${mininet_conn}=${EMPTY}
127     [Documentation]    Stops Mininet and exits session ${mininet_conn}
128     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
129     SSHLibrary.Write    exit
130     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
131     SSHLibrary.Close Connection
132
133 Stop Mininet And Exit Multiple Sessions
134     [Arguments]    ${mininet_conn_list}
135     [Documentation]    Stops Mininet and exits sessions in ${mininet_conn_list}.
136     FOR    ${mininet_conn}    IN    @{mininet_conn_list}
137         MininetKeywords.Stop Mininet And Exit    ${mininet_conn}
138     END
139
140 Disconnect Cluster Mininet
141     [Arguments]    ${action}=break    ${member_index_list}=${EMPTY}
142     [Documentation]    Break and restore controller to mininet connection via iptables.
143     ${index_list} =    ClusterManagement.List_Indices_Or_All    given_list=${member_index_list}
144     FOR    ${index}    IN    @{index_list}
145         ${rule} =    BuiltIn.Set Variable    OUTPUT -p all --source ${ODL_SYSTEM_${index}_IP} --destination ${TOOLS_SYSTEM_IP} -j DROP
146         ${command} =    BuiltIn.Set Variable If    '${action}'=='restore'    sudo /sbin/iptables -D ${rule}    sudo /sbin/iptables -I ${rule}
147         Log To Console    ${ODL_SYSTEM_${index}_IP}
148         Utils.Run Command On Controller    ${ODL_SYSTEM_${index}_IP}    cmd=${command}
149         ${command} =    BuiltIn.Set Variable    sudo /sbin/iptables -L -n
150         ${output} =    Utils.Run Command On Controller    cmd=${command}
151         BuiltIn.Log    ${output}
152     END
153
154 Verify Aggregate Flow From Mininet Session
155     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0    ${time_out}=0s
156     [Documentation]    Verify flow count per switch
157     Wait Until Keyword Succeeds    ${time_out}    2s    MininetKeywords.Check Flows In Mininet    ${mininet_conn}    ${flow_count}
158
159 Check Flows In Mininet
160     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0
161     [Documentation]    Sync with mininet to match exact number of flows
162     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
163     ${cmd} =    Set Variable    dpctl dump-aggregate -O OpenFlow13
164     ${output}=    MininetKeywords.Send Mininet Command    ${mininet_conn}    ${cmd}
165     ${flows}=    String.Get RegExp Matches    ${output}    (?<=flow_count\=).*?(?=\r)
166     ${total_flows}=    BuiltIn.Evaluate    sum(map(int, ${flows}))
167     Should Be Equal As Numbers    ${total_flows}    ${flow_count}
168
169 Verify Mininet Ping
170     [Arguments]    ${host1}    ${host2}
171     [Documentation]    Send ping from mininet and verify connectivity.
172     SSHLibrary.Write    ${host1} ping -w 3 ${host2}
173     ${result}=    SSHLibrary.Read Until    mininet>
174     Should Contain    ${result}    64 bytes
175
176 Verify Mininet No Ping
177     [Arguments]    ${host1}    ${host2}
178     [Documentation]    Send ping from mininet and verify no conectivity.
179     SSHLibrary.Write    ${host1} ping -w 3 ${host2}
180     ${result}=    SSHLibrary.Read Until    mininet>
181     Should Contain    ${result}    100% packet loss
182
183 Ping All Hosts
184     [Arguments]    @{host_list}
185     [Documentation]    Do one round of ping from one host to all other hosts in mininet.
186     ...    Note that a single ping failure will exit the loop and return a non zero value.
187     ${source}=    Get From List    ${host_list}    ${0}
188     FOR    ${h}    IN    @{host_list}
189         ${status}=    Ping Two Hosts    ${source}    ${h}    1
190         Exit For Loop If    ${status}!=${0}
191     END
192     [Return]    ${status}
193
194 Ping Two Hosts
195     [Arguments]    ${host1}    ${host2}    ${pingcount}=2
196     [Documentation]    Ping between mininet hosts. Must be used only after a mininet session is in place.
197     ...    Returns non zero value if there is 100% packet loss.
198     SSHLibrary.Write    ${host1} ping -c ${pingcount} ${host2}
199     ${out}=    SSHLibrary.Read Until    mininet>
200     ${ret}=    String.Get Lines Matching Regexp    ${out}    .*100% packet loss.*
201     ${len}=    Get Length    ${ret}
202     [Return]    ${len}
203
204 Get Mininet Hosts
205     [Documentation]    Get all the hosts from mininet
206     ${host_list}=    Create List
207     SSHLibrary.Write    nodes
208     ${out}=    SSHLibrary.Read Until    mininet>
209     @{words}=    String.Split String    ${out}    ${SPACE}
210     FOR    ${item}    IN    @{words}
211         ${h}=    String.Get Lines Matching Regexp    ${item}    h[0-9]*
212         Run Keyword If    '${h}' != '${EMPTY}'    Collections.Append To List    ${host_list}    ${h}
213     END
214     [Return]    ${host_list}
215
216 Install Certificates In Mininet
217     [Documentation]    Copy and install certificates in simulator.
218     Comment    Copy Certificates
219     SSHLibrary.Put File    ${CURDIR}/tls/${switch_private_key}    .
220     SSHLibrary.Put File    ${CURDIR}/tls/${switch_certificate}    .
221     SSHLibrary.Put File    ${CURDIR}/tls/${controller_ca_list}    .
222     Comment    Install Certificates
223     SSHLibrary.Execute Command    sudo mv ${switch_private_key} /etc/openvswitch && sudo mv ${switch_certificate} /etc/openvswitch && sudo mv ${controller_ca_list} /etc/openvswitch
224     SSHLibrary.Execute Command    sudo ovs-vsctl set-ssl /etc/openvswitch/${switch_private_key} /etc/openvswitch/${switch_certificate} /etc/openvswitch/${controller_ca_list}
225     ${std_out}=    SSHLibrary.Execute Command    .    sudo ovs-vsctl get-ssl
226     Log    ${std_out}