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