4d55e50f1020e6edfc6b039d6debf5cb399c5bf1
[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 Put_Xml_Via_Restconf
118     [Arguments]    ${uri_part}    ${xml_data}
119     [Documentation]    Put XML data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
120     BuiltIn.Log    ${uri_part}
121     BuiltIn.Log    ${xml_data}
122     ${response}=    RequestsLibrary.Put Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${xml_data}
123     BuiltIn.Log    ${response.text}
124     BuiltIn.Log    ${response.status_code}
125     BuiltIn.Should_Be_Empty    ${response.text}
126     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
127
128 Put_Xml_Template_Folder_Via_Restconf
129     [Arguments]    ${folder}    ${mapping_as_string}={}
130     [Documentation]    Resolve URI and data from folder, PUT to controller config.
131     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
132     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
133     Put_Xml_Via_Restconf    ${uri_part}    ${xml_data}
134
135 Put_Json_Via_Restconf
136     [Arguments]    ${uri_part}    ${json_data}
137     [Documentation]    Put JSON data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
138     BuiltIn.Log    ${uri_part}
139     BuiltIn.Log    ${json_data}
140     ${response}=    RequestsLibrary.Put Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${json_data}    headers=${HEADERS_YANG_JSON}
141     BuiltIn.Log    ${response.text}
142     BuiltIn.Log    ${response.status_code}
143     BuiltIn.Should_Be_Empty    ${response.text}
144     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
145
146 Put_Json_Template_Folder_Via_Restconf
147     [Arguments]    ${folder}    ${mapping_as_string}={}
148     [Documentation]    Resolve URI and data from folder, PUT to controller config.
149     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
150     ${json_data}=    Resolve_Json_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
151     Put_Json_Via_Restconf    ${uri_part}    ${json_data}
152
153 Delete_Via_Restconf
154     [Arguments]    ${uri_part}
155     [Documentation]    Delete resource at controller-config URI, check reponse text is empty and status_code is 204.
156     BuiltIn.Log    ${uri_part}
157     ${response}=    RequestsLibrary.Delete Request    ${NetconfViaRestconf__active_config_session}    ${uri_part}
158     BuiltIn.Log    ${response.text}
159     BuiltIn.Should_Be_Empty    ${response.text}
160     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
161
162 Delete_Xml_Template_Folder_Via_Restconf
163     [Arguments]    ${folder}    ${mapping_as_string}={}
164     [Documentation]    Resolve URI from folder, DELETE from controller config.
165     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
166     Delete_Via_Restconf    ${uri_part}
167
168 Create_NVR_Session
169     [Arguments]    ${name}    ${host}
170     [Documentation]    Create a Netconf Via Restconf session pointing to the given host with the given name. The new session is NOT made active.
171     RequestsLibrary.Create_Session    ${name}    http://${host}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS_XML}    auth=${AUTH}
172     RequestsLibrary.Create_Session    ${name}_operational    http://${host}:${RESTCONFPORT}${OPERATIONAL_API}    headers=${HEADERS_XML}    auth=${AUTH}
173
174 Get_Active_NVR_Session
175     [Documentation]    Get the name of the currently active NVR session.
176     [Return]    ${NetconfViaRestconf__active_config_session}
177
178 Activate_NVR_Session
179     [Arguments]    ${name}
180     [Documentation]    Activate the given NVR session.
181     BuiltIn.Set_Suite_Variable    ${NetconfViaRestconf__active_config_session}    ${name}
182     BuiltIn.Set_Suite_Variable    ${NetconfViaRestconf__active_operational_session}    ${name}_operational
183
184 Get_Config_Data_From_URI
185     [Arguments]    ${uri}    ${headers}=${NONE}
186     ${data}=    Utils.Get_Data_From_URI    ${NetconfViaRestconf__active_config_session}    ${uri}    ${headers}
187     [Return]    ${data}
188
189 Get_Operational_Data_From_URI
190     [Arguments]    ${uri}    ${headers}=${NONE}
191     ${data}=    Utils.Get_Data_From_URI    ${NetconfViaRestconf__active_operational_session}    ${uri}    ${headers}
192     [Return]    ${data}