Cluster Library consolidation 1
[integration/test.git] / csit / libraries / ClusterManagement.robot
1 *** Settings ***
2 Documentation     Resource housing Keywords common to several suites for cluster functional testing.
3 ...
4 ...               Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
5 ...               Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
6 ...
7 ...               This program and the accompanying materials are made available under the
8 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
9 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
10 ...
11 ...
12 ...               This resource holds private state (in suite variables),
13 ...               which is generated once at Setup with ClusterManagement_Setup KW.
14 ...               The state includes member indexes, IP addresses and Http (RequestsLibrary) sessions.
15 ...               Cluster Keywords normally use member index, member list or nothing (all members) as argument.
16 ...
17 ...               Requirements:
18 ...               odl-jolokia is assumed to be installed.
19 ...
20 ...               Keywords are ordered as follows:
21 ...               - Cluster Setup
22 ...               - Shard state, leader and followers
23 ...               - Entity Owner, candidates and successors
24 ...               - Kill and Start Member
25 ...               - Isolate and Rejoin Member
26 ...               - Run Commands On Member
27 ...               - REST requests and checks on Members
28 ...
29 ...               TODO: Unify capitalization of Leaders and Followers.
30 Library           RequestsLibrary    # for Create_Session and To_Json
31 Library           Collections
32 Resource          ${CURDIR}/TemplatedRequests.robot    # for Get_As_Json_From_Uri
33 Resource          ${CURDIR}/Utils.robot    # for Run_Command_On_Controller
34
35 *** Variables ***
36 ${JAVA_HOME}      ${EMPTY}    # releng/builder scripts should provide correct value
37 ${JOLOKIA_CONF_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore
38 ${JOLOKIA_OPER_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-operational,type=DistributedOperationalDatastore
39 ${JOLOKIA_READ_URI}    jolokia/read/org.opendaylight.controller
40 ${ENTITY_OWNER_URI}    restconf/operational/entity-owners:entity-owners
41 ${RESTCONF_MODULES_DIR}    ${CURDIR}/../variables/restconf/modules
42
43 *** Keywords ***
44 ClusterManagement_Setup
45     [Documentation]    Detect repeated call, or detect number of members and initialize derived suite variables.
46     # Avoid multiple initialization by several downstream libraries.
47     ${already_done} =    BuiltIn.Get_Variable_Value    \${ClusterManagement__has_setup_run}    False
48     BuiltIn.Return_From_Keyword_If    ${already_done}
49     BuiltIn.Set_Suite_Variable    \${ClusterManagement__has_setup_run}    True
50     ${cluster_size} =    BuiltIn.Get_Variable_Value    \${NUM_ODL_SYSTEM}    1
51     ${status}    ${possibly_int_of_members} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Convert_To_Integer    ${cluster_size}
52     ${int_of_members} =    BuiltIn.Set_Variable_If    '${status}' != 'PASS'    ${1}    ${possibly_int_of_members}
53     ClusterManagement__Compute_Derived_Variables    int_of_members=${int_of_members}
54
55 Check_Cluster_Is_In_Sync
56     [Arguments]    ${member_index_list}=${EMPTY}
57     [Documentation]    Fail if no-sync is detected on a member from list (or any).
58     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
59     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
60     \    ${status} =    Get_Sync_Status_Of_Member    member_index=${index}
61     \    BuiltIn.Continue_For_Loop_If    'True' == '${status}'
62     \    BuiltIn.Fail    Index ${index} has incorrect status: ${status}
63
64 Get_Sync_Status_Of_Member
65     [Arguments]    ${member_index}
66     [Documentation]    Obtain IP, two GETs from jolokia URIs, return combined sync status as string.
67     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
68     ${conf_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${JOLOKIA_CONF_SHARD_MANAGER_URI}    session=${session}
69     ${conf_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${conf_text}
70     BuiltIn.Return_From_Keyword_If    'False' == ${conf_status}    False
71     ${oper_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${JOLOKIA_OPER_SHARD_MANAGER_URI}    session=${session}
72     ${oper_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${oper_text}
73     [Return]    ${oper_status}
74
75 Verify_Leader_Exists_For_Each_Shard
76     [Arguments]    ${shard_name_list}    ${shard_type}=operational    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
77     [Documentation]    For each shard name, call Get_Leader_And_Followers_For_Shard.
78     ...    Not much logic there, but single Keyword is useful when using BuiltIn.Wait_Until_Keyword_Succeeds.
79     : FOR    ${shard_name}    IN    @{shard_name_list}
80     \    Get_Leader_And_Followers_For_Shard    shard_name=${shard_name}    shard_type=${shard_type}    validate=True    member_index_list=${member_index_list}    verify_restconf=${verify_restconf}
81
82 Get_Leader_And_Followers_For_Shard
83     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=True    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
84     [Documentation]    Get role lists, validate there is one leader, return the leader and list of followers.
85     ...    Optionally, issue GET to a simple restconf URL to make sure subsequent operations will not encounter 503.
86     ${leader_list}    ${follower_list} =    Get_State_Info_For_Shard    shard_name=${shard_name}    shard_type=${shard_type}    validate=True    member_index_list=${member_index_list}
87     ...    verify_restconf=${verify_restconf}
88     ${leader_count} =    BuiltIn.Get_Length    ${leader_list}
89     BuiltIn.Run_Keyword_If    ${leader_count} < 1    BuiltIn.Fail    No leader found.
90     BuiltIn.Length_Should_Be    ${leader_list}    ${1}    Too many Leaders.
91     ${leader} =    Collections.Get_From_List    ${leader_list}    0
92     [Return]    ${leader}    ${follower_list}
93
94 Get_State_Info_For_Shard
95     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=False    ${member_index_list}=${EMPTY}    ${verify_restconf}=False
96     [Documentation]    Return lists of Leader and Follower member indices from a given member index list
97     ...    (or from the full list if empty). If \${shard_type} is not 'config', 'operational' is assumed.
98     ...    If \${validate}, Fail if raft state is not Leader or Follower (for example on Candidate).
99     ...    The biggest difference from Get_Leader_And_Followers_For_Shard
100     ...    is that no check on number of Leaders is performed.
101     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
102     # TODO: Support alternative capitalization of 'config'?
103     ${ds_type} =    BuiltIn.Set_Variable_If    '${shard_type}' != 'config'    operational    config
104     ${leader_list} =    BuiltIn.Create_List
105     ${follower_list} =    BuiltIn.Create_List
106     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
107     \    ${raft_state} =    Get_Raft_State_Of_Shard_At_Member    shard_name=${shard_name}    shard_type=${ds_type}    member_index=${index}    verify_restconf=${verify_restconf}
108     \    BuiltIn.Run_Keyword_If    'Follower' == '${raft_state}'    Collections.Append_To_List    ${follower_list}    ${index}
109     \    ...    ELSE IF    'Leader' == '${raft_state}'    Collections.Append_To_List    ${leader_list}    ${index}
110     \    ...    ELSE IF    ${validate}    BuiltIn.Fail    Unrecognized Raft state: ${raft_state}
111     [Return]    ${leader_list}    ${follower_list}
112
113 Get_Raft_State_Of_Shard_At_Member
114     [Arguments]    ${shard_name}    ${shard_type}    ${member_index}    ${verify_restconf}=False
115     [Documentation]    Send request to Jolokia on indexed member, return extracted Raft status.
116     ...    Optionally, check restconf works.
117     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
118     # TODO: Does the used URI tend to generate large data which floods log.html?
119     BuiltIn.Run_Keyword_If    ${verify_restconf}    TemplatedRequests.Get_As_Json_Templated    session=${session}    folder=${RESTCONF_MODULES_DIR}    verify=False
120     ${type_class} =    Resolve_Shard_Type_Class    shard_type=${shard_type}
121     ${uri} =    BuiltIn.Set_Variable    ${JOLOKIA_READ_URI}:Category=Shards,name=member-${member_index}-shard-${shard_name}-${shard_type},type=${type_class}
122     ${data_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${uri}    session=${session}
123     ${data_object} =    RequestsLibrary.To_Json    ${data_text}
124     ${value} =    Collections.Get_From_Dictionary    ${data_object}    value
125     ${raft_state} =    Collections.Get_From_Dictionary    ${value}    RaftState
126     [Return]    ${raft_state}
127
128 Verify_Owner_And_Successors_For_Device
129     [Arguments]    ${device_name}    ${device_type}    ${member_index}    ${candidate_list}=${EMPTY}
130     [Documentation]    Returns the owner and successors for the SB device ${device_name} of type ${device_type}. Request is sent to member ${member_index}.
131     ...    Extra check is done to verify owner and successors are within the ${candidate_list}. This KW is useful when combined with WUKS.
132     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${candidate_list}
133     ${owner}    ${successor_list} =    Get_Owner_And_Successors_For_Device    device_name=${device_name}    device_type=${device_type}    member_index=${member_index}
134     Collections.List_Should_Contain_Value    ${index_list}    ${owner}    Owner ${owner} is not in candidate list ${index_list}
135     ${expected_successor_list} =    BuiltIn.Create_List    @{index_list}
136     Collections.Remove_Values_From_List    ${expected_successor_list}    ${owner}
137     Collections.Lists_Should_Be_Equal    ${expected_successor_list}    ${successor_list}    Successor list ${successor_list} is not in candidate list ${index_list}
138     [Return]    ${owner}    ${successor_list}
139
140 Get_Owner_And_Successors_For_device
141     [Arguments]    ${device_name}    ${device_type}    ${member_index}
142     [Documentation]    Returns the owner and a list of successors for the SB device ${device_name} of type ${device_type}. Request is sent to member ${member_index}.
143     ...    Successors are those device candidates not elected as owner. The list of successors = (list of candidates) - (owner).
144     ${owner}    ${candidate_list} =    Get_Owner_And_Candidates_For_Device    device_name=${device_name}    device_type=${device_type}    member_index=${member_index}
145     ${successor_list} =    BuiltIn.Create_List    @{candidate_list}
146     Collections.Remove_Values_From_List    ${successor_list}    ${owner}
147     [Return]    ${owner}    ${successor_list}
148
149 Get_Owner_And_Candidates_For_Device
150     [Arguments]    ${device_name}    ${device_type}    ${member_index}
151     [Documentation]    Returns the owner and a list of candidates for the SB device ${device_name} of type ${device_type}. Request is sent to member ${member_index}.
152     ...    Candidates are all members that register to own a device, so the list of candiates includes the owner.
153     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
154     ${data} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${ENTITY_OWNER_URI}    session=${session}
155     ${candidate_list} =    BuiltIn.Create_List
156     ${entity_type} =    BuiltIn.Set_Variable_If    '${device_type}' == 'netconf'    netconf-node/${device_name}    ${device_type}
157     ${clear_data} =    BuiltIn.Run_Keyword_If    '${device_type}' == 'openflow' or '${device_type}' == 'netconf'    Extract_OpenFlow_Device_Data    ${data}
158     ...    ELSE IF    '${device_type}' == 'ovsdb'    Extract_Ovsdb_Device_Data    ${data}
159     ...    ELSE    Fail    Not recognized device type: ${device_type}
160     ${json} =    RequestsLibrary.To_Json    ${clear_data}
161     ${entity_type_list} =    Collections.Get_From_Dictionary    &{json}[entity-owners]    entity-type
162     ${entity_type_index} =    Utils.Get_Index_From_List_Of_Dictionaries    ${entity_type_list}    type    ${entity_type}
163     BuiltIn.Should_Not_Be_Equal    ${entity_type_index}    -1    No Entity Owner found for ${device_type}
164     ${entity_list} =    Collections.Get_From_Dictionary    @{entity_type_list}[${entity_type_index}]    entity
165     ${entity_index} =    Utils.Get_Index_From_List_Of_Dictionaries    ${entity_list}    id    ${device_name}
166     BuiltIn.Should Not Be Equal    ${entity_index}    -1    Device ${device_name} not found in Entity Owner ${device_type}
167     ${entity_owner} =    Collections.Get_From_Dictionary    @{entity_list}[${entity_index}]    owner
168     BuiltIn.Should_Not_Be_Empty    ${entity_owner}    No owner found for ${device_name}
169     ${owner} =    String.Replace_String    ${entity_owner}    member-    ${EMPTY}
170     ${owner} =    BuiltIn.Convert_To_Integer    ${owner}
171     ${entity_candidates_list} =    Collections.Get_From_Dictionary    @{entity_list}[${entity_index}]    candidate
172     : FOR    ${entity_candidate}    IN    @{entity_candidates_list}
173     \    ${candidate} =    String.Replace_String    &{entity_candidate}[name]    member-    ${EMPTY}
174     \    ${candidate} =    BuiltIn.Convert_To_Integer    ${candidate}
175     \    Collections.Append_To_List    ${candidate_list}    ${candidate}
176     [Return]    ${owner}    ${candidate_list}
177
178 Extract_OpenFlow_Device_Data
179     [Arguments]    ${data}
180     [Documentation]    Remove superfluous OpenFlow device data from Entity Owner printout.
181     ${clear_data} =    String.Replace_String    ${data}    /general-entity:entity[general-entity:name='    ${EMPTY}
182     ${clear_data} =    String.Replace_String    ${clear_data}    ']    ${EMPTY}
183     Log    ${clear_data}
184     [Return]    ${clear_data}
185
186 Extract_Ovsdb_Device_Data
187     [Arguments]    ${data}
188     [Documentation]    Remove superfluous OVSDB device data from Entity Owner printout.
189     ${clear_data} =    String.Replace_String    ${data}    /network-topology:network-topology/network-topology:topology[network-topology:topology-id='ovsdb:1']/network-topology:node[network-topology:node-id='    ${EMPTY}
190     ${clear_data} =    String.Replace_String    ${clear_data}    ']    ${EMPTY}
191     Log    ${clear_data}
192     [Return]    ${clear_data}
193
194 Kill_Single_Member
195     [Arguments]    ${member}    ${confirm}=True
196     [Documentation]    Convenience keyword that kills the specified member of the cluster.
197     ${index_list} =    ClusterManagement__Build_List    ${member}
198     Kill_Members_From_List_Or_All    ${index_list}    ${confirm}
199
200 Kill_Members_From_List_Or_All
201     [Arguments]    ${member_index_list}=${EMPTY}    ${confirm}=True
202     [Documentation]    If the list is empty, kill all ODL instances. Otherwise, kill members based on present indices.
203     ...    If \${confirm} is True, sleep 1 second and verify killed instances are not there anymore.
204     ${command} =    BuiltIn.Set_Variable    ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
205     Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
206     BuiltIn.Return_From_Keyword_If    not ${confirm}
207     # TODO: Convert to WUKS with configurable timeout if it turns out 1 second is not enough.
208     BuiltIn.Sleep    1s    Kill -9 closes open files, which may take longer than ssh overhead, but not long enough to warrant WUKS.
209     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
210     : FOR    ${index}    IN    @{index_list}
211     \    Verify_Karaf_Is_Not_Running_On_Member    member_index=${index}
212
213 Start_Single_Member
214     [Arguments]    ${member}    ${wait_for_sync}=True    ${timeout}=300s
215     [Documentation]    Convenience keyword that starts the specified member of the cluster.
216     ${index_list} =    ClusterManagement__Build_List    ${member}
217     Start_Members_From_List_Or_All    ${index_list}    ${wait_for_sync}    ${timeout}
218
219 Start_Members_From_List_Or_All
220     [Arguments]    ${member_index_list}=${EMPTY}    ${wait_for_sync}=True    ${timeout}=300s    ${karaf_home}=${WORKSPACE}${/}${BUNDLEFOLDER}    ${export_java_home}=${JAVA_HOME}
221     [Documentation]    If the list is empty, start all cluster members. Otherwise, start members based on present indices.
222     ...    If ${wait_for_sync}, wait for cluster sync on listed members.
223     ...    Optionally karaf_home can be overriden. Optionally specific JAVA_HOME is used for starting.
224     ${base_command} =    BuiltIn.Set_Variable    ${karaf_home}/bin/start
225     ${command} =    BuiltIn.Set_Variable_If    "${export_java_home}"    export JAVA_HOME="${export_java_home}"; ${base_command}    ${base_command}
226     Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
227     BuiltIn.Return_From_Keyword_If    not ${wait_for_sync}
228     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    1s    Check_Cluster_Is_In_Sync    member_index_list=${member_index_list}
229     # TODO: Do we also want to check Shard Leaders here?
230
231 Clean_Journals_And_Snapshots_On_List_Or_All
232     [Arguments]    ${member_index_list}=${EMPTY}    ${karaf_home}=${WORKSPACE}${/}${BUNDLEFOLDER}
233     [Documentation]    Delete journal and snapshots directories on every node listed (or all).
234     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
235     ${command} =    Set Variable    rm -rf "${karaf_home}/journal" "${karaf_home}/snapshots"
236     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
237     \    Run_Command_On_Member    command=${command}    member_index=${index}
238
239 Verify_Karaf_Is_Not_Running_On_Member
240     [Arguments]    ${member_index}
241     [Documentation]    Fail if non-zero karaf instances are counted on member of given index.
242     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
243     BuiltIn.Should_Be_Equal    0    ${count}    Found running Karaf count: ${count}
244
245 Verify_Single_Karaf_Is_Running_On_Member
246     [Arguments]    ${member_index}
247     [Documentation]    Fail if number of karaf instances on member of given index is not one.
248     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
249     BuiltIn.Should_Be_Equal    1    ${count}    Wrong number of Karafs running: ${count}
250
251 Count_Running_Karafs_On_Member
252     [Arguments]    ${member_index}
253     [Documentation]    Remotely execute grep for karaf process, return count as string.
254     ${command} =    BuiltIn.Set_Variable    ps axf | grep karaf | grep -v grep | wc -l
255     ${count} =    Run_Command_On_Member    command=${command}    member_index=${member_index}
256     [Return]    ${count}
257
258 Isolate_Member_From_List_Or_All
259     [Arguments]    ${isolate_member_index}    ${member_index_list}=${EMPTY}
260     [Documentation]    If the list is empty, isolate member from all ODL instances. Otherwise, isolate member based on present indices.
261     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
262     ${source} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${isolate_member_index}
263     : FOR    ${index}    IN    @{index_list}
264     \    ${destination} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${index}
265     \    ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -I OUTPUT -p all --source ${source} --destination ${destination} -j DROP
266     \    BuiltIn.Run_Keyword_If    "${index}" != "${isolate_member_index}"    Run_Command_On_Member    command=${command}    member_index=${isolate_member_index}
267     ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -L -n
268     ${output} =    Run_Command_On_Member    command=${command}    member_index=${isolate_member_index}
269     BuiltIn.Log    ${output}
270
271 Rejoin_Member_From_List_Or_All
272     [Arguments]    ${rejoin_member_index}    ${member_index_list}=${EMPTY}
273     [Documentation]    If the list is empty, rejoin member from all ODL instances. Otherwise, rejoin member based on present indices.
274     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
275     ${source} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${rejoin_member_index}
276     : FOR    ${index}    IN    @{index_list}
277     \    ${destination} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${index}
278     \    ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -D OUTPUT -p all --source ${source} --destination ${destination} -j DROP
279     \    BuiltIn.Run_Keyword_If    "${index}" != "${rejoin_member_index}"    Run_Command_On_Member    command=${command}    member_index=${rejoin_member_index}
280     ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -L -n
281     ${output} =    Run_Command_On_Member    command=${command}    member_index=${rejoin_member_index}
282     BuiltIn.Log    ${output}
283
284 Flush_Iptables_From_List_Or_All
285     [Arguments]    ${member_index_list}=${EMPTY}
286     [Documentation]    If the list is empty, flush IPTables in all ODL instances. Otherwise, flush member based on present indices.
287     ${command} =    BuiltIn.Set_Variable    sudo iptables -v -F
288     ${output} =    Run_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
289
290 Run_Command_On_List_Or_All
291     [Arguments]    ${command}    ${member_index_list}=${EMPTY}
292     [Documentation]    Cycle through indices (or all), run command on each.
293     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
294     : FOR    ${index}    IN    @{index_list}
295     \    Run_Command_On_Member    command=${command}    member_index=${index}
296
297 Run_Command_On_Member
298     [Arguments]    ${command}    ${member_index}
299     [Documentation]    Obtain IP, call Utils and return output. This does not preserve active ssh session.
300     ${member_ip} =    Collections.Get_From_Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${member_index}
301     ${output} =    Utils.Run_Command_On_Controller    ${member_ip}    ${command}
302     [Return]    ${output}
303
304 Put_As_Json_And_Check_Member_List_Or_All
305     [Arguments]    ${uri}    ${data}    ${member_index}    ${member_index_list}=${EMPTY}
306     [Documentation]    Send a PUT with the supplied uri ${uri} and body ${data} to member ${member_index}.
307     ...    Then check data is replicated in all or some members defined in ${member_index_list}.
308     ${response_text} =    Put_As_Json_To_Member    uri=${uri}    data=${data}    member_index=${member_index}
309     Wait Until Keyword Succeeds    5s    1s    Check_Json_Member_List_Or_All    uri=${uri}    expected_data=${data}    member_index_list=${member_index_list}
310     [Return]    ${response_text}
311
312 Put_As_Json_To_Member
313     [Arguments]    ${uri}    ${data}    ${member_index}
314     [Documentation]    Send a PUT with the supplied uri and data to member ${member_index}.
315     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
316     ${response_text} =    TemplatedRequests.Put_As_Json_To_Uri    uri=${uri}    data=${data}    session=${session}
317     [Return]    ${response_text}
318
319 Post_As_Json_To_Member
320     [Arguments]    ${uri}    ${data}    ${member_index}
321     [Documentation]    Send a POST with the supplied uri and data to member ${member_index}.
322     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
323     ${response_text} =    TemplatedRequests.Post_As_Json_To_Uri    uri=${uri}    data=${data}    session=${session}
324     [Return]    ${response_text}
325
326 Delete_And_Check_Member_List_Or_All
327     [Arguments]    ${uri}    ${member_index}    ${member_index_list}=${EMPTY}
328     [Documentation]    Send a DELETE with the supplied uri to the member ${member_index}.
329     ...    Then check the data is removed from all members in ${member_index_list}.
330     ${response_text} =    Delete_From_Member    ${uri}    ${member_index}
331     BuiltIn.Wait_Until_Keyword_Succeeds    5s    1s    Check_No_Content_Member_List_Or_All    uri=${uri}    member_index_list=${member_index_list}
332     [Return]    ${response_text}
333
334 Delete_From_Member
335     [Arguments]    ${uri}    ${member_index}
336     [Documentation]    Send a DELETE with the supplied uri to member ${member_index}.
337     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
338     ${response_text} =    TemplatedRequests.Delete_From_Uri    uri=${uri}    session=${session}
339     [Return]    ${response_text}
340
341 Check_Json_Member_List_Or_All
342     [Arguments]    ${uri}    ${expected_data}    ${member_index_list}=${EMPTY}
343     [Documentation]    Send a GET with the supplied uri to all or some members defined in ${member_index_list}.
344     ...    Then check received data is = ${expected data}.
345     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
346     : FOR    ${index}    IN    @{index_list}
347     \    ${data} =    Get_From_Member    uri=${uri}    member_index=${index}
348     \    TemplatedRequests.Normalize_Jsons_And_Compare    ${expected_data}    ${data}
349
350 Check_Item_Occurrence_Member_List_Or_All
351     [Arguments]    ${uri}    ${dictionary}    ${member_index_list}=${EMPTY}
352     [Documentation]    Send a GET with the supplied uri to all or some members defined in ${member_index_list}.
353     ...    Then check received for occurrences of items expressed in a dictionary ${dictionary}.
354     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
355     : FOR    ${index}    IN    @{index_list}
356     \    ${data} =    Get_From_Member    uri=${uri}    member_index=${index}
357     \    Utils.Check Item Occurrence    ${data}    ${dictionary}
358
359 Check_No_Content_Member_List_Or_All
360     [Arguments]    ${uri}    ${member_index_list}=${EMPTY}
361     [Documentation]    Send a GET with the supplied uri to all or some members defined in ${member_index_list}.
362     ...    Then check there is no content.
363     ${index_list} =    ClusterManagement__Given_Or_Internal_Index_List    given_list=${member_index_list}
364     : FOR    ${index}    IN    @{index_list}
365     \    ${session} =    Resolve_Http_Session_For_Member    member_index=${index}
366     \    Utils.No_Content_From_URI    ${session}    ${uri}
367
368 Get_From_Member
369     [Arguments]    ${uri}    ${member_index}    ${access}=${ACCEPT_EMPTY}
370     [Documentation]    Send a GET with the supplied uri to member ${member_index}.
371     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
372     ${response_text} =    TemplatedRequests.Get_From_Uri    uri=${uri}    accept=${access}    session=${session}
373     [Return]    ${response_text}
374
375 Resolve_Http_Session_For_Member
376     [Arguments]    ${member_index}
377     [Documentation]    Return RequestsLibrary session alias pointing to node of given index.
378     ${session} =    BuiltIn.Set_Variable    ClusterManagement__session_${member_index}
379     [Return]    ${session}
380
381 Resolve_Shard_Type_Class
382     [Arguments]    ${shard_type}
383     [Documentation]    Simple lookup for class name corresponding to desired type.
384     BuiltIn.Run_Keyword_If    '${shard_type}' == 'config'    BuiltIn.Return_From_Keyword    DistributedConfigDatastore
385     ...    ELSE IF    '${shard_type}' == 'operational'    BuiltIn.Return_From_Keyword    DistributedOperationalDatastore
386     BuiltIn.Fail    Unrecognized shard type: ${shard_type}
387
388 ClusterManagement__Build_List
389     [Arguments]    ${member}
390     ${member_int} =    BuiltIn.Convert_To_Integer    ${member}
391     ${index_list} =    BuiltIn.Create_List    ${member_int}
392     [Return]    ${index_list}
393
394 ClusterManagement__Parse_Sync_Status
395     [Arguments]    ${shard_manager_text}
396     [Documentation]    Return sync status parsed out of given text. Called twice by Get_Sync_Status_Of_Member.
397     BuiltIn.Log    ${shard_manager_text}
398     ${manager_object} =    RequestsLibrary.To_Json    ${shard_manager_text}
399     ${value_object} =    Collections.Get_From_Dictionary    dictionary=${manager_object}    key=value
400     ${sync_status} =    Collections.Get_From_Dictionary    dictionary=${value_object}    key=SyncStatus
401     [Return]    ${sync_status}
402
403 ClusterManagement__Given_Or_Internal_Index_List
404     [Arguments]    ${given_list}=${EMPTY}
405     [Documentation]    Utility to allow \${EMPTY} as default argument value, as the internal list is computed at runtime.
406     ${given_length} =    BuiltIn.Get_Length    ${given_list}
407     ${return_list} =    BuiltIn.Set_Variable_If    ${given_length} > 0    ${given_list}    ${ClusterManagement__member_index_list}
408     [Return]    ${return_list}
409
410 ClusterManagement__Compute_Derived_Variables
411     [Arguments]    ${int_of_members}
412     [Documentation]    Construct index list, session list and IP mapping, publish them as suite variables.
413     @{member_index_list} =    BuiltIn.Create_List
414     @{session_list} =    BuiltIn.Create_List
415     &{index_to_ip_mapping} =    BuiltIn.Create_Dictionary
416     : FOR    ${index}    IN RANGE    1    ${int_of_members+1}
417     \    ClusterManagement__Include_Member_Index    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
418     BuiltIn.Set_Suite_Variable    \${ClusterManagement__member_index_list}    ${member_index_list}
419     BuiltIn.Set_Suite_Variable    \${ClusterManagement__index_to_ip_mapping}    ${index_to_ip_mapping}
420     BuiltIn.Set_Suite_Variable    \${ClusterManagement__session_list}    ${session_list}
421
422 ClusterManagement__Include_Member_Index
423     [Arguments]    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
424     [Documentation]    Add a corresponding item based on index into the last three arguments.
425     ...    Create the Http session whose alias is added to list.
426     Collections.Append_To_List    ${member_index_list}    ${index}
427     ${member_ip} =    BuiltIn.Set_Variable    ${ODL_SYSTEM_${index}_IP}
428     # ${index} is int (not string) so "key=value" syntax does not work in the following line.
429     Collections.Set_To_Dictionary    ${index_to_ip_mapping}    ${index}    ${member_ip}
430     # Http session, with ${AUTH}, without headers.
431     ${session_alias} =    Resolve_Http_Session_For_Member    member_index=${index}
432     RequestsLibrary.Create_Session    ${session_alias}    http://${member_ip}:${RESTCONFPORT}    auth=${AUTH}    max_retries=0
433     Collections.Append_To_List    ${session_list}    ${session_alias}