blob: 6b596d772519519d3fe3c1299a4e62380e9f78da [file] [log] [blame]
tiernoc94c3df2018-02-09 15:38:54 +01001"""
2Contains html text in variables to make and html response
3"""
4
5import yaml
6from http import HTTPStatus
tiernoef4e2242018-10-31 17:09:52 +01007from html import escape as html_escape
tiernoc94c3df2018-02-09 15:38:54 +01008
9__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
10
11html_start = """
12 <!DOCTYPE html>
13<html>
14<head>
15 <link href="/osm/static/style.css" rel="stylesheet">
16<title>Welcome to OSM</title>
17</head>
18<body>
19 <div id="osm_topmenu">
20 <div>
tierno2236d202018-05-16 19:05:16 +020021 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100"
22 style="vertical-align:middle"> </a>
tiernoc94c3df2018-02-09 15:38:54 +010023 <a>( {} )</a>
tierno36ec8602018-11-02 17:27:11 +010024 <a href="/osm/pdu/v1/pdu_descriptors">PDUs </a>
tiernob24258a2018-10-04 18:39:49 +020025 <a href="/osm/vnfpkgm/v1/vnf_packages">VNFDs </a>
26 <a href="/osm/nsd/v1/ns_descriptors">NSDs </a>
27 <a href="/osm/nslcm/v1/ns_instances">NSs </a>
Felipe Vicensb57758d2018-10-16 16:00:20 +020028 <a href="/osm/nst/v1/netslice_templates">NSTDs </a>
Felipe Vicens07f31722018-10-29 15:16:44 +010029 <a href="/osm/nsilcm/v1/netslice_instances">NSIs </a>
tiernof27c79b2018-03-12 17:08:42 +010030 <a href="/osm/admin/v1/users">USERs </a>
31 <a href="/osm/admin/v1/projects">PROJECTs </a>
32 <a href="/osm/admin/v1/tokens">TOKENs </a>
tiernof759d822018-06-11 18:54:54 +020033 <a href="/osm/admin/v1/vim_accounts">VIMs </a>
34 <a href="/osm/admin/v1/sdns">SDNs </a>
tiernof27c79b2018-03-12 17:08:42 +010035 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
tiernoc94c3df2018-02-09 15:38:54 +010036 </div>
37 </div>
38"""
39
40html_body = """
41<h1>{item}</h1>
42"""
43
44html_end = """
45</body>
tierno2236d202018-05-16 19:05:16 +020046</html>
tiernoc94c3df2018-02-09 15:38:54 +010047"""
48
49html_body_error = "<h2> Error <pre>{}</pre> </h2>"
50
51
tiernoc94c3df2018-02-09 15:38:54 +010052html_auth2 = """
53<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
54<html>
55<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
56 <link href="/osm/static/style.css" rel="stylesheet">
57 <title>OSM Login</title>
58</head>
59<body>
60 <div id="osm_header">
61 <div>
62 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
63 </div>
64 </div>
65 <div id="osm_error_message">
66 <h1>{error}</h1>
67 </div>
68 <div class="gerritBody" id="osm_body">
69 <h1>Sign in to OSM</h1>
tiernof27c79b2018-03-12 17:08:42 +010070 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
tiernoc94c3df2018-02-09 15:38:54 +010071 <table style="border: 0;">
72 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
73 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
74 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
75 </table>
76 </form>
77 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
78 <div id="osm_footer">
79 <div></div>
80 </div>
81 </div>
82 </div>
83 <script src="/osm/static/login.js"> </script>
84</body>
85</html>
86"""
87
88html_upload_body = """
89<form action="/osm{}" method="post" enctype="multipart/form-data">
90 <h3> <table style="border: 0;"> <tr>
91 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
92 <td> <input type="submit" value="Upload"/> </td>
93 </tr> </table> </h3>
94</form>
95"""
96
tierno65acb4d2018-04-06 16:42:40 +020097html_nslcmop_body = """
98<a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
tiernof759d822018-06-11 18:54:54 +020099<a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
tierno65acb4d2018-04-06 16:42:40 +0200100<form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
101 <h3> <table style="border: 0;"> <tr>
102 <td> <input type="submit" value="Terminate"/> </td>
103 </tr> </table> </h3>
104</form>
105"""
106
Felipe Vicens07f31722018-10-29 15:16:44 +0100107html_nsilcmop_body = """
108<a href="/osm/nsilcm/v1/nsi_lcm_op_occs?nsiInstanceId={id}">nsilcm operations </a>
109<form action="/osm/nsilcm/v1/netslice_instances/{id}/terminate" method="post" enctype="multipart/form-data">
110 <h3> <table style="border: 0;"> <tr>
111 <td> <input type="submit" value="Terminate"/> </td>
112 </tr> </table> </h3>
113</form>
114"""
115
tiernoc94c3df2018-02-09 15:38:54 +0100116
117def format(data, request, response, session):
118 """
119 Format a nice html response, depending on the data
120 :param data:
121 :param request: cherrypy request
122 :param response: cherrypy response
123 :return: string with teh html response
124 """
125 response.headers["Content-Type"] = 'text/html'
126 if response.status == HTTPStatus.UNAUTHORIZED.value:
127 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
128 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
129 return
130 else:
131 return html_auth2.format(error=data)
132 body = html_body.format(item=request.path_info)
133 if response.status and response.status > 202:
134 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
135 elif isinstance(data, (list, tuple)):
tiernob24258a2018-10-04 18:39:49 +0200136 if request.path_info == "/vnfpkgm/v1/vnf_packages":
tierno0f98af52018-03-19 10:28:22 +0100137 body += html_upload_body.format(request.path_info, "VNFD")
tiernob24258a2018-10-04 18:39:49 +0200138 elif request.path_info == "/nsd/v1/ns_descriptors":
139 body += html_upload_body.format(request.path_info + "_content", "NSD")
Felipe Vicensb57758d2018-10-16 16:00:20 +0200140 elif request.path_info == "/nst/v1/nst_templates":
141 body += html_upload_body.format(request.path_info + "_content", "NSTD")
tiernoc94c3df2018-02-09 15:38:54 +0100142 for k in data:
tiernof27c79b2018-03-12 17:08:42 +0100143 if isinstance(k, dict):
144 data_id = k.pop("_id", None)
145 elif isinstance(k, str):
146 data_id = k
tiernoef4e2242018-10-31 17:09:52 +0100147 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id,
148 t=html_escape(str(k)))
tiernoc94c3df2018-02-09 15:38:54 +0100149 elif isinstance(data, dict):
150 if "Location" in response.headers:
151 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
152 else:
tierno2236d202018-05-16 19:05:16 +0200153 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\
154 .format(request.path_info)
tierno65acb4d2018-04-06 16:42:40 +0200155 if request.path_info.startswith("/nslcm/v1/ns_instances_content/") or \
156 request.path_info.startswith("/nslcm/v1/ns_instances/"):
157 _id = request.path_info[request.path_info.rfind("/")+1:]
158 body += html_nslcmop_body.format(id=_id)
Felipe Vicens07f31722018-10-29 15:16:44 +0100159 elif request.path_info.startswith("/nsilcm/v1/netslice_instances_content/") or \
160 request.path_info.startswith("/nsilcm/v1/netslice_instances/"):
161 _id = request.path_info[request.path_info.rfind("/")+1:]
162 body += html_nsilcmop_body.format(id=_id)
tiernoef4e2242018-10-31 17:09:52 +0100163 body += "<pre>" + html_escape(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)) + \
164 "</pre>"
tierno0f98af52018-03-19 10:28:22 +0100165 elif data is None:
166 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
167 body += "<pre> deleted </pre>"
tiernoc94c3df2018-02-09 15:38:54 +0100168 else:
tiernoef4e2242018-10-31 17:09:52 +0100169 body = html_escape(str(data))
tiernoc94c3df2018-02-09 15:38:54 +0100170 user_text = " "
171 if session:
172 if session.get("username"):
173 user_text += "user: {}".format(session.get("username"))
174 if session.get("project_id"):
175 user_text += ", project: {}".format(session.get("project_id"))
176 return html_start.format(user_text) + body + html_end
tierno2236d202018-05-16 19:05:16 +0200177 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
tiernoc94c3df2018-02-09 15:38:54 +0100178 # tags=False,
179 # encoding='utf-8', allow_unicode=True)