Added more thorough CRUD testing
[integration/test.git] / csit / libraries / NetconfViaRestconf.robot
1 *** Settings ***
2 Documentation     Access Netconf via Restconf.
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 ...               FIXME: This whole thing needs to be refactored and merged with
11 ...               "ConfigViaRestconf.robot" which contains nearly identical (or even completely
12 ...               identical) pieces of code.
13 ...
14 ...               FIXME: This module (along with "ConfigViaRestconf.robot") uses the deprecated
15 ...               "RequestsLibrary.Put" keyword to issue requests to the ODL. Convert these
16 ...               statements to use "RequestsLibrary.Put_Request" instead. Similarly for
17 ...               "RequestsLibrary.Post" and "RequestsLibrary.Delete". It might be best to do
18 ...               this change after the code duplication in these two modules is removed (see
19 ...               the previous FIXME).
20 Library           RequestsLibrary
21 Library           OperatingSystem
22 Resource          Utils.robot
23
24 *** Variables ***
25 @{allowed_status_codes}    ${200}    ${201}    ${204}    # List of integers, not strings. Used by both PUT and DELETE.
26
27 *** Keywords ***
28 FIXME__POLISH_THIS
29     # The following code has a bunch of problems which are very hard to fix in
30     # terms of debugging and review times. Therefore I propose to merge this test
31     # first "as is" and solve the problems of this code in later commits. These
32     # problems were identified so far:
33     #
34     # - Code duplication. The following code is almost identical to what is
35     #    present in ConfigViaRestconf.robot. Fixing this means refactoring
36     #    ConfigViaRestconf.robot to use this library instead of doing
37     #    everything on its own.
38     # - The interface of this code might be too optimized for the needs of
39     #    one test suite. Maybe it should be generalized.
40     # - The Teardown_Netconf_Via_Restconf seems to be incomplete. It is
41     #    supposed to close the session but (as the code suggests in a
42     #    comment), the functionality needed is not implemented.
43     #
44     # Issues identified when trying to make this a library:
45     #
46     # - A better name might be necessary (this actually allows not only
47     #    netconf to be accessed but other restconf accessible subsystems as
48     #    well).
49     # - The ConfigViaRestconf might need to be merged with this code to avoid
50     #    strange name clashes when using both in a suite.
51
52 Setup_Netconf_Via_Restconf
53     [Documentation]    Creates a default requests session to be used by subsequent keywords.
54     # Do not append slash at the end uf URL, Requests would add another, resulting in error.
55     Create_NVR_Session    nvr_session    ${ODL_SYSTEM_IP}
56     Activate_NVR_Session    nvr_session
57
58 Teardown_Netconf_Via_Restconf
59     [Documentation]    Teardown to pair with Setup (otherwise no-op).
60     BuiltIn.Comment    TODO: The following line does not seem to be implemented by RequestsLibrary. Look for a workaround.
61     BuiltIn.Comment    Delete_Session    nvr_session
62
63 Resolve_URI_From_Template_Folder
64     [Arguments]    ${folder}    ${mapping_as_string}
65     [Documentation]    Read URI template from folder, strip endline, make changes according to mapping, return the result.
66     ${uri_template}=    OperatingSystem.Get_File    ${folder}${/}config.uri
67     BuiltIn.Log    ${uri_template}
68     ${uri_part}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${uri_template}    ${mapping_as_string}
69     [Return]    ${uri_part}
70
71 Resolve_Xml_Data_From_Template_Folder
72     [Arguments]    ${folder}    ${mapping_as_string}
73     [Documentation]    Read data template from folder, strip endline, make changes according to mapping, return the result.
74     ${data_template}=    OperatingSystem.Get_File    ${folder}${/}data.xml
75     BuiltIn.Log    ${data_template}
76     ${xml_data}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${data_template}    ${mapping_as_string}
77     [Return]    ${xml_data}
78
79 Resolve_Json_Data_From_Template_Folder
80     [Arguments]    ${folder}    ${mapping_as_string}
81     [Documentation]    Read data template from folder, strip endline, make changes according to mapping, return the result.
82     ${data_template}=    OperatingSystem.Get_File    ${folder}${/}data.json
83     BuiltIn.Log    ${data_template}
84     ${json_data}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${data_template}    ${mapping_as_string}
85     [Return]    ${json_data}
86
87 Strip_Endline_And_Apply_Substitutions_From_Mapping
88     [Arguments]    ${template_as_string}    ${mapping_as_string}
89     [Documentation]    Strip endline, apply substitutions, Log and return the result.
90     # Robot Framework does not understand dictionaries well, so resort to Evaluate.
91     # Needs python module "string", and since the template string is expected to contain newline, it has to be enclosed in triple quotes.
92     # Using rstrip() removes all trailing whitespace, which is what we want if there is something more than an endline.
93     ${final_text}=    BuiltIn.Evaluate    string.Template('''${template_as_string}'''.rstrip()).substitute(${mapping_as_string})    modules=string
94     BuiltIn.Log    ${final_text}
95     [Return]    ${final_text}
96
97 Post_Xml_Via_Restconf
98     [Arguments]    ${uri_part}    ${xml_data}
99     [Documentation]    Post XML data to given controller-config URI, check reponse text is empty and status_code is 204.
100     BuiltIn.Log    ${uri_part}
101     BuiltIn.Log    ${xml_data}
102     # As seen in previous two Keywords, Post does not need long specific URI.
103     # But during Lithium development, Post ceased to do merge, so those Keywords do not work anymore.
104     # This Keyword can still be used with specific URI to create a new container and fail if a container was already present.
105     ${response}=    RequestsLibrary.Post Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${xml_data}
106     BuiltIn.Log    ${response.text}
107     BuiltIn.Should_Be_Empty    ${response.text}
108     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    204
109
110 Post_Xml_Template_Folder_Via_Restconf
111     [Arguments]    ${folder}    ${mapping_as_string}={}
112     [Documentation]    Resolve URI and data from folder, POST to restconf.
113     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
114     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
115     Post_Xml_Via_Restconf    ${uri_part}    ${xml_data}
116
117 Post_Json_Via_Restconf
118     [Arguments]    ${uri_part}    ${json_data}
119     [Documentation]    Post JSON data to given controller-config URI, check reponse text is empty and status_code is 204.
120     BuiltIn.Log    ${uri_part}
121     BuiltIn.Log    ${json_data}
122     # As seen in previous two Keywords, Post does not need long specific URI.
123     # But during Lithium development, Post ceased to do merge, so those Keywords do not work anymore.
124     # This Keyword can still be used with specific URI to create a new container and fail if a container was already present.
125     ${response}=    RequestsLibrary.Post    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${json_data}    headers=${HEADERS_YANG_JSON}
126     BuiltIn.Log    ${response.text}
127     BuiltIn.Should_Be_Empty    ${response.text}
128     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    204
129
130 Post_Json_Template_Folder_Via_Restconf
131     [Arguments]    ${folder}    ${mapping_as_string}={}
132     [Documentation]    Resolve URI and data from folder, POST to restconf.
133     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
134     ${json_data}=    Resolve_Json_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
135     Post_Json_Via_Restconf    ${uri_part}    ${json_data}
136
137 Put_Xml_Via_Restconf
138     [Arguments]    ${uri_part}    ${xml_data}
139     [Documentation]    Put XML data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
140     BuiltIn.Log    ${uri_part}
141     BuiltIn.Log    ${xml_data}
142     ${response}=    RequestsLibrary.Put Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${xml_data}
143     BuiltIn.Log    ${response.text}
144     BuiltIn.Log    ${response.status_code}
145     BuiltIn.Should_Be_Empty    ${response.text}
146     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
147
148 Put_Xml_Template_Folder_Via_Restconf
149     [Arguments]    ${folder}    ${mapping_as_string}={}
150     [Documentation]    Resolve URI and data from folder, PUT to controller config.
151     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
152     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
153     Put_Xml_Via_Restconf    ${uri_part}    ${xml_data}
154
155 Put_Json_Via_Restconf
156     [Arguments]    ${uri_part}    ${json_data}
157     [Documentation]    Put JSON data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
158     BuiltIn.Log    ${uri_part}
159     BuiltIn.Log    ${json_data}
160     ${response}=    RequestsLibrary.Put Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${json_data}    headers=${HEADERS_YANG_JSON}
161     BuiltIn.Log    ${response.text}
162     BuiltIn.Log    ${response.status_code}
163     BuiltIn.Should_Be_Empty    ${response.text}
164     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
165
166 Put_Json_Template_Folder_Via_Restconf
167     [Arguments]    ${folder}    ${mapping_as_string}={}
168     [Documentation]    Resolve URI and data from folder, PUT to controller config.
169     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
170     ${json_data}=    Resolve_Json_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
171     Put_Json_Via_Restconf    ${uri_part}    ${json_data}
172
173 Delete_Via_Restconf
174     [Arguments]    ${uri_part}
175     [Documentation]    Delete resource at controller-config URI, check reponse text is empty and status_code is 204.
176     BuiltIn.Log    ${uri_part}
177     ${response}=    RequestsLibrary.Delete Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}
178     BuiltIn.Log    ${response.text}
179     BuiltIn.Should_Be_Empty    ${response.text}
180     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
181
182 Delete_Xml_Template_Folder_Via_Restconf
183     [Arguments]    ${folder}    ${mapping_as_string}={}
184     [Documentation]    Resolve URI from folder, DELETE from controller config.
185     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
186     Delete_Via_Restconf    ${uri_part}
187
188 Create_NVR_Session
189     [Arguments]    ${name}    ${host}
190     [Documentation]    Create a Netconf Via Restconf session pointing to the given host with the given name. The new session is NOT made active.
191     RequestsLibrary.Create_Session    ${name}    http://${host}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS_XML}    auth=${AUTH}
192     RequestsLibrary.Create_Session    ${name}_operational    http://${host}:${RESTCONFPORT}${OPERATIONAL_API}    headers=${HEADERS_XML}    auth=${AUTH}
193
194 Get_Active_NVR_Session
195     [Documentation]    Get the name of the currently active NVR session.
196     [Return]    ${NetconfViaRestconf__active_config_session}
197
198 Activate_NVR_Session
199     [Arguments]    ${name}
200     [Documentation]    Activate the given NVR session.
201     BuiltIn.Set_Suite_Variable    ${NetconfViaRestconf__active_config_session}    ${name}
202     BuiltIn.Set_Suite_Variable    ${NetconfViaRestconf__active_operational_session}    ${name}_operational
203
204 Get_Config_Data_From_URI
205     [Arguments]    ${uri}    ${headers}=${NONE}
206     ${data}=    Utils.Get_Data_From_URI    ${NetconfViaRestconf__active_config_session}    ${uri}    ${headers}
207     [Return]    ${data}
208
209 Get_Operational_Data_From_URI
210     [Arguments]    ${uri}    ${headers}=${NONE}
211     ${data}=    Utils.Get_Data_From_URI    ${NetconfViaRestconf__active_operational_session}    ${uri}    ${headers}
212     [Return]    ${data}