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