Merge device specific keywords to retrieve entity ownership data
[integration/test.git] / csit / libraries / NetconfKeywords.robot
1 *** Settings ***
2 Documentation     Perform complex operations on netconf.
3 ...
4 ...               Copyright (c) 2015,2017 Cisco Systems, Inc. and others. All rights reserved.
5 ...
6 ...               This program and the accompanying materials are made available under the
7 ...               terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 ...               and is available at http://www.eclipse.org/legal/epl-v10.html
9 ...
10 ...
11 ...               This library encapsulates a bunch of somewhat complex and commonly used
12 ...               netconf operations into reusable keywords to make writing netconf
13 ...               test suites easier.
14 ...
15 ...               TODO: RemoteBash.robot contains logic which could be reused here.
16 Library           Collections
17 Library           DateTime
18 Library           RequestsLibrary
19 Library           SSHLibrary
20 Resource          NexusKeywords.robot
21 Resource          Restconf.robot
22 Resource          SSHKeywords.robot
23 Resource          TemplatedRequests.robot
24 Resource          Utils.robot
25 Resource          RemoteBash.robot
26
27 *** Variables ***
28 ${MAX_HEAP}       1G
29 ${TESTTOOL_DEFAULT_JAVA_OPTIONS}    -Xmx${MAX_HEAP} -Djava.security.egd=file:/dev/./urandom
30 ${DIRECTORY_WITH_DEVICE_TEMPLATES}    ${CURDIR}/../variables/netconf/device
31 ${FIRST_TESTTOOL_PORT}    17830
32 ${BASE_NETCONF_DEVICE_PORT}    17830
33 ${DEVICE_NAME_BASE}    netconf-scaling-device
34 ${TESTTOOL_BOOT_TIMEOUT}    60s
35 ${ENABLE_NETCONF_TEST_TIMEOUT}    ${ENABLE_GLOBAL_TEST_DEADLINES}
36 ${SSE_CFG_FILE}    ${WORKSPACE}/${BUNDLEFOLDER}/etc/org.opendaylight.restconf.nb.rfc8040.cfg
37
38 *** Keywords ***
39 Setup_NetconfKeywords
40     [Arguments]    ${create_session_for_templated_requests}=True
41     [Documentation]    Setup the environment for the other keywords of this Resource to work properly.
42     ${tmp}=    BuiltIn.Create_Dictionary
43     BuiltIn.Set_Suite_Variable    ${NetconfKeywords__mounted_device_types}    ${tmp}
44     BuiltIn.Run_Keyword_If    ${create_session_for_templated_requests}    TemplatedRequests.Create_Default_Session    timeout=2
45     NexusKeywords.Initialize_Artifact_Deployment_And_Usage
46
47 Configure_Device_In_Netconf
48     [Arguments]    ${device_name}    ${device_type}=default    ${device_port}=${FIRST_TESTTOOL_PORT}    ${device_address}=${TOOLS_SYSTEM_IP}    ${device_user}=admin    ${device_password}=topsecret
49     ...    ${device_key}=device-key    ${session}=default    ${schema_directory}=/tmp/schema    ${http_timeout}=${EMPTY}    ${http_method}=put
50     [Documentation]    Tell Netconf about the specified device so it can add it into its configuration.
51     ${mapping}=    BuiltIn.Create_dictionary    DEVICE_IP=${device_address}    DEVICE_NAME=${device_name}    DEVICE_PORT=${device_port}    DEVICE_USER=${device_user}    DEVICE_PASSWORD=${device_password}
52     ...    DEVICE_KEY=${device_key}    SCHEMA_DIRECTORY=${schema_directory}
53     # TODO: Is it possible to use &{kwargs} as a mapping directly?
54     Run Keyword if    '${http_method}'=='post'    TemplatedRequests.Post_As_Xml_Templated    folder=${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    mapping=${mapping}    session=${session}    http_timeout=${http_timeout}
55     ...    ELSE    TemplatedRequests.Put_As_Xml_Templated    folder=${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    mapping=${mapping}    session=${session}    http_timeout=${http_timeout}
56     Collections.Set_To_Dictionary    ${NetconfKeywords__mounted_device_types}    ${device_name}    ${device_type}
57
58 Configure_Device
59     [Arguments]    ${current_name}    ${log_response}=True
60     [Documentation]    Operation for configuring the device.
61     KarafKeywords.Log_Message_To_Controller_Karaf    Configuring device ${current_name} to Netconf
62     Configure_Device_In_Netconf    ${current_name}    device_type=${device_type}    device_port=${current_port}
63     KarafKeywords.Log_Message_To_Controller_Karaf    Device ${current_name} configured
64
65 Configure_Device_And_Verify
66     [Arguments]    ${current_name}    ${log_response}=True
67     [Documentation]    Operation for configuring the device in the Netconf subsystem and connecting to it.
68     Configure_Device    ${current_name}    ${log_response}
69     KarafKeywords.Log_Message_To_Controller_Karaf    Waiting for device ${current_name} to connect
70     Wait_Device_Connected    ${current_name}    period=0.5s    log_response=${log_response}
71     KarafKeywords.Log_Message_To_Controller_Karaf    Device ${current_name} connected
72
73 Count_Netconf_Connectors_For_Device
74     [Arguments]    ${device_name}    ${session}=default
75     [Documentation]    Count all instances of the specified device in the Netconf topology (usually 0 or 1).
76     # FIXME: This no longer counts netconf connectors, it counts "device instances in Netconf topology".
77     # This keyword should be renamed but without an automatic keyword naming standards checker this is
78     # potentially destabilizing change so right now it is as FIXME. Proposed new name:
79     # Count_Device_Instances_In_Netconf_Topology
80     ${uri} =    Restconf.Generate URI    network-topology:network-topology    operational
81     ${mounts}=    TemplatedRequests.Get_As_Json_From_Uri    ${uri}    session=${session}
82     Builtin.Log    ${mounts}
83     ${actual_count}=    Builtin.Evaluate    len('''${mounts}'''.split('"node-id": "${device_name}"'))-1
84     Builtin.Return_From_Keyword    ${actual_count}
85
86 Wait_Connected
87     [Arguments]    ${current_name}    ${log_response}=True
88     [Documentation]    Operation for waiting until the device is connected.
89     KarafKeywords.Log_Message_To_Controller_Karaf    Waiting for device ${current_name} to connect
90     Wait_Device_Connected    ${current_name}    period=0.5s    timeout=300s    log_response=${log_response}
91     KarafKeywords.Log_Message_To_Controller_Karaf    Device ${current_name} connected
92
93 Check_Device_Has_No_Netconf_Connector
94     [Arguments]    ${device_name}    ${session}=default
95     [Documentation]    Check that there are no instances of the specified device in the Netconf topology.
96     # FIXME: Similarlt to "Count_Netconf_Connectors_For_Device", this does not check whether the device has
97     # no netconf connector but whether the device is present in the netconf topology or not. Rename, proposed
98     # new name: Check_Device_Not_Present_In_Netconf_Topology
99     ${count}    Count_Netconf_Connectors_For_Device    ${device_name}    session=${session}
100     Builtin.Should_Be_Equal_As_Strings    ${count}    0
101
102 Check_Device_Completely_Gone
103     [Arguments]    ${device_name}    ${session}=default    ${log_response}=True
104     [Documentation]    Check that the specified device has no Netconf connectors nor associated data.
105     Check_Device_Has_No_Netconf_Connector    ${device_name}    session=${session}
106     ${uri} =    Restconf.Generate URI    network-topology:network-topology    config    topology=topology-netconf    node=${device_name}
107     Utils.No Content From URI    ${session}    ${uri}
108
109 Check_Device_Connected
110     [Arguments]    ${device_name}    ${session}=default    ${log_response}=True
111     [Documentation]    Check that the specified device is accessible from Netconf.
112     ${uri} =    Restconf.Generate URI    network-topology:network-topology    operational    topology=topology-netconf    node=${device_name}
113     ${device_status}=    TemplatedRequests.Get_As_Json_From_Uri    ${uri}    session=${session}    log_response=${log_response}
114     Builtin.Should_Contain    ${device_status}    "netconf-node-topology:connection-status": "connected"
115
116 Wait_Device_Connected
117     [Arguments]    ${device_name}    ${timeout}=20s    ${period}=1s    ${session}=default    ${log_response}=True
118     [Documentation]    Wait for the device to become connected.
119     ...    It is more readable to use this keyword in a test case than to put the whole WUKS below into it.
120     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${period}    Check_Device_Connected    ${device_name}    session=${session}    log_response=${log_response}
121
122 Remove_Device_From_Netconf
123     [Arguments]    ${device_name}    ${session}=default    ${location}=location
124     [Documentation]    Tell Netconf to deconfigure the specified device
125     ${device_type}=    Collections.Pop_From_Dictionary    ${NetconfKeywords__mounted_device_types}    ${device_name}
126     ${template_as_string}=    BuiltIn.Set_Variable    {'DEVICE_NAME': '${device_name}'}
127     TemplatedRequests.Delete_Templated    ${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    ${template_as_string}    session=${session}    location=${location}
128
129 Deconfigure_Device
130     [Arguments]    ${current_name}    ${log_response}=True
131     [Documentation]    Operation for deconfiguring the device.
132     KarafKeywords.Log_Message_To_Controller_Karaf    Deconfiguring device ${current_name}
133     Remove_Device_From_Netconf    ${current_name}
134     KarafKeywords.Log_Message_To_Controller_Karaf    Device ${current_name} deconfigured
135
136 Deconfigure_Device_And_Verify
137     [Arguments]    ${current_name}    ${log_response}=True
138     [Documentation]    Operation for deconfiguring the device from Netconf.
139     Deconfigure_Device    ${current_name}    ${log_response}
140     Check_Device_Deconfigured    ${current_name}
141
142 Check_Device_Deconfigured
143     [Arguments]    ${current_name}    ${log_response}=True
144     [Documentation]    Operation for making sure the device is really deconfigured.
145     KarafKeywords.Log_Message_To_Controller_Karaf    Waiting for device ${current_name} to disappear
146     Wait_Device_Fully_Removed    ${current_name}    period=0.5s    timeout=120s
147     KarafKeywords.Log_Message_To_Controller_Karaf    Device ${current_name} removed
148
149 Wait_Device_Fully_Removed
150     [Arguments]    ${device_name}    ${timeout}=10s    ${period}=1s    ${session}=default    ${log_response}=True
151     [Documentation]    Wait until all netconf connectors for the device with the given name disappear.
152     ...    Call of Remove_Device_From_Netconf returns before netconf gets
153     ...    around deleting the device's connector. To ensure the device is
154     ...    really gone from netconf, use this keyword to make sure all
155     ...    connectors disappear. If a call to Remove_Device_From_Netconf
156     ...    is not made before using this keyword, the wait will fail.
157     ...    Using this keyword is more readable than putting the WUKS below
158     ...    into a test case.
159     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${period}    Check_Device_Completely_Gone    ${device_name}    session=${session}    log_response=${log_response}
160
161 NetconfKeywords__Deploy_Additional_Schemas
162     [Arguments]    ${schemas}
163     [Documentation]    Internal keyword for Install_And_Start_TestTool
164     ...    This deploys the additional schemas if any and returns a
165     ...    command line argument to be added to the testtool commandline
166     ...    to tell it to load them. While this code could be integrated
167     ...    into its only user, I considered the resulting code to be too
168     ...    unreadable as the actions are quite different in the two
169     ...    possibilities (additional schemas present versus no additional
170     ...    schemas present), therefore a separate keyword is used.
171     # Make sure there is no schemas directory on the remote machine. A
172     # previous test suite might have left some debris there and that might
173     # lead to spurious failures, so it is better to make sure we start with a
174     # clean slate. Additionally when the caller did not specify any
175     # additional schemas for testtool, we want to make extra sure none are
176     # used.
177     ${response}=    SSHLibrary.Execute_Command    rm -rf schemas 2>&1
178     BuiltIn.Log    ${response}
179     # Drop out of the keyword, returning no command line argument when there
180     # are no additional schemas to deploy.
181     BuiltIn.Return_From_Keyword_If    '${schemas}' == 'none'    ${EMPTY}
182     # Deploy the additional schemas into a standard directory on the remote
183     # machine and construct a command line argument pointing to that
184     # directory from the point of view of the process running on that
185     # machine.
186     SSHLibrary.Put_Directory    ${schemas}    destination=./schemas
187     SSHLibrary.List_Directory    ./schemas
188     [Return]    --schemas-dir ./schemas
189
190 NetconfKeywords__Deploy_Custom_RPC
191     [Arguments]    ${rpc_config}
192     [Documentation]    Internal keyword for Install_And_Start_TestTool
193     ...    This deploys the optional custom rpc file.
194     ...    Drop out of the keyword, returning no command line argument when there
195     ...    is no rpc file to deploy.
196     BuiltIn.Return_From_Keyword_If    '${rpc_config}' == 'none'    ${EMPTY}
197     SSHKeywords.Copy_File_To_Tools_System    ${TOOLS_SYSTEM_1_IP}    ${rpc_config}    /tmp
198     [Return]    --rpc-config /tmp/customaction.xml
199
200 NetconfKeywords__Check_Device_Is_Up
201     [Arguments]    ${last-port}
202     ${count}=    SSHKeywords.Count_Port_Occurences    ${last-port}    LISTEN    java
203     BuiltIn.Should_Be_Equal_As_Integers    ${count}    1
204
205 NetconfKeywords__Wait_Device_Is_Up_And_Running
206     [Arguments]    ${device_name}    ${log_response}=True
207     ${number}=    BuiltIn.Evaluate    '${device_name}'.split('-').pop()
208     BuiltIn.Wait_Until_Keyword_Succeeds    ${TESTTOOL_BOOT_TIMEOUT}    1s    Check_Device_Up_And_Running    ${number}
209
210 Install_And_Start_Testtool
211     [Arguments]    ${device-count}=10    ${debug}=true    ${schemas}=none    ${rpc_config}=none    ${tool_options}=${EMPTY}    ${java_options}=${TESTTOOL_DEFAULT_JAVA_OPTIONS}
212     ...    ${mdsal}=true    ${log_response}=True
213     [Documentation]    Install and run testtool.
214     ${filename}=    NexusKeywords.Deploy_Test_Tool    netconf    netconf-testtool
215     Start_Testtool    ${filename}    ${device-count}    ${debug}    ${schemas}    ${rpc_config}    ${tool_options}
216     ...    ${java_options}    ${mdsal}    log_response=${log_response}
217
218 Start_Testtool
219     [Arguments]    ${filename}    ${device-count}=10    ${debug}=true    ${schemas}=none    ${rpc_config}=none    ${tool_options}=${EMPTY}
220     ...    ${java_options}=${TESTTOOL_DEFAULT_JAVA_OPTIONS}    ${mdsal}=true    ${log_response}=True
221     [Documentation]    Arrange to collect tool's output into a log file.
222     ...    Will use specific ${schemas} unless argument resolves to 'none',
223     ...    which signifies that there are no additional schemas to be deployed.
224     ...    If so the directory for the additional schemas is deleted on the
225     ...    remote machine and the additional schemas argument is left out.
226     ${schemas_option}=    NetconfKeywords__Deploy_Additional_Schemas    ${schemas}
227     ${rpc_config_option}=    NetconfKeywords__Deploy_Custom_RPC    ${rpc_config}
228     ${command}=    NexusKeywords.Compose_Full_Java_Command    ${java_options} -jar ${filename} ${tool_options} --device-count ${device-count} --debug ${debug} ${schemas_option} ${rpc_config_option} --md-sal ${mdsal}
229     BuiltIn.Log    Running testtool: ${command}
230     ${logfile}=    Utils.Get_Log_File_Name    testtool
231     BuiltIn.Set_Suite_Variable    ${testtool_log}    ${logfile}
232     SSHLibrary.Write    ${command} >${logfile} 2>&1
233     # Store information needed by other keywords.
234     BuiltIn.Set_Suite_Variable    ${NetconfKeywords__testtool_device_count}    ${device-count}
235     # Wait for the testtool to boot up.
236     Perform_Operation_On_Each_Device    NetconfKeywords__Wait_Device_Is_Up_And_Running    log_response=${log_response}
237
238 Check_Device_Up_And_Running
239     [Arguments]    ${device-number}
240     [Documentation]    Query netstat on remote machine whether testtool device with the specified number has its port open and fail if not.
241     ${device-port}=    BuiltIn.Evaluate    ${FIRST_TESTTOOL_PORT}+${device-number}-1
242     NetconfKeywords__Check_Device_Is_Up    ${device-port}
243
244 Stop_Testtool
245     [Documentation]    Stop testtool and download its log.
246     RemoteBash.Write_Bare_Ctrl_C
247     SSHLibrary.Read_Until_Prompt
248     # TODO: Unify with play.py and pcc-mock handling.
249     # TODO: Maybe this keyword's content shall be moved into SSHUtils and named somewhat like
250     # "Interrupt_Program_And_Download_Its_Log" which will get an argument stating the name of
251     # the log file to get.
252     SSHLibrary.Get_File    ${testtool_log}
253
254 NetconfKeywords__Check_Netconf_Test_Timeout_Not_Expired
255     [Arguments]    ${deadline_Date}
256     BuiltIn.Return_From_Keyword_If    not ${ENABLE_NETCONF_TEST_TIMEOUT}
257     ${current_Date}=    DateTime.Get_Current_Date
258     ${ellapsed_seconds}=    DateTime.Subtract_Date_From_Date    ${deadline_Date}    ${current_Date}
259     BuiltIn.Run_Keyword_If    ${ellapsed_seconds}<0    Fail    The global time out period expired
260
261 NetconfKeywords__Perform_Operation_With_Checking_On_Next_Device
262     [Arguments]    ${operation}    ${deadline_Date}    ${log_response}=True
263     NetconfKeywords__Check_Netconf_Test_Timeout_Not_Expired    ${deadline_Date}
264     ${number}=    BuiltIn.Evaluate    ${current_port}-${BASE_NETCONF_DEVICE_PORT}+1
265     BuiltIn.Run_Keyword    ${operation}    ${DEVICE_NAME_BASE}-${number}    log_response=${log_response}
266     ${next}=    BuiltIn.Evaluate    ${current_port}+1
267     BuiltIn.Set_Suite_Variable    ${current_port}    ${next}
268
269 Perform_Operation_On_Each_Device
270     [Arguments]    ${operation}    ${count}=${NetconfKeywords__testtool_device_count}    ${timeout}=45m    ${log_response}=True
271     ${current_Date}=    DateTime.Get_Current_Date
272     ${deadline_Date}=    DateTime.Add_Time_To_Date    ${current_Date}    ${timeout}
273     BuiltIn.Set_Suite_Variable    ${current_port}    ${BASE_NETCONF_DEVICE_PORT}
274     BuiltIn.Repeat_Keyword    ${count} times    NetconfKeywords__Perform_Operation_With_Checking_On_Next_Device    ${operation}    ${deadline_Date}    log_response=${log_response}
275
276 Disable SSE On Controller
277     [Arguments]    ${controller_ip}
278     [Documentation]    Sets the config for using SSE (Server Side Events) to false. Note that
279     ...    this keyword only changes the config. A controller restart is needed for the config to
280     ...    to take effect.
281     SSHLibrary.Open Connection    ${controller_ip}
282     Login With Public Key    ${ODL_SYSTEM_USER}    ${USER_HOME}/.ssh/${SSH_KEY}    any
283     ${cmd}=    Set Variable    echo "use-sse=false" > ${SSE_CFG_FILE}
284     SSHLibrary.Execute Command    ${cmd}
285     SSHLibrary.Close Connection
286
287 Enable SSE On Controller
288     [Arguments]    ${controller_ip}
289     [Documentation]    Sets the config for using SSE (Server Side Events) to true. Note that
290     ...    this keyword only changes the config. A controller restart is needed for the config to
291     ...    to take effect.
292     SSHLibrary.Open Connection    ${controller_ip}
293     Login With Public Key    ${ODL_SYSTEM_USER}    ${USER_HOME}/.ssh/${SSH_KEY}    any
294     ${cmd}=    Set Variable    echo "use-sse=true" > ${SSE_CFG_FILE}
295     SSHLibrary.Execute Command    ${cmd}
296     SSHLibrary.Close Connection