SXP: Fix clustering tests
[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 ${SXP_LOG_LEVEL}    INFO
15 ${VIRTUAL_IP}     ${TOOLS_SYSTEM_2_IP}
16 ${VIRTUAL_IP_MASK}    255.255.255.0
17 ${VIRTUAL_INTERFACE}    eth0
18 ${MAC_ADDRESS_TABLE}    &{EMPTY}
19 ${DEVICE_NODE_ID}    ${TOOLS_SYSTEM_IP}
20 ${CLUSTER_NODE_ID}    ${TOOLS_SYSTEM_2_IP}
21 ${INADDR_ANY}     0.0.0.0
22
23 *** Keywords ***
24 Setup SXP Cluster Session
25     [Documentation]    Create sessions asociated with SXP cluster setup
26     ClusterManagement.ClusterManagement_Setup
27     SetupUtils.Setup_Utils_For_Setup_And_Teardown
28     SetupUtils.Setup_Logging_For_Debug_Purposes_On_List_Or_All    ${SXP_LOG_LEVEL}    ${SXP_PACKAGE}
29
30 Setup Device Session
31     [Documentation]    Create session on the SXP device
32     RequestsLibrary.Create Session    ${DEVICE_SESSION}    url=http://${DEVICE_NODE_ID}:${RESTCONFPORT}    auth=${AUTH}    timeout=${DEFAULT_TIMEOUT_HTTP}    max_retries=0
33
34 Setup SXP Cluster Session With Device
35     [Documentation]    Create sessions asociated with SXP cluster setup and one SXP device
36     Setup SXP Cluster Session
37     Setup Device Session
38
39 Clean SXP Cluster Session
40     [Documentation]    Clean sessions asociated with SXP cluster setup
41     ClusterManagement.Flush_Iptables_From_List_Or_All
42     ClusterManagement.Check_Cluster_Is_In_Sync
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     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
57     \    SxpLib.Add Connection    version4    ${peer_mode}    ${ODL_SYSTEM_${i+1}_IP}    64999    node=${DEVICE_NODE_ID}
58     \    ...    session=${DEVICE_SESSION}
59     ${cluster_mode} =    Sxp.Get Opposing Mode    ${peer_mode}
60     SxpLib.Add Node    ${INADDR_ANY}    session=${CONTROLLER_SESSION}
61     SxpLib.Add Connection    version4    ${cluster_mode}    ${DEVICE_NODE_ID}    64999    ${INADDR_ANY}    session=${CONTROLLER_SESSION}
62     BuiltIn.Wait Until Keyword Succeeds    1m    1x    SxpLib.Check Node Started    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
63     BuiltIn.Wait Until Keyword Succeeds    1m    1x    Check Cluster Node started    ${INADDR_ANY}    ip=${EMPTY}
64     BuiltIn.Wait Until Keyword Succeeds    4m    1x    Check Device is Connected    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
65
66 Clean SXP Cluster
67     [Documentation]    Disconnect SXP cluster topology
68     ClusterManagement.Flush_Iptables_From_List_Or_All
69     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
70     \    BuiltIn.Wait Until Keyword Succeeds    240    1    ClusterManagement.Sync_Status_Should_Be_True    ${i+1}
71     SxpLib.Delete Node    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
72     SxpLib.Delete Node    ${INADDR_ANY}    session=${CONTROLLER_SESSION}
73
74 Check Cluster Node started
75     [Arguments]    ${node}    ${port}=64999    ${ip}=${node}
76     [Documentation]    Verify that SxpNode has data written to Operational datastore and Node is running on one of cluster nodes
77     ${started} =    BuiltIn.Set Variable    ${False}
78     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
79     \    ${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}
80     \    ...    prompt=${ODL_SYSTEM_PROMPT}
81     \    ${started} =    BuiltIn.Set Variable If    '${rc}' == '0'    ${True}    ${started}
82     BuiltIn.Should Be True    ${started}
83
84 Check Device is Connected
85     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${session}=session
86     [Documentation]    Checks if SXP device is connected to the cluster. It means it has connection in state "on" with one of the cluster members.
87     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
88     ${is_connected} =    BuiltIn.Set Variable    ${False}
89     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
90     \    ${is_connected} =    Sxp.Find Connection    ${resp}    ${version}    any    ${ODL_SYSTEM_${i+1}_IP}
91     \    ...    ${port}    on
92     \    BuiltIn.Exit For Loop If    ${is_connected}
93     BuiltIn.Should Be True    ${is_connected}
94
95 Check Cluster is Connected
96     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${mode}=speaker    ${session}=session
97     [Documentation]    Get SXP connections from cluster and verify that they contain a connection to the device in state "on"
98     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
99     SxpLib.Should Contain Connection    ${resp}    ${DEVICE_NODE_ID}    ${port}    ${mode}    ${version}    on
100
101 Get Owner Controller
102     [Arguments]    ${running_member}=1
103     [Documentation]    Find cluster controller that is marked as cluster owner by requesting ownership data from ${running_member} node of the cluster
104     ${owner}    ${candidates} =    BuiltIn.Wait_Until_Keyword_Succeeds    5x    2s    ClusterManagement.Get_Owner_And_Successors_For_Device    org.opendaylight.sxp.controller.boot.SxpControllerInstance
105     ...    Sxp    ${running_member}
106     [Return]    ${owner}
107
108 Get Not Owner Controller
109     [Documentation]    Find cluster controller that is not marked as owner for SXP service in cluster
110     ${owner_controller} =    Get Owner Controller
111     ${controller} =    BuiltIn.Evaluate    random.choice( filter( lambda i: i!=${owner_controller}, range(1, ${NUM_ODL_SYSTEM} + 1)))    random
112     [Return]    ${controller}
113
114 Get Any Controller
115     [Documentation]    Get any controller from cluster range
116     ${follower} =    BuiltIn.Evaluate    random.choice( range(1, ${NUM_ODL_SYSTEM} + 1))    random
117     [Return]    ${follower}
118
119 Map Followers To Mac Addresses
120     [Documentation]    Creates Map containing ODL_SYSTEM_IP to corresponding MAC-ADDRESS
121     ${mac_addresses} =    BuiltIn.Create dictionary
122     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
123     \    ${mac_address}    Find Mac Address Of Ip Address    ${ODL_SYSTEM_${i+1}_IP}
124     \    Collections.Set To Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${i+1}_IP}    ${mac_address}
125     BuiltIn.Log    ${mac_addresses}
126     [Return]    ${mac_addresses}
127
128 Find Mac Address Of Ip Address
129     [Arguments]    ${ip}
130     [Documentation]    Finds out MAC-ADDRESS of specified IP by pinging it from TOOLS_SYSTEM machine
131     ${mac_address} =    Utils.Run Command On Remote System    ${TOOLS_SYSTEM_IP}    ping -c 1 -W 1 ${ip} >/dev/null && arp -n | grep ${ip} | awk '{print $3}'    ${TOOLS_SYSTEM_USER}    ${TOOLS_SYSTEM_PASSWORD}
132     [Return]    ${mac_address}
133
134 Ip Addres Should Not Be Routed To Follower
135     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
136     [Documentation]    Verify that IP-ADDRESS is not routed to follower specified by ID
137     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
138     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
139     BuiltIn.Should Not Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
140
141 Ip Addres Should Be Routed To Follower
142     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
143     [Documentation]    Verify that IP-ADDRESS is routed to follower specified by ID
144     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
145     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
146     BuiltIn.Should Not Be Empty    ${mac_address_resolved}
147     BuiltIn.Should Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
148
149 Shutdown Tools Node
150     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_2_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${passwd}=${TOOLS_SYSTEM_PASSWORD}
151     [Documentation]    Shutdown Tools node to avoid conflict in resolving virtual ip that is overlaping that node.
152     ${rc} =    OperatingSystem.Run And Return Rc    ping -q -c 3 ${ip_address}
153     ${stdout} =    BuiltIn.Run Keyword And Return If    ${rc} == 0    Utils.Run Command On Remote System    ${ip_address}    sudo shutdown -P 0    ${user}
154     ...    ${passwd}
155     BuiltIn.Log    ${stdout}
156
157 Create Virtual Interface
158     [Documentation]    Create virtual interface on all of the cluster nodes
159     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
160     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo modprobe dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
161     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link set name ${VIRTUAL_INTERFACE} dev dummy0    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
162     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
163
164 Delete Virtual Interface
165     [Documentation]    Create virtual interface on all of the cluster nodes
166     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
167     \    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}
168     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo rmmod dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
169     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}