TLS case with CA signed certificate for server
[integration/test.git] / csit / libraries / KarafKeywords.robot
1 *** Settings ***
2 Documentation     Karaf library. This library is useful to deal with controller Karaf console for ssh sessions in cluster.
3 ...               Running Setup Karaf Keywords is necessary. If SetupUtils initialization is called, this gets initialized as well.
4 ...               If this gets initialized, ClusterManagement gets initialized as well.
5 Library           SSHLibrary
6 Library           OperatingSystem
7 Resource          ${CURDIR}/ClusterManagement.robot
8 Resource          ${CURDIR}/SSHKeywords.robot
9 Variables         ${CURDIR}/../variables/Variables.py
10
11 *** Variables ***
12 ${WORKSPACE}      /tmp
13 ${connection_index_dict}    &{EMPTY}
14
15 *** Keywords ***
16 Setup Karaf Keywords
17     [Documentation]    Initialize ClusterManagement. Open ssh karaf connections to each ODL.
18     ClusterManagement.ClusterManagement_Setup
19     BuiltIn.Comment    First connections to Karaf console may fail, so WUKS is used. TODO: Track as a Bug.
20     : FOR    ${index}    IN    @{ClusterManagement__member_index_list}
21     \    BuiltIn.Wait_Until_Keyword_Succeeds    10x    0.2s    Open Controller Karaf Console On Background    member_index=${index}
22
23 Verify Feature Is Installed
24     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
25     [Documentation]    Will Succeed if the given ${feature_name} is found in the output of "feature:list -i"
26     ${output}=    Issue Command On Karaf Console    feature:list -i | grep ${feature_name}    ${controller}    ${karaf_port}
27     Should Contain    ${output}    ${feature_name}
28     [Return]    ${output}
29
30 Issue Command On Karaf Console
31     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=10    ${loglevel}=INFO
32     [Documentation]    Will execute the given ${cmd} by ssh'ing to the karaf console running on ${controller}
33     ...    Note that this keyword will open&close new SSH connection, without switching back to previously current session.
34     Open Connection    ${controller}    port=${karaf_port}    prompt=${KARAF_PROMPT}    timeout=${timeout}
35     Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
36     Write    ${cmd}
37     ${output}    Read Until    ${KARAF_PROMPT}
38     Close Connection
39     Log    ${output}
40     [Return]    ${output}
41
42 Safe_Issue_Command_On_Karaf_Console
43     [Arguments]    ${cmd}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=10    ${loglevel}=INFO
44     [Documentation]    Run Issue_Command_On_Karaf_Console but restore previous connection afterwards.
45     BuiltIn.Run_Keyword_And_Return    SSHKeywords.Run_Keyword_Preserve_Connection    Issue_Command_On_Karaf_Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
46     ...    ${loglevel}
47
48 Check For Elements On Karaf Command Output Message
49     [Arguments]    ${cmd}    ${elements}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=5
50     [Documentation]    Will execute the command using Issue Command On Karaf Console then check for the given elements
51     ...    in the command output message
52     ${output}    Issue Command On Karaf Console    ${cmd}    ${controller}    ${karaf_port}    ${timeout}
53     : FOR    ${i}    IN    @{elements}
54     \    Should Contain    ${output}    ${i}
55
56 Verify Bundle Is Installed
57     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
58     [Documentation]    Will succeed if the given ${bundle name} is present in the output of "bundle:list -s "
59     ${output}=    Issue Command On Karaf Console    bundle:list -s | grep ${bundle_name}    ${controller}    ${karaf_port}
60     Should Contain    ${output}    ${bundle_name}
61     [Return]    ${output}
62
63 Verify Bundle Is Not Installed
64     [Arguments]    ${bundle_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}
65     [Documentation]    Will succeed if the given ${bundle_name} is NOT found in the output of "bundle:list -s"
66     ${output}=    Issue Command On Karaf Console    bundle:list -i | grep ${bundle_name}    ${controller}    ${karaf_port}
67     Should Not Contain    ${output}    ${bundle_name}
68     [Return]    ${output}
69
70 Check Karaf Log Has Messages
71     [Arguments]    ${filter_string}    @{message_list}
72     [Documentation]    Will succeed if the @{messages} are found in \ the output of "log:display"
73     ${output}=    Issue Command On Karaf Console    log:display | grep ${filter_string}
74     : FOR    ${message}    IN    @{message_list}
75     \    Should Contain    ${output}    ${message}
76     [Return]    ${output}
77
78 Install a Feature
79     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
80     [Documentation]    Will Install the given ${feature_name}
81     Log    ${timeout}
82     ${output}=    Issue Command On Karaf Console    feature:install ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
83     Log    ${output}
84     [Return]    ${output}
85
86 Install a Feature Using Active Connection
87     [Arguments]    ${feature_name}
88     [Documentation]    Will Install the given ${feature_name} using active connection
89     ${cmd}=    BuiltIn.Set_Variable    feature:install ${feature_name}
90     Write    ${cmd}
91     ${output}    SSHLibrary.Read_Until    ${KARAF_PROMPT}
92     Log    ${output}
93     [Return]    ${output}
94
95 Uninstall a Feature
96     [Arguments]    ${feature_name}    ${controller}=${ODL_SYSTEM_IP}    ${karaf_port}=${KARAF_SHELL_PORT}    ${timeout}=180
97     [Documentation]    Will UnInstall the given ${feature_name}
98     ${output}=    Issue Command On Karaf Console    feature:uninstall ${feature_name}    ${controller}    ${karaf_port}    ${timeout}
99     Log    ${output}
100     [Return]    ${output}
101
102 Open Controller Karaf Console On Background
103     [Arguments]    ${member_index}=${1}    ${timeout}=10    ${loglevel}=INFO
104     [Documentation]    If there is a stored ssh connection index of connection to the controller's karaf console for ${member_index},
105     ...    close the previous connection. In any case create a new connection
106     ...    to karaf console for ${member_index}, set correct prompt set and login to karaf console.
107     ...    Store connection index for ${member_index} and restore the previous active connection.
108     ${current_ssh_connection_object}=    SSHLibrary.Get Connection
109     BuiltIn.Log    ${connection_index_dict}
110     BuiltIn.Log    ${member_index}
111     ${status}    ${old_connection_index} =    BuiltIn.Run Keyword And Ignore Error    Get From Dictionary    ${connection_index_dict}    ${member_index}
112     BuiltIn.Run Keyword If    '${status}'=='PASS'    BuiltIn.Run Keywords    SSHLibrary.Switch Connection    ${old_connection_index}
113     ...    AND    SSHLibrary.Close Connection
114     ${odl_ip}=    ClusterManagement.Resolve_IP_Address_For_Member    ${member_index}
115     SSHLibrary.Open Connection    ${odl_ip}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}    timeout=${timeout}
116     ${karaf_connection_object}=    SSHLibrary.Get Connection
117     Collections.Set To Dictionary    ${connection_index_dict}    ${member_index}    ${karaf_connection_object.index}
118     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
119     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_ssh_connection_object.index}
120
121 Open Controller Karaf Console With Timeout
122     [Arguments]    ${member_index}=${1}    ${timeout}=3s
123     [Documentation]    Open new connection to karaf console for member index with specified timeout.
124     BuiltIn.Log    ${member_index}
125     ${odl_ip}=    ClusterManagement.Resolve_IP_Address_For_Member    ${member_index}
126     SSHLibrary.Open Connection    ${odl_ip}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_DETAILED_PROMPT}    timeout=${timeout}
127     SSHLibrary.Login    ${KARAF_USER}    ${KARAF_PASSWORD}
128
129 Configure Timeout For Karaf Console
130     [Arguments]    ${timeout}    ${member_index_list}=${EMPTY}
131     [Documentation]    Configure a different timeout for each Karaf console.
132     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${member_index_list}
133     ${current_connection_object}=    SSHLibrary.Get Connection
134     : FOR    ${member_index}    IN    @{index_list}    # usually: 1, 2, 3
135     \    ${karaf_connection_index}=    Collections.Get From Dictionary    ${connection_index_dict}    ${member_index}
136     \    SSHLibrary.Switch Connection    ${karaf_connection_index}
137     \    SSHLibrary.Set_Client_Configuration    timeout=${timeout}
138     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_connection_object.index}
139
140 Execute Controller Karaf Command On Background
141     [Arguments]    ${command}    ${member_index}=${1}
142     [Documentation]    Send command to karaf without affecting current SSH connection. Read, log and return response.
143     ${karaf_connection_index}=    Collections.Get From Dictionary    ${connection_index_dict}    ${member_index}
144     ${current_connection_index}=    SSHLibrary.Switch Connection    ${karaf_connection_index}
145     ${status_write}    ${message_write}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Write    ${command}
146     ${status_wait}    ${message_wait}=    BuiltIn.Run Keyword And Ignore Error    SSHLibrary.Read Until Prompt
147     BuiltIn.Run Keyword If    '${status_write}' != 'PASS'    BuiltIn.Fail    Failed to send the command: ${command}
148     BuiltIn.Log    ${message_wait}
149     BuiltIn.Run Keyword If    '${status_wait}' != 'PASS'    BuiltIn.Fail    Failed to see prompt after sending the command: ${command}
150     [Teardown]    SSHKeywords.Restore Current SSH Connection From Index    ${current_connection_index}
151     [Return]    ${message_wait}
152
153 Execute Controller Karaf Command With Retry On Background
154     [Arguments]    ${command}    ${member_index}=${1}
155     [Documentation]    Attemp to send command to karaf for ${member_index}, if fail then open connection and try again.
156     ${status}    ${message}=    BuiltIn.Run Keyword And Ignore Error    Execute Controller Karaf Command On Background    ${command}    ${member_index}
157     BuiltIn.Return_From_Keyword_If    '${status}' == 'PASS'    ${message}
158     # TODO: Verify this does not leak connections indices.
159     Open Controller Karaf Console On Background    ${member_index}
160     ${message}=    Execute Controller Karaf Command On Background    ${command}    ${member_index}
161     [Return]    ${message}
162
163 Log Message To Controller Karaf
164     [Arguments]    ${message}    ${member_index_list}=${EMPTY}    ${tolerate_failure}=True
165     [Documentation]    Make sure this resource is initialized. Send a message into the controller's karaf log file on every node listed (or all).
166     ...    By default, failure while processing a node is silently ignored, unless ${tolerate_failure} is False.
167     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${member_index_list}
168     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
169     \    ${status}    ${output}=    BuiltIn.Run Keyword And Ignore Error    Execute Controller Karaf Command With Retry On Background    log:log "ROBOT MESSAGE: ${message}"    member_index=${index}
170     \    BuiltIn.Run_Keyword_Unless    ${tolerate_failure} or "${status}" == "PASS"    BuiltIn.Fail    ${output}
171
172 Log Test Suite Start To Controller Karaf
173     [Arguments]    ${member_index_list}=${EMPTY}
174     [Documentation]    Log suite name to karaf log, useful in suite setup.
175     Log Message To Controller Karaf    Starting suite ${SUITE_SOURCE}    ${member_index_list}
176
177 Log Testcase Start To Controller Karaf
178     [Arguments]    ${member_index_list}=${EMPTY}
179     [Documentation]    Log test case name to karaf log, useful in test case setup.
180     Log Message To Controller Karaf    Starting test ${TEST_NAME}    ${member_index_list}
181
182 Set Bgpcep Log Levels
183     [Arguments]    ${bgpcep_level}=${DEFAULT_BGPCEP_LOG_LEVEL}    ${protocol_level}=${DEFAULT_PROTOCOL_LOG_LEVEL}    ${member_index_list}=${EMPTY}
184     [Documentation]    Assuming OCKCOB was used, set logging level on bgpcep and protocol loggers without affecting current SSH session.
185     # FIXME: Move to appropriate Resource
186     ${index_list} =    ClusterManagement.List Indices Or All    given_list=${member_index_list}
187     : FOR    ${index}    IN    @{index_list}    # usually: 1, 2, 3.
188     \    Execute Controller Karaf Command On Background    log:set ${bgpcep_level} org.opendaylight.bgpcep    member_index=${index}
189     \    Execute Controller Karaf Command On Background    log:set ${protocol_level} org.opendaylight.protocol    member_index=${index}
190
191 Wait For Karaf Log
192     [Arguments]    ${message}    ${timeout}=60    ${member_index}=${1}
193     [Documentation]    Read karaf logs until message appear
194     # TODO: refactor this keyword to use the new workflow to account for multiple controllers.    Initial work was done
195     # in this patch https://git.opendaylight.org/gerrit/#/c/45596/
196     # however, the consumers of this keyword were breaking after that change.    Initial theory is that a previous
197     # keyword used before this "Wait For Karaf Log" keyword was closing the karaf console connection, so the
198     # "Flexible SSH Login" keyword from the patch above (45596) was failing.
199     Log    Waiting for '${message}' in karaf log
200     Open Connection    ${ODL_SYSTEM_IP}    port=${KARAF_SHELL_PORT}    prompt=${KARAF_PROMPT}    timeout=${timeout}
201     Login    ${KARAF_USER}    ${KARAF_PASSWORD}    loglevel=${loglevel}
202     Write    log:tail
203     Read Until    ${message}
204     Close Connection
205
206 Restart Bundle
207     [Arguments]    ${bundle_id}
208     [Documentation]    Restarts bundle passed as argument. Note this operation is only for testing and not production environments
209     # TODO: prepare this for cluster environment and multiple controllers
210     Safe_Issue_Command_On_Karaf_Console    bundle:restart -f $(bundle:id '${bundle_id}')
211
212 Restart Karaf
213     [Documentation]    Restarts Karaf and polls log to detect when Karaf is up and running again
214     # TODO: prepare this for cluster environment and multiple controllers
215     Safe_Issue_Command_On_Karaf_Console    log:clear
216     Issue_Command_On_Karaf_Console    shutdown -r -f
217     Run Keyword And Return Status    Wait Until Keyword Succeeds    240s    60s    Wait For Karaf Log    Karaf started in
218
219 Restart Jetty
220     [Documentation]    Restarts jetty bundle (to reload certificates or key/truststore information)
221     Safe_Issue_Command_On_Karaf_Console    log:clear
222     Restart Bundle    OPS4J Pax Web - Jetty
223     Wait For Karaf Log    Instantiated the Application class org.opendaylight.restconf.RestconfApplication
224     Wait For Karaf Log    Instantiated the Application class org.opendaylight.netconf.sal.rest.impl.RestconfApplication
225     Wait For Karaf Log    Instantiated the Application class org.opendaylight.aaa.idm.IdmLightApplication