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