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