ClusterManagement.List_Indices_Or_All
[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 Linear
62     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${mininet_timeout}=${DEFAULT_TIMEOUT}
63     [Documentation]    Start mininet linear topology with ${switches} nodes.
64     Log    Start Mininet Linear
65     MininetKeywords.StartMininet Single Controller    options=--topo linear,${switches} --switch ovsk,protocols=OpenFlow13    timeout=${mininet_timeout}
66
67 Start Mininet Full Mesh
68     [Arguments]    ${switches}    ${mininet}=${TOOLS_SYSTEM_IP}    ${controller}=${ODL_SYSTEM_IP}    ${hosts}=0    ${mininet_timeout}=${DEFAULT_TIMEOUT}
69     [Documentation]    Start a custom mininet topology.
70     ${mininet_conn_id}=    SSHLibrary.Open Connection    ${mininet}    prompt=${TOOLS_SYSTEM_PROMPT}    timeout=${mininet_timeout}
71     Set Suite Variable    ${mininet_conn_id}
72     Utils.Flexible_Mininet_Login
73     Log    Copying ${topology_file_path} file to Mininet VM and Creating Full Mesh topology
74     SSHLibrary.Put File    ${CURDIR}/${topology_file_path}
75     SSHLibrary.Write    python ${topology_file} ${switches} ${hosts} 00:00:00:00:00:00 10.0.0.0
76     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
77     Log    Start Mininet Full Mesh
78     SSHLibrary.Write    sudo mn --controller=remote,ip=${controller} --custom switch.py --topo demotopo --switch ovsk,protocols=OpenFlow13
79     Read Until    mininet>
80     Log    Check OVS configuratiom
81     Write    sh ovs-vsctl show
82     ${output}=    Read Until    mininet>
83     # Ovsdb connection is sometimes lost after mininet is started. Checking if the connection is alive before proceeding.
84     Should Not Contain    ${output}    database connection failed
85
86 Send Mininet Command
87     [Arguments]    ${mininet_conn}=${EMPTY}    ${cmd}=help
88     [Documentation]    Sends Command ${cmd} to Mininet session ${mininet_conn} and returns read buffer response.
89     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
90     SSHLibrary.Write    ${cmd}
91     ${output}=    SSHLibrary.Read Until    mininet>
92     [Return]    ${output}
93
94 Send Mininet Command Multiple Sessions
95     [Arguments]    ${mininet_conn_list}    ${cmd}=help
96     [Documentation]    Sends Command ${cmd} to Mininet sessions in ${mininet_conn_list} and returns list of read buffer responses.
97     ${output_list}=    Create List
98     : FOR    ${mininet_conn}    IN    @{mininet_conn_list}
99     \    ${output}=    Utils.Send Mininet Command    ${mininet_conn}    ${cmd}
100     \    Append To List    ${output_list}    ${output}
101     [Return]    ${output_list}
102
103 Stop Mininet And Exit
104     [Arguments]    ${mininet_conn}=${EMPTY}
105     [Documentation]    Stops Mininet and exits session ${mininet_conn}
106     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
107     SSHLibrary.Write    exit
108     SSHLibrary.Read Until    ${TOOLS_SYSTEM_PROMPT}
109     SSHLibrary.Close Connection
110
111 Stop Mininet And Exit Multiple Sessions
112     [Arguments]    ${mininet_conn_list}
113     [Documentation]    Stops Mininet and exits sessions in ${mininet_conn_list}.
114     : FOR    ${mininet_conn}    IN    @{mininet_conn_list}
115     \    MininetKeywords.Stop Mininet And Exit    ${mininet_conn}
116
117 Verify Aggregate Flow From Mininet Session
118     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0    ${time_out}=0s
119     [Documentation]    Verify flow count per switch
120     Wait Until Keyword Succeeds    ${time_out}    2s    MininetKeywords.Check Flows In Mininet    ${mininet_conn}    ${flow_count}
121
122 Check Flows In Mininet
123     [Arguments]    ${mininet_conn}=${EMPTY}    ${flow_count}=0
124     [Documentation]    Sync with mininet to match exact number of flows
125     Run Keyword If    """${mininet_conn}""" != ""    SSHLibrary.Switch Connection    ${mininet_conn}
126     ${cmd} =    Set Variable    dpctl dump-aggregate -O OpenFlow13
127     ${output}=    MininetKeywords.Send Mininet Command    ${mininet_conn}    ${cmd}
128     ${flows}=    String.Get RegExp Matches    ${output}    (?<=flow_count\=).*?(?=\r)
129     ${total_flows}=    BuiltIn.Evaluate    sum(map(int, ${flows}))
130     Should Be Equal As Numbers    ${total_flows}    ${flow_count}
131
132 Verify Mininet Ping
133     [Arguments]    ${host1}    ${host2}
134     [Documentation]    Send ping from mininet and verify connectivity.
135     Write    ${host1} ping -w 3 ${host2}
136     ${result}=    Read Until    mininet>
137     Should Contain    ${result}    64 bytes
138
139 Verify Mininet No Ping
140     [Arguments]    ${host1}    ${host2}
141     [Documentation]    Send ping from mininet and verify no conectivity.
142     Write    ${host1} ping -w 3 ${host2}
143     ${result}=    Read Until    mininet>
144     Should Contain    ${result}    100% packet loss