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