Add Library/Keywords to dynamically generate URI
[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}
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
37 *** Keywords ***
38 Setup_NetconfKeywords
39     [Arguments]    ${create_session_for_templated_requests}=True
40     [Documentation]    Setup the environment for the other keywords of this Resource to work properly.
41     ${tmp}=    BuiltIn.Create_Dictionary
42     BuiltIn.Set_Suite_Variable    ${NetconfKeywords__mounted_device_types}    ${tmp}
43     BuiltIn.Run_Keyword_If    ${create_session_for_templated_requests}    TemplatedRequests.Create_Default_Session    timeout=2
44     NexusKeywords.Initialize_Artifact_Deployment_And_Usage
45
46 Configure_Device_In_Netconf
47     [Arguments]    ${device_name}    ${device_type}=default    ${device_port}=${FIRST_TESTTOOL_PORT}    ${device_address}=${TOOLS_SYSTEM_IP}    ${device_user}=admin    ${device_password}=topsecret
48     ...    ${device_key}=device-key    ${session}=default    ${schema_directory}=/tmp/schema    ${http_timeout}=${EMPTY}    ${http_method}=put
49     [Documentation]    Tell Netconf about the specified device so it can add it into its configuration.
50     ${mapping}=    BuiltIn.Create_dictionary    DEVICE_IP=${device_address}    DEVICE_NAME=${device_name}    DEVICE_PORT=${device_port}    DEVICE_USER=${device_user}    DEVICE_PASSWORD=${device_password}
51     ...    DEVICE_KEY=${device_key}    SCHEMA_DIRECTORY=${schema_directory}
52     # TODO: Is it possible to use &{kwargs} as a mapping directly?
53     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}
54     ...    ELSE    TemplatedRequests.Put_As_Xml_Templated    folder=${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    mapping=${mapping}    session=${session}    http_timeout=${http_timeout}
55     Collections.Set_To_Dictionary    ${NetconfKeywords__mounted_device_types}    ${device_name}    ${device_type}
56
57 Count_Netconf_Connectors_For_Device
58     [Arguments]    ${device_name}    ${session}=default
59     [Documentation]    Count all instances of the specified device in the Netconf topology (usually 0 or 1).
60     # FIXME: This no longer counts netconf connectors, it counts "device instances in Netconf topology".
61     # This keyword should be renamed but without an automatic keyword naming standards checker this is
62     # potentially destabilizing change so right now it is as FIXME. Proposed new name:
63     # Count_Device_Instances_In_Netconf_Topology
64     ${uri} =    Restconf.Generate URI    network-topology:network-topology    operational
65     ${mounts}=    TemplatedRequests.Get_As_Json_From_Uri    ${uri}    session=${session}
66     Builtin.Log    ${mounts}
67     ${actual_count}=    Builtin.Evaluate    len('''${mounts}'''.split('"node-id": "${device_name}"'))-1
68     Builtin.Return_From_Keyword    ${actual_count}
69
70 Check_Device_Has_No_Netconf_Connector
71     [Arguments]    ${device_name}    ${session}=default
72     [Documentation]    Check that there are no instances of the specified device in the Netconf topology.
73     # FIXME: Similarlt to "Count_Netconf_Connectors_For_Device", this does not check whether the device has
74     # no netconf connector but whether the device is present in the netconf topology or not. Rename, proposed
75     # new name: Check_Device_Not_Present_In_Netconf_Topology
76     ${count}    Count_Netconf_Connectors_For_Device    ${device_name}    session=${session}
77     Builtin.Should_Be_Equal_As_Strings    ${count}    0
78
79 Check_Device_Completely_Gone
80     [Arguments]    ${device_name}    ${session}=default
81     [Documentation]    Check that the specified device has no Netconf connectors nor associated data.
82     Check_Device_Has_No_Netconf_Connector    ${device_name}    session=${session}
83     ${uri} =    Restconf.Generate URI    network-topology:network-topology    config    topology=topology-netconf    node=${device_name}
84     ${status}    ${response}=    BuiltIn.Run_Keyword_And_Ignore_Error    TemplatedRequests.Get_As_Xml_From_Uri    ${uri}    session=${session}
85     BuiltIn.Should_Be_Equal_As_Strings    ${status}    FAIL
86     BuiltIn.Should_Contain    ${response}    404
87
88 Check_Device_Connected
89     [Arguments]    ${device_name}    ${session}=default
90     [Documentation]    Check that the specified device is accessible from Netconf.
91     ${uri} =    Restconf.Generate URI    network-topology:network-topology    operational    topology=topology-netconf    node=${device_name}
92     ${device_status}=    TemplatedRequests.Get_As_Json_From_Uri    ${uri}    session=${session}
93     Builtin.Should_Contain    ${device_status}    "netconf-node-topology:connection-status": "connected"
94
95 Wait_Device_Connected
96     [Arguments]    ${device_name}    ${timeout}=20s    ${period}=1s    ${session}=default
97     [Documentation]    Wait for the device to become connected.
98     ...    It is more readable to use this keyword in a test case than to put the whole WUKS below into it.
99     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${period}    Check_Device_Connected    ${device_name}    session=${session}
100
101 Remove_Device_From_Netconf
102     [Arguments]    ${device_name}    ${session}=default    ${location}=location
103     [Documentation]    Tell Netconf to deconfigure the specified device
104     ${device_type}=    Collections.Pop_From_Dictionary    ${NetconfKeywords__mounted_device_types}    ${device_name}
105     ${template_as_string}=    BuiltIn.Set_Variable    {'DEVICE_NAME': '${device_name}'}
106     TemplatedRequests.Delete_Templated    ${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    ${template_as_string}    session=${session}    location=${location}
107
108 Wait_Device_Fully_Removed
109     [Arguments]    ${device_name}    ${timeout}=10s    ${period}=1s    ${session}=default
110     [Documentation]    Wait until all netconf connectors for the device with the given name disappear.
111     ...    Call of Remove_Device_From_Netconf returns before netconf gets
112     ...    around deleting the device's connector. To ensure the device is
113     ...    really gone from netconf, use this keyword to make sure all
114     ...    connectors disappear. If a call to Remove_Device_From_Netconf
115     ...    is not made before using this keyword, the wait will fail.
116     ...    Using this keyword is more readable than putting the WUKS below
117     ...    into a test case.
118     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${period}    Check_Device_Completely_Gone    ${device_name}    session=${session}
119
120 NetconfKeywords__Deploy_Additional_Schemas
121     [Arguments]    ${schemas}
122     [Documentation]    Internal keyword for Install_And_Start_TestTool
123     ...    This deploys the additional schemas if any and returns a
124     ...    command line argument to be added to the testtool commandline
125     ...    to tell it to load them. While this code could be integrated
126     ...    into its only user, I considered the resulting code to be too
127     ...    unreadable as the actions are quite different in the two
128     ...    possibilities (additional schemas present versus no additional
129     ...    schemas present), therefore a separate keyword is used.
130     # Make sure there is no schemas directory on the remote machine. A
131     # previous test suite might have left some debris there and that might
132     # lead to spurious failures, so it is better to make sure we start with a
133     # clean slate. Additionally when the caller did not specify any
134     # additional schemas for testtool, we want to make extra sure none are
135     # used.
136     ${response}=    SSHLibrary.Execute_Command    rm -rf schemas 2>&1
137     BuiltIn.Log    ${response}
138     # Drop out of the keyword, returning no command line argument when there
139     # are no additional schemas to deploy.
140     BuiltIn.Return_From_Keyword_If    '${schemas}' == 'none'    ${EMPTY}
141     # Deploy the additional schemas into a standard directory on the remote
142     # machine and construct a command line argument pointing to that
143     # directory from the point of view of the process running on that
144     # machine.
145     SSHLibrary.Put_Directory    ${schemas}    destination=./schemas
146     SSHLibrary.List_Directory    ./schemas
147     [Return]    --schemas-dir ./schemas
148
149 NetconfKeywords__Deploy_Custom_RPC
150     [Arguments]    ${rpc_config}
151     [Documentation]    Internal keyword for Install_And_Start_TestTool
152     ...    This deploys the optional custom rpc file.
153     ...    Drop out of the keyword, returning no command line argument when there
154     ...    is no rpc file to deploy.
155     BuiltIn.Return_From_Keyword_If    '${rpc_config}' == 'none'    ${EMPTY}
156     SSHKeywords.Copy_File_To_Tools_System    ${TOOLS_SYSTEM_1_IP}    ${rpc_config}    /tmp
157     [Return]    --rpc-config /tmp/customaction.xml
158
159 NetconfKeywords__Check_Device_Is_Up
160     [Arguments]    ${last-port}
161     ${count}=    SSHKeywords.Count_Port_Occurences    ${last-port}    LISTEN    java
162     BuiltIn.Should_Be_Equal_As_Integers    ${count}    1
163
164 NetconfKeywords__Wait_Device_Is_Up_And_Running
165     [Arguments]    ${device_name}
166     ${number}=    BuiltIn.Evaluate    '${device_name}'.split('-').pop()
167     BuiltIn.Wait_Until_Keyword_Succeeds    ${TESTTOOL_BOOT_TIMEOUT}    1s    Check_Device_Up_And_Running    ${number}
168
169 Install_And_Start_Testtool
170     [Arguments]    ${device-count}=10    ${debug}=true    ${schemas}=none    ${rpc_config}=none    ${tool_options}=${EMPTY}    ${java_options}=${TESTTOOL_DEFAULT_JAVA_OPTIONS}
171     ...    ${mdsal}=true
172     [Documentation]    Install and run testtool.
173     ${filename}=    NexusKeywords.Deploy_Test_Tool    netconf    netconf-testtool
174     Start_Testtool    ${filename}    ${device-count}    ${debug}    ${schemas}    ${rpc_config}    ${tool_options}
175     ...    ${java_options}    ${mdsal}
176
177 Start_Testtool
178     [Arguments]    ${filename}    ${device-count}=10    ${debug}=true    ${schemas}=none    ${rpc_config}=none    ${tool_options}=${EMPTY}
179     ...    ${java_options}=${TESTTOOL_DEFAULT_JAVA_OPTIONS}    ${mdsal}=true
180     [Documentation]    Arrange to collect tool's output into a log file.
181     ...    Will use specific ${schemas} unless argument resolves to 'none',
182     ...    which signifies that there are no additional schemas to be deployed.
183     ...    If so the directory for the additional schemas is deleted on the
184     ...    remote machine and the additional schemas argument is left out.
185     ${schemas_option}=    NetconfKeywords__Deploy_Additional_Schemas    ${schemas}
186     ${rpc_config_option}=    NetconfKeywords__Deploy_Custom_RPC    ${rpc_config}
187     ${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}
188     BuiltIn.Log    Running testtool: ${command}
189     ${logfile}=    Utils.Get_Log_File_Name    testtool
190     BuiltIn.Set_Suite_Variable    ${testtool_log}    ${logfile}
191     SSHLibrary.Write    ${command} >${logfile} 2>&1
192     # Store information needed by other keywords.
193     BuiltIn.Set_Suite_Variable    ${NetconfKeywords__testtool_device_count}    ${device-count}
194     # Wait for the testtool to boot up.
195     Perform_Operation_On_Each_Device    NetconfKeywords__Wait_Device_Is_Up_And_Running
196
197 Check_Device_Up_And_Running
198     [Arguments]    ${device-number}
199     [Documentation]    Query netstat on remote machine whether testtool device with the specified number has its port open and fail if not.
200     ${device-port}=    BuiltIn.Evaluate    ${FIRST_TESTTOOL_PORT}+${device-number}-1
201     NetconfKeywords__Check_Device_Is_Up    ${device-port}
202
203 Stop_Testtool
204     [Documentation]    Stop testtool and download its log.
205     RemoteBash.Write_Bare_Ctrl_C
206     SSHLibrary.Read_Until_Prompt
207     # TODO: Unify with play.py and pcc-mock handling.
208     # TODO: Maybe this keyword's content shall be moved into SSHUtils and named somewhat like
209     # "Interrupt_Program_And_Download_Its_Log" which will get an argument stating the name of
210     # the log file to get.
211     SSHLibrary.Get_File    ${testtool_log}
212
213 NetconfKeywords__Check_Netconf_Test_Timeout_Not_Expired
214     [Arguments]    ${deadline_Date}
215     BuiltIn.Return_From_Keyword_If    not ${ENABLE_NETCONF_TEST_TIMEOUT}
216     ${current_Date}=    DateTime.Get_Current_Date
217     ${ellapsed_seconds}=    DateTime.Subtract_Date_From_Date    ${deadline_Date}    ${current_Date}
218     BuiltIn.Run_Keyword_If    ${ellapsed_seconds}<0    Fail    The global time out period expired
219
220 NetconfKeywords__Perform_Operation_With_Checking_On_Next_Device
221     [Arguments]    ${operation}    ${deadline_Date}
222     NetconfKeywords__Check_Netconf_Test_Timeout_Not_Expired    ${deadline_Date}
223     ${number}=    BuiltIn.Evaluate    ${current_port}-${BASE_NETCONF_DEVICE_PORT}+1
224     BuiltIn.Run_Keyword    ${operation}    ${DEVICE_NAME_BASE}-${number}
225     ${next}=    BuiltIn.Evaluate    ${current_port}+1
226     BuiltIn.Set_Suite_Variable    ${current_port}    ${next}
227
228 Perform_Operation_On_Each_Device
229     [Arguments]    ${operation}    ${count}=${NetconfKeywords__testtool_device_count}    ${timeout}=30m
230     ${current_Date}=    DateTime.Get_Current_Date
231     ${deadline_Date}=    DateTime.Add_Time_To_Date    ${current_Date}    ${timeout}
232     BuiltIn.Set_Suite_Variable    ${current_port}    ${BASE_NETCONF_DEVICE_PORT}
233     BuiltIn.Repeat_Keyword    ${count} times    NetconfKeywords__Perform_Operation_With_Checking_On_Next_Device    ${operation}    ${deadline_Date}