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