070e268007697c01b82143efac31d0e6572cafd9
[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 Active 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 Active Controller
100     [Documentation]    Find cluster controller that is marked as leader for SXP service in cluster
101     @{votes} =    BuiltIn.Create List
102     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
103     \    ${resp} =    RequestsLibrary.Get Request    controller${i+1}    /restconf/operational/entity-owners:entity-owners
104     \    BuiltIn.Continue For Loop If    ${resp.status_code} != 200
105     \    ${controller} =    Sxp.Get Active Controller From Json    ${resp.content}    SxpControllerInstance
106     \    Collections.Append To List    ${votes}    ${controller}
107     ${length} =    BuiltIn.Get Length    ${votes}
108     BuiltIn.Should Not Be Equal As Integers    ${length}    0
109     ${active_controller} =    BuiltIn.Evaluate    collections.Counter(${votes}).most_common(1)[0][0]    collections
110     [Return]    ${active_controller}
111
112 Get Inactive Controller
113     [Documentation]    Find cluster controller that is not marked as leader for SXP service in cluster
114     ${active_controller} =    Get Active Controller
115     ${controller} =    BuiltIn.Evaluate    random.choice( filter( lambda i: i!=${active_controller}, range(1, ${NUM_ODL_SYSTEM} + 1)))    random
116     [Return]    ${controller}
117
118 Get Any Controller
119     [Documentation]    Get any controller from cluster range
120     ${follower} =    BuiltIn.Evaluate    random.choice( range(1, ${NUM_ODL_SYSTEM} + 1))    random
121     [Return]    ${follower}
122
123 Map Followers To Mac Addresses
124     [Documentation]    Creates Map containing ODL_SYSTEM_IP to corresponding MAC-ADDRESS
125     ${mac_addresses} =    BuiltIn.Create dictionary
126     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
127     \    ${mac_address}    Find Mac Address Of Ip Address    ${ODL_SYSTEM_${i+1}_IP}
128     \    Collections.Set To Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${i+1}_IP}    ${mac_address}
129     BuiltIn.Log    ${mac_addresses}
130     [Return]    ${mac_addresses}
131
132 Find Mac Address Of Ip Address
133     [Arguments]    ${ip}
134     [Documentation]    Finds out MAC-ADDRESS of specified IP by pinging it from TOOLS_SYSTEM machine
135     ${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}
136     [Return]    ${mac_address}
137
138 Ip Addres Should Not Be Routed To Follower
139     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
140     [Documentation]    Verify that IP-ADDRESS is not routed to follower specified by ID
141     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
142     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
143     BuiltIn.Should Not Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
144
145 Ip Addres Should Be Routed To Follower
146     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
147     [Documentation]    Verify that IP-ADDRESS is routed to follower specified by ID
148     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
149     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
150     BuiltIn.Should Not Be Empty    ${mac_address_resolved}
151     BuiltIn.Should Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
152
153 Shutdown Tools Node
154     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_2_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${passwd}=${TOOLS_SYSTEM_PASSWORD}
155     [Documentation]    Shutdown Tools node to avoid conflict in resolving virtual ip that is overlaping that node.
156     ${rc} =    OperatingSystem.Run And Return Rc    ping -q -c 3 ${ip_address}
157     ${stdout} =    BuiltIn.Run Keyword And Return If    ${rc} == 0    Utils.Run Command On Remote System    ${ip_address}    sudo shutdown -P 0    ${user}
158     ...    ${passwd}
159     BuiltIn.Log    ${stdout}
160
161 Create Virtual Interface
162     [Documentation]    Create virtual interface on all of the cluster nodes
163     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
164     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo modprobe dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
165     \    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}
166     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
167
168 Delete Virtual Interface
169     [Documentation]    Create virtual interface on all of the cluster nodes
170     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
171     \    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}
172     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo rmmod dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
173     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}