e9237e92528ace0680315184fbcab49c8ff43400
[osm/NBI.git] / osm_nbi / html_out.py
1 """
2 Contains html text in variables to make and html response
3 """
4
5 import yaml
6 from http import HTTPStatus
7 from html import escape as html_escape
8
9 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
10
11 html_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>
21 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100"
22 style="vertical-align:middle"> </a>
23 <a>( {} )</a>
24 <a href="/osm/vnfpkgm/v1/vnf_packages">VNFDs </a>
25 <a href="/osm/nsd/v1/ns_descriptors">NSDs </a>
26 <a href="/osm/nslcm/v1/ns_instances">NSs </a>
27 <a href="/osm/nst/v1/netslice_templates">NSTDs </a>
28 <a href="/osm/admin/v1/users">USERs </a>
29 <a href="/osm/admin/v1/projects">PROJECTs </a>
30 <a href="/osm/admin/v1/tokens">TOKENs </a>
31 <a href="/osm/admin/v1/vim_accounts">VIMs </a>
32 <a href="/osm/admin/v1/sdns">SDNs </a>
33 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
34 </div>
35 </div>
36 """
37
38 html_body = """
39 <h1>{item}</h1>
40 """
41
42 html_end = """
43 </body>
44 </html>
45 """
46
47 html_body_error = "<h2> Error <pre>{}</pre> </h2>"
48
49
50 html_auth2 = """
51 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
52 <html>
53 <head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
54 <link href="/osm/static/style.css" rel="stylesheet">
55 <title>OSM Login</title>
56 </head>
57 <body>
58 <div id="osm_header">
59 <div>
60 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
61 </div>
62 </div>
63 <div id="osm_error_message">
64 <h1>{error}</h1>
65 </div>
66 <div class="gerritBody" id="osm_body">
67 <h1>Sign in to OSM</h1>
68 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
69 <table style="border: 0;">
70 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
71 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
72 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
73 </table>
74 </form>
75 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
76 <div id="osm_footer">
77 <div></div>
78 </div>
79 </div>
80 </div>
81 <script src="/osm/static/login.js"> </script>
82 </body>
83 </html>
84 """
85
86 html_upload_body = """
87 <form action="/osm{}" method="post" enctype="multipart/form-data">
88 <h3> <table style="border: 0;"> <tr>
89 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
90 <td> <input type="submit" value="Upload"/> </td>
91 </tr> </table> </h3>
92 </form>
93 """
94
95 html_nslcmop_body = """
96 <a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
97 <a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
98 <form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
99 <h3> <table style="border: 0;"> <tr>
100 <td> <input type="submit" value="Terminate"/> </td>
101 </tr> </table> </h3>
102 </form>
103 """
104
105
106 def format(data, request, response, session):
107 """
108 Format a nice html response, depending on the data
109 :param data:
110 :param request: cherrypy request
111 :param response: cherrypy response
112 :return: string with teh html response
113 """
114 response.headers["Content-Type"] = 'text/html'
115 if response.status == HTTPStatus.UNAUTHORIZED.value:
116 if response.headers.get("WWW-Authenticate") and request.config.get("auth.allow_basic_authentication"):
117 response.headers["WWW-Authenticate"] = "Basic" + response.headers["WWW-Authenticate"][6:]
118 return
119 else:
120 return html_auth2.format(error=data)
121 body = html_body.format(item=request.path_info)
122 if response.status and response.status > 202:
123 body += html_body_error.format(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False))
124 elif isinstance(data, (list, tuple)):
125 if request.path_info == "/vnfpkgm/v1/vnf_packages":
126 body += html_upload_body.format(request.path_info, "VNFD")
127 elif request.path_info == "/nsd/v1/ns_descriptors":
128 body += html_upload_body.format(request.path_info + "_content", "NSD")
129 elif request.path_info == "/nst/v1/nst_templates":
130 body += html_upload_body.format(request.path_info + "_content", "NSTD")
131 for k in data:
132 if isinstance(k, dict):
133 data_id = k.pop("_id", None)
134 elif isinstance(k, str):
135 data_id = k
136 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(url=request.path_info, id=data_id,
137 t=html_escape(str(k)))
138 elif isinstance(data, dict):
139 if "Location" in response.headers:
140 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
141 else:
142 body += '<a href="/osm/{}?METHOD=DELETE"> <img src="/osm/static/delete.png" height="25" width="25"> </a>'\
143 .format(request.path_info)
144 if request.path_info.startswith("/nslcm/v1/ns_instances_content/") or \
145 request.path_info.startswith("/nslcm/v1/ns_instances/"):
146 _id = request.path_info[request.path_info.rfind("/")+1:]
147 body += html_nslcmop_body.format(id=_id)
148 body += "<pre>" + html_escape(yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)) + \
149 "</pre>"
150 elif data is None:
151 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
152 body += "<pre> deleted </pre>"
153 else:
154 body = html_escape(str(data))
155 user_text = " "
156 if session:
157 if session.get("username"):
158 user_text += "user: {}".format(session.get("username"))
159 if session.get("project_id"):
160 user_text += ", project: {}".format(session.get("project_id"))
161 return html_start.format(user_text) + body + html_end
162 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
163 # tags=False,
164 # encoding='utf-8', allow_unicode=True)