SXP: Fix 020_Sxp_Node_Switching.robot
[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
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    ${SXP_LOG_LEVEL}    ${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 Clean SXP Cluster Session
34     [Documentation]    Clean sessions asociated with SXP cluster setup
35     ClusterManagement.Flush_Iptables_From_List_Or_All
36     ClusterManagement.Check_Cluster_Is_In_Sync
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=ClusterManagement__session_${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=ClusterManagement__session_${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     SxpLib.Delete Node    ${DEVICE_NODE_ID}    session=${DEVICE_SESSION}
68     SxpLib.Delete Node    ${CLUSTER_NODE_ID}    session=${CONTROLLER_SESSION}
69
70 Check Cluster Node started
71     [Arguments]    ${node}    ${port}=64999    ${ip}=${node}
72     [Documentation]    Verify that SxpNode has data written to Operational datastore and Node is running on one of cluster nodes
73     ${started} =    BuiltIn.Set Variable    ${False}
74     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
75     \    ${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}
76     \    ...    prompt=${ODL_SYSTEM_PROMPT}
77     \    ${started} =    BuiltIn.Set Variable If    '${rc}' == '0'    ${True}    ${started}
78     BuiltIn.Should Be True    ${started}
79
80 Check Device is Connected
81     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${session}=session
82     [Documentation]    Checks if SXP device is connected to the cluster. It means it has connection in state "on" with one of the cluster members.
83     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
84     ${is_connected} =    BuiltIn.Set Variable    ${False}
85     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
86     \    ${is_connected} =    Sxp.Find Connection    ${resp}    ${version}    any    ${ODL_SYSTEM_${i+1}_IP}
87     \    ...    ${port}    on
88     \    BuiltIn.Exit For Loop If    ${is_connected}
89     BuiltIn.Should Be True    ${is_connected}
90
91 Check Cluster is Connected
92     [Arguments]    ${node}    ${version}=version4    ${port}=64999    ${mode}=speaker    ${session}=session
93     [Documentation]    Get SXP connections from cluster and verify that they contain a connection to the device in state "on"
94     ${resp} =    SxpLib.Get Connections    node=${node}    session=${session}
95     SxpLib.Should Contain Connection    ${resp}    ${DEVICE_NODE_ID}    ${port}    ${mode}    ${version}    on
96
97 Get Owner Controller
98     [Arguments]    ${running_member}=1
99     [Documentation]    Find cluster controller that is marked as cluster owner by requesting ownership data from ${running_member} node of the cluster
100     ${owner}    ${candidates} =    BuiltIn.Wait_Until_Keyword_Succeeds    5x    2s    ClusterManagement.Get_Owner_And_Successors_For_Device    org.opendaylight.sxp.controller.boot.SxpControllerInstance
101     ...    Sxp    ${running_member}
102     [Return]    ${owner}
103
104 Get Not Owner Controller
105     [Documentation]    Find cluster controller that is not marked as owner for SXP service in cluster
106     ${owner_controller} =    Get Owner Controller
107     ${controller} =    BuiltIn.Evaluate    random.choice( filter( lambda i: i!=${owner_controller}, range(1, ${NUM_ODL_SYSTEM} + 1)))    random
108     [Return]    ${controller}
109
110 Get Any Controller
111     [Documentation]    Get any controller from cluster range
112     ${follower} =    BuiltIn.Evaluate    random.choice( range(1, ${NUM_ODL_SYSTEM} + 1))    random
113     [Return]    ${follower}
114
115 Map Followers To Mac Addresses
116     [Documentation]    Creates Map containing ODL_SYSTEM_IP to corresponding MAC-ADDRESS
117     ${mac_addresses} =    BuiltIn.Create dictionary
118     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
119     \    ${mac_address}    Find Mac Address Of Ip Address    ${ODL_SYSTEM_${i+1}_IP}
120     \    Collections.Set To Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${i+1}_IP}    ${mac_address}
121     BuiltIn.Log    ${mac_addresses}
122     [Return]    ${mac_addresses}
123
124 Find Mac Address Of Ip Address
125     [Arguments]    ${ip}
126     [Documentation]    Finds out MAC-ADDRESS of specified IP by pinging it from TOOLS_SYSTEM machine
127     ${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}
128     [Return]    ${mac_address}
129
130 Ip Addres Should Not Be Routed To Follower
131     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
132     [Documentation]    Verify that IP-ADDRESS is not routed to follower specified by ID
133     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
134     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
135     BuiltIn.Should Not Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
136
137 Ip Addres Should Be Routed To Follower
138     [Arguments]    ${mac_addresses}    ${ip_address}    ${follower_index}
139     [Documentation]    Verify that IP-ADDRESS is routed to follower specified by ID
140     ${mac_address_assigned} =    Collections.Get From Dictionary    ${mac_addresses}    ${ODL_SYSTEM_${follower_index}_IP}
141     ${mac_address_resolved} =    Find Mac Address Of Ip Address    ${ip_address}
142     BuiltIn.Should Not Be Empty    ${mac_address_resolved}
143     BuiltIn.Should Be Equal As Strings    ${mac_address_assigned}    ${mac_address_resolved}
144
145 Shutdown Tools Node
146     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_2_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${passwd}=${TOOLS_SYSTEM_PASSWORD}
147     [Documentation]    Shutdown Tools node to avoid conflict in resolving virtual ip that is overlaping that node.
148     ${rc} =    OperatingSystem.Run And Return Rc    ping -q -c 3 ${ip_address}
149     ${stdout} =    BuiltIn.Run Keyword And Return If    ${rc} == 0    Utils.Run Command On Remote System    ${ip_address}    sudo shutdown -P 0    ${user}
150     ...    ${passwd}
151     BuiltIn.Log    ${stdout}
152
153 Create Virtual Interface
154     [Documentation]    Create virtual interface on all of the cluster nodes
155     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
156     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo modprobe dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
157     \    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}
158     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
159
160 Delete Virtual Interface
161     [Documentation]    Create virtual interface on all of the cluster nodes
162     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
163     \    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}
164     \    Utils.Run Command On Remote System    ${ODL_SYSTEM_${i+1}_IP}    sudo rmmod dummy    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}
165     \    Utils.Run Command On Remote System And Log    ${ODL_SYSTEM_${i+1}_IP}    sudo ip link show    ${ODL_SYSTEM_USER}    ${ODL_SYSTEM_PASSWORD}