2 Documentation Access Netconf via Restconf.
4 ... Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
13 @{allowed_status_codes} ${200} ${201} ${204} # List of integers, not strings. Used by both PUT and DELETE.
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:
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.
32 # Issues identified when trying to make this a library:
34 # - A better name might be necessary (this actually allows not only
35 # netconf to be accessed but other restconf accessible subsystems as
37 # - The ConfigViaRestconf might need to be merged with this code to avoid
38 # strange name clashes when using both in a suite.
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 Create_NVR_Session nvr_session ${ODL_SYSTEM_IP}
44 BuiltIn.Set_Suite_Variable ${NetconfViaRestconf__active_session} nvr_session
46 Teardown_Netconf_Via_Restconf
47 [Documentation] Teardown to pair with Setup (otherwise no-op).
48 BuiltIn.Comment TODO: The following line does not seem to be implemented by RequestsLibrary. Look for a workaround.
49 BuiltIn.Comment Delete_Session nvr_session
51 Resolve_URI_From_Template_Folder
52 [Arguments] ${folder} ${mapping_as_string}
53 [Documentation] Read URI template from folder, strip endline, make changes according to mapping, return the result.
54 ${uri_template}= OperatingSystem.Get_File ${folder}${/}config.uri
55 BuiltIn.Log ${uri_template}
56 ${uri_part}= Strip_Endline_And_Apply_Substitutions_From_Mapping ${uri_template} ${mapping_as_string}
59 Resolve_Xml_Data_From_Template_Folder
60 [Arguments] ${folder} ${mapping_as_string}
61 [Documentation] Read data template from folder, strip endline, make changes according to mapping, return the result.
62 ${data_template}= OperatingSystem.Get_File ${folder}${/}data.xml
63 BuiltIn.Log ${data_template}
64 ${xml_data}= Strip_Endline_And_Apply_Substitutions_From_Mapping ${data_template} ${mapping_as_string}
67 Strip_Endline_And_Apply_Substitutions_From_Mapping
68 [Arguments] ${template_as_string} ${mapping_as_string}
69 [Documentation] Strip endline, apply substitutions, Log and return the result.
70 # Robot Framework does not understand dictionaries well, so resort to Evaluate.
71 # Needs python module "string", and since the template string is expected to contain newline, it has to be enclosed in triple quotes.
72 # Using rstrip() removes all trailing whitespace, which is what we want if there is something more than an endline.
73 ${final_text}= BuiltIn.Evaluate string.Template('''${template_as_string}'''.rstrip()).substitute(${mapping_as_string}) modules=string
74 BuiltIn.Log ${final_text}
75 [Return] ${final_text}
78 [Arguments] ${uri_part} ${xml_data}
79 [Documentation] Post XML data to given controller-config URI, check reponse text is empty and status_code is 204.
80 BuiltIn.Log ${uri_part}
81 BuiltIn.Log ${xml_data}
82 # As seen in previous two Keywords, Post does not need long specific URI.
83 # But during Lithium development, Post ceased to do merge, so those Keywords do not work anymore.
84 # This Keyword can still be used with specific URI to create a new container and fail if a container was already present.
85 ${response}= RequestsLibrary.Post ${NetconfViaRestconf__active_session} ${uri_part} data=${xml_data}
86 BuiltIn.Log ${response.text}
87 BuiltIn.Should_Be_Empty ${response.text}
88 BuiltIn.Should_Be_Equal_As_Strings ${response.status_code} 204
90 Post_Xml_Template_Folder_Via_Restconf
91 [Arguments] ${folder} ${mapping_as_string}={}
92 [Documentation] Resolve URI and data from folder, POST to restconf.
93 ${uri_part}= Resolve_URI_From_Template_Folder ${folder} ${mapping_as_string}
94 ${xml_data}= Resolve_Xml_Data_From_Template_Folder ${folder} ${mapping_as_string}
95 Post_Xml_Via_Restconf ${uri_part} ${xml_data}
98 [Arguments] ${uri_part} ${xml_data}
99 [Documentation] Put XML data to given controller-config URI, check reponse text is empty and status_code is one of allowed ones.
100 BuiltIn.Log ${uri_part}
101 BuiltIn.Log ${xml_data}
102 ${response}= RequestsLibrary.Put ${NetconfViaRestconf__active_session} ${uri_part} data=${xml_data}
103 BuiltIn.Log ${response.text}
104 BuiltIn.Log ${response.status_code}
105 BuiltIn.Should_Be_Empty ${response.text}
106 BuiltIn.Should_Contain ${allowed_status_codes} ${response.status_code}
108 Put_Xml_Template_Folder_Via_Restconf
109 [Arguments] ${folder} ${mapping_as_string}={}
110 [Documentation] Resolve URI and data from folder, PUT to controller config.
111 ${uri_part}= Resolve_URI_From_Template_Folder ${folder} ${mapping_as_string}
112 ${xml_data}= Resolve_Xml_Data_From_Template_Folder ${folder} ${mapping_as_string}
113 Put_Xml_Via_Restconf ${uri_part} ${xml_data}
116 [Arguments] ${uri_part}
117 [Documentation] Delete resource at controller-config URI, check reponse text is empty and status_code is 204.
118 BuiltIn.Log ${uri_part}
119 ${response}= RequestsLibrary.Delete ${NetconfViaRestconf__active_session} ${uri_part}
120 BuiltIn.Log ${response.text}
121 BuiltIn.Should_Be_Empty ${response.text}
122 BuiltIn.Should_Contain ${allowed_status_codes} ${response.status_code}
124 Delete_Xml_Template_Folder_Via_Restconf
125 [Arguments] ${folder} ${mapping_as_string}={}
126 [Documentation] Resolve URI from folder, DELETE from controller config.
127 ${uri_part}= Resolve_URI_From_Template_Folder ${folder} ${mapping_as_string}
128 Delete_Via_Restconf ${uri_part}
131 [Arguments] ${name} ${host}
132 [Documentation] Create a Netconf Via Restconf session pointing to the given host with the given name. The new session is NOT made active.
133 RequestsLibrary.Create_Session ${name} http://${host}:${RESTCONFPORT}${CONFIG_API} headers=${HEADERS_XML} auth=${AUTH}
137 [Documentation] Activate the given session and return the name of the previously active session.
138 ${result}= BuiltIn.Set_Variable ${NetconfViaRestconf__active_session}
139 BuiltIn.Set_Suite_Variable ${NetconfViaRestconf__active_session} ${name}