Testplan and suite for 1 iBGP peer performance tests
[integration/test.git] / csit / libraries / ConfigViaRestconf.robot
1 *** Settings ***
2 Documentation     Robot keyword library (Resource) for runtime changes to config subsystem state using restconf calls.
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 ...               The purpose of this library is to make runtime changes to ODL configuration
12 ...               easier from Robot suite contributor point of view.
13 ...
14 ...               Different ODL parts have varying ways of configuration,
15 ...               this library affects only the Config Subsystem way.
16 ...               Config Subsystem has (apart Java APIs mostly available only from inside of ODL)
17 ...               NETCONF server as its publicly available entry point.
18 ...               Netconf-connector feature makes this netconf server available for RESTCONF calls.
19 ...               Unfortunately, URIs and data payloads tend to be quite convoluted,
20 ...               so using RequestsLibrary directly from test cases is unwieldy.
21 ...
22 ...               The main strength of this library are *_Template_Folder_Config_Via_Restconf keywords
23 ...               User gives a path to directory where files with templates for URI fragment
24 ...               and XML (or JSON) data are present, and a mapping with substitution to make;
25 ...               the keywords will take it from there.
26 ...
27 ...               Prerequisities:
28 ...               * netconf-connector feature installed on ODL.
29 ...               * Setup_Config_Via_Restconf called from suite Setup
30 ...               (or before any other call to a keyword from this library) at least once.
31 Library           OperatingSystem
32 Library           RequestsLibrary
33 Library           String
34 Library           ${CURDIR}/HsfJson/hsf_json.py
35 Variables         ${CURDIR}/../variables/Variables.py
36
37 *** Variables ***
38 # TODO: Make the following list more narrow when Bug 2594 is fixed.
39 @{allowed_status_codes}    ${200}    ${201}    ${204}    # List of integers, not strings. Used by both PUT and DELETE.
40 ${cvr_workspace}    /tmp
41
42 *** Keywords ***
43 Setup_Config_Via_Restconf
44     [Documentation]    Creates Requests session to be used by subsequent keywords.
45     ...    Also remembers worspace to use when needed and two temp files for JSON data.
46     # Check for multiple Setup calls.
47     ${variable_was_set}=    BuiltIn.Get_Variable_Value    ${cvr_actfile}    NEVER
48     BuiltIn.Return_From_Keyword_If    '''${variable_was_set}''' != '''NEVER'''
49     # Do not append slash at the end uf URL, Requests would add another, resulting in error.
50     RequestsLibrary.Create_Session    cvr_session    http://${CONTROLLER}:${RESTCONFPORT}${CONTROLLER_CONFIG_MOUNT}    headers=${HEADERS_XML}    auth=${AUTH}
51     ${workspace_defined}=    BuiltIn.Run_Keyword_And_return_Status    BuiltIn.Variable_Should_Exist    ${WORKSPACE}
52     BuiltIn.Run_Keyword_If    ${workspace_defined}    BuiltIn.Set_Suite_Variable    ${cvr_workspace}    ${WORKSPACE}
53     BuiltIn.Set_Suite_Variable    ${cvr_actfile}    ${cvr_workspace}${/}actual.json
54     BuiltIn.Set_Suite_Variable    ${cvr_expfile}    ${cvr_workspace}${/}expected.json
55
56 Teardown_Config_Via_Restconf
57     [Documentation]    Teardown to pair with Setup (otherwise no-op).
58     BuiltIn.Comment    TODO: The following line does not seem to be implemented by RequestsLibrary. Look for a workaround.
59     BuiltIn.Comment    Delete_Session    cvr_session
60
61 Put_Xml_Template_Folder_Config_Via_Restconf
62     [Arguments]    ${folder}    ${mapping_as_string}={}
63     [Documentation]    Resolve URI and data from folder, PUT to controller config.
64     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
65     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
66     Put_Xml_Config_Via_Restconf    ${uri_part}    ${xml_data}
67
68 Get_Xml_Template_Folder_Config_Via_Restconf
69     [Arguments]    ${folder}    ${mapping_as_string}={}
70     [Documentation]    Resolve URI from folder, GET from controller config in XML form.
71     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
72     ${xml_data}=    Get_Xml_Config_Via_Restconf    ${uri_part}
73     [Return]    ${xml_data}
74
75 Get_Json_Template_Folder_Config_Via_Restconf
76     [Arguments]    ${folder}    ${mapping_as_string}={}
77     [Documentation]    Resolve URI from folder, GET from controller config in JSON form.
78     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
79     ${json_string}=    Get_Json_Config_Via_Restconf    ${uri_part}
80     [Return]    ${json_string}
81
82 Verify_Xml_Template_Folder_Config_Via_Restconf
83     [Arguments]    ${folder}    ${mapping_as_string}={}
84     [Documentation]    Resolve URI from folder, GET from controller config, compare to expected data.
85     # FIXME: This is subject to pseudorandom field ordering issue, use with care.
86     ${expected_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
87     ${actual_data}=    Get_Xml_Template_Folder_Config_Via_Restconf    ${folder}    ${mapping_as_string}
88     BuiltIn.Should_Be_Equal    ${actual_data}    ${expected_data}
89
90 Verify_Json_Template_Folder_Config_Via_Restconf
91     [Arguments]    ${folder}    ${mapping_as_string}={}
92     [Documentation]    Resolve URI from folder, GET from controller config, compare to expected data as normalized JSONs.
93     ${expected}=    Resolve_Json_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
94     ${actual}=    Get_Json_Template_Folder_Config_Via_Restconf    ${folder}    ${mapping_as_string}
95     Normalize_Jsons_And_Compare    ${actual}    ${expected}
96
97 Normalize_Jsons_And_Compare
98     [Arguments]    ${actual_raw}    ${expected_raw}
99     [Documentation]    Use HsfJson to normalize both arguments, compute and Log diff, fail if diff is non-empty.
100     ...    This keywords assumes ${WORKSPACE} is defined as a suite variable.
101     ${actual_normalized}=    hsf_json.Hsf_Json    ${actual_raw}
102     ${expected_normalized}=    hsf_json.Hsf_Json    ${expected_raw}
103     OperatingSystem.Create_File    ${cvr_expfile}    ${expected_normalized}
104     OperatingSystem.Create_File    ${cvr_actfile}    ${actual_normalized}
105     ${diff}=    OperatingSystem.Run    diff -du '${cvr_expfile}' '${cvr_actfile}'
106     BuiltIn.Log    ${diff}
107     BuiltIn.Should_Be_Empty    ${diff}
108
109 Delete_Xml_Template_Folder_Config_Via_Restconf
110     [Arguments]    ${folder}    ${mapping_as_string}={}
111     [Documentation]    Resolve URI from folder, DELETE from controller config.
112     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
113     Delete_Config_Via_Restconf    ${uri_part}
114
115 Resolve_URI_From_Template_Folder
116     [Arguments]    ${folder}    ${mapping_as_string}
117     [Documentation]    Read URI template from folder, strip endline, make changes according to mapping, return the result.
118     ${uri_template}=    OperatingSystem.Get_File    ${folder}${/}config.uri
119     BuiltIn.Log    ${uri_template}
120     ${uri_part}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${uri_template}    ${mapping_as_string}
121     [Return]    ${uri_part}
122
123 Resolve_Xml_Data_From_Template_Folder
124     [Arguments]    ${folder}    ${mapping_as_string}
125     [Documentation]    Read XML data template from folder, strip endline, make changes according to mapping, return the result.
126     ${data_template}=    OperatingSystem.Get_File    ${folder}${/}data.xml
127     BuiltIn.Log    ${data_template}
128     ${xml_data}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${data_template}    ${mapping_as_string}
129     [Return]    ${xml_data}
130
131 Resolve_Json_Data_From_Template_Folder
132     [Arguments]    ${folder}    ${mapping_as_string}
133     [Documentation]    Read JSON data template from folder, strip endline, make changes according to mapping, return the result.
134     ${data_template}=    OperatingSystem.Get_File    ${folder}${/}data.json
135     BuiltIn.Log    ${data_template}
136     ${json_data}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${data_template}    ${mapping_as_string}
137     [Return]    ${json_data}
138
139 Strip_Endline_And_Apply_Substitutions_From_Mapping
140     [Arguments]    ${template_as_string}    ${mapping_as_string}
141     [Documentation]    Strip endline, apply substitutions, Log and return the result.
142     # Robot Framework does not understand dictionaries well, so resort to Evaluate.
143     # Needs python module "string", and since the template string is expected to contain newline, it has to be enclosed in triple quotes.
144     # Using rstrip() removes all trailing whitespace, which is what we want if there is something more than an endline.
145     ${final_text}=    BuiltIn.Evaluate    string.Template('''${template_as_string}'''.rstrip()).substitute(${mapping_as_string})    modules=string
146     BuiltIn.Log    ${final_text}
147     [Return]    ${final_text}
148
149 Put_Xml_Config_Module_Via_Restconf
150     [Arguments]    ${xml_data}    ${type}    ${name}
151     [Documentation]    Put new XML configuration to config:modules URI based on given module type and name.
152     # Also no slash here
153     Put_Xml_Config_Via_Restconf    config:modules/module/${type}/${name}    ${xml_data}
154
155 Put_Xml_Config_Service_Via_Restconf
156     [Arguments]    ${xml_data}    ${type}    ${name}
157     [Documentation]    Put new XML configuration to config:services URI based on given service type and instance name.
158     Put_Xml_Config_Via_Restconf    config:services/service/${type}/config:instance/${name}    ${xml_data}
159
160 Put_Xml_Config_Via_Restconf
161     [Arguments]    ${uri_part}    ${xml_data}
162     [Documentation]    Put XML data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
163     BuiltIn.Log    ${uri_part}
164     BuiltIn.Log    ${xml_data}
165     ${response}=    RequestsLibrary.Put    cvr_session    ${uri_part}    data=${xml_data}
166     BuiltIn.Log    ${response.text}
167     BuiltIn.Log    ${response.status_code}
168     BuiltIn.Should_Be_Empty    ${response.text}
169     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
170
171 Get_Xml_Config_Via_Restconf
172     [Arguments]    ${uri_part}
173     [Documentation]    Get XML data from given controller-config URI, check status_code is one of allowed ones, return response text.
174     BuiltIn.Log    ${uri_part}
175     ${response}=    RequestsLibrary.Get    cvr_session    ${uri_part}    headers=${ACCEPT_XML}
176     BuiltIn.Log    ${response.text}
177     BuiltIn.Log    ${response.status_code}
178     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
179     [Return]    ${response.text}
180
181 Get_Json_Config_Via_Restconf
182     [Arguments]    ${uri_part}
183     [Documentation]    Get XML data from given controller-config URI, check status_code is one of allowed ones, return response text.
184     BuiltIn.Log    ${uri_part}
185     ${response}=    RequestsLibrary.Get    cvr_session    ${uri_part}    headers=${ACCEPT_JSON}
186     BuiltIn.Log    ${response.text}
187     BuiltIn.Log    ${response.status_code}
188     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
189     [Return]    ${response.text}
190
191 Delete_Config_Via_Restconf
192     [Arguments]    ${uri_part}
193     [Documentation]    Delete resource at controller-config URI, check reponse text is empty and status_code is 204.
194     BuiltIn.Log    ${uri_part}
195     ${response}=    RequestsLibrary.Delete    cvr_session    ${uri_part}
196     BuiltIn.Log    ${response.text}
197     BuiltIn.Should_Be_Empty    ${response.text}
198     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
199
200 Post_Xml_Config_Module_Via_Restconf
201     [Arguments]    ${xml_data}
202     [Documentation]    Post new XML configuration to config:modules.
203     # Also no slash here
204     Post_Xml_Config_Via_Restconf    config:modules    ${xml_data}
205
206 Post_Xml_Config_Service_Via_Restconf
207     [Arguments]    ${xml_data}
208     [Documentation]    Post new XML configuration to config:services.
209     Post_Xml_Config_Via_Restconf    config:services    ${xml_data}
210
211 Post_Xml_Config_Via_Restconf
212     [Arguments]    ${uri_part}    ${xml_data}
213     [Documentation]    Post XML data to given controller-config URI, check reponse text is empty and status_code is 204.
214     # As seen in previous two Keywords, Post does not need long specific URI.
215     # But during Lithium development, Post ceased to do merge, so those Keywords do not work anymore.
216     # This Keyword can still be used with specific URI to create a new container and fail if a container was already present.
217     ${response}=    RequestsLibrary.Post_Request    cvr_session    ${uri_part}    data=${xml_data}
218     BuiltIn.Log    ${response.text}
219     BuiltIn.Should_Be_Empty    ${response.text}
220     # TODO: status_code is integrer, so compare to ${204}. Also, there is a Improvement for 201 to be a better code.
221     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    204