OVSDB bridge create test case now using UUID
[integration/test.git] / csit / libraries / ClusterKeywords.robot
1 *** Settings ***
2 Library           RequestsLibrary
3 Library           Collections
4 Library           UtilLibrary.py
5 Library           ClusterStateLibrary.py
6 Library           ./HsfJson/hsf_json.py
7 Resource          Utils.robot
8
9 *** Variables ***
10 ${jolokia_conf}    /jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-config,type=DistributedConfigDatastore
11 ${jolokia_oper}    /jolokia/read/org.opendaylight.controller:Category=ShardManager,name=shard-manager-operational,type=DistributedOperationalDatastore
12 ${jolokia_read}    /jolokia/read/org.opendaylight.controller
13
14 *** Keywords ***
15 Create Controller Index List
16     [Documentation]    Reads number of controllers and returns a list with all controllers indexes.
17     ${controller_index_list}    Create List
18     ${NUM_ODL_SYSTEM}=    Convert to Integer    ${NUM_ODL_SYSTEM}
19     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
20     \    Append To List    ${controller_index_list}    ${i+1}
21     [Return]    ${controller_index_list}
22
23 Create Controller Sessions
24     [Documentation]    Creates REST session to all controller instances.
25     ${NUM_ODL_SYSTEM}=    Convert to Integer    ${NUM_ODL_SYSTEM}
26     : FOR    ${i}    IN RANGE    ${NUM_ODL_SYSTEM}
27     \    Log    Create Session ${ODL_SYSTEM_${i+1}_IP}
28     \    Create Session    controller${i+1}    http://${ODL_SYSTEM_${i+1}_IP}:${RESTCONFPORT}    auth=${AUTH}
29
30 Get Cluster Shard Status
31     [Arguments]    ${controller_index_list}    ${shard_type}    ${shard}
32     [Documentation]    Checks ${shard} status and returns Leader index and a list of Followers from a ${controller_index_list}.
33     ...    ${shard_type} is either config or operational.
34     ${lenght}=    Get Length    ${controller_index_list}
35     Run Keyword If    '${shard_type}' == 'config'    Set Test Variable    ${type}    DistributedConfigDatastore
36     Run Keyword If    '${shard_type}' == 'operational'    Set Test Variable    ${type}    DistributedOperationalDatastore
37     Should Not Be Empty    ${type}    Wrong type, valid values are config and operational.
38     ${leader}=    Set Variable    0
39     ${follower_list}=    Create List
40     : FOR    ${i}    IN    @{controller_index_list}
41     \    ${data}=    Get Data From URI    controller${i}    ${jolokia_read}:Category=Shards,name=member-${i}-shard-${shard}-${shard_type},type=${type}
42     \    Log    ${data}
43     \    ${json}=    To Json    ${data}
44     \    ${status}=    Get From Dictionary    &{json}[value]    RaftState
45     \    Log    Controller ${ODL_SYSTEM_${i}_IP} is ${status} for shard ${shard}
46     \    Run Keyword If    '${status}' == 'Leader'    Set Test Variable    ${leader}    ${i}
47     \    Run Keyword If    '${status}' == 'Follower'    Append To List    ${follower_list}    ${i}
48     Should Not Be Equal    ${leader}    0    No Leader elected in shard ${shard_type} ${shard}
49     Length Should Be    ${follower_list}    ${lenght-1}    Not enough or too many Followers in shard ${shard_type} ${shard}
50     [Return]    ${leader}    ${follower_list}
51
52 Get Cluster Entity Owner Status
53     [Arguments]    ${controller_index_list}    ${device_type}    ${device}
54     [Documentation]    Checks Entity Owner status for a ${device} and returns owner index and list of candidates from a ${controller_index_list}.
55     ...    ${device_type} is openflow, ovsdb, etc...
56     ${length}=    Get Length    ${controller_index_list}
57     ${candidates_list}=    Create List
58     ${data}=    Get Data From URI    controller@{controller_index_list}[0]    /restconf/operational/entity-owners:entity-owners
59     Log    ${data}
60     ${data}=    Replace String    ${data}    /general-entity:entity[general-entity:name='    ${EMPTY}
61     ${clear_data}=    Replace String    ${data}    ']    ${EMPTY}
62     Log    ${clear_data}
63     ${json}=    To Json    ${clear_data}
64     ${entity_type_list}=    Get From Dictionary    &{json}[entity-owners]    entity-type
65     ${entity_type_index}=    Get Index From List Of Dictionaries    ${entity_type_list}    type    ${device_type}
66     Should Not Be Equal    ${entity_type_index}    -1    No Entity Owner found for ${device_type}
67     ${entity_list}=    Get From Dictionary    @{entity_type_list}[${entity_type_index}]    entity
68     ${entity_index}=    Get Index From List Of Dictionaries    ${entity_list}    id    ${device}
69     Should Not Be Equal    ${entity_index}    -1    Device ${device} not found in Entity Owner ${device_type}
70     ${entity_owner}=    Get From Dictionary    @{entity_list}[${entity_index}]    owner
71     Should Not Be Empty    ${entity_owner}    No owner found for ${device}
72     ${owner}=    Replace String    ${entity_owner}    member-    ${EMPTY}
73     ${owner}=    Convert To Integer    ${owner}
74     List Should Contain Value    ${controller_index_list}    ${owner}    Owner ${owner} not exisiting in ${controller_index_list}
75     ${entity_candidates_list}=    Get From Dictionary    @{entity_list}[${entity_index}]    candidate
76     ${list_length}=    Get Length    ${entity_candidates_list}
77     : FOR    ${entity_candidate}    IN    @{entity_candidates_list}
78     \    ${candidate}=    Replace String    &{entity_candidate}[name]    member-    ${EMPTY}
79     \    ${candidate}=    Convert To Integer    ${candidate}
80     \    List Should Contain Value    ${controller_index_list}    ${candidate}    Candidate ${candidate} not exisiting in ${controller_index_list}
81     \    Run Keyword If    '${candidate}' != '${owner}'    Append To List    ${candidates_list}    ${candidate}
82     [Return]    ${owner}    ${candidates_list}
83
84 Check Item Occurrence At URI In Cluster
85     [Arguments]    ${controller_index_list}    ${dictionary_item_occurrence}    ${uri}    ${headers}=${HEADERS}
86     [Documentation]    Send a GET with the supplied ${uri} to all cluster instances in ${controller_index_list}
87     ...    and check for occurrences of items expressed in a dictionary ${dictionary_item_occurrence}.
88     : FOR    ${i}    IN    @{controller_index_list}
89     \    ${data}    Get Data From URI    controller${i}    ${uri}    ${headers}
90     \    Log    ${data}
91     \    Check Item Occurrence    ${data}    ${dictionary_item_occurrence}
92
93 Put And Check At URI In Cluster
94     [Arguments]    ${controller_index_list}    ${controller_index}    ${uri}    ${body}    ${headers}=${HEADERS}
95     [Documentation]    Send a PUT with the supplied ${uri} and ${body} to a ${controller_index}
96     ...    and check the data is replicated in all instances in ${controller_index_list}.
97     ${expected_body}=    Hsf Json    ${body}
98     ${resp}    RequestsLibrary.Put Request    controller${controller_index}    ${uri}    ${body}    ${headers}
99     ${status_code}=    Convert To String    ${resp.status_code}
100     Should Match Regexp    ${status_code}    20(0|1)
101     : FOR    ${i}    IN    @{controller_index_list}
102     \    ${data}    Wait Until Keyword Succeeds    5s    1s    Get Data From URI    controller${i}
103     \    ...    ${uri}    ${headers}
104     \    Log    ${data}
105     \    ${received_body}    Hsf Json    ${data}
106     \    Should Be Equal    ${expected_body}    ${received_body}
107
108 Delete And Check At URI In Cluster
109     [Arguments]    ${controller_index_list}    ${controller_index}    ${uri}    ${headers}=${HEADERS}
110     [Documentation]    Send a DELETE with the supplied ${uri} to a ${controller_index}
111     ...    and check the data is removed from all instances in ${controller_index_list}.
112     ${resp}    RequestsLibrary.Delete Request    controller${controller_index}    ${uri}    ${headers}
113     Should Be Equal As Strings    ${resp.status_code}    200
114     : FOR    ${i}    IN    @{controller_index_list}
115     \    Wait Until Keyword Succeeds    5s    1s    No Content From URI    controller${i}    ${uri}
116     \    ...    ${headers}
117
118 Kill Multiple Controllers
119     [Arguments]    @{controller_index_list}
120     [Documentation]    Give this keyword a scalar or list of controllers to be stopped.
121     : FOR    ${i}    IN    @{controller_index_list}
122     \    ${output}=    Run Command On Controller    ${ODL_SYSTEM_${i}_IP}    ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
123     \    Controller Down Check    ${ODL_SYSTEM_${i}_IP}
124
125 Start Multiple Controllers
126     [Arguments]    ${timeout}    @{controller_index_list}
127     [Documentation]    Give this keyword a scalar or list of controllers to be started.
128     : FOR    ${i}    IN    @{controller_index_list}
129     \    ${output}=    Run Command On Controller    ${ODL_SYSTEM_${i}_IP}    ${WORKSPACE}/${BUNDLEFOLDER}/bin/start
130     \    Wait For Controller Sync    ${timeout}    ${ODL_SYSTEM_${i}_IP}
131
132 Get Controller List
133     [Arguments]    ${exclude_controller}=${EMPTY}
134     [Documentation]    Creates a list of all controllers minus any excluded controller.
135     Log    ${exclude_controller}
136     @{searchlist}    Create List    ${CONTROLLER}    ${CONTROLLER1}    ${CONTROLLER2}
137     Remove Values From List    ${searchlist}    ${exclude_controller}
138     Log    ${searchlist}
139     [Return]    ${searchlist}
140
141 Get Leader And Verify
142     [Arguments]    ${shard_name}    ${old_leader}=${EMPTY}
143     [Documentation]    Returns the IP addr or hostname of the leader of the specified shard.
144     ...    Controllers are specifed in the pybot command line.
145     ${searchlist}    Get Controller List    ${old_leader}
146     ${leader}    GetLeader    ${shard_name}    ${3}    ${3}    ${1}    ${RESTCONFPORT}
147     ...    @{searchlist}
148     Should Not Be Equal As Strings    ${leader}    None
149     Run Keyword If    '${old_leader}'!='${EMPTY}'    Should Not Be Equal    ${old_leader}    ${leader}
150     [Return]    ${leader}
151
152 Expect No Leader
153     [Arguments]    ${shard_name}
154     [Documentation]    No leader is elected in the car shard
155     ${leader}    GetLeader    ${shard_name}    ${3}    ${1}    ${1}    ${RESTCONFPORT}
156     ...    ${CURRENT_CAR_LEADER}
157     Should Be Equal As Strings    ${leader}    None
158
159 Get All Followers
160     [Arguments]    ${shard_name}    ${exclude_controller}=${EMPTY}
161     [Documentation]    Returns the IP addresses or hostnames of all followers of the specified shard.
162     ${searchlist}    Get Controller List    ${exclude_controller}
163     ${followers}    GetFollowers    ${shard_name}    ${3}    ${3}    ${1}    ${RESTCONFPORT}
164     ...    @{searchlist}
165     Log    ${followers}
166     Should Not Be Empty    ${followers}
167     [Return]    ${followers}
168
169 Stop One Or More Controllers
170     [Arguments]    @{controllers}
171     [Documentation]    Give this keyword a scalar or list of controllers to be stopped.
172     ${cmd} =    Set Variable    ${KARAF_HOME}/bin/stop
173     : FOR    ${ip}    IN    @{controllers}
174     \    Run Command On Remote System    ${ip}    ${cmd}
175
176 Kill One Or More Controllers
177     [Arguments]    @{controllers}
178     [Documentation]    Give this keyword a scalar or list of controllers to be stopped.
179     ${cmd} =    Set Variable    ps axf | grep karaf | grep -v grep | awk '{print \"kill -9 \" $1}' | sh
180     log    ${cmd}
181     : FOR    ${ip}    IN    @{controllers}
182     \    Run Command On Remote System    ${ip}    ${cmd}
183
184 Wait For Cluster Down
185     [Arguments]    ${timeout}    @{controllers}
186     [Documentation]    Waits for one or more clustered controllers to be down.
187     : FOR    ${ip}    IN    @{controllers}
188     \    ${status}=    Run Keyword And Return Status    Wait For Controller Down    ${timeout}    ${ip}
189     \    Exit For Loop If    '${status}' == 'FAIL'
190
191 Wait For Controller Down
192     [Arguments]    ${timeout}    ${ip}
193     [Documentation]    Waits for one controllers to be down.
194     Wait Until Keyword Succeeds    ${timeout}    2s    Controller Down Check    ${ip}
195
196 Controller Down Check
197     [Arguments]    ${ip}
198     [Documentation]    Checks to see if a controller is down by verifying that the karaf process isn't present.
199     ${cmd} =    Set Variable    ps axf | grep karaf | grep -v grep | wc -l
200     ${response}    Run Command On COntroller    ${ip}    ${cmd}
201     Log    Number of controller instances running: ${response}
202     Should Start With    ${response}    0    Controller process found or there may be extra instances of karaf running on the host machine.
203
204 Start One Or More Controllers
205     [Arguments]    @{controllers}
206     [Documentation]    Give this keyword a scalar or list of controllers to be started.
207     ${cmd} =    Set Variable    ${KARAF_HOME}/bin/start
208     : FOR    ${ip}    IN    @{controllers}
209     \    Run Command On Remote System    ${ip}    ${cmd}
210
211 Wait For Cluster Sync
212     [Arguments]    ${timeout}    @{controllers}
213     [Documentation]    Waits for one or more clustered controllers to report Sync Status as true.
214     : FOR    ${ip}    IN    @{controllers}
215     \    ${status}=    Run Keyword And Return Status    Wait For Controller Sync    ${timeout}    ${ip}
216     \    Exit For Loop If    '${status}' == 'FAIL'
217
218 Wait For Controller Sync
219     [Arguments]    ${timeout}    ${ip}
220     [Documentation]    Waits for one controllers to report Sync Status as true.
221     Wait Until Keyword Succeeds    ${timeout}    2s    Controller Sync Status Should Be True    ${ip}
222
223 Controller Sync Status Should Be True
224     [Arguments]    ${ip}
225     [Documentation]    Checks if Sync Status is true.
226     ${SyncStatus}=    Get Controller Sync Status    ${ip}
227     Should Be Equal    ${SyncStatus}    ${TRUE}
228
229 Controller Sync Status Should Be False
230     [Arguments]    ${ip}
231     [Documentation]    Checks if Sync Status is false.
232     ${SyncStatus}=    Get Controller Sync Status    ${ip}
233     Should Be Equal    ${SyncStatus}    ${FALSE}
234
235 Get Controller Sync Status
236     [Arguments]    ${controller_ip}
237     [Documentation]    Return Sync Status.
238     Create_Session    session    http://${controller_ip}:${RESTCONFPORT}    headers=${HEADERS}    auth=${AUTH}
239     ${data}=    Get Data From URI    session    ${jolokia_conf}
240     Log    ${data}
241     ${json}=    To Json    ${data}
242     ${value}=    Get From Dictionary    ${json}    value
243     ${ConfSyncStatus}=    Get From Dictionary    ${value}    SyncStatus
244     Log    Configuration Sync Status: ${ConfSyncStatus}
245     ${data}=    Get Data From URI    session    ${jolokia_oper}
246     Log    ${data}
247     ${json}=    To Json    ${data}
248     ${value}=    Get From Dictionary    ${json}    value
249     ${OperSyncStatus}=    Get From Dictionary    ${value}    SyncStatus
250     Log    Operational Sync Status: ${OperSyncStatus}
251     Run Keyword If    ${OperSyncStatus} and ${ConfSyncStatus}    Set Test Variable    ${SyncStatus}    ${TRUE}
252     ...    ELSE    Set Test Variable    ${SyncStatus}    ${FALSE}
253     [Return]    ${SyncStatus}
254
255 Clean One Or More Journals
256     [Arguments]    @{controllers}
257     [Documentation]    Give this keyword a scalar or list of controllers on which to clean journals.
258     ${del_cmd} =    Set Variable    rm -rf ${KARAF_HOME}/journal
259     : FOR    ${ip}    IN    @{controllers}
260     \    Run Command On Remote System    ${ip}    ${del_cmd}
261
262 Clean One Or More Snapshots
263     [Arguments]    @{controllers}
264     [Documentation]    Give this keyword a scalar or list of controllers on which to clean snapshots.
265     ${del_cmd} =    Set Variable    rm -rf ${KARAF_HOME}/snapshots
266     : FOR    ${ip}    IN    @{controllers}
267     \    Run Command On Remote System    ${ip}    ${del_cmd}
268
269 Show Cluster Configuation Files
270     [Arguments]    @{controllers}
271     [Documentation]    Prints out the cluster configuration files for one or more controllers.
272     Log    controllers: @{controllers}
273     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/akka.conf
274     : FOR    ${ip}    IN    @{controllers}
275     \    Run Command On Remote System    ${ip}    ${cmd}
276     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/modules.conf
277     : FOR    ${ip}    IN    @{controllers}
278     \    Run Command On Remote System    ${ip}    ${cmd}
279     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/module-shards.conf
280     : FOR    ${ip}    IN    @{controllers}
281     \    Run Command On Remote System    ${ip}    ${cmd}
282     ${cmd} =    Set Variable    cat ${KARAF_HOME}/configuration/initial/jolokia.xml
283     : FOR    ${ip}    IN    @{controllers}
284     \    Run Command On Remote System    ${ip}    ${cmd}
285     ${cmd} =    Set Variable    cat ${KARAF_HOME}/etc/initial/org.apache.karaf.management.cfg
286     : FOR    ${ip}    IN    @{controllers}
287     \    Run Command On Remote System    ${ip}    ${cmd}
288     ${cmd} =    Set Variable    cat ${KARAF_HOME}/etc/org.apache.karaf.features.cfg
289     : FOR    ${ip}    IN    @{controllers}
290     \    Run Command On Remote System    ${ip}    ${cmd}
291
292 Isolate a Controller From Cluster
293     [Arguments]    ${isolated controller}    @{controllers}
294     [Documentation]    Use IPTables to isolate one controller from the cluster.
295     ...    On the isolated controller it blocks IP traffic to and from each of the other controllers.
296     : FOR    ${controller}    IN    @{controllers}
297     \    ${other controller}=    Evaluate    "${isolated controller}" != "${controller}"
298     \    Run Keyword If    ${other controller}    Isolate One Controller From Another    ${isolated controller}    ${controller}
299
300 Rejoin a Controller To Cluster
301     [Arguments]    ${isolated controller}    @{controllers}
302     [Documentation]    Use IPTables to rejoin one controller to the cluster.
303     ...    On the isolated controller it unblocks IP traffic to and from each of the other controllers.
304     : FOR    ${controller}    IN    @{controllers}
305     \    ${other controller}=    Evaluate    "${isolated controller}" != "${controller}"
306     \    Run Keyword If    ${other controller}    Rejoin One Controller To Another    ${isolated controller}    ${controller}
307
308 Isolate One Controller From Another
309     [Arguments]    ${isolated controller}    ${controller}
310     [Documentation]    Inserts an IPTable rule to disconnect one controller from another controller in the cluster.
311     Modify IPTables    ${isolated controller}    ${controller}    -I
312
313 Rejoin One Controller To Another
314     [Arguments]    ${isolated controller}    ${controller}
315     [Documentation]    Deletes an IPTable rule, allowing one controller to reconnect to another controller in the cluster.
316     Modify IPTables    ${isolated controller}    ${controller}    -D
317
318 Modify IPTables
319     [Arguments]    ${isolated controller}    ${controller}    ${rule type}
320     [Documentation]    Adds a rule, usually inserting or deleting an entry between two controllers.
321     ${base string}    Set Variable    sudo iptables ${rule type} OUTPUT -p all --source
322     ${cmd string}    Catenate    ${base string}    ${isolated controller} --destination ${controller} -j DROP
323     Run Command On Remote System    ${isolated controller}    ${cmd string}
324     ${cmd string}    Catenate    ${base string}    ${controller} --destination ${isolated controller} -j DROP
325     Run Command On Remote System    ${isolated controller}    ${cmd string}
326     ${cmd string}    Set Variable    sudo iptables -L -n
327     ${return string}=    Run Command On Remote System    ${isolated controller}    ${cmd string}
328     #If inserting rules:
329     Run Keyword If    "${rule type}" == '-I'    Should Match Regexp    ${return string}    [\s\S]*DROP *all *-- *${isolated controller} *${controller}[\s\S]*
330     Run Keyword If    "${rule type}" == '-I'    Should Match Regexp    ${return string}    [\s\S]*DROP *all *-- *${controller} *${isolated controller}[\s\S]*
331     #If deleting rules:
332     Run Keyword If    "${rule type}" == '-D'    Should Match Regexp    ${return string}    (?![\s\S]*DROP *all *-- *${isolated controller} *${controller}[\s\S]*)
333     Run Keyword If    "${rule type}" == '-D'    Should Match Regexp    ${return string}    (?![\s\S]*DROP *all *-- *${controller} *${isolated controller}[\s\S]*)
334
335 Rejoin All Isolated Controllers
336     [Arguments]    @{controllers}
337     [Documentation]    Wipe all IPTables rules from all controllers, thus rejoining all controllers.
338     : FOR    ${isolated controller}    IN    @{controllers}
339     \    Flush IPTables    ${isolated controller}
340
341 Flush IPTables
342     [Arguments]    ${isolated controller}
343     [Documentation]    This keyword is generally not called from a test case but supports a complete wipe of all rules on
344     ...    all contollers.
345     ${cmd string}    Set Variable    sudo iptables -v -F
346     ${return string}=    Run Command On Remote System    ${isolated controller}    ${cmd string}
347     Log    return: ${return string}
348     Should Contain    ${return string}    Flushing chain `INPUT'
349     Should Contain    ${return string}    Flushing chain `FORWARD'
350     Should Contain    ${return string}    Flushing chain `OUTPUT'