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