COE-43: Check for stale veth ports
[integration/test.git] / csit / libraries / SxpClusterLib.robot
1 *** Settings ***
2 Documentation     Library containing Keywords used for SXP cluster testing
3 Library           RequestsLibrary
4 Resource          ./ClusterManagement.robot
5 Resource          ./SetupUtils.robot
6 Resource          ./SxpLib.robot
7
8 *** Variables ***
9 @{SHARD_OPER_LIST}    inventory    topology    default    entity-ownership
10 @{SHARD_CONF_LIST}    inventory    topology    default
11 @{SXP_PACKAGE}    org.opendaylight.sxp
12 ${DEVICE_SESSION}    device_1
13 ${CONTROLLER_SESSION}    ClusterManagement__session_1
14 ${VIRTUAL_IP}     ${TOOLS_SYSTEM_2_IP}
15 ${VIRTUAL_IP_MASK}    255.255.255.0
16 ${VIRTUAL_INTERFACE}    dummy0
17 ${MAC_ADDRESS_TABLE}    &{EMPTY}
18 ${DEVICE_NODE_ID}    ${TOOLS_SYSTEM_IP}
19 ${CLUSTER_NODE_ID}    ${TOOLS_SYSTEM_2_IP}
20 ${INADDR_ANY}     0.0.0.0
21
22 *** Keywords ***
23 Setup SXP Cluster Session
24     [Documentation]    Create sessions asociated with SXP cluster setup
25     ClusterManagement.ClusterManagement_Setup
26     SetupUtils.Setup_Utils_For_Setup_And_Teardown
27     SetupUtils.Setup_Logging_For_Debug_Purposes_On_List_Or_All    DEBUG    ${SXP_PACKAGE}
28
29 Setup Device Session
30     [Documentation]    Create session on the SXP device
31     RequestsLibrary.Create Session    ${DEVICE_SESSION}    url=http://${DEVICE_NODE_ID}:${RESTCONFPORT}    auth=${AUTH}    timeout=${DEFAULT_TIMEOUT_HTTP}    max_retries=0
32
33 Setup SXP Cluster Session With Device
34     [Documentation]    Create sessions asociated with SXP cluster setup and one SXP device
35     Setup SXP Cluster Session
36     Setup Device Session
37
38 Clean SXP Cluster Session
39     [Documentation]    Clean sessions asociated with SXP cluster setup
40     ClusterManagement.Flush_Iptables_From_List_Or_All
41     BuiltIn.Wait Until Keyword Succeeds    60x    1s    ClusterManagement.Verify_Members_Are_Ready    member_index_list=${EMPTY}    verify_cluster_sync=True    verify_restconf=True
42     ...    verify_system_status=False    service_list=@{EMPTY}
43     RequestsLibrary.Delete All Sessions
44     SetupUtils.Setup_Logging_For_Debug_Purposes_On_List_Or_All    INFO    ${SXP_PACKAGE}
45
46 Check Shards Status
47     [Documentation]    Check Status for all shards in SXP application.
48     ClusterManagement.Check_Cluster_Is_In_Sync
49     ClusterManagement.Verify_Leader_Exists_For_Each_Shard    shard_name_list=${SHARD_OPER_LIST}    shard_type=operational
50     ClusterManagement.Verify_Leader_Exists_For_Each_Shard    shard_name_list=${SHARD_CONF_LIST}    shard_type=config
51
52 Setup SXP Cluster
53     [Arguments]    ${peer_mode}=listener
54     [Documentation]    Setup and connect SXP cluster topology
55     SxpLib.Add Node    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
56     BuiltIn.Wait Until Keyword Succeeds    240x    1s    SxpLib.Check Node Started    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
57     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
58     \    SxpLib.Add Connection    version4    ${peer_mode}    ${ODL_SYSTEM_${i+1}_IP}    64999    node=${DEVICE_NODE_ID}
59     \    ...    session=${DEVICE_SESSION}
60     ${cluster_mode} =    Sxp.Get Opposing Mode    ${peer_mode}
61     SxpLib.Add Node    ${INADDR_ANY}    session=${CONTROLLER_SESSION}
62     BuiltIn.Wait Until Keyword Succeeds    240x    1s    Check Cluster Node Started    ${INADDR_ANY}    ip=${EMPTY}
63     SxpLib.Add Connection    version4    ${cluster_mode}    ${DEVICE_NODE_ID}    64999    ${INADDR_ANY}    session=${CONTROLLER_SESSION}
64     BuiltIn.Wait Until Keyword Succeeds    480x    1s    Check Device is Connected    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
65
66 Clean SXP Cluster
67     [Documentation]    Disconnect SXP cluster topology
68     SxpLib.Delete Node    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
69     BuiltIn.Wait Until Keyword Succeeds    240x    1s    SxpLib.Check Node Stopped    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
70     BuiltIn.Wait Until Keyword Succeeds    60x    1s    SxpLib.Delete Node    ${INADDR_ANY}    session=${CONTROLLER_SESSION}
71     BuiltIn.Wait Until Keyword Succeeds    240x    1s    SxpClusterLib.Check Cluster Node Stopped    ${INADDR_ANY}    ip=${EMPTY}
72
73 Check Cluster Node Started
74     [Arguments]    ${node}    ${port}=64999    ${ip}=${node}
75     [Documentation]    Verify that SxpNode has data written to Operational datastore and Node is running on one of cluster nodes
76     ${resp} =    RequestsLibrary.Get Request    ${CONTROLLER_SESSION}    /restconf/operational/network-topology:network-topology/topology/sxp/node/${node}/
77     BuiltIn.Should Be Equal As Strings    ${resp.status_code}    200
78     ${started} =    BuiltIn.Set Variable    ${False}
79     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
80     \    ${rc} =    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    netstat -tln | grep -q ${ip}:${port} && echo 0 || echo 1    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
81     \    ...    prompt=${ODL_SYSTEM_PROMPT}
82     \    ${started} =    BuiltIn.Set Variable If    '${rc}' == '0'    ${True}    ${started}
83     BuiltIn.Should Be True    ${started}
84
85 Check Cluster Node Stopped
86     [Arguments]    ${node}    ${port}=64999    ${ip}=${node}
87     [Documentation]    Verify that SxpNode has data removed from Operational datastore and Node is stopped
88     ${resp} =    RequestsLibrary.Get Request    ${CONTROLLER_SESSION}    /restconf/operational/network-topology:network-topology/topology/sxp/node/${node}/
89     BuiltIn.Should Be Equal As Strings    ${resp.status_code}    404
90     ${stopped} =    BuiltIn.Set Variable    ${False}
91     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
92     \    ${rc} =    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    netstat -tln | grep -q ${ip}:${port} && echo 0 || echo 1    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
93     \    ...    prompt=${ODL_SYSTEM_PROMPT}
94     \    ${stopped} =    BuiltIn.Set Variable If    '${rc}' == '1'    ${True}    ${stopped}
95     BuiltIn.Should Be True    ${stopped}
96
97 Check Device is Connected
98     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${session}=session
99     [Documentation]    Checks if SXP device is connected to the cluster. It means it has connection in state "on" with one of the cluster members.
100     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
101     ${is_connected} =    BuiltIn.Set Variable    ${False}
102     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
103     \    ${is_connected} =    Sxp.Find Connection    ${resp}    ${version}    any    ${ODL_SYSTEM_${i+1}_IP}
104     \    ...    ${port}    on
105     \    BuiltIn.Exit For Loop If    ${is_connected}
106     BuiltIn.Should Be True    ${is_connected}
107
108 Check Cluster is Connected
109     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${mode}=speaker    ${session}=session
110     [Documentation]    Get SXP connections of cluster ${node} and verify that they contain a connection to the device ${DEVICE_NODE_ID} in state "on"
111     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
112     SxpLib.Should Contain Connection    ${resp}    ${DEVICE_NODE_ID}    ${port}    ${mode}    ${version}    on
113
114 Get Owner Controller
115     [Arguments]    ${running_member}=1
116     [Documentation]    Find cluster controller that is marked as cluster owner by requesting ownership data from ${running_member} node of the cluster
117     ${owner}    ${candidates} =    BuiltIn.Wait Until Keyword Succeeds    60x    1s    ClusterManagement.Get_Owner_And_Successors_For_Device    org.opendaylight.sxp.controller.boot.SxpControllerInstance
118     ...    Sxp    ${running_member}
119     [Return]    ${owner}
120
121 Get Not Owner Controller
122     [Documentation]    Find cluster controller that is not marked as owner for SXP service in cluster
123     ${owner_controller} =    Get Owner Controller
124     ${controller} =    BuiltIn.Evaluate    random.choice( filter( lambda i: i!=${owner_controller}, range(1, ${NUM_ODL_SYSTEM} + 1)))    random
125     [Return]    ${controller}
126
127 Get Any Controller
128     [Documentation]    Get any controller from cluster range
129     ${follower} =    BuiltIn.Evaluate    random.choice( range(1, ${NUM_ODL_SYSTEM} + 1))    random
130     [Return]    ${follower}
131
132 Map Followers To Mac Addresses
133     [Documentation]    Creates Map containing ODL_SYSTEM_IP to corresponding MAC-ADDRESS
134     ${mac_addresses} =    BuiltIn.Create dictionary
135     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
136     \    ${mac_address}    Find Mac Address Of Ip Address    ${ODL_SYSTEM_${i+1}_IP}
137     \    Collections.Set To Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${i+1}_IP}    ${mac_address}
138     BuiltIn.Log    ${mac_addresses}
139     [Return]    ${mac_addresses}
140
141 Find Mac Address Of Ip Address
142     [Arguments]    ${ip}
143     [Documentation]    Finds out MAC-ADDRESS of specified IP by pinging it from TOOLS_SYSTEM machine
144     ${mac_address} =    Utils.Run Command On Remote System And Log    ${TOOLS_SYSTEM_IP}    ping -c 10 -W 10 ${ip} >/dev/null && sudo ip neighbor show ${ip} | awk '{print $5}'    ${TOOLS_SYSTEM_USER}    ${TOOLS_SYSTEM_PASSWORD}
145     [Return]    ${mac_address}
146
147 Ip Addres Should Not Be Routed To Follower
148     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
149     [Documentation]    Verify that IP-ADDRESS is not routed to follower specified by ID
150     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
151     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
152     BuiltIn.Should Not Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
153
154 Ip Addres Should Be Routed To Follower
155     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
156     [Documentation]    Verify that IP-ADDRESS is routed to follower specified by ID
157     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
158     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
159     BuiltIn.Should Not Be Empty    ${mac_address_resolved}
160     BuiltIn.Should Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
161
162 Shutdown Tools Node
163     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_2_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${passwd}=${TOOLS_SYSTEM_PASSWORD}
164     [Documentation]    Shutdown Tools node to avoid conflict in resolving virtual ip that is overlaping that node.
165     ${rc} =    OperatingSystem.Run And Return Rc    ping -q -c 3 ${ip_address}
166     ${stdout} =    BuiltIn.Run Keyword And Return If    ${rc} == 0    Utils.Run Command On Remote System    ${ip_address}    sudo shutdown -P 0    ${user}
167     ...    ${passwd}
168     BuiltIn.Log    ${stdout}
169
170 Create Virtual Interface
171     [Documentation]    Create virtual interface on all of the cluster nodes
172     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
173     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo modprobe dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
174     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
175
176 Delete Virtual Interface
177     [Documentation]    Create virtual interface on all of the cluster nodes
178     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
179     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link delete ${VIRTUAL_INTERFACE} type dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
180     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo rmmod dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
181     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}