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