Pip standerdization and tox replacement
[osm/RO.git] / NG-RO / osm_ng_ro / html_out.py
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 # implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """
15 Contains html text in variables to make and html response
16 """
17
18 import yaml
19 from http import HTTPStatus
20 from html import escape as html_escape
21
22 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
23
24 html_start = """
25 <!DOCTYPE html>
26 <html>
27 <head>
28 <link href="/ro/static/style.css" rel="stylesheet">
29 <title>Welcome to OSM</title>
30 </head>
31 <body>
32 <div id="osm_topmenu">
33 <div>
34 <a href="https://osm.etsi.org"> <img src="/ro/static/OSM-logo.png" height="42" width="100"
35 style="vertical-align:middle"> </a>
36 <a>( {} )</a>
37 <a href="/ro/ns/v1/deploy">NSs </a>
38 <a href="/ro/admin/v1/k8srepos">K8s_repos </a>
39 <a href="/ro/admin/v1/tokens?METHOD=DELETE">logout </a>
40 </div>
41 </div>
42 """
43
44 html_body = """
45 <h1>{item}</h1>
46 """
47
48 html_end = """
49 </body>
50 </html>
51 """
52
53 html_body_error = "<h2> Error <pre>{}</pre> </h2>"
54
55
56 html_auth2 = """
57 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
58 <html>
59 <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
60 <link href="/ro/static/style.css" rel="stylesheet">
61 <title>OSM Login</title>
62 </head>
63 <body>
64 <div id="osm_header">
65 <div>
66 <a href="https://osm.etsi.org"> <h1><img src="/ro/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
67 </div>
68 </div>
69 <div id="osm_error_message">
70 <h1>{error}</h1>
71 </div>
72 <div class="gerritBody" id="osm_body">
73 <h1>Sign in to OSM</h1>
74 <form action="/ro/admin/v1/tokens" id="login_form" method="POST">
75 <table style="border: 0;">
76 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
77 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
78 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
79 </table>
80 </form>
81 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
82 <div id="osm_footer">
83 <div></div>
84 </div>
85 </div>
86 </div>
87 <script src="/ro/static/login.js"> </script>
88 </body>
89 </html>
90 """
91
92
93 html_nslcmop_body = """
94 <a href="/ro/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
95 <a href="/ro/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
96 <form action="/ro/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
97 <h3> <table style="border: 0;"> <tr>
98 <td> <input type="submit" value="Terminate"/> </td>
99 </tr> </table> </h3>
100 </form>
101 """
102
103 html_nsilcmop_body = """
104 <a href="/ro/nsilcm/v1/nsi_lcm_op_occs?netsliceInstanceId={id}">nsilcm operations </a>
105 <form action="/ro/nsilcm/v1/netslice_instances/{id}/terminate" method="post" enctype="multipart/form-data">
106 <h3> <table style="border: 0;"> <tr>
107 <td> <input type="submit" value="Terminate"/> </td>
108 </tr> </table> </h3>
109 </form>
110 """
111
112
113 def format(data, request, response, toke_info):
114 """
115 Format a nice html response, depending on the data
116 :param data:
117 :param request: cherrypy request
118 :param response: cherrypy response
119 :return: string with teh html response
120 """
121 response.headers["Content-Type"] = "text/html"
122
123 if response.status == HTTPStatus.UNAUTHORIZED.value:
124 if response.headers.get("WWW-Authenticate") and request.config.get(
125 "auth.allow_basic_authentication"
126 ):
127 response.headers["WWW-Authenticate"] = (
128 "Basic" + response.headers["WWW-Authenticate"][6:]
129 )
130
131 return
132 else:
133 return html_auth2.format(error=data)
134
135 if request.path_info in ("/version", "/system"):
136 return (
137 "<pre>"
138 + yaml.safe_dump(
139 data, explicit_start=False, indent=4, default_flow_style=False
140 )
141 + "</pre>"
142 )
143
144 body = html_body.format(item=request.path_info)
145
146 if response.status and response.status > 202:
147 body += html_body_error.format(
148 yaml.safe_dump(
149 data, explicit_start=True, indent=4, default_flow_style=False
150 )
151 )
152 elif isinstance(data, (list, tuple)):
153 # if request.path_info == "/ns/v1/deploy":
154 # body += html_upload_body.format(request.path_info + "_content", "VNFD")
155 # elif request.path_info == "/nsd/v1/ns_descriptors":
156 # body += html_upload_body.format(request.path_info + "_content", "NSD")
157 # elif request.path_info == "/nst/v1/nst_templates":
158 # body += html_upload_body.format(request.path_info + "_content", "NSTD")
159 for k in data:
160 if isinstance(k, dict):
161 data_id = k.pop("_id", None)
162 elif isinstance(k, str):
163 data_id = k
164
165 if request.path_info == "/ns/v1/deploy":
166 body += (
167 '<p> <a href="/ro/{url}/{id}?METHOD=DELETE"> <img src="/ro/static/delete.png" height="25"'
168 ' width="25"> </a><a href="/ro/{url}/{id}">{id}</a>: {t} </p>'.format(
169 url=request.path_info, id=data_id, t=html_escape(str(k))
170 )
171 )
172 else:
173 body += '<p> <a href="/ro/{url}/{id}">{id}</a>: {t} </p>'.format(
174 url=request.path_info, id=data_id, t=html_escape(str(k))
175 )
176 elif isinstance(data, dict):
177 if "Location" in response.headers:
178 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
179 else:
180 body += (
181 '<a href="/ro/{}?METHOD=DELETE"> <img src="/ro/static/delete.png" height="25" width="25"> </a>'
182 ).format(request.path_info[: request.path_info.rfind("/")])
183
184 if request.path_info.startswith(
185 "/nslcm/v1/ns_instances_content/"
186 ) or request.path_info.startswith("/nslcm/v1/ns_instances/"):
187 _id = request.path_info[request.path_info.rfind("/") + 1 :]
188 body += html_nslcmop_body.format(id=_id)
189 elif request.path_info.startswith(
190 "/nsilcm/v1/netslice_instances_content/"
191 ) or request.path_info.startswith("/nsilcm/v1/netslice_instances/"):
192 _id = request.path_info[request.path_info.rfind("/") + 1 :]
193 body += html_nsilcmop_body.format(id=_id)
194
195 body += (
196 "<pre>"
197 + html_escape(
198 yaml.safe_dump(
199 data, explicit_start=True, indent=4, default_flow_style=False
200 )
201 )
202 + "</pre>"
203 )
204 elif data is None:
205 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
206 body += "<pre> deleted </pre>"
207 else:
208 body = html_escape(str(data))
209
210 user_text = " "
211
212 if toke_info:
213 if toke_info.get("username"):
214 user_text += "user: {}".format(toke_info.get("username"))
215
216 if toke_info.get("project_id"):
217 user_text += ", project: {}".format(toke_info.get("project_name"))
218
219 return html_start.format(user_text) + body + html_end
220 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
221 # tags=False,
222 # encoding='utf-8', allow_unicode=True)