Add FIXMEs to point out maintenance that becomes due
[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 Strip_Endline_And_Apply_Substitutions_From_Mapping
80     [Arguments]    ${template_as_string}    ${mapping_as_string}
81     [Documentation]    Strip endline, apply substitutions, Log and return the result.
82     # Robot Framework does not understand dictionaries well, so resort to Evaluate.
83     # Needs python module "string", and since the template string is expected to contain newline, it has to be enclosed in triple quotes.
84     # Using rstrip() removes all trailing whitespace, which is what we want if there is something more than an endline.
85     ${final_text}=    BuiltIn.Evaluate    string.Template('''${template_as_string}'''.rstrip()).substitute(${mapping_as_string})    modules=string
86     BuiltIn.Log    ${final_text}
87     [Return]    ${final_text}
88
89 Post_Xml_Via_Restconf
90     [Arguments]    ${uri_part}    ${xml_data}
91     [Documentation]    Post XML data to given controller-config URI, check reponse text is empty and status_code is 204.
92     BuiltIn.Log    ${uri_part}
93     BuiltIn.Log    ${xml_data}
94     # As seen in previous two Keywords, Post does not need long specific URI.
95     # But during Lithium development, Post ceased to do merge, so those Keywords do not work anymore.
96     # This Keyword can still be used with specific URI to create a new container and fail if a container was already present.
97     ${response}=    RequestsLibrary.Post    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${xml_data}
98     BuiltIn.Log    ${response.text}
99     BuiltIn.Should_Be_Empty    ${response.text}
100     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    204
101
102 Post_Xml_Template_Folder_Via_Restconf
103     [Arguments]    ${folder}    ${mapping_as_string}={}
104     [Documentation]    Resolve URI and data from folder, POST to restconf.
105     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
106     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
107     Post_Xml_Via_Restconf    ${uri_part}    ${xml_data}
108
109 Put_Xml_Via_Restconf
110     [Arguments]    ${uri_part}    ${xml_data}
111     [Documentation]    Put XML data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
112     BuiltIn.Log    ${uri_part}
113     BuiltIn.Log    ${xml_data}
114     ${response}=    RequestsLibrary.Put    ${NetconfViaRestconf__active_config_session}    ${uri_part}    data=${xml_data}
115     BuiltIn.Log    ${response.text}
116     BuiltIn.Log    ${response.status_code}
117     BuiltIn.Should_Be_Empty    ${response.text}
118     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
119
120 Put_Xml_Template_Folder_Via_Restconf
121     [Arguments]    ${folder}    ${mapping_as_string}={}
122     [Documentation]    Resolve URI and data from folder, PUT to controller config.
123     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
124     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
125     Put_Xml_Via_Restconf    ${uri_part}    ${xml_data}
126
127 Delete_Via_Restconf
128     [Arguments]    ${uri_part}
129     [Documentation]    Delete resource at controller-config URI, check reponse text is empty and status_code is 204.
130     BuiltIn.Log    ${uri_part}
131     ${response}=    RequestsLibrary.Delete    ${NetconfViaRestconf__active_config_session}    ${uri_part}
132     BuiltIn.Log    ${response.text}
133     BuiltIn.Should_Be_Empty    ${response.text}
134     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
135
136 Delete_Xml_Template_Folder_Via_Restconf
137     [Arguments]    ${folder}    ${mapping_as_string}={}
138     [Documentation]    Resolve URI from folder, DELETE from controller config.
139     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
140     Delete_Via_Restconf    ${uri_part}
141
142 Create_NVR_Session
143     [Arguments]    ${name}    ${host}
144     [Documentation]    Create a Netconf Via Restconf session pointing to the given host with the given name. The new session is NOT made active.
145     RequestsLibrary.Create_Session    ${name}    http://${host}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS_XML}    auth=${AUTH}
146     RequestsLibrary.Create_Session    ${name}_operational    http://${host}:${RESTCONFPORT}${OPERATIONAL_API}    headers=${HEADERS_XML}    auth=${AUTH}
147
148 Get_Active_NVR_Session
149     [Documentation]    Get the name of the currently active NVR session.
150     [Return]    ${NetconfViaRestconf__active_config_session}
151
152 Activate_NVR_Session
153     [Arguments]    ${name}
154     [Documentation]    Activate the given NVR session.
155     BuiltIn.Set_Suite_Variable    ${NetconfViaRestconf__active_config_session}    ${name}
156     BuiltIn.Set_Suite_Variable    ${NetconfViaRestconf__active_operational_session}    ${name}_operational
157
158 Get_Config_Data_From_URI
159     [Arguments]    ${uri}    ${headers}=${NONE}
160     ${data}=    Utils.Get_Data_From_URI    ${NetconfViaRestconf__active_config_session}    ${uri}    ${headers}
161     [Return]    ${data}
162
163 Get_Operational_Data_From_URI
164     [Arguments]    ${uri}    ${headers}=${NONE}
165     ${data}=    Utils.Get_Data_From_URI    ${NetconfViaRestconf__active_operational_session}    ${uri}    ${headers}
166     [Return]    ${data}