d4bdc2bb4429b666ed837ea93fa483a97537559d
[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 Library           RequestsLibrary
10 Library           OperatingSystem
11
12 *** Variables ***
13 @{allowed_status_codes}    ${200}    ${201}    ${204}    # List of integers, not strings. Used by both PUT and DELETE.
14
15 *** Keywords ***
16 FIXME__POLISH_THIS
17     # The following code has a bunch of problems which are very hard to fix in
18     # terms of debugging and review times. Therefore I propose to merge this test
19     # first "as is" and solve the problems of this code in later commits. These
20     # problems were identified so far:
21     #
22     # - Code duplication. The following code is almost identical to what is
23     #    present in ConfigViaRestconf.robot. Fixing this means refactoring
24     #    ConfigViaRestconf.robot to use this library instead of doing
25     #    everything on its own.
26     # - The interface of this code might be too optimized for the needs of
27     #    one test suite. Maybe it should be generalized.
28     # - The Teardown_Netconf_Via_Restconf seems to be incomplete. It is
29     #    supposed to close the session but (as the code suggests in a
30     #    comment), the functionality needed is not implemented.
31     #
32     # Issues identified when trying to make this a library:
33     #
34     # - A better name might be necessary (this actually allows not only
35     #    netconf to be accessed but other restconf accessible subsystems as
36     #    well).
37     # - The ConfigViaRestconf might need to be merged with this code to avoid
38     #    strange name clashes when using both in a suite.
39
40 Setup_Netconf_Via_Restconf
41     [Documentation]    Creates Requests session to be used by subsequent keywords.
42     # Do not append slash at the end uf URL, Requests would add another, resulting in error.
43     RequestsLibrary.Create_Session    nvr_session    http://${CONTROLLER}:${RESTCONFPORT}${CONFIG_API}    headers=${HEADERS_XML}    auth=${AUTH}
44
45 Teardown_Netconf_Via_Restconf
46     [Documentation]    Teardown to pair with Setup (otherwise no-op).
47     BuiltIn.Comment    TODO: The following line does not seem to be implemented by RequestsLibrary. Look for a workaround.
48     BuiltIn.Comment    Delete_Session    nvr_session
49
50 Resolve_URI_From_Template_Folder
51     [Arguments]    ${folder}    ${mapping_as_string}
52     [Documentation]    Read URI template from folder, strip endline, make changes according to mapping, return the result.
53     ${uri_template}=    OperatingSystem.Get_File    ${folder}${/}config.uri
54     BuiltIn.Log    ${uri_template}
55     ${uri_part}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${uri_template}    ${mapping_as_string}
56     [Return]    ${uri_part}
57
58 Resolve_Xml_Data_From_Template_Folder
59     [Arguments]    ${folder}    ${mapping_as_string}
60     [Documentation]    Read data template from folder, strip endline, make changes according to mapping, return the result.
61     ${data_template}=    OperatingSystem.Get_File    ${folder}${/}data.xml
62     BuiltIn.Log    ${data_template}
63     ${xml_data}=    Strip_Endline_And_Apply_Substitutions_From_Mapping    ${data_template}    ${mapping_as_string}
64     [Return]    ${xml_data}
65
66 Strip_Endline_And_Apply_Substitutions_From_Mapping
67     [Arguments]    ${template_as_string}    ${mapping_as_string}
68     [Documentation]    Strip endline, apply substitutions, Log and return the result.
69     # Robot Framework does not understand dictionaries well, so resort to Evaluate.
70     # Needs python module "string", and since the template string is expected to contain newline, it has to be enclosed in triple quotes.
71     # Using rstrip() removes all trailing whitespace, which is what we want if there is something more than an endline.
72     ${final_text}=    BuiltIn.Evaluate    string.Template('''${template_as_string}'''.rstrip()).substitute(${mapping_as_string})    modules=string
73     BuiltIn.Log    ${final_text}
74     [Return]    ${final_text}
75
76 Post_Xml_Via_Restconf
77     [Arguments]    ${uri_part}    ${xml_data}
78     [Documentation]    Post XML data to given controller-config URI, check reponse text is empty and status_code is 204.
79     BuiltIn.Log    ${uri_part}
80     BuiltIn.Log    ${xml_data}
81     # As seen in previous two Keywords, Post does not need long specific URI.
82     # But during Lithium development, Post ceased to do merge, so those Keywords do not work anymore.
83     # This Keyword can still be used with specific URI to create a new container and fail if a container was already present.
84     ${response}=    RequestsLibrary.Post    nvr_session    ${uri_part}    data=${xml_data}
85     BuiltIn.Log    ${response.text}
86     BuiltIn.Should_Be_Empty    ${response.text}
87     BuiltIn.Should_Be_Equal_As_Strings    ${response.status_code}    204
88
89 Post_Xml_Template_Folder_Via_Restconf
90     [Arguments]    ${folder}    ${mapping_as_string}={}
91     [Documentation]    Resolve URI and data from folder, POST to restconf.
92     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
93     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
94     Post_Xml_Via_Restconf    ${uri_part}    ${xml_data}
95
96 Put_Xml_Via_Restconf
97     [Arguments]    ${uri_part}    ${xml_data}
98     [Documentation]    Put XML data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
99     BuiltIn.Log    ${uri_part}
100     BuiltIn.Log    ${xml_data}
101     ${response}=    RequestsLibrary.Put    nvr_session    ${uri_part}    data=${xml_data}
102     BuiltIn.Log    ${response.text}
103     BuiltIn.Log    ${response.status_code}
104     BuiltIn.Should_Be_Empty    ${response.text}
105     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
106
107 Put_Xml_Template_Folder_Via_Restconf
108     [Arguments]    ${folder}    ${mapping_as_string}={}
109     [Documentation]    Resolve URI and data from folder, PUT to controller config.
110     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
111     ${xml_data}=    Resolve_Xml_Data_From_Template_Folder    ${folder}    ${mapping_as_string}
112     Put_Xml_Via_Restconf    ${uri_part}    ${xml_data}
113
114 Delete_Via_Restconf
115     [Arguments]    ${uri_part}
116     [Documentation]    Delete resource at controller-config URI, check reponse text is empty and status_code is 204.
117     BuiltIn.Log    ${uri_part}
118     ${response}=    RequestsLibrary.Delete    nvr_session    ${uri_part}
119     BuiltIn.Log    ${response.text}
120     BuiltIn.Should_Be_Empty    ${response.text}
121     BuiltIn.Should_Contain    ${allowed_status_codes}    ${response.status_code}
122
123 Delete_Xml_Template_Folder_Via_Restconf
124     [Arguments]    ${folder}    ${mapping_as_string}={}
125     [Documentation]    Resolve URI from folder, DELETE from controller config.
126     ${uri_part}=    Resolve_URI_From_Template_Folder    ${folder}    ${mapping_as_string}
127     Delete_Via_Restconf    ${uri_part}