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