e09f1e2e54779dbfbe9008ea357e1796628cb125
[integration/test.git] / csit / libraries / OVSDB.robot
1 *** Settings ***
2 Library           SSHLibrary
3 Library           String
4 Library           Collections
5 Library           RequestsLibrary
6 Library           ipaddress
7 Resource          Utils.robot
8 Resource          ClusterManagement.robot
9 Resource          ${CURDIR}/TemplatedRequests.robot
10 Variables         ../variables/Variables.py
11
12 *** Variables ***
13 ${OVSDB_CONFIG_DIR}    ../variables/ovsdb
14 ${SOUTHBOUND_CONFIG_API}    ${CONFIG_TOPO_API}/topology/ovsdb:1/node/ovsdb:%2F%2F
15
16 *** Keywords ***
17 Connect To Ovsdb Node
18     [Arguments]    ${mininet_ip}
19     [Documentation]    This will Initiate the connection to OVSDB node from controller
20     ${sample}    OperatingSystem.Get File    ${OVSDB_CONFIG_DIR}/connect.json
21     ${sample1}    Replace String    ${sample}    127.0.0.1    ${mininet_ip}
22     ${body}    Replace String    ${sample1}    61644    ${OVSDB_PORT}
23     Log    URL is ${SOUTHBOUND_CONFIG_API}${mininet_ip}:${OVSDB_PORT}
24     Log    data: ${body}
25     ${resp}    RequestsLibrary.Put Request    session    ${SOUTHBOUND_CONFIG_API}${mininet_ip}:${OVSDB_PORT}    data=${body}
26     Log    ${resp.content}
27     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
28
29 Disconnect From Ovsdb Node
30     [Arguments]    ${mininet_ip}
31     [Documentation]    This request will disconnect the OVSDB node from the controller
32     ${resp}    RequestsLibrary.Delete Request    session    ${SOUTHBOUND_CONFIG_API}${mininet_ip}:${OVSDB_PORT}
33     Should Be Equal As Strings    ${resp.status_code}    200
34
35 Add Bridge To Ovsdb Node
36     [Arguments]    ${mininet_ip}    ${bridge_num}    ${datapath_id}
37     [Documentation]    This will create a bridge and add it to the OVSDB node.
38     ${sample}    OperatingSystem.Get File    ${OVSDB_CONFIG_DIR}/create_bridge.json
39     ${sample1}    Replace String    ${sample}    tcp:127.0.0.1:6633    tcp:${ODL_SYSTEM_IP}:6633
40     ${sample2}    Replace String    ${sample1}    127.0.0.1    ${mininet_ip}
41     ${sample3}    Replace String    ${sample2}    br01    ${bridge_num}
42     ${sample4}    Replace String    ${sample3}    61644    ${OVSDB_PORT}
43     ${body}    Replace String    ${sample4}    0000000000000001    ${datapath_id}
44     Log    URL is ${SOUTHBOUND_CONFIG_API}${mininet_ip}:${OVSDB_PORT}%2Fbridge%2F${bridge_num}
45     Log    data: ${body}
46     ${resp}    RequestsLibrary.Put Request    session    ${SOUTHBOUND_CONFIG_API}${mininet_ip}:${OVSDB_PORT}%2Fbridge%2F${bridge_num}    data=${body}
47     Log    ${resp.content}
48     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
49
50 Delete Bridge From Ovsdb Node
51     [Arguments]    ${mininet_ip}    ${bridge_num}
52     [Documentation]    This request will delete the bridge node from the OVSDB
53     ${resp}    RequestsLibrary.Delete Request    session    ${SOUTHBOUND_CONFIG_API}${mininet_ip}:${OVSDB_PORT}%2Fbridge%2F${bridge_num}
54     Should Be Equal As Strings    ${resp.status_code}    200
55
56 Add Vxlan To Bridge
57     [Arguments]    ${mininet_ip}    ${bridge_num}    ${vxlan_port}    ${remote_ip}    ${custom_port}=create_port.json
58     [Documentation]    This request will create vxlan port for vxlan tunnel and attach it to the specific bridge
59     Add Termination Point    ${mininet_ip}:${OVSDB_PORT}    ${bridge_num}    ${vxlan_port}    ${remote_ip}
60
61 Add Termination Point
62     [Arguments]    ${node_id}    ${bridge_name}    ${tp_name}    ${remote_ip}=${TOOLS_SYSTEM_IP}
63     [Documentation]    Using the json data body file as a template, a REST config request is made to
64     ...    create a termination-point ${tp_name} on ${bridge_name} for the given ${node_id}. The ports
65     ...    remote-ip defaults to ${TOOLS_SYSTEM_IP}
66     ${body}    OperatingSystem.Get File    ${OVSDB_CONFIG_DIR}/create_port.json
67     ${body}    Replace String    ${body}    192.168.0.21    ${remote_ip}
68     ${body}    Replace String    ${body}    vxlanport    ${tp_name}
69     ${uri}=    Set Variable    ${CONFIG_TOPO_API}/topology/ovsdb:1/node/ovsdb:%2F%2F${node_id}%2Fbridge%2F${bridge_name}
70     ${resp}    RequestsLibrary.Put Request    session    ${uri}/termination-point/${tp_name}/    data=${body}
71     Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
72
73 Verify OVS Reports Connected
74     [Arguments]    ${tools_system}=${TOOLS_SYSTEM_IP}
75     [Documentation]    Uses "vsctl show" to check for string "is_connected"
76     ${output}    Verify Ovs-vsctl Output    show    is_connected    ${tools_system}
77     [Return]    ${output}
78
79 Verify Ovs-vsctl Output
80     [Arguments]    ${vsctl_args}    ${expected_output}    ${ovs_system}=${TOOLS_SYSTEM_IP}    ${should_match}=True
81     [Documentation]    A wrapper keyword to make it easier to validate ovs-vsctl output, and gives an easy
82     ...    way to check this output in a WUKS. The argument ${should_match} can control if the match should
83     ...    exist (True} or not (False) or don't care (anything but True or False). ${should_match} is True by default
84     ${output}=    Utils.Run Command On Mininet    ${ovs_system}    sudo ovs-vsctl ${vsctl_args}
85     Log    ${output}
86     Run Keyword If    "${should_match}"=="True"    Should Contain    ${output}    ${expected_output}
87     Run Keyword If    "${should_match}"=="False"    Should Not Contain    ${output}    ${expected_output}
88     [Return]    ${output}
89
90 Get OVSDB UUID
91     [Arguments]    ${ovs_system_ip}=${TOOLS_SYSTEM_IP}    ${controller_http_session}=session
92     [Documentation]    Queries the topology in the operational datastore and searches for the node that has
93     ...    the ${ovs_system_ip} argument as the "remote-ip". If found, the value returned will be the value of
94     ...    node-id stripped of "ovsdb://uuid/". If not found, ${EMPTY} will be returned.
95     ${uuid}=    Set Variable    ${EMPTY}
96     ${resp}=    RequestsLibrary.Get Request    ${controller_http_session}    ${OPERATIONAL_TOPO_API}/topology/ovsdb:1
97     Log    ${resp.content}
98     Should Be Equal As Strings    ${resp.status_code}    200
99     ${resp_json}=    To Json    ${resp.content}
100     ${topologies}=    Get From Dictionary    ${resp_json}    topology
101     ${topology}=    Get From List    ${topologies}    0
102     ${node_list}=    Get From Dictionary    ${topology}    node
103     Log    ${node_list}
104     # Since bridges are also listed as nodes, but will not have the extra "ovsdb:connection-info data,
105     # we need to use "Run Keyword And Ignore Error" below.
106     : FOR    ${node}    IN    @{node_list}
107     \    ${node_id}=    Get From Dictionary    ${node}    node-id
108     \    ${node_uuid}=    Replace String    ${node_id}    ovsdb://uuid/    ${EMPTY}
109     \    ${status}    ${connection_info}    Run Keyword And Ignore Error    Get From Dictionary    ${node}    ovsdb:connection-info
110     \    ${status}    ${remote_ip}    Run Keyword And Ignore Error    Get From Dictionary    ${connection_info}    remote-ip
111     \    ${uuid}=    Set Variable If    '${remote_ip}' == '${ovs_system_ip}'    ${node_uuid}    ${uuid}
112     [Return]    ${uuid}
113
114 Collect OVSDB Debugs
115     [Arguments]    ${switch}=br-int
116     [Documentation]    Used to log useful test debugs for OVSDB related system tests.
117     ${output}=    Utils.Run Command On Mininet    ${TOOLS_SYSTEM_IP}    sudo ovs-vsctl show
118     Log    ${output}
119     ${output}=    Utils.Run Command On Mininet    ${TOOLS_SYSTEM_IP}    sudo ovs-ofctl -O OpenFlow13 dump-flows ${switch} | cut -d',' -f3-
120     Log    ${output}
121
122 Clean OVSDB Test Environment
123     [Arguments]    ${tools_system}=${TOOLS_SYSTEM_IP}
124     [Documentation]    General Use Keyword attempting to sanitize test environment for OVSDB related
125     ...    tests. Not every step will always be neccessary, but should not cause any problems for
126     ...    any new ovsdb test suites.
127     Utils.Clean Mininet System    ${tools_system}
128     Utils.Run Command On Mininet    ${tools_system}    sudo ovs-vsctl del-manager
129     Utils.Run Command On Mininet    ${tools_system}    sudo /usr/share/openvswitch/scripts/ovs-ctl stop
130     Utils.Run Command On Mininet    ${tools_system}    sudo rm -rf /etc/openvswitch/conf.db
131     Utils.Run Command On Mininet    ${tools_system}    sudo /usr/share/openvswitch/scripts/ovs-ctl start
132
133 Restart OVSDB
134     [Arguments]    ${ovs_ip}
135     [Documentation]    Restart the OVS node without cleaning the current configuration.
136     ${output} =    Utils.Run Command On Mininet    ${ovs_ip}    sudo /usr/share/openvswitch/scripts/ovs-ctl stop
137     Log    ${output}
138     ${output} =    Utils.Run Command On Mininet    ${ovs_ip}    sudo /usr/share/openvswitch/scripts/ovs-ctl start
139     Log    ${output}
140
141 Set Controller In OVS Bridge
142     [Arguments]    ${tools_system}    ${bridge}    ${controller_opt}    ${ofversion}=13
143     [Documentation]    Sets controller for the OVS bridge ${bridge} using ${controller_opt} and OF version ${ofversion}.
144     Utils.Run Command On Mininet    ${tools_system}    sudo ovs-vsctl set bridge ${bridge} protocols=OpenFlow${ofversion}
145     Utils.Run Command On Mininet    ${tools_system}    sudo ovs-vsctl set-controller ${bridge} ${controller_opt}
146
147 Check OVS OpenFlow Connections
148     [Arguments]    ${tools_system}    ${of_connections}
149     [Documentation]    Check OVS instance with IP ${tools_system} has ${of_connections} OpenFlow connections.
150     ${output}=    Utils.Run Command On Mininet    ${tools_system}    sudo ovs-vsctl show
151     Log    ${output}
152     BuiltIn.Should Contain X Times    ${output}    is_connected    ${of_connections}
153
154 Add Multiple Managers to OVS
155     [Arguments]    ${tools_system}=${TOOLS_SYSTEM_IP}    ${controller_index_list}=${EMPTY}    ${ovs_mgr_port}=6640
156     [Documentation]    Connect OVS to the list of controllers in the ${controller_index_list} or all if no list is provided.
157     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${controller_index_list}
158     Log    Clear any existing mininet
159     Utils.Clean Mininet System    ${tools_system}
160     ${ovs_opt}=    Set Variable
161     : FOR    ${index}    IN    @{index_list}
162     \    ${ovs_opt}=    Catenate    ${ovs_opt}    ${SPACE}tcp:${ODL_SYSTEM_${index}_IP}:${ovs_mgr_port}
163     \    Log    ${ovs_opt}
164     Log    Configure OVS Managers in the OVS
165     Utils.Run Command On Mininet    ${tools_system}    sudo ovs-vsctl set-manager ${ovs_opt}
166     Log    Check OVS configuration
167     ${output}=    Wait Until Keyword Succeeds    5s    1s    Verify OVS Reports Connected    ${tools_system}
168     Log    ${output}
169     ${controller_index}=    Collections.Get_From_List    ${index_list}    0
170     ${session}=    ClusterManagement.Resolve_Http_Session_For_Member    member_index=${controller_index}
171     ${ovsdb_uuid}=    Wait Until Keyword Succeeds    30s    2s    Get OVSDB UUID    controller_http_session=${session}
172     [Return]    ${ovsdb_uuid}
173
174 Get DPID
175     [Arguments]    ${ip}
176     [Documentation]    Returns the dpnid from the system at the given ip address using ovs-ofctl assuming br-int is present.
177     ${output} =    Run Command On Remote System    ${ip}    sudo ovs-ofctl show -O Openflow13 br-int | head -1 | awk -F "dpid:" '{print $2}'
178     ${dpnid} =    Convert To Integer    ${output}    16
179     Log    ${dpnid}
180     [Return]    ${dpnid}
181
182 Get Subnet
183     [Arguments]    ${ip}
184     [Documentation]    Return the subnet from the system at the given ip address and interface
185     ${output} =    Run Command On Remote System    ${ip}    /usr/sbin/ip addr show | grep ${ip} | cut -d' ' -f6
186     ${interface} =    ipaddress.ip_interface    ${output}
187     ${network}=    Set Variable    ${interface.network.__str__()}
188     [Return]    ${network}
189
190 Get Ethernet Adapter
191     [Arguments]    ${ip}
192     [Documentation]    Returns the ethernet adapter name from the system at the given ip address using ip addr show.
193     ${adapter} =    Run Command On Remote System    ${ip}    /usr/sbin/ip addr show | grep ${ip} | cut -d " " -f 11
194     Log    ${adapter}
195     [Return]    ${adapter}
196
197 Get Default Gateway
198     [Arguments]    ${ip}
199     [Documentation]    Returns the default gateway at the given ip address using route command.
200     ${gateway} =    Run Command On Remote System    ${ip}    /usr/sbin/route -n | grep '^0.0.0.0' | cut -d " " -f 10
201     Log    ${gateway}
202     [Return]    ${gateway}
203
204 Add OVS Logging
205     [Arguments]    ${conn_id}
206     [Documentation]    Add higher levels of OVS logging
207     SSHLibrary.Switch Connection    ${conn_id}
208     @{modules} =    BuiltIn.Create List    bridge:file:dbg    connmgr:file:dbg    inband:file:dbg    ofp_actions:file:dbg    ofp_errors:file:dbg
209     ...    ofp_msgs:file:dbg    ovsdb_error:file:dbg    rconn:file:dbg    tunnel:file:dbg    vconn:file:dbg
210     : FOR    ${module}    IN    @{modules}
211     \    Write Commands Until Expected Prompt    sudo ovs-appctl --target ovs-vswitchd vlog/set ${module}    ${DEFAULT_LINUX_PROMPT_STRICT}
212     Write Commands Until Expected Prompt    sudo ovs-appctl --target ovs-vswitchd vlog/list    ${DEFAULT_LINUX_PROMPT_STRICT}
213
214 Reset OVS Logging
215     [Arguments]    ${conn_id}
216     [Documentation]    Reset the OVS logging
217     SSHLibrary.Switch Connection    ${conn_id}
218     ${output} =    Write Commands Until Expected Prompt    sudo ovs-appctl --target ovs-vswitchd vlog/set :file:info    ${DEFAULT_LINUX_PROMPT_STRICT}