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