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