416b27274baf1eb75ac88292aca81c77749b43f3
[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          Utils.robot
5 Resource          OVSDB.robot
6 Resource          ClusterManagement.robot
7 Variables         ../variables/Variables.py
8
9 *** Variables ***
10 ${topology_file}    create_fullymesh.py
11 ${topology_file_path}    MininetTopo/${topology_file}
12
13 *** Keywords ***
14 Start Mininet Single Controller
15     [Arguments]    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${options}=--topo tree,1 --switch ovsk,protocols=OpenFlow13    ${custom}=${EMPTY}    ${ofport}=${ODL_OF_PORT}    ${timeout}=${DEFAULT_TIMEOUT}
16     [Documentation]    Start Mininet with custom topology and connect to controller.
17     Log    Clear any existing mininet
18     Utils.Clean Mininet System    ${mininet}
19     ${mininet_conn_id}=    SSHLibrary.Open Connection    ${mininet}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${timeout}
20     Set Suite Variable    ${mininet_conn_id}
21     Utils.Flexible Mininet Login
22     Run Keyword If    '${custom}' != '${EMPTY}'    Put File    ${custom}
23     Log    Start mininet ${options} to ${controller}
24     SSHLibrary.Write    sudo mn --controller 'remote,ip=${controller},port=${ofport}' ${options}
25     SSHLibrary.Read Until    mininet>
26     Log    Check OVS configuratiom
27     SSHLibrary.Write    sh ovs-vsctl show
28     SSHLibrary.Read Until    mininet>
29     [Return]    ${mininet_conn_id}
30
31 Start Mininet Multiple Controllers
32     [Arguments]    ${mininet}    ${controller_index_list}=${EMPTY}    ${options}=--topo tree,1 --switch ovsk,protocols=OpenFlow13    ${custom}=${EMPTY}    ${ofport}=${ODL_OF_PORT}    ${timeout}=${DEFAULT_TIMEOUT}
33     [Documentation]    Start Mininet with custom topology and connect to list of controllers in ${controller_index_list} or all if no list is provided.
34     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${controller_index_list}
35     Log    Clear any existing mininet
36     Utils.Clean Mininet System    ${mininet}
37     ${mininet_conn_id}=    SSHLibrary.Open Connection    ${mininet}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${timeout}
38     Set Suite Variable    ${mininet_conn_id}
39     Utils.Flexible Mininet Login
40     Run Keyword If    '${custom}' != '${EMPTY}'    Put File    ${custom}
41     Log    Start mininet ${options}
42     SSHLibrary.Write    sudo mn ${options}
43     SSHLibrary.Read Until    mininet>
44     Log    Create controller configuration
45     ${controller_opt}=    Set Variable
46     : FOR    ${index}    IN    @{index_list}
47     \    ${controller_opt}=    Catenate    ${controller_opt}    ${SPACE}tcp:${ODL_SYSTEM_${index}_IP}:${ofport}
48     \    Log    ${controller_opt}
49     Log    Find Number of OVS bridges
50     ${num_bridges}    Utils.Run Command On Mininet    ${mininet}    sudo ovs-vsctl show | grep Bridge | wc -l
51     ${num_bridges}=    Convert To Integer    ${num_bridges}
52     Log    Configure OVS controllers ${controller_opt} in all bridges
53     : FOR    ${i}    IN RANGE    1    ${num_bridges+1}
54     \    ${bridge}=    Utils.Run Command On Mininet    ${mininet}    sudo ovs-vsctl show | grep Bridge | cut -c 12- | sort | head -${i} | tail -1
55     \    OVSDB.Set Controller In OVS Bridge    ${mininet}    ${bridge}    ${controller_opt}
56     Log    Check OVS configuratiom
57     SSHLibrary.Write    sh ovs-vsctl show
58     SSHLibrary.Read Until    mininet>
59     [Return]    ${mininet_conn_id}
60
61 Start Mininet Multiple Hosts
62     [Arguments]    ${hosts}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
63     [Documentation]    Start mininet 1 switch with ${hosts} hosts attached.
64     Log    Start Mininet Linear
65     MininetKeywords.StartMininet Single Controller    options=--topo single,${hosts} --mac --switch ovsk,protocols=OpenFlow13    timeout=${mininet_timeout}
66
67 Start Mininet Linear
68     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
69     [Documentation]    Start mininet linear topology with ${switches} nodes.
70     Log    Start Mininet Linear
71     MininetKeywords.StartMininet Single Controller    options=--topo linear,${switches} --switch ovsk,protocols=OpenFlow13    timeout=${mininet_timeout}
72
73 Start Mininet Full Mesh
74     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${hosts}=0    ${mininet_timeout}=${DEFAULT_TIMEOUT}
75     [Documentation]    Start a custom mininet topology.
76     ${mininet_conn_id}=    SSHLibrary.Open Connection    ${mininet}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${mininet_timeout}
77     Set Suite Variable    ${mininet_conn_id}
78     Utils.Flexible_Mininet_Login
79     Log    Copying ${topology_file_path} file to Mininet VM and Creating Full Mesh topology
80     SSHLibrary.Put File    ${CURDIR}/${topology_file_path}
81     SSHLibrary.Write    python ${topology_file} ${switches} ${hosts} 00:00:00:00:00:00 10.0.0.0
82     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
83     Log    Start Mininet Full Mesh
84     SSHLibrary.Write    sudo mn --controller=remote,ip=${controller} --custom switch.py --topo demotopo --switch ovsk,protocols=OpenFlow13
85     SSHLibrary.Read Until    mininet>
86     Log    Check OVS configuratiom
87     SSHLibrary.Write    sh ovs-vsctl show
88     ${output}=    Read Until    mininet>
89     # Ovsdb connection is sometimes lost after mininet is started. Checking if the connection is alive before proceeding.
90     Should Not Contain    ${output}    database connection failed
91
92 Send Mininet Command
93     [Arguments]    ${mininet_conn}=${EMPTY}    ${cmd}=help
94     [Documentation]    Sends Command ${cmd} to Mininet session ${mininet_conn} and returns read buffer response.
95     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
96     SSHLibrary.Write    ${cmd}
97     ${output}=    SSHLibrary.Read Until    mininet>
98     [Return]    ${output}
99
100 Send Mininet Command Multiple Sessions
101     [Arguments]    ${mininet_conn_list}    ${cmd}=help
102     [Documentation]    Sends Command ${cmd} to Mininet sessions in ${mininet_conn_list} and returns list of read buffer responses.
103     ${output_list}=    Create List
104     : FOR    ${mininet_conn}    IN    @{mininet_conn_list}
105     \    ${output}=    Utils.Send Mininet Command    ${mininet_conn}    ${cmd}
106     \    Append To List    ${output_list}    ${output}
107     [Return]    ${output_list}
108
109 Stop Mininet And Exit
110     [Arguments]    ${mininet_conn}=${EMPTY}
111     [Documentation]    Stops Mininet and exits session ${mininet_conn}
112     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
113     SSHLibrary.Write    exit
114     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
115     SSHLibrary.Close Connection
116
117 Stop Mininet And Exit Multiple Sessions
118     [Arguments]    ${mininet_conn_list}
119     [Documentation]    Stops Mininet and exits sessions in ${mininet_conn_list}.
120     : FOR    ${mininet_conn}    IN    @{mininet_conn_list}
121     \    MininetKeywords.Stop Mininet And Exit    ${mininet_conn}
122
123 Verify Aggregate Flow From Mininet Session
124     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0    ${time_out}=0s
125     [Documentation]    Verify flow count per switch
126     Wait Until Keyword Succeeds    ${time_out}    2s    MininetKeywords.Check Flows In Mininet    ${mininet_conn}    ${flow_count}
127
128 Check Flows In Mininet
129     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0
130     [Documentation]    Sync with mininet to match exact number of flows
131     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
132     ${cmd} =    Set Variable    dpctl dump-aggregate -O OpenFlow13
133     ${output}=    MininetKeywords.Send Mininet Command    ${mininet_conn}    ${cmd}
134     ${flows}=    String.Get RegExp Matches    ${output}    (?<=flow_count\=).*?(?=\r)
135     ${total_flows}=    BuiltIn.Evaluate    sum(map(int, ${flows}))
136     Should Be Equal As Numbers    ${total_flows}    ${flow_count}
137
138 Verify Mininet Ping
139     [Arguments]    ${host1}    ${host2}
140     [Documentation]    Send ping from mininet and verify connectivity.
141     SSHLibrary.Write    ${host1} ping -w 3 ${host2}
142     ${result}=    SSHLibrary.Read Until    mininet>
143     Should Contain    ${result}    64 bytes
144
145 Verify Mininet No Ping
146     [Arguments]    ${host1}    ${host2}
147     [Documentation]    Send ping from mininet and verify no conectivity.
148     SSHLibrary.Write    ${host1} ping -w 3 ${host2}
149     ${result}=    SSHLibrary.Read Until    mininet>
150     Should Contain    ${result}    100% packet loss
151
152 Ping All Hosts
153     [Arguments]    @{host_list}
154     [Documentation]    Do one round of ping from one host to all other hosts in mininet.
155     ...    Note that a single ping failure will exit the loop and return a non zero value.
156     ${source}=    Get From List    ${host_list}    ${0}
157     : FOR    ${h}    IN    @{host_list}
158     \    ${status}=    Ping Two Hosts    ${source}    ${h}    1
159     \    Exit For Loop If    ${status}!=${0}
160     [Return]    ${status}
161
162 Ping Two Hosts
163     [Arguments]    ${host1}    ${host2}    ${pingcount}=2
164     [Documentation]    Ping between mininet hosts. Must be used only after a mininet session is in place.
165     ...    Returns non zero value if there is 100% packet loss.
166     SSHLibrary.Write    ${host1} ping -c ${pingcount} ${host2}
167     ${out}=    SSHLibrary.Read Until    mininet>
168     ${ret}=    String.Get Lines Matching Regexp    ${out}    .*100% packet loss.*
169     ${len}=    Get Length    ${ret}
170     [Return]    ${len}
171
172 Get Mininet Hosts
173     [Documentation]    Get all the hosts from mininet
174     ${host_list}=    Create List
175     SSHLibrary.Write    nodes
176     ${out}=    SSHLibrary.Read Until    mininet>
177     @{words}=    String.Split String    ${out}    ${SPACE}
178     : FOR    ${item}    IN    @{words}
179     \    ${h}=    String.Get Lines Matching Regexp    ${item}    h[0-9]*
180     \    Run Keyword If    '${h}' != '${EMPTY}'    Append To List    ${host_list}    ${h}
181     [Return]    ${host_list}