fixing play.py and data change counter
[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://${ODL_SYSTEM_IP}:${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     ${status}    ${uri_template}=    BuiltIn.Run_Keyword_And_Ignore_Error    OperatingSystem.Get_File    ${folder}${/}config.uri.${ODL_STREAM}
119     ${uri_template}=     BuiltIn.Run Keyword If     '${status}' != 'PASS'    OperatingSystem.Get_File    ${folder}${/}config.uri    ELSE   BuiltIn.Set Variable    ${uri_template}
120     BuiltIn.Log    ${uri_template}
121     ${uri_part}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${uri_template}    ${mapping_as_string}
122     [Return]    ${uri_part}
123
124 Resolve_Xml_Data_From_Template_Folder
125     [Arguments]    ${folder}    ${mapping_as_string}
126     [Documentation]    Read XML data template from folder, strip endline, make changes according to mapping, return the result.
127     ${status}    ${data_template}=    BuiltIn.Run_Keyword_And_Ignore_Error    OperatingSystem.Get_File    ${folder}${/}data.xml.${ODL_STREAM}
128     ${data_template}=     BuiltIn.Run Keyword If     '${status}' != 'PASS'    OperatingSystem.Get_File    ${folder}${/}data.xml    ELSE   BuiltIn.Set Variable    ${data_template}
129     BuiltIn.Log    ${data_template}
130     ${xml_data}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${data_template}    ${mapping_as_string}
131     [Return]    ${xml_data}
132
133 Resolve_Json_Data_From_Template_Folder
134     [Arguments]    ${folder}    ${mapping_as_string}
135     [Documentation]    Read JSON data template from folder, strip endline, make changes according to mapping, return the result.
136     ${status}    ${data_template}=    BuiltIn.Run_Keyword_And_Ignore_Error    OperatingSystem.Get_File    ${folder}${/}data.json.${ODL_STREAM}
137     ${data_template}=     BuiltIn.Run Keyword If     '${status}' != 'PASS'    OperatingSystem.Get_File    ${folder}${/}data.json    ELSE   BuiltIn.Set Variable    ${data_template}
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