blob: 94793acb70e207804b0d4a5257309ad7bab217e3 [file] [log] [blame]
tiernod125caf2018-11-22 16:05:54 +00001# 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
tiernoc94c3df2018-02-09 15:38:54 +010014"""
15Contains html text in variables to make and html response
16"""
17
18import yaml
19from http import HTTPStatus
tiernoef4e2242018-10-31 17:09:52 +010020from html import escape as html_escape
tiernoc94c3df2018-02-09 15:38:54 +010021
22__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
23
24html_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>
tierno2236d202018-05-16 19:05:16 +020034 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100"
35 style="vertical-align:middle"> </a>
tiernoc94c3df2018-02-09 15:38:54 +010036 <a>( {} )</a>
tierno36ec8602018-11-02 17:27:11 +010037 <a href="/osm/pdu/v1/pdu_descriptors">PDUs </a>
tiernob24258a2018-10-04 18:39:49 +020038 <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>
Felipe Vicensb57758d2018-10-16 16:00:20 +020041 <a href="/osm/nst/v1/netslice_templates">NSTDs </a>
Felipe Vicens07f31722018-10-29 15:16:44 +010042 <a href="/osm/nsilcm/v1/netslice_instances">NSIs </a>
tiernof27c79b2018-03-12 17:08:42 +010043 <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>
tiernof759d822018-06-11 18:54:54 +020046 <a href="/osm/admin/v1/vim_accounts">VIMs </a>
47 <a href="/osm/admin/v1/sdns">SDNs </a>
tiernof27c79b2018-03-12 17:08:42 +010048 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
tiernoc94c3df2018-02-09 15:38:54 +010049 </div>
50 </div>
51"""
52
53html_body = """
54<h1>{item}</h1>
55"""
56
57html_end = """
58</body>
tierno2236d202018-05-16 19:05:16 +020059</html>
tiernoc94c3df2018-02-09 15:38:54 +010060"""
61
62html_body_error = "<h2> Error <pre>{}</pre> </h2>"
63
64
tiernoc94c3df2018-02-09 15:38:54 +010065html_auth2 = """
66<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
67<html>
68<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
69 <link href="/osm/static/style.css" rel="stylesheet">
70 <title>OSM Login</title>
71</head>
72<body>
73 <div id="osm_header">
74 <div>
75 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
76 </div>
77 </div>
78 <div id="osm_error_message">
79 <h1>{error}</h1>
80 </div>
81 <div class="gerritBody" id="osm_body">
82 <h1>Sign in to OSM</h1>
tiernof27c79b2018-03-12 17:08:42 +010083 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
tiernoc94c3df2018-02-09 15:38:54 +010084 <table style="border: 0;">
85 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
86 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
87 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
88 </table>
89 </form>
90 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
91 <div id="osm_footer">
92 <div></div>
93 </div>
94 </div>
95 </div>
96 <script src="/osm/static/login.js"> </script>
97</body>
98</html>
99"""
100
101html_upload_body = """
102<form action="/osm{}" method="post" enctype="multipart/form-data">
103 <h3> <table style="border: 0;"> <tr>
104 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
105 <td> <input type="submit" value="Upload"/> </td>
106 </tr> </table> </h3>
107</form>
108"""
109
tierno65acb4d2018-04-06 16:42:40 +0200110html_nslcmop_body = """
111<a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
tiernof759d822018-06-11 18:54:54 +0200112<a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
tierno65acb4d2018-04-06 16:42:40 +0200113<form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
114 <h3> <table style="border: 0;"> <tr>
115 <td> <input type="submit" value="Terminate"/> </td>
116 </tr> </table> </h3>
117</form>
118"""
119
Felipe Vicens07f31722018-10-29 15:16:44 +0100120html_nsilcmop_body = """
121<a href="/osm/nsilcm/v1/nsi_lcm_op_occs?nsiInstanceId={id}">nsilcm operations </a>
122<form action="/osm/nsilcm/v1/netslice_instances/{id}/terminate" method="post" enctype="multipart/form-data">
123 <h3> <table style="border: 0;"> <tr>
124 <td> <input type="submit" value="Terminate"/> </td>
125 </tr> </table> </h3>
126</form>
127"""
128
tiernoc94c3df2018-02-09 15:38:54 +0100129
130def format(data, request, response, session):
131 """
132 Format a nice html response, depending on the data
133 :param data:
134 :param request: cherrypy request
135 :param response: cherrypy response
136 :return: string with teh html response
137 """
138 response.headers["Content-Type"] = 'text/html'
139 if response.status == HTTPStatus.UNAUTHORIZED.value:
140 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
141 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
142 return
143 else:
144 return html_auth2.format(error=data)
145 body = html_body.format(item=request.path_info)
146 if response.status and response.status > 202:
147 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
148 elif isinstance(data, (list, tuple)):
tiernob24258a2018-10-04 18:39:49 +0200149 if request.path_info == "/vnfpkgm/v1/vnf_packages":
tierno0f98af52018-03-19 10:28:22 +0100150 body += html_upload_body.format(request.path_info, "VNFD")
tiernob24258a2018-10-04 18:39:49 +0200151 elif request.path_info == "/nsd/v1/ns_descriptors":
152 body += html_upload_body.format(request.path_info + "_content", "NSD")
Felipe Vicensb57758d2018-10-16 16:00:20 +0200153 elif request.path_info == "/nst/v1/nst_templates":
154 body += html_upload_body.format(request.path_info + "_content", "NSTD")
tiernoc94c3df2018-02-09 15:38:54 +0100155 for k in data:
tiernof27c79b2018-03-12 17:08:42 +0100156 if isinstance(k, dict):
157 data_id = k.pop("_id", None)
158 elif isinstance(k, str):
159 data_id = k
tiernoef4e2242018-10-31 17:09:52 +0100160 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id,
161 t=html_escape(str(k)))
tiernoc94c3df2018-02-09 15:38:54 +0100162 elif isinstance(data, dict):
163 if "Location" in response.headers:
164 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
165 else:
tierno2236d202018-05-16 19:05:16 +0200166 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\
167 .format(request.path_info)
tierno65acb4d2018-04-06 16:42:40 +0200168 if request.path_info.startswith("/nslcm/v1/ns_instances_content/") or \
169 request.path_info.startswith("/nslcm/v1/ns_instances/"):
170 _id = request.path_info[request.path_info.rfind("/")+1:]
171 body += html_nslcmop_body.format(id=_id)
Felipe Vicens07f31722018-10-29 15:16:44 +0100172 elif request.path_info.startswith("/nsilcm/v1/netslice_instances_content/") or \
173 request.path_info.startswith("/nsilcm/v1/netslice_instances/"):
174 _id = request.path_info[request.path_info.rfind("/")+1:]
175 body += html_nsilcmop_body.format(id=_id)
tiernoef4e2242018-10-31 17:09:52 +0100176 body += "<pre>" + html_escape(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)) + \
177 "</pre>"
tierno0f98af52018-03-19 10:28:22 +0100178 elif data is None:
179 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
180 body += "<pre> deleted </pre>"
tiernoc94c3df2018-02-09 15:38:54 +0100181 else:
tiernoef4e2242018-10-31 17:09:52 +0100182 body = html_escape(str(data))
tiernoc94c3df2018-02-09 15:38:54 +0100183 user_text = " "
184 if session:
185 if session.get("username"):
186 user_text += "user: {}".format(session.get("username"))
187 if session.get("project_id"):
188 user_text += ", project: {}".format(session.get("project_id"))
189 return html_start.format(user_text) + body + html_end
tierno2236d202018-05-16 19:05:16 +0200190 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
tiernoc94c3df2018-02-09 15:38:54 +0100191 # tags=False,
192 # encoding='utf-8', allow_unicode=True)