8b46af6b0b3f750759cb2b63c3cc698a125be66c
[integration/test.git] / csit / libraries / NetconfKeywords.robot
1 *** Settings ***
2 Documentation     Perform complex operations on netconf.
3 ...
4 ...               Copyright (c) 2015 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 Library           Collections
15 Library           DateTime
16 Library           RequestsLibrary
17 Resource          NetconfViaRestconf.robot
18 Resource          NexusKeywords.robot
19 Resource          SSHKeywords.robot
20
21 *** Variables ***
22 ${TESTTOOL_DEFAULT_JAVA_OPTIONS}    -Xmx1G -XX:MaxPermSize=256M -Dorg.apache.sshd.registerBouncyCastle=false
23 ${DIRECTORY_WITH_DEVICE_TEMPLATES}    ${CURDIR}/../variables/netconf/device
24 ${FIRST_TESTTOOL_PORT}    17830
25 ${BASE_NETCONF_DEVICE_PORT}    17830
26 ${DEVICE_NAME_BASE}    netconf-scaling-device
27 ${TESTTOOL_DEVICE_TIMEOUT}    60s
28 ${ENABLE_NETCONF_TEST_TIMEOUT}    ${ENABLE_GLOBAL_TEST_DEADLINES}
29
30 *** Keywords ***
31 Setup_NetconfKeywords
32     [Documentation]    Setup the environment for the other keywords of this Resource to work properly.
33     ${tmp}=    BuiltIn.Create_Dictionary
34     BuiltIn.Set_Suite_Variable    ${NetconfKeywords__mounted_device_types}    ${tmp}
35     NetconfViaRestconf.Setup_Netconf_Via_Restconf
36
37 Configure_Device_In_Netconf
38     [Arguments]    ${device_name}    ${device_type}=default    ${device_port}=${FIRST_TESTTOOL_PORT}
39     [Documentation]    Tell Netconf about the specified device so it can add it into its configuration.
40     ${template_as_string}=    BuiltIn.Set_Variable    {'DEVICE_IP': '${TOOLS_SYSTEM_IP}', 'DEVICE_NAME': '${device_name}', 'DEVICE_PORT': '${device_port}'}
41     NetconfViaRestconf.Put_Xml_Template_Folder_Via_Restconf    ${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    ${template_as_string}
42     Collections.Set_To_Dictionary    ${NetconfKeywords__mounted_device_types}    ${device_name}    ${device_type}
43
44 Count_Netconf_Connectors_For_Device
45     [Arguments]    ${device_name}
46     [Documentation]    Count all Netconf connectors referring to the specified device (usually 0 or 1).
47     ${mounts}=    Utils.Get_Data_From_URI    operational    network-topology:network-topology/topology/topology-netconf
48     Builtin.Log    ${mounts}
49     ${actual_count}=    Builtin.Evaluate    len('''${mounts}'''.split('"node-id":"${device_name}"'))-1
50     Builtin.Return_From_Keyword    ${actual_count}
51
52 Check_Device_Has_No_Netconf_Connector
53     [Arguments]    ${device_name}
54     [Documentation]    Check that there are no Netconf connectors referring to the specified device.
55     ${count}    Count_Netconf_Connectors_For_Device    ${device_name}
56     Builtin.Should_Be_Equal_As_Strings    ${count}    0
57
58 Check_Device_Completely_Gone
59     [Arguments]    ${device_name}
60     [Documentation]    Check that the specified device has no Netconf connectors nor associated data.
61     Check_Device_Has_No_Netconf_Connector    ${device_name}
62     ${uri}=    Builtin.Set_Variable    network-topology:network-topology/topology/topology-netconf/node/${device_name}
63     ${response}=    RequestsLibrary.Get Request    nvr_session    ${uri}    ${ACCEPT_XML}
64     BuiltIn.Should_Be_Equal_As_Integers    ${response.status_code}    404
65
66 Check_Device_Connected
67     [Arguments]    ${device_name}
68     [Documentation]    Check that the specified device is accessible from Netconf.
69     ${device_status}=    Utils.Get_Data_From_URI    operational    network-topology:network-topology/topology/topology-netconf/node/${device_name}
70     Builtin.Should_Contain    ${device_status}    "netconf-node-topology:connection-status":"connected"
71
72 Wait_Device_Connected
73     [Arguments]    ${device_name}    ${timeout}=10s    ${period}=1s
74     [Documentation]    Wait for the device to become connected.
75     ...    It is more readable to use this keyword in a test case than to put the whole WUKS below into it.
76     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${period}    Check_Device_Connected    ${device_name}
77
78 Remove_Device_From_Netconf
79     [Arguments]    ${device_name}
80     [Documentation]    Tell Netconf to deconfigure the specified device
81     ${device_type}=    Collections.Pop_From_Dictionary    ${NetconfKeywords__mounted_device_types}    ${device_name}
82     ${template_as_string}=    BuiltIn.Set_Variable    {'DEVICE_NAME': '${device_name}'}
83     NetconfViaRestconf.Delete_Xml_Template_Folder_Via_Restconf    ${DIRECTORY_WITH_DEVICE_TEMPLATES}${/}${device_type}    ${template_as_string}
84
85 Wait_Device_Fully_Removed
86     [Arguments]    ${device_name}    ${timeout}=10s    ${period}=1s
87     [Documentation]    Wait until all netconf connectors for the device with the given name disappear.
88     ...    Call of Remove_Device_From_Netconf returns before netconf gets
89     ...    around deleting the device's connector. To ensure the device is
90     ...    really gone from netconf, use this keyword to make sure all
91     ...    connectors disappear. If a call to Remove_Device_From_Netconf
92     ...    is not made before using this keyword, the wait will fail.
93     ...    Using this keyword is more readable than putting the WUKS below
94     ...    into a test case.
95     BuiltIn.Wait_Until_Keyword_Succeeds    ${timeout}    ${period}    Check_Device_Completely_Gone    ${device_name}
96
97 NetconfKeywords__Deploy_Additional_Schemas
98     [Arguments]    ${schemas}
99     [Documentation]    Internal keyword for Install_And_Start_TestTool
100     ...    This deploys the additional schemas if any and returns a
101     ...    command line argument to be added to the testtool commandline
102     ...    to tell it to load them. While this code could be integrated
103     ...    into its only user, I considered the resulting code to be too
104     ...    unreadable as the actions are quite different in the two
105     ...    possibilities (additional schemas present versus no additional
106     ...    schemas present), therefore a separate keyword is used.
107     # Make sure there is no schemas directory on the remote machine. A
108     # previous test suite might have left some debris there and that might
109     # lead to spurious failures, so it is better to make sure we start with a
110     # clean slate. Additionally when the caller did not specify any
111     # additional schemas for testtool, we want to make extra sure none are
112     # used.
113     ${response}=    SSHLibrary.Execute_Command    rm -rf schemas 2>&1
114     BuiltIn.Log    ${response}
115     # Drop out of the keyword, returning no command line argument when there
116     # are no additional schemas to deploy.
117     BuiltIn.Return_From_Keyword_If    '${schemas}' == 'none'    ${EMPTY}
118     # Deploy the additional schemas into a standard directory on the remote
119     # machine and construct a command line argument pointing to that
120     # directory from the point of view of the process running on that
121     # machine.
122     SSHLibrary.Put_Directory    ${schemas}    destination=./schemas
123     [Return]    --schemas-dir ./schemas
124
125 NetconfKeywords__Check_Device_Is_Up
126     [Arguments]    ${last-port}
127     ${count}=    SSHKeywords.Count_Port_Occurences    ${last-port}    LISTEN    java
128     BuiltIn.Should_Be_Equal_As_Integers    ${count}    1
129
130 NetconfKeywords__Wait_Device_Is_Up_And_Running
131     [Arguments]    ${device_name}
132     ${number}=    BuiltIn.Evaluate    '${device_name}'.split('-').pop()
133     BuiltIn.Wait_Until_Keyword_Succeeds    ${TESTTOOL_DEVICE_TIMEOUT}    1s    Check_Device_Up_And_Running    ${number}
134
135 Install_And_Start_Testtool
136     [Arguments]    ${device-count}=10    ${debug}=true    ${schemas}=none    ${tool_options}=${EMPTY}    ${java_options}=${TESTTOOL_DEFAULT_JAVA_OPTIONS}    ${mdsal}=true
137     [Documentation]    Install and run testtool. Also arrange to collect its output into a log file.
138     ...    When the ${schemas} argument is set to 'none', it signifies that
139     ...    there are no additional schemas to be deployed, so the directory
140     ...    for the additional schemas is deleted on the remote machine and
141     ...    the additional schemas argument is left out.
142     # Install test tool on the machine.
143     ${filename}=    NexusKeywords.Deploy_Test_Tool    netconf/netconf-testtool
144     ${schemas_option}=    NetconfKeywords__Deploy_Additional_Schemas    ${schemas}
145     # Start the testtool
146     ${command}    BuiltIn.Set_Variable    java ${java_options} -jar ${filename} ${tool_options} --device-count ${device-count} --debug ${debug} ${schemas_option} --md-sal ${mdsal}
147     BuiltIn.Log    Running testtool: ${command}
148     SSHLibrary.Write    ${command} >testtool.log 2>&1
149     # Store information needed by other keywords.
150     BuiltIn.Set_Suite_Variable    ${NetconfKeywords__testtool_device_count}    ${device-count}
151     # Wait for the testtool to boot up.
152     Perform_Operation_On_Each_Device    NetconfKeywords__Wait_Device_Is_Up_And_Running
153
154 Check_Device_Up_And_Running
155     [Arguments]    ${device-number}
156     [Documentation]    Query netstat on remote machine whether testtool device with the specified number has its port open and fail if not.
157     ${device-port}=    BuiltIn.Evaluate    ${FIRST_TESTTOOL_PORT}+${device-number}-1
158     NetconfKeywords__Check_Device_Is_Up    ${device-port}
159
160 Stop_Testtool
161     [Documentation]    Stop testtool and download its log.
162     Utils.Write_Bare_Ctrl_C
163     SSHLibrary.Read_Until_Prompt
164     # TODO: Unify with play.py and pcc-mock handling.
165     # TODO: Maybe this keyword's content shall be moved into SSHUtils and named somewhat like
166     # "Interrupt_Program_And_Download_Its_Log" which will get an argument stating the name of
167     # the log file to get.
168     SSHLibrary.Get_File    testtool.log
169
170 NetconfKeywords__Check_Netconf_Test_Timeout_Not_Expired
171     [Arguments]    ${deadline_Date}
172     BuiltIn.Return_From_Keyword_If    not ${ENABLE_NETCONF_TEST_TIMEOUT}
173     ${current_Date}=    DateTime.Get_Current_Date
174     ${ellapsed_seconds}=    DateTime.Subtract_Date_From_Date    ${deadline_Date}    ${current_Date}
175     BuiltIn.Run_Keyword_If    ${ellapsed_seconds}<0    Fail    The global time out period expired
176
177 NetconfKeywords__Perform_Operation_With_Checking_On_Next_Device
178     [Arguments]    ${operation}    ${deadline_Date}
179     NetconfKeywords__Check_Netconf_Test_Timeout_Not_Expired    ${deadline_Date}
180     ${number}=    BuiltIn.Evaluate    ${current_port}-${BASE_NETCONF_DEVICE_PORT}+1
181     BuiltIn.Run_Keyword    ${operation}    ${DEVICE_NAME_BASE}-${number}
182     ${next}=    BuiltIn.Evaluate    ${current_port}+1
183     BuiltIn.Set_Suite_Variable    ${current_port}    ${next}
184
185 Perform_Operation_On_Each_Device
186     [Arguments]    ${operation}    ${count}=${NetconfKeywords__testtool_device_count}    ${timeout}=30m
187     ${current_Date}=    DateTime.Get_Current_Date
188     ${deadline_Date}=    DateTime.Add_Time_To_Date    ${current_Date}    ${timeout}
189     BuiltIn.Set_Suite_Variable    ${current_port}    ${BASE_NETCONF_DEVICE_PORT}
190     BuiltIn.Repeat_Keyword    ${count} times    NetconfKeywords__Perform_Operation_With_Checking_On_Next_Device    ${operation}    ${deadline_Date}