Fix typo in ClusterManagement.List_All_Indices
[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 ...               All index lists returned should be sorted numerically, fix if not.
18 ...
19 ...               Requirements:
20 ...               odl-jolokia is assumed to be installed.
21 ...
22 ...               Keywords are ordered as follows:
23 ...               - Cluster Setup
24 ...               - Shard state, leader and followers
25 ...               - Entity Owner, candidates and successors
26 ...               - Kill, Stop and Start Member
27 ...               - Isolate and Rejoin Member
28 ...               - Run Commands On Member
29 ...               - REST requests and checks on Members
30 ...
31 ...               TODO: Unify capitalization of Leaders and Followers.
32 Library           RequestsLibrary    # for Create_Session and To_Json
33 Library           Collections
34 Resource          ${CURDIR}/CompareStream.robot
35 Resource          ${CURDIR}/KarafKeywords.robot
36 Resource          ${CURDIR}/SSHKeywords.robot
37 Resource          ${CURDIR}/TemplatedRequests.robot    # for Get_As_Json_From_Uri
38 Resource          ${CURDIR}/Utils.robot    # for Run_Command_On_Controller
39
40 *** Variables ***
41 ${ENTITY_OWNER_URI}    restconf/operational/entity-owners:entity-owners
42 ${JAVA_HOME}      ${EMPTY}    # releng/builder scripts should provide correct value
43 ${JOLOKIA_CONF_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore
44 ${JOLOKIA_OPER_SHARD_MANAGER_URI}    jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-operational,type=DistributedOperationalDatastore
45 ${JOLOKIA_READ_URI}    jolokia/read/org.opendaylight.controller
46 ${KARAF_HOME}     ${WORKSPACE}${/}${BUNDLEFOLDER}    # TODO: Migrate to Variables.robot
47 @{ODL_DEFAULT_DATA_PATHS}    tmp/    data/    cache/    snapshots/    journal/    etc/opendaylight/current/
48 ${RESTCONF_MODULES_DIR}    ${CURDIR}/../variables/restconf/modules
49 ${SINGLETON_NETCONF_DEVICE_ID_PREFIX}    /odl-general-entity:entity[odl-general-entity:name='KeyedInstanceIdentifier{targetType=interface org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node, path=[org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology, org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology[key=TopologyKey [_topologyId=Uri [_value=topology-netconf]]], org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node[key=NodeKey [_nodeId=Uri [_value=
50 ${SINGLETON_NETCONF_DEVICE_ID_SUFFIX}    ]]]]}']
51 ${SINGLETON_ELECTION_ENTITY_TYPE}    org.opendaylight.mdsal.ServiceEntityType
52 ${SINGLETON_CHANGE_OWNERSHIP_ENTITY_TYPE}    org.opendaylight.mdsal.AsyncServiceCloseEntityType
53 ${NODE_START_COMMAND}    ${KARAF_HOME}/bin/start
54 ${NODE_STOP_COMMAND}    ${KARAF_HOME}/bin/stop
55 ${NODE_KILL_COMMAND}    ps axf | grep org.apache.karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
56 ${NODE_FREEZE_COMMAND}    ps axf | grep org.apache.karaf | grep -v grep | awk '{print \"kill -STOP \" $1}' | sh
57 ${NODE_UNFREEZE_COMMAND}    ps axf | grep org.apache.karaf | grep -v grep | awk '{print \"kill -CONT \" $1}' | sh
58
59 *** Keywords ***
60 ClusterManagement_Setup
61     [Documentation]    Detect repeated call, or detect number of members and initialize derived suite variables.
62     # Avoid multiple initialization by several downstream libraries.
63     ${already_done} =    BuiltIn.Get_Variable_Value    \${ClusterManagement__has_setup_run}    False
64     BuiltIn.Return_From_Keyword_If    ${already_done}
65     BuiltIn.Set_Suite_Variable    \${ClusterManagement__has_setup_run}    True
66     ${cluster_size} =    BuiltIn.Get_Variable_Value    \${NUM_ODL_SYSTEM}    1
67     ${status}    ${possibly_int_of_members} =    BuiltIn.Run_Keyword_And_Ignore_Error    BuiltIn.Convert_To_Integer    ${cluster_size}
68     ${int_of_members} =    BuiltIn.Set_Variable_If    '${status}' != 'PASS'    ${1}    ${possibly_int_of_members}
69     ClusterManagement__Compute_Derived_Variables    int_of_members=${int_of_members}
70
71 Check_Cluster_Is_In_Sync
72     [Arguments]    ${member_index_list}=${EMPTY}
73     [Documentation]    Fail if no-sync is detected on a member from list (or any).
74     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
75     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
76     \    ${status} =    Get_Sync_Status_Of_Member    member_index=${index}
77     \    BuiltIn.Continue_For_Loop_If    'True' == '${status}'
78     \    BuiltIn.Fail    Index ${index} has incorrect status: ${status}
79
80 Get_Sync_Status_Of_Member
81     [Arguments]    ${member_index}
82     [Documentation]    Obtain IP, two GETs from jolokia URIs, return combined sync status as string.
83     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
84     ${conf_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${JOLOKIA_CONF_SHARD_MANAGER_URI}    session=${session}
85     ${conf_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${conf_text}
86     BuiltIn.Return_From_Keyword_If    'False' == ${conf_status}    False
87     ${oper_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${JOLOKIA_OPER_SHARD_MANAGER_URI}    session=${session}
88     ${oper_status} =    ClusterManagement__Parse_Sync_Status    shard_manager_text=${oper_text}
89     [Return]    ${oper_status}
90
91 Verify_Leader_Exists_For_Each_Shard
92     [Arguments]    ${shard_name_list}    ${shard_type}=operational    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
93     [Documentation]    For each shard name, call Get_Leader_And_Followers_For_Shard.
94     ...    Not much logic there, but single Keyword is useful when using BuiltIn.Wait_Until_Keyword_Succeeds.
95     : FOR    ${shard_name}    IN    @{shard_name_list}
96     \    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}
97
98 Get_Leader_And_Followers_For_Shard
99     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=True    ${member_index_list}=${EMPTY}    ${verify_restconf}=True
100     [Documentation]    Get role lists, validate there is one leader, return the leader and list of followers.
101     ...    Optionally, issue GET to a simple restconf URL to make sure subsequent operations will not encounter 503.
102     ${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}
103     ...    verify_restconf=${verify_restconf}
104     ${leader_count} =    BuiltIn.Get_Length    ${leader_list}
105     BuiltIn.Run_Keyword_If    ${leader_count} < 1    BuiltIn.Fail    No leader found.
106     BuiltIn.Length_Should_Be    ${leader_list}    ${1}    Too many Leaders.
107     ${leader} =    Collections.Get_From_List    ${leader_list}    0
108     [Return]    ${leader}    ${follower_list}
109
110 Get_State_Info_For_Shard
111     [Arguments]    ${shard_name}=default    ${shard_type}=operational    ${validate}=False    ${member_index_list}=${EMPTY}    ${verify_restconf}=False
112     [Documentation]    Return lists of Leader and Follower member indices from a given member index list
113     ...    (or from the full list if empty). If \${shard_type} is not 'config', 'operational' is assumed.
114     ...    If \${validate}, Fail if raft state is not Leader or Follower (for example on Candidate).
115     ...    The biggest difference from Get_Leader_And_Followers_For_Shard
116     ...    is that no check on number of Leaders is performed.
117     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
118     Collections.Sort_List    ${index_list}    # to guarantee return values are also sorted lists
119     # TODO: Support alternative capitalization of 'config'?
120     ${ds_type} =    BuiltIn.Set_Variable_If    '${shard_type}' != 'config'    operational    config
121     ${leader_list} =    BuiltIn.Create_List
122     ${follower_list} =    BuiltIn.Create_List
123     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
124     \    ${raft_state} =    Get_Raft_State_Of_Shard_At_Member    shard_name=${shard_name}    shard_type=${ds_type}    member_index=${index}    verify_restconf=${verify_restconf}
125     \    BuiltIn.Run_Keyword_If    'Follower' == '${raft_state}'    Collections.Append_To_List    ${follower_list}    ${index}
126     \    ...    ELSE IF    'Leader' == '${raft_state}'    Collections.Append_To_List    ${leader_list}    ${index}
127     \    ...    ELSE IF    ${validate}    BuiltIn.Fail    Unrecognized Raft state: ${raft_state}
128     [Return]    ${leader_list}    ${follower_list}
129
130 Get_Raft_State_Of_Shard_At_Member
131     [Arguments]    ${shard_name}    ${shard_type}    ${member_index}    ${verify_restconf}=False
132     [Documentation]    Send request to Jolokia on indexed member, return extracted Raft status.
133     ...    Optionally, check restconf works.
134     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
135     # TODO: Does the used URI tend to generate large data which floods log.html?
136     BuiltIn.Run_Keyword_If    ${verify_restconf}    TemplatedRequests.Get_As_Json_Templated    session=${session}    folder=${RESTCONF_MODULES_DIR}    verify=False
137     ${type_class} =    Resolve_Shard_Type_Class    shard_type=${shard_type}
138     ${uri} =    BuiltIn.Set_Variable    ${JOLOKIA_READ_URI}:Category=Shards,name=member-${member_index}-shard-${shard_name}-${shard_type},type=${type_class}
139     ${data_text} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${uri}    session=${session}
140     ${data_object} =    RequestsLibrary.To_Json    ${data_text}
141     ${value} =    Collections.Get_From_Dictionary    ${data_object}    value
142     ${raft_state} =    Collections.Get_From_Dictionary    ${value}    RaftState
143     [Return]    ${raft_state}
144
145 Verify_Shard_Leader_Elected
146     [Arguments]    ${shard_name}    ${shard_type}    ${new_elected}    ${old_leader}    ${member_index_list}=${EMPTY}
147     [Documentation]    Verify new leader was elected or remained the same. Bool paramter ${new_elected} indicates if
148     ...    new leader is elected or should remained the same as ${old_leader}
149     ${leader}    ${followers}=    Get_Leader_And_Followers_For_Shard    shard_name=${shard_name}    shard_type=${shard_type}    member_index_list=${member_index_list}
150     BuiltIn.Run_Keyword_If    ${new_elected}    BuiltIn.Should_Not_Be_Equal_As_Numbers    ${old_leader}    ${leader}
151     BuiltIn.Run_Keyword_Unless    ${new_elected}    BuiltIn.Should_Be_Equal_As_numbers    ${old_leader}    ${leader}
152     BuiltIn.Return_From_Keyword    ${leader}    ${followers}
153
154 Verify_Owner_And_Successors_For_Device
155     [Arguments]    ${device_name}    ${device_type}    ${member_index}    ${candidate_list}=${EMPTY}    ${after_stop}=False
156     [Documentation]    Returns the owner and successors for the SB device ${device_name} of type ${device_type}. Request is sent to member ${member_index}.
157     ...    For Boron and beyond, candidates are not removed on node down or isolation,
158     ...    so this keyword expects candidates to be all members from Boron on.
159     ...    Extra check is done to verify owner and successors are within the ${candidate_list}. This KW is useful when combined with WUKS.
160     ...    ${candidate_list} minus owner is returned as ${successor list}.
161     ...    Users can still use Get_Owner_And_Successors_For_Device if they are interested in downed candidates,
162     ...    or for testing heterogeneous clusters.
163     ${index_list} =    List_Indices_Or_All    given_list=${candidate_list}
164     ${owner}    ${successor_list} =    Get_Owner_And_Successors_For_Device    device_name=${device_name}    device_type=${device_type}    member_index=${member_index}
165     Collections.List_Should_Contain_Value    ${index_list}    ${owner}    Owner ${owner} is not in candidate list ${index_list}
166     # In Beryllium or after stopping an instance, the removed instance does not show in the candidate list.
167     ${expected_candidate_list_origin} =    BuiltIn.Set_Variable_If    '${ODL_STREAM}' == 'beryllium' or ${after_stop}    ${index_list}    ${ClusterManagement__member_index_list}
168     # We do not want to manipulate either origin list.
169     ${expected_successor_list} =    BuiltIn.Create_List    @{expected_candidate_list_origin}
170     Collections.Remove_Values_From_List    ${expected_successor_list}    ${owner}
171     Collections.Lists_Should_Be_Equal    ${expected_successor_list}    ${successor_list}    Successor list ${successor_list} is not the came as expected ${expected_successor_list}
172     # User expects the returned successor list to be the provided candidate list minus the owner.
173     Collections.Remove_Values_From_List    ${index_list}    ${owner}
174     [Return]    ${owner}    ${index_list}
175
176 Get_Owner_And_Successors_For_Device
177     [Arguments]    ${device_name}    ${device_type}    ${member_index}
178     [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}.
179     ...    Successors are those device candidates not elected as owner. The list of successors = (list of candidates) - (owner).
180     ...    The returned successor list is sorted numerically.
181     ...    Note that "candidate list" definition currently differs between Beryllium and Boron.
182     ...    Use Verify_Owner_And_Successors_For_Device if you want the older semantics (inaccessible nodes not present in the list).
183     ${owner}    ${candidate_list} =    Get_Owner_And_Candidates_For_Device    device_name=${device_name}    device_type=${device_type}    member_index=${member_index}
184     ${successor_list} =    BuiltIn.Create_List    @{candidate_list}    # Copy operation is not required, but new variable name requires a line anyway.
185     Collections.Remove_Values_From_List    ${successor_list}    ${owner}
186     [Return]    ${owner}    ${successor_list}
187
188 Get_Owner_And_Candidates_For_Device_Old
189     [Arguments]    ${device_name}    ${device_type}    ${member_index}
190     [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}.
191     ...    Candidates are all members that register to own a device, so the list of candiates includes the owner.
192     ...    The returned candidate list is sorted numerically.
193     ...    Note that "candidate list" definition currently differs between Beryllium and Boron.
194     ...    It is recommended to use Get_Owner_And_Successors_For_Device instead of this keyword, see documentation there.
195     BuiltIn.Comment    TODO: Can this implementation be changed to call Get_Owner_And_Candidates_For_Type_And_Id?
196     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
197     ${data} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${ENTITY_OWNER_URI}    session=${session}
198     ${candidate_list} =    BuiltIn.Create_List
199     ${entity_type} =    BuiltIn.Set_Variable_If    '${device_type}' == 'netconf'    netconf-node/${device_name}    ${device_type}
200     ${clear_data} =    BuiltIn.Run_Keyword_If    '${device_type}' == 'openflow' or '${device_type}' == 'netconf'    Extract_OpenFlow_Device_Data    ${data}
201     ...    ELSE IF    '${device_type}' == 'ovsdb'    Extract_Ovsdb_Device_Data    ${data}
202     ...    ELSE IF    '${device_type}' == 'org.opendaylight.mdsal.ServiceEntityType'    Extract_Service_Entity_Type    ${data}
203     ...    ELSE    Fail    Not recognized device type: ${device_type}
204     ${json} =    RequestsLibrary.To_Json    ${clear_data}
205     ${entity_type_list} =    Collections.Get_From_Dictionary    &{json}[entity-owners]    entity-type
206     ${entity_type_index} =    Utils.Get_Index_From_List_Of_Dictionaries    ${entity_type_list}    type    ${entity_type}
207     BuiltIn.Should_Not_Be_Equal_As_Integers    ${entity_type_index}    -1    No Entity Owner found for ${device_type}
208     ${entity_list} =    Collections.Get_From_Dictionary    @{entity_type_list}[${entity_type_index}]    entity
209     ${entity_index} =    Utils.Get_Index_From_List_Of_Dictionaries    ${entity_list}    id    ${device_name}
210     BuiltIn.Should_Not_Be_Equal_As_Integers    ${entity_index}    -1    Device ${device_name} not found in Entity Owner ${device_type}
211     ${entity_owner} =    Collections.Get_From_Dictionary    @{entity_list}[${entity_index}]    owner
212     BuiltIn.Should_Not_Be_Empty    ${entity_owner}    No owner found for ${device_name}
213     ${owner} =    String.Replace_String    ${entity_owner}    member-    ${EMPTY}
214     ${owner} =    BuiltIn.Convert_To_Integer    ${owner}
215     ${entity_candidates_list} =    Collections.Get_From_Dictionary    @{entity_list}[${entity_index}]    candidate
216     : FOR    ${entity_candidate}    IN    @{entity_candidates_list}
217     \    ${candidate} =    String.Replace_String    &{entity_candidate}[name]    member-    ${EMPTY}
218     \    ${candidate} =    BuiltIn.Convert_To_Integer    ${candidate}
219     \    Collections.Append_To_List    ${candidate_list}    ${candidate}
220     Collections.Sort_List    ${candidate_list}
221     [Return]    ${owner}    ${candidate_list}
222
223 Get_Owner_And_Candidates_For_Device_Singleton
224     [Arguments]    ${device_name}    ${device_type}    ${member_index}
225     [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}.
226     ...    Parsing method is selected by device type
227     ...    Separate kw for every supported device type must be defined
228     BuiltIn.Keyword_Should_Exist    Get_Owner_And_Candidates_For_Device_Singleton_${device_type}
229     BuiltIn.Run_Keyword_And_Return    Get_Owner_And_Candidates_For_Device_Singleton_${device_type}    ${device_name}    ${member_index}
230
231 Get_Owner_And_Candidates_For_Device_Singleton_Netconf
232     [Arguments]    ${device_name}    ${member_index}
233     [Documentation]    Returns the owner and a list of candidates for the SB device ${device_name} of type netconf. Request is sent to member ${member_index}.
234     ...    Parsing method is set as netconf (using netconf device id prefix and suffix)
235     # Get election entity type results
236     ${type} =    BuiltIn.Set_Variable    ${SINGLETON_ELECTION_ENTITY_TYPE}
237     ${id} =    BuiltIn.Set_Variable    ${SINGLETON_NETCONF_DEVICE_ID_PREFIX}${device_name}${SINGLETON_NETCONF_DEVICE_ID_SUFFIX}
238     ${owner_1}    ${candidate_list_1} =    Get_Owner_And_Candidates_For_Type_And_Id    ${type}    ${id}    ${member_index}
239     # Get change ownership entity type results
240     ${type} =    BuiltIn.Set_Variable    ${SINGLETON_CHANGE_OWNERSHIP_ENTITY_TYPE}
241     ${id} =    BuiltIn.Set_Variable    ${SINGLETON_NETCONF_DEVICE_ID_PREFIX}${device_name}${SINGLETON_NETCONF_DEVICE_ID_SUFFIX}
242     ${owner_2}    ${candidate_list_2} =    Get_Owner_And_Candidates_For_Type_And_Id    ${type}    ${id}    ${member_index}
243     # Owners must be same, if not, there is still some election or change ownership in progress
244     BuiltIn.Should_Be_Equal_As_Integers    ${owner_1}    ${owner_2}    Owners for device ${device_name} are not same
245     [Return]    ${owner_1}    ${candidate_list_1}
246
247 Get_Owner_And_Candidates_For_Device
248     [Arguments]    ${device_name}    ${device_type}    ${member_index}
249     [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}.
250     ...    If parsing as singleton failed, kw try to parse data in old way (without singleton).
251     ...    Candidates are all members that register to own a device, so the list of candiates includes the owner.
252     ...    The returned candidate list is sorted numerically.
253     ...    Note that "candidate list" definition currently differs between Beryllium and Boron.
254     ...    It is recommended to use Get_Owner_And_Successors_For_Device instead of this keyword, see documentation there.
255     # Try singleton
256     ${status}    ${results} =    BuiltIn.Run_Keyword_And_Ignore_Error    Get_Owner_And_Candidates_For_Device_Singleton    device_name=${device_name}    device_type=${device_type}    member_index=${member_index}
257     BuiltIn.Return_From_Keyword_If    "${status}"=="PASS"    ${results}
258     # If singleton failed, try parsing in old way
259     ${status}    ${results} =    BuiltIn.Run_Keyword_And_Ignore_Error    Get_Owner_And_Candidates_For_Device_Old    device_name=${device_name}    device_type=${device_type}    member_index=${member_index}
260     # previous 3 lines (BuilIn.Return.., # If singleton..., ${status}....) could be deleted when old way will not be supported anymore
261     BuiltIn.Run_Keyword_If    '${status}'=='FAIL'    BuiltIn.Fail    Could not parse owner and candidates for device ${device_name}
262     [Return]    @{results}
263
264 Get_Owner_And_Candidates_For_Type_And_Id
265     [Arguments]    ${type}    ${id}    ${member_index}    ${require_candidate_list}=${EMPTY}
266     [Documentation]    Returns the owner and a list of candidates for entity specified by ${type} and ${id}
267     ...    Request is sent to member ${member_index}.
268     ...    Candidates are all members that register to own a device, so the list of candiates includes the owner.
269     ...    Bear in mind that for Boron and beyond, candidates are not removed on node down or isolation.
270     ...    If ${require_candidate_list} is not \${EMPTY}, check whether the actual list of candidates matches.
271     ...    Note that differs from "given list" semantics used in other keywords,
272     ...    namely you cannot use \${EMPTY} to stand for "full list" in this keyword.
273     BuiltIn.Comment    TODO: Find a way to unify and deduplicate code blocks in Get_Owner_And_Candidates_* keywords.
274     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
275     ${data} =    TemplatedRequests.Get_As_Json_From_Uri    uri=${ENTITY_OWNER_URI}    session=${session}
276     ${candidate_list} =    BuiltIn.Create_List
277     ${json} =    RequestsLibrary.To_Json    ${data}
278     ${entity_type_list} =    Collections.Get_From_Dictionary    &{json}[entity-owners]    entity-type
279     ${entity_type_index} =    Utils.Get_Index_From_List_Of_Dictionaries    ${entity_type_list}    type    ${type}
280     BuiltIn.Should_Not_Be_Equal_As_Integers    ${entity_type_index}    -1    No Entity Owner found for ${type}
281     ${entity_list} =    Collections.Get_From_Dictionary    @{entity_type_list}[${entity_type_index}]    entity
282     ${entity_index} =    Utils.Get_Index_From_List_Of_Dictionaries    ${entity_list}    id    ${id}
283     BuiltIn.Should Not_Be_Equal_As_Integers    ${entity_index}    -1    Id ${id} not found in Entity Owner ${type}
284     ${entity_owner} =    Collections.Get_From_Dictionary    @{entity_list}[${entity_index}]    owner
285     BuiltIn.Should_Not_Be_Empty    ${entity_owner}    No owner found for type=${type} id=${id}
286     ${owner} =    String.Replace_String    ${entity_owner}    member-    ${EMPTY}
287     ${owner} =    BuiltIn.Convert_To_Integer    ${owner}
288     ${entity_candidates_list} =    Collections.Get_From_Dictionary    @{entity_list}[${entity_index}]    candidate
289     : FOR    ${entity_candidate}    IN    @{entity_candidates_list}
290     \    ${candidate} =    String.Replace_String    &{entity_candidate}[name]    member-    ${EMPTY}
291     \    ${candidate} =    BuiltIn.Convert_To_Integer    ${candidate}
292     \    Collections.Append_To_List    ${candidate_list}    ${candidate}
293     Collections.Sort_List    ${candidate_list}
294     BuiltIn.Comment    TODO: Separate check lines into Verify_Owner_And_Candidates_For_Type_And_Id
295     BuiltIn.Run_Keyword_If    """${require_candidate_list}"""    BuiltIn.Should_Be_Equal    ${require_candidate_list}    ${candidate_list}    Candidate list does not match: ${candidate_list} is not ${require_candidate_list}
296     [Return]    ${owner}    ${candidate_list}
297
298 Extract_Service_Entity_Type
299     [Arguments]    ${data}
300     [Documentation]    Remove superfluous device data from Entity Owner printout.
301     ${clear_data} =    String.Replace_String    ${data}    /odl-general-entity:entity[odl-general-entity:name='    ${EMPTY}
302     ${clear_data} =    String.Replace_String    ${clear_data}    -service-group']    ${EMPTY}
303     Log    ${clear_data}
304     [Return]    ${clear_data}
305
306 Extract_OpenFlow_Device_Data
307     [Arguments]    ${data}
308     [Documentation]    Remove superfluous OpenFlow device data from Entity Owner printout.
309     ${clear_data} =    BuiltIn.Run Keyword If    '${ODL_STREAM}' != 'beryllium' and '${ODL_OF_PLUGIN}' == 'lithium'    String.Replace_String    ${data}    org.opendaylight.mdsal.ServiceEntityType    openflow
310     ...    ELSE    BuiltIn.Set_Variable    ${data}
311     ${clear_data} =    String.Replace_String    ${clear_data}    /odl-general-entity:entity[odl-general-entity:name='    ${EMPTY}
312     ${clear_data} =    String.Replace_String    ${clear_data}    /general-entity:entity[general-entity:name='    ${EMPTY}
313     ${clear_data} =    String.Replace_String    ${clear_data}    ']    ${EMPTY}
314     Log    ${clear_data}
315     [Return]    ${clear_data}
316
317 Extract_Ovsdb_Device_Data
318     [Arguments]    ${data}
319     [Documentation]    Remove superfluous OVSDB device data from Entity Owner printout.
320     ${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}
321     ${clear_data} =    String.Replace_String    ${clear_data}    ']    ${EMPTY}
322     Log    ${clear_data}
323     [Return]    ${clear_data}
324
325 Kill_Single_Member
326     [Arguments]    ${member}    ${original_index_list}=${EMPTY}    ${confirm}=True
327     [Documentation]    Convenience keyword that kills the specified member of the cluster.
328     ...    The KW will return a list of available members: \${updated index_list}=\${original_index_list}-\${member}
329     ${index_list} =    ClusterManagement__Build_List    ${member}
330     ${updated_index_list} =    Kill_Members_From_List_Or_All    ${index_list}    ${original_index_list}    ${confirm}
331     [Return]    ${updated_index_list}
332
333 Kill_Members_From_List_Or_All
334     [Arguments]    ${member_index_list}=${EMPTY}    ${original_index_list}=${EMPTY}    ${confirm}=True
335     [Documentation]    If the list is empty, kill all ODL instances. Otherwise, kill members based on \${kill_index_list}
336     ...    If \${confirm} is True, sleep 1 second and verify killed instances are not there anymore.
337     ...    The KW will return a list of available members: \${updated index_list}=\${original_index_list}-\${member_index_list}
338     ${kill_index_list} =    List_Indices_Or_All    given_list=${member_index_list}
339     ${index_list} =    List_Indices_Or_All    given_list=${original_index_list}
340     Run_Bash_Command_On_List_Or_All    command=${NODE_KILL_COMMAND}    member_index_list=${member_index_list}
341     ${updated_index_list} =    BuiltIn.Create_List    @{index_list}
342     Collections.Remove_Values_From_List    ${updated_index_list}    @{kill_index_list}
343     BuiltIn.Return_From_Keyword_If    not ${confirm}    ${updated_index_list}
344     # TODO: Convert to WUKS with configurable timeout if it turns out 1 second is not enough.
345     BuiltIn.Sleep    1s    Kill -9 closes open files, which may take longer than ssh overhead, but not long enough to warrant WUKS.
346     : FOR    ${index}    IN    @{kill_index_list}
347     \    Verify_Karaf_Is_Not_Running_On_Member    member_index=${index}
348     [Return]    ${updated_index_list}
349
350 Stop_Single_Member
351     [Arguments]    ${member}    ${original_index_list}=${EMPTY}    ${confirm}=True
352     [Documentation]    Convenience keyword that stops the specified member of the cluster.
353     ...    The KW will return a list of available members: \${updated index_list}=\${original_index_list}-\${member}
354     ${index_list} =    ClusterManagement__Build_List    ${member}
355     ${updated_index_list} =    Stop_Members_From_List_Or_All    ${index_list}    ${original_index_list}    ${confirm}
356     [Return]    ${updated_index_list}
357
358 Stop_Members_From_List_Or_All
359     [Arguments]    ${member_index_list}=${EMPTY}    ${original_index_list}=${EMPTY}    ${confirm}=True    ${timeout}=120s
360     [Documentation]    If the list is empty, stops all ODL instances. Otherwise stop members based on \${stop_index_list}
361     ...    If \${confirm} is True, verify stopped instances are not there anymore.
362     ...    The KW will return a list of available members: \${updated index_list}=\${original_index_list}-\${member_index_list}
363     ${stop_index_list} =    List_Indices_Or_All    given_list=${member_index_list}
364     ${index_list} =    List_Indices_Or_All    given_list=${original_index_list}
365     Run_Bash_Command_On_List_Or_All    command=${NODE_STOP_COMMAND}    member_index_list=${member_index_list}
366     ${updated_index_list} =    BuiltIn.Create_List    @{index_list}
367     Collections.Remove_Values_From_List    ${updated_index_list}    @{stop_index_list}
368     BuiltIn.Return_From_Keyword_If    not ${confirm}    ${updated_index_list}
369     : FOR    ${index}    IN    @{stop_index_list}
370     \    BuiltIn.Wait Until Keyword Succeeds    ${timeout}    2s    Verify_Karaf_Is_Not_Running_On_Member    member_index=${index}
371     [Return]    ${updated_index_list}
372
373 Start_Single_Member
374     [Arguments]    ${member}    ${wait_for_sync}=True    ${timeout}=300s
375     [Documentation]    Convenience keyword that starts the specified member of the cluster.
376     ${index_list} =    ClusterManagement__Build_List    ${member}
377     Start_Members_From_List_Or_All    ${index_list}    ${wait_for_sync}    ${timeout}
378
379 Start_Members_From_List_Or_All
380     [Arguments]    ${member_index_list}=${EMPTY}    ${wait_for_sync}=True    ${timeout}=300s    ${karaf_home}=${EMPTY}    ${export_java_home}=${EMPTY}
381     [Documentation]    If the list is empty, start all cluster members. Otherwise, start members based on present indices.
382     ...    If ${wait_for_sync}, wait for cluster sync on listed members.
383     ...    Optionally karaf_home can be overriden. Optionally specific JAVA_HOME is used for starting.
384     ${base_command} =    BuiltIn.Set_Variable_If    "${karaf_home}"    ${karaf_home}/bin/start    ${NODE_START_COMMAND}
385     ${command} =    BuiltIn.Set_Variable_If    "${export_java_home}"    export JAVA_HOME="${export_java_home}"; ${base_command}    ${base_command}
386     Run_Bash_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
387     BuiltIn.Return_From_Keyword_If    not ${wait_for_sync}
388     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    10s    Check_Cluster_Is_In_Sync    member_index_list=${member_index_list}
389     # TODO: Do we also want to check Shard Leaders here?
390
391 Freeze_Single_Member
392     [Arguments]    ${member}
393     [Documentation]    Convenience keyword that stops the specified member of the cluster by freezing the jvm.
394     ${index_list} =    ClusterManagement__Build_List    ${member}
395     Freeze_Or_Unfreeze_Members_From_List_Or_All    ${NODE_FREEZE_COMMAND}    ${index_list}
396
397 Unfreeze_Single_Member
398     [Arguments]    ${member}    ${wait_for_sync}=True    ${timeout}=60s
399     [Documentation]    Convenience keyword that "continues" the specified member of the cluster by unfreezing the jvm.
400     ${index_list} =    ClusterManagement__Build_List    ${member}
401     Freeze_Or_Unfreeze_Members_From_List_Or_All    ${NODE_UNFREEZE_COMMAND}    ${index_list}
402     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    10s    Check_Cluster_Is_In_Sync
403
404 Freeze_Or_Unfreeze_Members_From_List_Or_All
405     [Arguments]    ${command}    ${member_index_list}=${EMPTY}
406     [Documentation]    If the list is empty, stops/runs all ODL instances. Otherwise stop/run members based on \${stop_index_list}
407     ...    For command parameter only ${NODE_FREEZE_COMMAND} and ${NODE_UNFREEZE_COMMAND} should be used
408     ${freeze_index_list} =    List_Indices_Or_All    given_list=${member_index_list}
409     Run_Bash_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
410
411 Clean_Journals_And_Snapshots_On_List_Or_All
412     [Arguments]    ${member_index_list}=${EMPTY}    ${karaf_home}=${KARAF_HOME}
413     [Documentation]    Delete journal and snapshots directories on every node listed (or all).
414     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
415     ${command} =    Set Variable    rm -rf "${karaf_home}/journal" "${karaf_home}/snapshots"
416     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
417     \    Run_Bash_Command_On_Member    command=${command}    member_index=${index}
418
419 Verify_Karaf_Is_Not_Running_On_Member
420     [Arguments]    ${member_index}
421     [Documentation]    Fail if non-zero karaf instances are counted on member of given index.
422     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
423     BuiltIn.Should_Be_Equal    0    ${count}    Found running Karaf count: ${count}
424
425 Verify_Single_Karaf_Is_Running_On_Member
426     [Arguments]    ${member_index}
427     [Documentation]    Fail if number of karaf instances on member of given index is not one.
428     ${count} =    Count_Running_Karafs_On_Member    member_index=${member_index}
429     BuiltIn.Should_Be_Equal    1    ${count}    Wrong number of Karafs running: ${count}
430
431 Count_Running_Karafs_On_Member
432     [Arguments]    ${member_index}
433     [Documentation]    Remotely execute grep for karaf process, return count as string.
434     ${command} =    BuiltIn.Set_Variable    ps axf | grep org.apache.karaf | grep -v grep | wc -l
435     ${count} =    Run_Bash_Command_On_Member    command=${command}    member_index=${member_index}
436     [Return]    ${count}
437
438 Isolate_Member_From_List_Or_All
439     [Arguments]    ${isolate_member_index}    ${member_index_list}=${EMPTY}
440     [Documentation]    If the list is empty, isolate member from all ODL instances. Otherwise, isolate member based on present indices.
441     ...    The KW will return a list of available members: \${updated index_list}=\${member_index_list}-\${isolate_member_index}
442     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
443     ${source} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${isolate_member_index}
444     : FOR    ${index}    IN    @{index_list}
445     \    ${destination} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${index}
446     \    ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -I OUTPUT -p all --source ${source} --destination ${destination} -j DROP
447     \    BuiltIn.Run_Keyword_If    "${index}" != "${isolate_member_index}"    Run_Bash_Command_On_Member    command=${command}    member_index=${isolate_member_index}
448     ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -L -n
449     ${output} =    Run_Bash_Command_On_Member    command=${command}    member_index=${isolate_member_index}
450     BuiltIn.Log    ${output}
451     ${updated_index_list} =    BuiltIn.Create_List    @{index_list}
452     Collections.Remove_Values_From_List    ${updated_index_list}    ${isolate_member_index}
453     [Return]    ${updated_index_list}
454
455 Rejoin_Member_From_List_Or_All
456     [Arguments]    ${rejoin_member_index}    ${member_index_list}=${EMPTY}
457     [Documentation]    If the list is empty, rejoin member from all ODL instances. Otherwise, rejoin member based on present indices.
458     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
459     ${source} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${rejoin_member_index}
460     : FOR    ${index}    IN    @{index_list}
461     \    ${destination} =    Collections.Get_From_Dictionary    ${ClusterManagement__index_to_ip_mapping}    ${index}
462     \    ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -D OUTPUT -p all --source ${source} --destination ${destination} -j DROP
463     \    BuiltIn.Run_Keyword_If    "${index}" != "${rejoin_member_index}"    Run_Bash_Command_On_Member    command=${command}    member_index=${rejoin_member_index}
464     ${command} =    BuiltIn.Set_Variable    sudo /sbin/iptables -L -n
465     ${output} =    Run_Bash_Command_On_Member    command=${command}    member_index=${rejoin_member_index}
466     BuiltIn.Log    ${output}
467
468 Flush_Iptables_From_List_Or_All
469     [Arguments]    ${member_index_list}=${EMPTY}
470     [Documentation]    If the list is empty, flush IPTables in all ODL instances. Otherwise, flush member based on present indices.
471     ${command} =    BuiltIn.Set_Variable    sudo iptables -v -F
472     ${output} =    Run_Bash_Command_On_List_Or_All    command=${command}    member_index_list=${member_index_list}
473
474 Run_Bash_Command_On_List_Or_All
475     [Arguments]    ${command}    ${member_index_list}=${EMPTY}
476     [Documentation]    Cycle through indices (or all), run command on each.
477     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
478     : FOR    ${index}    IN    @{index_list}
479     \    Run_Bash_Command_On_Member    command=${command}    member_index=${index}
480
481 Run_Bash_Command_On_Member
482     [Arguments]    ${command}    ${member_index}
483     [Documentation]    Obtain IP, call Utils and return output. This does not preserve active ssh session.
484     # TODO: Rename these keyword to Run_Bash_Command_On_Member to distinguish from Karaf (or even Windows) commands.
485     ${member_ip} =    Collections.Get_From_Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${member_index}
486     ${output} =    SSHKeywords.Run_Keyword_Preserve_Connection    Utils.Run_Command_On_Controller    ${member_ip}    ${command}
487     [Return]    ${output}
488
489 Run_Karaf_Command_On_List_Or_All
490     [Arguments]    ${command}    ${member_index_list}=${EMPTY}    ${timeout}=10s
491     [Documentation]    Cycle through indices (or all), run karaf command on each.
492     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
493     : FOR    ${index}    IN    @{index_list}
494     \    ${member_ip} =    Collections.Get_From_Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${index}
495     \    KarafKeywords.Safe_Issue_Command_On_Karaf_Console    ${command}    ${member_ip}    timeout=${timeout}
496
497 Run_Karaf_Command_On_Member
498     [Arguments]    ${command}    ${member_index}    ${timeout}=10s
499     [Documentation]    Obtain IP address, call KarafKeywords and return output. This does not preserve active ssh session.
500     ...    This keyword is not used by Run_Karaf_Command_On_List_Or_All, but returned output may be useful.
501     ${member_ip} =    Collections.Get_From_Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${member_index}
502     ${output} =    KarafKeywords.Safe_Issue_Command_On_Karaf_Console    ${command}    controller=${member_ip}    timeout=${timeout}
503     [Return]    ${output}
504
505 Install_Feature_On_List_Or_All
506     [Arguments]    ${feature_name}    ${member_index_list}=${EMPTY}    ${timeout}=60s
507     [Documentation]    Attempt installation on each member from list (or all). Then look for failures.
508     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
509     ${status_list} =    BuiltIn.Create_List
510     : FOR    ${index}    IN    @{index_list}
511     \    ${status}    ${text} =    BuiltIn.Run_Keyword_And_Ignore_Error    Install_Feature_On_Member    feature_name=${feature_name}    member_index=${index}
512     \    ...    timeout=${timeout}
513     \    BuiltIn.Log    ${text}
514     \    Collections.Append_To_List    ${status_list}    ${status}
515     : FOR    ${status}    IN    @{status_list}
516     \    BuiltIn.Run_Keyword_If    "${status}" != "PASS"    BuiltIn.Fail    ${feature_name} installation failed, see log.
517
518 Install_Feature_On_Member
519     [Arguments]    ${feature_name}    ${member_index}    ${timeout}=60s
520     [Documentation]    Run feature:install karaf command, fail if installation was not successful. Return output.
521     ${status}    ${output} =    BuiltIn.Run_Keyword_And_Ignore_Error    Run_Karaf_Command_On_Member    command=feature:install ${feature_name}    member_index=${member_index}    timeout=${timeout}
522     BuiltIn.Run_Keyword_If    "${status}" != "PASS"    BuiltIn.Fail    Failed to install ${feature_name}: ${output}
523     BuiltIn.Should_Not_Contain    ${output}    Can't install    Failed to install ${feature_name}: ${output}
524     [Return]    ${output}
525
526 With_Ssh_To_List_Or_All_Run_Keyword
527     [Arguments]    ${member_index_list}    ${keyword_name}    @{args}    &{kwargs}
528     [Documentation]    For each index in given list (or all): activate SSH connection, run given Keyword, close active connection. Return None.
529     ...    Note that if the Keyword affects SSH connections, results are still deterministic, but perhaps undesirable.
530     ...    Beware that in order to avoid "got positional argument after named arguments", first two arguments in the call should not be named.
531     BuiltIn.Comment    This keyword is experimental and there is high risk of being replaced by another approach.
532     # TODO: For_Index_From_List_Or_All_Run_Keyword applied to With_Ssh_To_Member_Run_Keyword?
533     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
534     : FOR    ${member_index}    IN    @{index_list}
535     \    ${member_ip} =    Resolve_IP_Address_For_Member    ${member_index}
536     \    SSHKeywords.Open_Connection_To_Odl_System    ip_address=${member_ip}
537     \    BuiltIn.Run_Keyword    ${keyword_name}    @{args}    &{kwargs}
538     \    SSHLibrary.Close_Connection
539
540 Safe_With_Ssh_To_List_Or_All_Run_Keyword
541     [Arguments]    ${member_index_list}    ${keyword_name}    @{args}    &{kwargs}
542     [Documentation]    Remember active ssh connection index, call With_Ssh_To_List_Or_All_Run_Keyword, return None. Restore the conection index on teardown.
543     SSHKeywords.Run_Keyword_Preserve_Connection    With_Ssh_To_List_Or_All_Run_Keyword    ${member_index_list}    ${keyword_name}    @{args}    &{kwargs}
544
545 Clean_Directories_On_List_Or_All
546     [Arguments]    ${member_index_list}=${EMPTY}    ${directory_list}=${EMPTY}    ${karaf_home}=${KARAF_HOME}
547     [Documentation]    Clear @{directory_list} or @{ODL_DEFAULT_DATA_PATHS} for members in given list or all. Return None.
548     ...    This is intended to return Karaf (offline) to the state it was upon the first boot.
549     ${path_list} =    Builtin.Set Variable If    "${directory_list}" == "${EMPTY}"    ${ODL_DEFAULT_DATA_PATHS}    ${directory_list}
550     Safe_With_Ssh_To_List_Or_All_Run_Keyword    ${member_index_list}    ClusterManagement__Clean_Directories    ${path_list}    ${karaf_home}
551
552 Store_Karaf_Log_On_List_Or_All
553     [Arguments]    ${member_index_list}=${EMPTY}    ${dst_dir}=/tmp    ${karaf_home}=${KARAF_HOME}
554     [Documentation]    Saves karaf.log to the ${dst_dir} for members in given list or all. Return None.
555     Safe_With_Ssh_To_List_Or_All_Run_Keyword    ${member_index_list}    SSHKeywords.Execute_Command_Should_Pass    cp ${karaf_home}/data/log/karaf.log ${dst_dir}
556
557 Restore_Karaf_Log_On_List_Or_All
558     [Arguments]    ${member_index_list}=${EMPTY}    ${src_dir}=/tmp    ${karaf_home}=${KARAF_HOME}
559     [Documentation]    Places stored karaf.log to the ${karaf_home}/data/log for members in given list or all. Return None.
560     Safe_With_Ssh_To_List_Or_All_Run_Keyword    ${member_index_list}    SSHKeywords.Execute_Command_Should_Pass    cp ${src_dir}/karaf.log ${karaf_home}/data/log/
561
562 ClusterManagement__Clean_Directories
563     [Arguments]    ${relative_path_list}    ${karaf_home}
564     [Documentation]    For each relative path, remove files with respect to ${karaf_home}. Return None.
565     : FOR    ${relative_path}    IN    @{relative_path_list}
566     \    SSHLibrary.Execute_Command    rm -rf ${karaf_home}${/}${relative_path}
567
568 Put_As_Json_And_Check_Member_List_Or_All
569     [Arguments]    ${uri}    ${data}    ${member_index}    ${member_index_list}=${EMPTY}
570     [Documentation]    Send a PUT with the supplied uri ${uri} and body ${data} to member ${member_index}.
571     ...    Then check data is replicated in all or some members defined in ${member_index_list}.
572     ${response_text} =    Put_As_Json_To_Member    uri=${uri}    data=${data}    member_index=${member_index}
573     Wait Until Keyword Succeeds    5s    1s    Check_Json_Member_List_Or_All    uri=${uri}    expected_data=${data}    member_index_list=${member_index_list}
574     [Return]    ${response_text}
575
576 Put_As_Json_To_Member
577     [Arguments]    ${uri}    ${data}    ${member_index}
578     [Documentation]    Send a PUT with the supplied uri and data to member ${member_index}.
579     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
580     ${response_text} =    TemplatedRequests.Put_As_Json_To_Uri    uri=${uri}    data=${data}    session=${session}
581     [Return]    ${response_text}
582
583 Post_As_Json_To_Member
584     [Arguments]    ${uri}    ${data}    ${member_index}
585     [Documentation]    Send a POST with the supplied uri and data to member ${member_index}.
586     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
587     ${response_text} =    TemplatedRequests.Post_As_Json_To_Uri    uri=${uri}    data=${data}    session=${session}
588     [Return]    ${response_text}
589
590 Delete_And_Check_Member_List_Or_All
591     [Arguments]    ${uri}    ${member_index}    ${member_index_list}=${EMPTY}
592     [Documentation]    Send a DELETE with the supplied uri to the member ${member_index}.
593     ...    Then check the data is removed from all members in ${member_index_list}.
594     ${response_text} =    Delete_From_Member    ${uri}    ${member_index}
595     BuiltIn.Wait_Until_Keyword_Succeeds    5s    1s    Check_No_Content_Member_List_Or_All    uri=${uri}    member_index_list=${member_index_list}
596     [Return]    ${response_text}
597
598 Delete_From_Member
599     [Arguments]    ${uri}    ${member_index}
600     [Documentation]    Send a DELETE with the supplied uri to member ${member_index}.
601     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
602     ${response_text} =    TemplatedRequests.Delete_From_Uri    uri=${uri}    session=${session}
603     [Return]    ${response_text}
604
605 Check_Json_Member_List_Or_All
606     [Arguments]    ${uri}    ${expected_data}    ${member_index_list}=${EMPTY}
607     [Documentation]    Send a GET with the supplied uri to all or some members defined in ${member_index_list}.
608     ...    Then check received data is = ${expected data}.
609     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
610     : FOR    ${index}    IN    @{index_list}
611     \    ${data} =    Get_From_Member    uri=${uri}    member_index=${index}
612     \    TemplatedRequests.Normalize_Jsons_And_Compare    ${expected_data}    ${data}
613
614 Check_Item_Occurrence_Member_List_Or_All
615     [Arguments]    ${uri}    ${dictionary}    ${member_index_list}=${EMPTY}
616     [Documentation]    Send a GET with the supplied uri to all or some members defined in ${member_index_list}.
617     ...    Then check received for occurrences of items expressed in a dictionary ${dictionary}.
618     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
619     : FOR    ${index}    IN    @{index_list}
620     \    ${data} =    Get_From_Member    uri=${uri}    member_index=${index}
621     \    Utils.Check Item Occurrence    ${data}    ${dictionary}
622
623 Check_No_Content_Member_List_Or_All
624     [Arguments]    ${uri}    ${member_index_list}=${EMPTY}
625     [Documentation]    Send a GET with the supplied uri to all or some members defined in ${member_index_list}.
626     ...    Then check there is no content.
627     ${index_list} =    List_Indices_Or_All    given_list=${member_index_list}
628     : FOR    ${index}    IN    @{index_list}
629     \    ${session} =    Resolve_Http_Session_For_Member    member_index=${index}
630     \    Utils.No_Content_From_URI    ${session}    ${uri}
631
632 Get_From_Member
633     [Arguments]    ${uri}    ${member_index}    ${access}=${ACCEPT_EMPTY}
634     [Documentation]    Send a GET with the supplied uri to member ${member_index}.
635     ${session} =    Resolve_Http_Session_For_Member    member_index=${member_index}
636     ${response_text} =    TemplatedRequests.Get_From_Uri    uri=${uri}    accept=${access}    session=${session}
637     [Return]    ${response_text}
638
639 Resolve_IP_Address_For_Member
640     [Arguments]    ${member_index}
641     [Documentation]    Return node IP address of given index.
642     ${ip_address} =    Collections.Get From Dictionary    dictionary=${ClusterManagement__index_to_ip_mapping}    key=${member_index}
643     [Return]    ${ip_address}
644
645 Resolve_Http_Session_For_Member
646     [Arguments]    ${member_index}
647     [Documentation]    Return RequestsLibrary session alias pointing to node of given index.
648     ${session} =    BuiltIn.Set_Variable    ClusterManagement__session_${member_index}
649     [Return]    ${session}
650
651 Resolve_Shard_Type_Class
652     [Arguments]    ${shard_type}
653     [Documentation]    Simple lookup for class name corresponding to desired type.
654     BuiltIn.Run_Keyword_If    '${shard_type}' == 'config'    BuiltIn.Return_From_Keyword    DistributedConfigDatastore
655     ...    ELSE IF    '${shard_type}' == 'operational'    BuiltIn.Return_From_Keyword    DistributedOperationalDatastore
656     BuiltIn.Fail    Unrecognized shard type: ${shard_type}
657
658 ClusterManagement__Build_List
659     [Arguments]    ${member}
660     ${member_int} =    BuiltIn.Convert_To_Integer    ${member}
661     ${index_list} =    BuiltIn.Create_List    ${member_int}
662     [Return]    ${index_list}
663
664 ClusterManagement__Parse_Sync_Status
665     [Arguments]    ${shard_manager_text}
666     [Documentation]    Return sync status parsed out of given text. Called twice by Get_Sync_Status_Of_Member.
667     BuiltIn.Log    ${shard_manager_text}
668     ${manager_object} =    RequestsLibrary.To_Json    ${shard_manager_text}
669     ${value_object} =    Collections.Get_From_Dictionary    dictionary=${manager_object}    key=value
670     ${sync_status} =    Collections.Get_From_Dictionary    dictionary=${value_object}    key=SyncStatus
671     [Return]    ${sync_status}
672
673 List_All_Indices
674     [Documentation]    Create a new list of all indices.
675     BuiltIn.Run_Keyword_And_Return    List_Indices_Or_All
676
677 List_Indices_Or_All
678     [Arguments]    ${given_list}=${EMPTY}
679     [Documentation]    Utility to allow \${EMPTY} as default argument value, as the internal list is computed at runtime.
680     ...    This keyword always returns a (shallow) copy of given or default list,
681     ...    so operations with the returned list should not affect other lists.
682     ...    Also note that this keyword does not consider empty list to be \${EMPTY}.
683     ${return_list_reference} =    BuiltIn.Set_Variable_If    """${given_list}""" != ""    ${given_list}    ${ClusterManagement__member_index_list}
684     ${return_list_copy} =    BuiltIn.Create_List    @{return_list_reference}
685     [Return]    ${return_list_copy}
686
687 List_Indices_Minus_Member
688     [Arguments]    ${member_index}    ${member_index_list}=${EMPTY}
689     [Documentation]    Create a new list which contains indices from ${member_index_list} (or all) without ${member_index}.
690     ${index_list} =    List_Indices_Or_All    ${member_index_list}
691     Collections.Remove Values From List    ${index_list}    ${member_index}
692     [Return]    ${index_list}
693
694 ClusterManagement__Compute_Derived_Variables
695     [Arguments]    ${int_of_members}
696     [Documentation]    Construct index list, session list and IP mapping, publish them as suite variables.
697     @{member_index_list} =    BuiltIn.Create_List
698     @{session_list} =    BuiltIn.Create_List
699     &{index_to_ip_mapping} =    BuiltIn.Create_Dictionary
700     : FOR    ${index}    IN RANGE    1    ${int_of_members+1}
701     \    ClusterManagement__Include_Member_Index    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
702     BuiltIn.Set_Suite_Variable    \${ClusterManagement__member_index_list}    ${member_index_list}
703     BuiltIn.Set_Suite_Variable    \${ClusterManagement__index_to_ip_mapping}    ${index_to_ip_mapping}
704     BuiltIn.Set_Suite_Variable    \${ClusterManagement__session_list}    ${session_list}
705
706 ClusterManagement__Include_Member_Index
707     [Arguments]    ${index}    ${member_index_list}    ${session_list}    ${index_to_ip_mapping}
708     [Documentation]    Add a corresponding item based on index into the last three arguments.
709     ...    Create the Http session whose alias is added to list.
710     Collections.Append_To_List    ${member_index_list}    ${index}
711     ${member_ip} =    BuiltIn.Set_Variable    ${ODL_SYSTEM_${index}_IP}
712     # ${index} is int (not string) so "key=value" syntax does not work in the following line.
713     Collections.Set_To_Dictionary    ${index_to_ip_mapping}    ${index}    ${member_ip}
714     # Http session, with ${AUTH}, without headers.
715     ${session_alias} =    Resolve_Http_Session_For_Member    member_index=${index}
716     RequestsLibrary.Create_Session    ${session_alias}    http://${member_ip}:${RESTCONFPORT}    auth=${AUTH}    max_retries=0
717     Collections.Append_To_List    ${session_list}    ${session_alias}
718
719 Sync_Status_Should_Be_False
720     [Arguments]    ${controller_index}
721     [Documentation]    Verify that cluster node is not in sync with others
722     ${status}    Get_Sync_Status_Of_Member    ${controller_index}
723     BuiltIn.Should_Not_Be_True    ${status}
724
725 Sync_Status_Should_Be_True
726     [Arguments]    ${controller_index}
727     [Documentation]    Verify that cluster node is in sync with others
728     ${status}    Get_Sync_Status_Of_Member    ${controller_index}
729     BuiltIn.Should_Be_True    ${status}