SXP: Get leader only from running members
[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 Resource          ../variables/Variables.robot
8
9 *** Variables ***
10 @{SHARD_OPER_LIST}    inventory    topology    default    entity-ownership
11 @{SHARD_CONF_LIST}    inventory    topology    default
12 @{SXP_PACKAGE}    org.opendaylight.sxp
13 ${DEVICE_SESSION}    device_1
14 ${DEVICE_NODE_ID}    1.1.1.1
15 ${CLUSTER_NODE_ID}    2.2.2.2
16 ${SXP_LOG_LEVEL}    INFO
17 ${VIRTUAL_IP}     ${TOOLS_SYSTEM_2_IP}
18 ${VIRTUAL_IP_MASK}    255.255.255.0
19 ${VIRTUAL_INTERFACE}    eth0
20 ${MAC_ADDRESS_TABLE}    &{EMPTY}
21
22 *** Keywords ***
23 Setup SXP Cluster Session
24     [Documentation]    Create sessions asociated with SXP cluster setup
25     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
26     \    BuiltIn.Wait Until Keyword Succeeds    120    10    SxpLib.Prepare SSH Keys On Karaf    ${ODL_SYSTEM_${i+1}_IP}
27     \    SxpLib.Setup SXP Session    controller${i+1}    ${ODL_SYSTEM_${i+1}_IP}
28     ClusterManagement.ClusterManagement_Setup
29     SetupUtils.Setup_Utils_For_Setup_And_Teardown
30     SetupUtils.Setup_Logging_For_Debug_Purposes_On_List_Or_All    ${SXP_LOG_LEVEL}    ${SXP_PACKAGE}
31
32 Clean SXP Cluster Session
33     [Documentation]    Clean sessions asociated with SXP cluster setup
34     ClusterManagement.Flush_Iptables_From_List_Or_All
35     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
36     \    BuiltIn.Wait Until Keyword Succeeds    240    1    ClusterManagement.Sync_Status_Should_Be_True    ${i+1}
37     SxpLib.Clean SXP Session
38     SetupUtils.Setup_Logging_For_Debug_Purposes_On_List_Or_All    INFO    ${SXP_PACKAGE}
39
40 Check Shards Status
41     [Documentation]    Check Status for all shards in SXP application.
42     ClusterManagement.Check_Cluster_Is_In_Sync
43     ClusterManagement.Verify_Leader_Exists_For_Each_Shard    shard_name_list=${SHARD_OPER_LIST}    shard_type=operational
44     ClusterManagement.Verify_Leader_Exists_For_Each_Shard    shard_name_list=${SHARD_CONF_LIST}    shard_type=config
45
46 Setup SXP Cluster
47     [Arguments]    ${peer_mode}=listener
48     [Documentation]    Setup and connect SXP cluster topology
49     SxpLib.Add Node    ${DEVICE_NODE_ID}    ip=0.0.0.0    session=${DEVICE_SESSION}
50     BuiltIn.Wait Until Keyword Succeeds    20    1    SxpLib.Check Node Started    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}    system=${TOOLS_SYSTEM_IP}
51     ...    ip=${EMPTY}
52     ${cluster_mode} =    Sxp.Get Opposing Mode    ${peer_mode}
53     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
54     \    SxpLib.Add Connection    version4    ${peer_mode}    ${ODL_SYSTEM_${i+1}_IP}    64999    ${DEVICE_NODE_ID}
55     \    ...    session=${DEVICE_SESSION}
56     ${controller_id} =    Get Any Controller
57     SxpLib.Add Node    ${CLUSTER_NODE_ID}    ip=0.0.0.0    session=controller${controller_id}
58     BuiltIn.Wait Until Keyword Succeeds    20    1    Check Cluster Node started    ${CLUSTER_NODE_ID}
59     SxpLib.Add Connection    version4    ${cluster_mode}    ${TOOLS_SYSTEM_IP}    64999    ${CLUSTER_NODE_ID}    session=controller${controller_id}
60     BuiltIn.Wait Until Keyword Succeeds    120    1    Check Device is Connected    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
61
62 Clean SXP Cluster
63     [Documentation]    Disconnect SXP cluster topology
64     ClusterManagement.Flush_Iptables_From_List_Or_All
65     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
66     \    BuiltIn.Wait Until Keyword Succeeds    240    1    ClusterManagement.Sync_Status_Should_Be_True    ${i+1}
67     ${controller_index} =    Get Leader Controller
68     SxpLib.Delete Node    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
69     SxpLib.Delete Node    ${CLUSTER_NODE_ID}    session=controller${controller_index}
70
71 Check Cluster Node started
72     [Arguments]    ${node}    ${port}=64999    ${ip}=${EMPTY}
73     [Documentation]    Verify that SxpNode has data written to Operational datastore and Node is running on one of cluster nodes
74     ${started} =    BuiltIn.Set Variable    ${False}
75     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
76     \    ${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}
77     \    ...    prompt=${ODL_SYSTEM_PROMPT}
78     \    ${started} =    BuiltIn.Set Variable If    '${rc}' == '0'    ${True}    ${started}
79     BuiltIn.Should Be True    ${started}
80
81 Check Device is Connected
82     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${session}=session
83     [Documentation]    Checks if SXP device is connected to at least one cluster node
84     ${is_connected} =    BuiltIn.Set Variable    ${False}
85     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
86     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
87     \    ${follower} =    Sxp.Find Connection    ${resp}    ${version}    any    ${ODL_SYSTEM_${i+1}_IP}
88     \    ...    ${port}    on
89     \    ${is_connected} =    BuiltIn.Run Keyword If    ${follower}    BuiltIn.Set Variable    ${True}
90     \    ...    ELSE    BuiltIn.Set Variable    ${is_connected}
91     BuiltIn.Should Be True    ${is_connected}
92
93 Check Cluster is Connected
94     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${mode}=speaker    ${session}=session
95     [Documentation]    Checks if SXP device is connected to at least one cluster node
96     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
97     SxpLib.Should Contain Connection    ${resp}    ${TOOLS_SYSTEM_IP}    ${port}    ${mode}    ${version}
98
99 Get Leader Controller
100     [Documentation]    Find cluster controller that is marked as leader in cluster with all members running
101     @{running_members} =    BuiltIn.Create List
102     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
103     \    Collections.Append To List    ${running_members}    ${i+1}
104     ${active_controller} =    Get Leader Controller From Running    @{running_members}
105     [Return]    ${active_controller}
106
107 Get Leader Controller From Running
108     [Arguments]    @{running_members}
109     [Documentation]    Find cluster controller that is marked as leader in cluster with only some members running
110     BuiltIn.Log    ${running_members}
111     @{votes} =    BuiltIn.Create List
112     : FOR    ${i}    IN    @{running_members}
113     \    ${resp} =    RequestsLibrary.Get Request    controller${i}    /restconf/operational/entity-owners:entity-owners
114     \    BuiltIn.Continue For Loop If    ${resp.status_code} != 200
115     \    ${controller} =    Sxp.Get Leader Controller From Json    ${resp.content}    SxpControllerInstance
116     \    Collections.Append To List    ${votes}    ${controller}
117     ${length} =    BuiltIn.Get Length    ${votes}
118     BuiltIn.Should Not Be Equal As Integers    ${length}    0
119     ${active_controller} =    BuiltIn.Evaluate    collections.Counter(${votes}).most_common(1)[0][0]    collections
120     [Return]    ${active_controller}
121
122 Get Inactive Controller
123     [Documentation]    Find cluster controller that is not marked as leader for SXP service in cluster
124     ${active_controller} =    Get Leader Controller
125     ${controller} =    BuiltIn.Evaluate    random.choice( filter( lambda i: i!=${active_controller}, range(1, ${NUM_ODL_SYSTEM} + 1)))    random
126     [Return]    ${controller}
127
128 Get Any Controller
129     [Documentation]    Get any controller from cluster range
130     ${follower} =    BuiltIn.Evaluate    random.choice( range(1, ${NUM_ODL_SYSTEM} + 1))    random
131     [Return]    ${follower}
132
133 Map Followers To Mac Addresses
134     [Documentation]    Creates Map containing ODL_SYSTEM_IP to corresponding MAC-ADDRESS
135     ${mac_addresses} =    BuiltIn.Create dictionary
136     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
137     \    ${mac_address}    Find Mac Address Of Ip Address    ${ODL_SYSTEM_${i+1}_IP}
138     \    Collections.Set To Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${i+1}_IP}    ${mac_address}
139     BuiltIn.Log    ${mac_addresses}
140     [Return]    ${mac_addresses}
141
142 Find Mac Address Of Ip Address
143     [Arguments]    ${ip}
144     [Documentation]    Finds out MAC-ADDRESS of specified IP by pinging it from TOOLS_SYSTEM machine
145     ${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}
146     [Return]    ${mac_address}
147
148 Ip Addres Should Not Be Routed To Follower
149     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
150     [Documentation]    Verify that IP-ADDRESS is not routed to follower specified by ID
151     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
152     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
153     BuiltIn.Should Not Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
154
155 Ip Addres Should Be Routed To Follower
156     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
157     [Documentation]    Verify that IP-ADDRESS is routed to follower specified by ID
158     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
159     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
160     BuiltIn.Should Not Be Empty    ${mac_address_resolved}
161     BuiltIn.Should Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
162
163 Shutdown Tools Node
164     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_2_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${passwd}=${TOOLS_SYSTEM_PASSWORD}
165     [Documentation]    Shutdown Tools node to avoid conflict in resolving virtual ip that is overlaping that node.
166     ${rc} =    OperatingSystem.Run And Return Rc    ping -q -c 3 ${ip_address}
167     ${stdout} =    BuiltIn.Run Keyword And Return If    ${rc} == 0    Utils.Run Command On Remote System    ${ip_address}    sudo shutdown -P 0    ${user}
168     ...    ${passwd}
169     BuiltIn.Log    ${stdout}
170
171 Create Virtual Interface
172     [Documentation]    Create virtual interface on all of the cluster nodes
173     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
174     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo modprobe dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
175     \    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}
176     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
177
178 Delete Virtual Interface
179     [Documentation]    Create virtual interface on all of the cluster nodes
180     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
181     \    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}
182     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo rmmod dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
183     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}