82362ecfb7cb323a7ed0b11f23d97e71e708c0c6
[osm/NBI.git] / osm_nbi / 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="/osm/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="/osm/static/OSM-logo.png" height="42" width="100"
35 style="vertical-align:middle"> </a>
36 <a>( {} )</a>
37 <a href="/osm/pdu/v1/pdu_descriptors">PDUs </a>
38 <a href="/osm/vnfpkgm/v1/vnf_packages">VNFDs </a>
39 <a href="/osm/nsd/v1/ns_descriptors">NSDs </a>
40 <a href="/osm/nslcm/v1/ns_instances">NSs </a>
41 <a href="/osm/nst/v1/netslice_templates">NSTDs </a>
42 <a href="/osm/nsilcm/v1/netslice_instances">NSIs </a>
43 <a href="/osm/admin/v1/users">USERs </a>
44 <a href="/osm/admin/v1/projects">PROJECTs </a>
45 <a href="/osm/admin/v1/tokens">TOKENs </a>
46 <a href="/osm/admin/v1/vim_accounts">VIMs </a>
47 <a href="/osm/admin/v1/wim_accounts">WIMs </a>
48 <a href="/osm/admin/v1/sdns">SDNs </a>
49 <a href="/osm/admin/v1/k8sclusters">K8s_clusters </a>
50 <a href="/osm/admin/v1/k8srepos">K8s_repos </a>
51 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
52 </div>
53 </div>
54 """
55
56 html_body = """
57 <h1>{item}</h1>
58 """
59
60 html_end = """
61 </body>
62 </html>
63 """
64
65 html_body_error = "<h2> Error <pre>{}</pre> </h2>"
66
67
68 html_auth2 = """
69 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
70 <html>
71 <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
72 <link href="/osm/static/style.css" rel="stylesheet">
73 <title>OSM Login</title>
74 </head>
75 <body>
76 <div id="osm_header">
77 <div>
78 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
79 </div>
80 </div>
81 <div id="osm_error_message">
82 <h1>{error}</h1>
83 </div>
84 <div class="gerritBody" id="osm_body">
85 <h1>Sign in to OSM</h1>
86 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
87 <table style="border: 0;">
88 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
89 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
90 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
91 </table>
92 </form>
93 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
94 <div id="osm_footer">
95 <div></div>
96 </div>
97 </div>
98 </div>
99 <script src="/osm/static/login.js"> </script>
100 </body>
101 </html>
102 """
103
104 html_upload_body = """
105 <form action="/osm{}" method="post" enctype="multipart/form-data">
106 <h3> <table style="border: 0;"> <tr>
107 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
108 <td> <input type="submit" value="Upload"/> </td>
109 </tr> </table> </h3>
110 </form>
111 """
112
113 html_nslcmop_body = """
114 <a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
115 <a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
116 <form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
117 <h3> <table style="border: 0;"> <tr>
118 <td> <input type="submit" value="Terminate"/> </td>
119 </tr> </table> </h3>
120 </form>
121 """
122
123 html_nsilcmop_body = """
124 <a href="/osm/nsilcm/v1/nsi_lcm_op_occs?netsliceInstanceId={id}">nsilcm operations </a>
125 <form action="/osm/nsilcm/v1/netslice_instances/{id}/terminate" method="post" enctype="multipart/form-data">
126 <h3> <table style="border: 0;"> <tr>
127 <td> <input type="submit" value="Terminate"/> </td>
128 </tr> </table> </h3>
129 </form>
130 """
131
132 html_vnfpackage_body = """<a href="/osm/vnfpkgm/v1/vnf_packages/{id}/artifacts">Artifacts </a>"""
133 html_nspackage_body = """<a href="/osm/nsd/v1/ns_descriptors/{id}/artifacts">Artifacts </a>"""
134
135
136 def format(data, request, response, toke_info):
137 """
138 Format a nice html response, depending on the data
139 :param data:
140 :param request: cherrypy request
141 :param response: cherrypy response
142 :return: string with teh html response
143 """
144 response.headers["Content-Type"] = 'text/html'
145 if response.status == HTTPStatus.UNAUTHORIZED.value:
146 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
147 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
148 return
149 else:
150 return html_auth2.format(error=data)
151 if request.path_info in ("/version", "/system"):
152 return "<pre>" + yaml.safe_dump(data, explicit_start=False, indent=4, default_flow_style=False) + "</pre>"
153 body = html_body.format(item=request.path_info)
154 if response.status and response.status > 202:
155 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
156 elif isinstance(data, (list, tuple)):
157 if request.path_info == "/vnfpkgm/v1/vnf_packages":
158 body += html_upload_body.format(request.path_info + "_content", "VNFD")
159 elif request.path_info == "/nsd/v1/ns_descriptors":
160 body += html_upload_body.format(request.path_info + "_content", "NSD")
161 elif request.path_info == "/nst/v1/nst_templates":
162 body += html_upload_body.format(request.path_info + "_content", "NSTD")
163 for k in data:
164 if isinstance(k, dict):
165 data_id = k.pop("_id", None)
166 elif isinstance(k, str):
167 data_id = k
168 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id,
169 t=html_escape(str(k)))
170 elif isinstance(data, dict):
171 if "Location" in response.headers:
172 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
173 else:
174 _id = request.path_info[request.path_info.rfind("/")+1:]
175 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\
176 .format(request.path_info)
177 if request.path_info.startswith("/nslcm/v1/ns_instances_content/") or \
178 request.path_info.startswith("/nslcm/v1/ns_instances/"):
179 body += html_nslcmop_body.format(id=_id)
180 elif request.path_info.startswith("/nsilcm/v1/netslice_instances_content/") or \
181 request.path_info.startswith("/nsilcm/v1/netslice_instances/"):
182 body += html_nsilcmop_body.format(id=_id)
183 elif request.path_info.startswith("/vnfpkgm/v1/vnf_packages/") or \
184 request.path_info.startswith("/vnfpkgm/v1/vnf_packages_content/"):
185 body += html_vnfpackage_body.format(id=_id)
186 elif request.path_info.startswith("/nsd/v1/ns_descriptors/") or \
187 request.path_info.startswith("/nsd/v1/ns_descriptors_content/"):
188 body += html_nspackage_body.format(id=_id)
189 body += "<pre>" + html_escape(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)) + \
190 "</pre>"
191 elif data is None:
192 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
193 body += "<pre> deleted </pre>"
194 else:
195 body = html_escape(str(data))
196 user_text = " "
197 if toke_info:
198 if toke_info.get("username"):
199 user_text += "user: {}".format(toke_info.get("username"))
200 if toke_info.get("project_id"):
201 user_text += ", project: {}".format(toke_info.get("project_name"))
202 return html_start.format(user_text) + body + html_end
203 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
204 # tags=False,
205 # encoding='utf-8', allow_unicode=True)