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