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