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