blob: 31728463353b789fe997715a136a7367760274a1 [file] [log] [blame]
tiernoc94c3df2018-02-09 15:38:54 +01001#!/usr/bin/python3
2# -*- coding: utf-8 -*-
3
tiernod125caf2018-11-22 16:05:54 +00004# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
tiernoc94c3df2018-02-09 15:38:54 +010017import cherrypy
18import time
19import json
20import yaml
tierno9c630112019-08-29 14:21:41 +000021import osm_nbi.html_out as html
tiernoc94c3df2018-02-09 15:38:54 +010022import logging
tiernof5298be2018-05-16 14:43:57 +020023import logging.handlers
24import getopt
25import sys
Eduardo Sousa2f988212018-07-26 01:04:11 +010026
tierno9c630112019-08-29 14:21:41 +000027from osm_nbi.authconn import AuthException, AuthconnException
28from osm_nbi.auth import Authenticator
29from osm_nbi.engine import Engine, EngineException
30from osm_nbi.subscriptions import SubscriptionThread
31from osm_nbi.validation import ValidationError
tiernoa8d63632018-05-10 13:12:32 +020032from osm_common.dbbase import DbException
33from osm_common.fsbase import FsException
34from osm_common.msgbase import MsgException
tiernoc94c3df2018-02-09 15:38:54 +010035from http import HTTPStatus
tiernoc94c3df2018-02-09 15:38:54 +010036from codecs import getreader
tiernof5298be2018-05-16 14:43:57 +020037from os import environ, path
tiernob2e48bd2020-02-04 15:47:18 +000038from osm_nbi import version as nbi_version, version_date as nbi_version_date
tiernoc94c3df2018-02-09 15:38:54 +010039
40__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
tiernodfe09572018-04-24 10:41:10 +020041
tierno9c630112019-08-29 14:21:41 +000042__version__ = "0.1.3" # file version, not NBI version
43version_date = "Aug 2019"
44
tierno1f029d82019-06-13 22:37:04 +000045database_version = '1.2'
Eduardo Sousa819d34c2018-07-31 01:20:02 +010046auth_database_version = '1.0'
tierno932499c2019-01-28 17:28:10 +000047nbi_server = None # instance of Server class
48subscription_thread = None # instance of SubscriptionThread class
tiernoc94c3df2018-02-09 15:38:54 +010049
50"""
tiernof27c79b2018-03-12 17:08:42 +010051North Bound Interface (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented)
tiernoc94c3df2018-02-09 15:38:54 +010052URL: /osm GET POST PUT DELETE PATCH
garciadeblas9750c5a2018-10-15 16:20:35 +020053 /nsd/v1
tierno2236d202018-05-16 19:05:16 +020054 /ns_descriptors_content O O
55 /<nsdInfoId> O O O O
tiernoc94c3df2018-02-09 15:38:54 +010056 /ns_descriptors O5 O5
57 /<nsdInfoId> O5 O5 5
58 /nsd_content O5 O5
tiernof27c79b2018-03-12 17:08:42 +010059 /nsd O
60 /artifacts[/<artifactPath>] O
tiernoc94c3df2018-02-09 15:38:54 +010061 /pnf_descriptors 5 5
62 /<pnfdInfoId> 5 5 5
63 /pnfd_content 5 5
tiernof27c79b2018-03-12 17:08:42 +010064 /subscriptions 5 5
65 /<subscriptionId> 5 X
tiernoc94c3df2018-02-09 15:38:54 +010066
67 /vnfpkgm/v1
tierno55945e72018-04-06 16:40:27 +020068 /vnf_packages_content O O
tierno2236d202018-05-16 19:05:16 +020069 /<vnfPkgId> O O
tiernoc94c3df2018-02-09 15:38:54 +010070 /vnf_packages O5 O5
71 /<vnfPkgId> O5 O5 5
tiernoc94c3df2018-02-09 15:38:54 +010072 /package_content O5 O5
73 /upload_from_uri X
tiernof27c79b2018-03-12 17:08:42 +010074 /vnfd O5
75 /artifacts[/<artifactPath>] O5
76 /subscriptions X X
77 /<subscriptionId> X X
tiernoc94c3df2018-02-09 15:38:54 +010078
79 /nslcm/v1
tiernof27c79b2018-03-12 17:08:42 +010080 /ns_instances_content O O
tierno2236d202018-05-16 19:05:16 +020081 /<nsInstanceId> O O
tiernof27c79b2018-03-12 17:08:42 +010082 /ns_instances 5 5
tierno95692442018-05-24 18:05:28 +020083 /<nsInstanceId> O5 O5
tierno65acb4d2018-04-06 16:42:40 +020084 instantiate O5
85 terminate O5
86 action O
87 scale O5
88 heal 5
tiernoc94c3df2018-02-09 15:38:54 +010089 /ns_lcm_op_occs 5 5
90 /<nsLcmOpOccId> 5 5 5
91 TO BE COMPLETED 5 5
tiernof759d822018-06-11 18:54:54 +020092 /vnf_instances (also vnfrs for compatibility) O
93 /<vnfInstanceId> O
tiernof27c79b2018-03-12 17:08:42 +010094 /subscriptions 5 5
95 /<subscriptionId> 5 X
garciadeblas9750c5a2018-10-15 16:20:35 +020096
tiernocb83c942018-09-24 17:28:13 +020097 /pdu/v1
tierno032916c2019-03-22 13:27:12 +000098 /pdu_descriptors O O
tiernocb83c942018-09-24 17:28:13 +020099 /<id> O O O O
garciadeblas9750c5a2018-10-15 16:20:35 +0200100
tiernof27c79b2018-03-12 17:08:42 +0100101 /admin/v1
102 /tokens O O
tierno2236d202018-05-16 19:05:16 +0200103 /<id> O O
tiernof27c79b2018-03-12 17:08:42 +0100104 /users O O
tiernocd54a4a2018-09-12 16:40:35 +0200105 /<id> O O O O
tiernof27c79b2018-03-12 17:08:42 +0100106 /projects O O
tierno2236d202018-05-16 19:05:16 +0200107 /<id> O O
tierno55ba2e62018-12-11 17:22:22 +0000108 /vim_accounts (also vims for compatibility) O O
109 /<id> O O O
110 /wim_accounts O O
tierno2236d202018-05-16 19:05:16 +0200111 /<id> O O O
tierno0f98af52018-03-19 10:28:22 +0100112 /sdns O O
tierno2236d202018-05-16 19:05:16 +0200113 /<id> O O O
delacruzramofe598fe2019-10-23 18:25:11 +0200114 /k8sclusters O O
115 /<id> O O O
116 /k8srepos O O
117 /<id> O O
Felipe Vicens220fd4e2020-05-06 10:11:00 +0200118 /osmrepos O O
119 /<id> O O
tiernoc94c3df2018-02-09 15:38:54 +0100120
garciadeblas9750c5a2018-10-15 16:20:35 +0200121 /nst/v1 O O
122 /netslice_templates_content O O
123 /<nstInfoId> O O O O
124 /netslice_templates O O
125 /<nstInfoId> O O O
126 /nst_content O O
127 /nst O
128 /artifacts[/<artifactPath>] O
129 /subscriptions X X
130 /<subscriptionId> X X
131
132 /nsilcm/v1
133 /netslice_instances_content O O
134 /<SliceInstanceId> O O
135 /netslice_instances O O
136 /<SliceInstanceId> O O
137 instantiate O
138 terminate O
139 action O
140 /nsi_lcm_op_occs O O
141 /<nsiLcmOpOccId> O O O
142 /subscriptions X X
143 /<subscriptionId> X X
144
tierno2236d202018-05-16 19:05:16 +0200145query string:
146 Follows SOL005 section 4.3.2 It contains extra METHOD to override http method, FORCE to force.
tierno36ec8602018-11-02 17:27:11 +0100147 simpleFilterExpr := <attrName>["."<attrName>]*["."<op>]"="<value>[","<value>]*
148 filterExpr := <simpleFilterExpr>["&"<simpleFilterExpr>]*
149 op := "eq" | "neq" (or "ne") | "gt" | "lt" | "gte" | "lte" | "cont" | "ncont"
150 attrName := string
tierno2236d202018-05-16 19:05:16 +0200151 For filtering inside array, it must select the element of the array, or add ANYINDEX to apply the filtering over any
152 item of the array, that is, pass if any item of the array pass the filter.
153 It allows both ne and neq for not equal
154 TODO: 4.3.3 Attribute selectors
155 all_fields, fields=x,y,.., exclude_default, exclude_fields=x,y,...
tiernoc94c3df2018-02-09 15:38:54 +0100156 (none) … same as “exclude_default”
157 all_fields … all attributes.
tierno2236d202018-05-16 19:05:16 +0200158 fields=<list> … all attributes except all complex attributes with minimum cardinality of zero that are not
159 conditionally mandatory, and that are not provided in <list>.
160 exclude_fields=<list> … all attributes except those complex attributes with a minimum cardinality of zero that
161 are not conditionally mandatory, and that are provided in <list>.
162 exclude_default … all attributes except those complex attributes with a minimum cardinality of zero that are not
163 conditionally mandatory, and that are part of the "default exclude set" defined in the present specification for
164 the particular resource
165 exclude_default and include=<list> … all attributes except those complex attributes with a minimum cardinality
166 of zero that are not conditionally mandatory and that are part of the "default exclude set" defined in the
167 present specification for the particular resource, but that are not part of <list>
tierno65ca36d2019-02-12 19:27:52 +0100168 Additionally it admits some administrator values:
169 FORCE: To force operations skipping dependency checkings
170 ADMIN: To act as an administrator or a different project
171 PUBLIC: To get public descriptors or set a descriptor as public
172 SET_PROJECT: To make a descriptor available for other project
173
tiernoc94c3df2018-02-09 15:38:54 +0100174Header field name Reference Example Descriptions
175 Accept IETF RFC 7231 [19] application/json Content-Types that are acceptable for the response.
176 This header field shall be present if the response is expected to have a non-empty message body.
177 Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the request.
178 This header field shall be present if the request has a non-empty message body.
tierno2236d202018-05-16 19:05:16 +0200179 Authorization IETF RFC 7235 [22] Bearer mF_9.B5f-4.1JqM The authorization token for the request.
180 Details are specified in clause 4.5.3.
tiernoc94c3df2018-02-09 15:38:54 +0100181 Range IETF RFC 7233 [21] 1000-2000 Requested range of bytes from a file
182Header field name Reference Example Descriptions
183 Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the response.
184 This header field shall be present if the response has a non-empty message body.
tierno2236d202018-05-16 19:05:16 +0200185 Location IETF RFC 7231 [19] http://www.example.com/vnflcm/v1/vnf_instances/123 Used in redirection, or when a
186 new resource has been created.
tiernoc94c3df2018-02-09 15:38:54 +0100187 This header field shall be present if the response status code is 201 or 3xx.
tierno2236d202018-05-16 19:05:16 +0200188 In the present document this header field is also used if the response status code is 202 and a new resource was
189 created.
190 WWW-Authenticate IETF RFC 7235 [22] Bearer realm="example" Challenge if the corresponding HTTP request has not
191 provided authorization, or error details if the corresponding HTTP request has provided an invalid authorization
192 token.
193 Accept-Ranges IETF RFC 7233 [21] bytes Used by the Server to signal whether or not it supports ranges for
194 certain resources.
195 Content-Range IETF RFC 7233 [21] bytes 21010-47021/ 47022 Signals the byte range that is contained in the
196 response, and the total length of the file.
tiernoc94c3df2018-02-09 15:38:54 +0100197 Retry-After IETF RFC 7231 [19] Fri, 31 Dec 1999 23:59:59 GMT
tiernoc94c3df2018-02-09 15:38:54 +0100198"""
199
tierno701018c2019-06-25 11:13:14 +0000200valid_query_string = ("ADMIN", "SET_PROJECT", "FORCE", "PUBLIC")
201# ^ Contains possible administrative query string words:
202# ADMIN=True(by default)|Project|Project-list: See all elements, or elements of a project
203# (not owned by my session project).
204# PUBLIC=True(by default)|False: See/hide public elements. Set/Unset a topic to be public
205# FORCE=True(by default)|False: Force edition/deletion operations
206# SET_PROJECT=Project|Project-list: Add/Delete the topic to the projects portfolio
207
208valid_url_methods = {
209 # contains allowed URL and methods, and the role_permission name
210 "admin": {
211 "v1": {
212 "tokens": {"METHODS": ("GET", "POST", "DELETE"),
213 "ROLE_PERMISSION": "tokens:",
214 "<ID>": {"METHODS": ("GET", "DELETE"),
215 "ROLE_PERMISSION": "tokens:id:"
216 }
217 },
218 "users": {"METHODS": ("GET", "POST"),
219 "ROLE_PERMISSION": "users:",
delacruzramo1459d602019-10-03 14:22:00 +0200220 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000221 "ROLE_PERMISSION": "users:id:"
222 }
223 },
224 "projects": {"METHODS": ("GET", "POST"),
225 "ROLE_PERMISSION": "projects:",
delacruzramo1459d602019-10-03 14:22:00 +0200226 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000227 "ROLE_PERMISSION": "projects:id:"}
228 },
229 "roles": {"METHODS": ("GET", "POST"),
230 "ROLE_PERMISSION": "roles:",
delacruzramo1459d602019-10-03 14:22:00 +0200231 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000232 "ROLE_PERMISSION": "roles:id:"
233 }
234 },
235 "vims": {"METHODS": ("GET", "POST"),
236 "ROLE_PERMISSION": "vims:",
delacruzramo1459d602019-10-03 14:22:00 +0200237 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000238 "ROLE_PERMISSION": "vims:id:"
239 }
240 },
241 "vim_accounts": {"METHODS": ("GET", "POST"),
242 "ROLE_PERMISSION": "vim_accounts:",
delacruzramo1459d602019-10-03 14:22:00 +0200243 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000244 "ROLE_PERMISSION": "vim_accounts:id:"
245 }
246 },
247 "wim_accounts": {"METHODS": ("GET", "POST"),
248 "ROLE_PERMISSION": "wim_accounts:",
delacruzramo1459d602019-10-03 14:22:00 +0200249 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000250 "ROLE_PERMISSION": "wim_accounts:id:"
251 }
252 },
253 "sdns": {"METHODS": ("GET", "POST"),
254 "ROLE_PERMISSION": "sdn_controllers:",
delacruzramo1459d602019-10-03 14:22:00 +0200255 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
tierno701018c2019-06-25 11:13:14 +0000256 "ROLE_PERMISSION": "sdn_controllers:id:"
257 }
258 },
delacruzramofe598fe2019-10-23 18:25:11 +0200259 "k8sclusters": {"METHODS": ("GET", "POST"),
260 "ROLE_PERMISSION": "k8sclusters:",
delacruzramo549dda62019-11-14 09:57:35 +0100261 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
delacruzramofe598fe2019-10-23 18:25:11 +0200262 "ROLE_PERMISSION": "k8sclusters:id:"
263 }
264 },
265 "k8srepos": {"METHODS": ("GET", "POST"),
266 "ROLE_PERMISSION": "k8srepos:",
267 "<ID>": {"METHODS": ("GET", "DELETE"),
268 "ROLE_PERMISSION": "k8srepos:id:"
269 }
270 },
Felipe Vicens220fd4e2020-05-06 10:11:00 +0200271 "osmrepos": {"METHODS": ("GET", "POST"),
272 "ROLE_PERMISSION": "osmrepos:",
273 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
274 "ROLE_PERMISSION": "osmrepos:id:"
275 }
276 },
tierno12eac3c2020-03-19 23:22:08 +0000277 "domains": {"METHODS": ("GET", ),
278 "ROLE_PERMISSION": "domains:",
279 },
tierno701018c2019-06-25 11:13:14 +0000280 }
281 },
282 "pdu": {
283 "v1": {
284 "pdu_descriptors": {"METHODS": ("GET", "POST"),
285 "ROLE_PERMISSION": "pduds:",
286 "<ID>": {"METHODS": ("GET", "POST", "DELETE", "PATCH", "PUT"),
287 "ROLE_PERMISSION": "pduds:id:"
288 }
289 },
290 }
291 },
292 "nsd": {
293 "v1": {
294 "ns_descriptors_content": {"METHODS": ("GET", "POST"),
295 "ROLE_PERMISSION": "nsds:",
296 "<ID>": {"METHODS": ("GET", "PUT", "DELETE"),
297 "ROLE_PERMISSION": "nsds:id:"
298 }
299 },
300 "ns_descriptors": {"METHODS": ("GET", "POST"),
301 "ROLE_PERMISSION": "nsds:",
302 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"),
303 "ROLE_PERMISSION": "nsds:id:",
304 "nsd_content": {"METHODS": ("GET", "PUT"),
305 "ROLE_PERMISSION": "nsds:id:content:",
306 },
307 "nsd": {"METHODS": ("GET",), # descriptor inside package
308 "ROLE_PERMISSION": "nsds:id:content:"
309 },
310 "artifacts": {"*": {"METHODS": ("GET",),
311 "ROLE_PERMISSION": "nsds:id:nsd_artifact:"
312 }
313 }
314 }
315 },
316 "pnf_descriptors": {"TODO": ("GET", "POST"),
317 "<ID>": {"TODO": ("GET", "DELETE", "PATCH"),
318 "pnfd_content": {"TODO": ("GET", "PUT")}
319 }
320 },
321 "subscriptions": {"TODO": ("GET", "POST"),
322 "<ID>": {"TODO": ("GET", "DELETE")}
323 },
324 }
325 },
326 "vnfpkgm": {
327 "v1": {
328 "vnf_packages_content": {"METHODS": ("GET", "POST"),
329 "ROLE_PERMISSION": "vnfds:",
330 "<ID>": {"METHODS": ("GET", "PUT", "DELETE"),
331 "ROLE_PERMISSION": "vnfds:id:"}
332 },
333 "vnf_packages": {"METHODS": ("GET", "POST"),
334 "ROLE_PERMISSION": "vnfds:",
335 "<ID>": {"METHODS": ("GET", "DELETE", "PATCH"), # GET: vnfPkgInfo
336 "ROLE_PERMISSION": "vnfds:id:",
337 "package_content": {"METHODS": ("GET", "PUT"), # package
338 "ROLE_PERMISSION": "vnfds:id:",
339 "upload_from_uri": {"METHODS": (),
340 "TODO": ("POST", ),
341 "ROLE_PERMISSION": "vnfds:id:upload:"
342 }
343 },
344 "vnfd": {"METHODS": ("GET", ), # descriptor inside package
345 "ROLE_PERMISSION": "vnfds:id:content:"
346 },
347 "artifacts": {"*": {"METHODS": ("GET", ),
348 "ROLE_PERMISSION": "vnfds:id:vnfd_artifact:"
349 }
delacruzramo271d2002019-12-02 21:00:37 +0100350 },
351 "action": {"METHODS": ("POST", ),
352 "ROLE_PERMISSION": "vnfds:id:action:"
353 },
tierno701018c2019-06-25 11:13:14 +0000354 }
355 },
356 "subscriptions": {"TODO": ("GET", "POST"),
357 "<ID>": {"TODO": ("GET", "DELETE")}
358 },
delacruzramo271d2002019-12-02 21:00:37 +0100359 "vnfpkg_op_occs": {"METHODS": ("GET", ),
360 "ROLE_PERMISSION": "vnfds:vnfpkgops:",
361 "<ID>": {"METHODS": ("GET", ),
362 "ROLE_PERMISSION": "vnfds:vnfpkgops:id:"
363 }
364 },
tierno701018c2019-06-25 11:13:14 +0000365 }
366 },
367 "nslcm": {
368 "v1": {
369 "ns_instances_content": {"METHODS": ("GET", "POST"),
370 "ROLE_PERMISSION": "ns_instances:",
371 "<ID>": {"METHODS": ("GET", "DELETE"),
372 "ROLE_PERMISSION": "ns_instances:id:"
373 }
374 },
375 "ns_instances": {"METHODS": ("GET", "POST"),
376 "ROLE_PERMISSION": "ns_instances:",
377 "<ID>": {"METHODS": ("GET", "DELETE"),
378 "ROLE_PERMISSION": "ns_instances:id:",
379 "scale": {"METHODS": ("POST",),
380 "ROLE_PERMISSION": "ns_instances:id:scale:"
381 },
382 "terminate": {"METHODS": ("POST",),
383 "ROLE_PERMISSION": "ns_instances:id:terminate:"
384 },
385 "instantiate": {"METHODS": ("POST",),
386 "ROLE_PERMISSION": "ns_instances:id:instantiate:"
387 },
388 "action": {"METHODS": ("POST",),
389 "ROLE_PERMISSION": "ns_instances:id:action:"
390 },
391 }
392 },
393 "ns_lcm_op_occs": {"METHODS": ("GET",),
394 "ROLE_PERMISSION": "ns_instances:opps:",
395 "<ID>": {"METHODS": ("GET",),
396 "ROLE_PERMISSION": "ns_instances:opps:id:"
397 },
398 },
399 "vnfrs": {"METHODS": ("GET",),
400 "ROLE_PERMISSION": "vnf_instances:",
401 "<ID>": {"METHODS": ("GET",),
402 "ROLE_PERMISSION": "vnf_instances:id:"
403 }
404 },
405 "vnf_instances": {"METHODS": ("GET",),
406 "ROLE_PERMISSION": "vnf_instances:",
407 "<ID>": {"METHODS": ("GET",),
408 "ROLE_PERMISSION": "vnf_instances:id:"
409 }
410 },
411 }
412 },
413 "nst": {
414 "v1": {
415 "netslice_templates_content": {"METHODS": ("GET", "POST"),
416 "ROLE_PERMISSION": "slice_templates:",
417 "<ID>": {"METHODS": ("GET", "PUT", "DELETE"),
418 "ROLE_PERMISSION": "slice_templates:id:", }
419 },
420 "netslice_templates": {"METHODS": ("GET", "POST"),
421 "ROLE_PERMISSION": "slice_templates:",
422 "<ID>": {"METHODS": ("GET", "DELETE"),
423 "TODO": ("PATCH",),
424 "ROLE_PERMISSION": "slice_templates:id:",
425 "nst_content": {"METHODS": ("GET", "PUT"),
426 "ROLE_PERMISSION": "slice_templates:id:content:"
427 },
428 "nst": {"METHODS": ("GET",), # descriptor inside package
429 "ROLE_PERMISSION": "slice_templates:id:content:"
430 },
431 "artifacts": {"*": {"METHODS": ("GET",),
432 "ROLE_PERMISSION": "slice_templates:id:content:"
433 }
434 }
435 }
436 },
437 "subscriptions": {"TODO": ("GET", "POST"),
438 "<ID>": {"TODO": ("GET", "DELETE")}
439 },
440 }
441 },
442 "nsilcm": {
443 "v1": {
444 "netslice_instances_content": {"METHODS": ("GET", "POST"),
445 "ROLE_PERMISSION": "slice_instances:",
446 "<ID>": {"METHODS": ("GET", "DELETE"),
447 "ROLE_PERMISSION": "slice_instances:id:"
448 }
449 },
450 "netslice_instances": {"METHODS": ("GET", "POST"),
451 "ROLE_PERMISSION": "slice_instances:",
452 "<ID>": {"METHODS": ("GET", "DELETE"),
453 "ROLE_PERMISSION": "slice_instances:id:",
454 "terminate": {"METHODS": ("POST",),
455 "ROLE_PERMISSION": "slice_instances:id:terminate:"
456 },
457 "instantiate": {"METHODS": ("POST",),
458 "ROLE_PERMISSION": "slice_instances:id:instantiate:"
459 },
460 "action": {"METHODS": ("POST",),
461 "ROLE_PERMISSION": "slice_instances:id:action:"
462 },
463 }
464 },
465 "nsi_lcm_op_occs": {"METHODS": ("GET",),
466 "ROLE_PERMISSION": "slice_instances:opps:",
467 "<ID>": {"METHODS": ("GET",),
468 "ROLE_PERMISSION": "slice_instances:opps:id:",
469 },
470 },
471 }
472 },
473 "nspm": {
474 "v1": {
475 "pm_jobs": {
476 "<ID>": {
477 "reports": {
478 "<ID>": {"METHODS": ("GET",),
479 "ROLE_PERMISSION": "reports:id:",
480 }
481 }
482 },
483 },
484 },
485 },
486}
487
tiernoc94c3df2018-02-09 15:38:54 +0100488
489class NbiException(Exception):
490
491 def __init__(self, message, http_code=HTTPStatus.METHOD_NOT_ALLOWED):
492 Exception.__init__(self, message)
493 self.http_code = http_code
494
495
496class Server(object):
497 instance = 0
498 # to decode bytes to str
499 reader = getreader("utf-8")
500
501 def __init__(self):
502 self.instance += 1
tierno701018c2019-06-25 11:13:14 +0000503 self.authenticator = Authenticator(valid_url_methods, valid_query_string)
delacruzramoad682a52019-12-10 16:26:34 +0100504 self.engine = Engine(self.authenticator)
tiernoc94c3df2018-02-09 15:38:54 +0100505
tiernoc94c3df2018-02-09 15:38:54 +0100506 def _format_in(self, kwargs):
507 try:
508 indata = None
509 if cherrypy.request.body.length:
510 error_text = "Invalid input format "
511
512 if "Content-Type" in cherrypy.request.headers:
513 if "application/json" in cherrypy.request.headers["Content-Type"]:
514 error_text = "Invalid json format "
515 indata = json.load(self.reader(cherrypy.request.body))
gcalvinode4adfe2018-10-30 11:46:09 +0100516 cherrypy.request.headers.pop("Content-File-MD5", None)
tiernoc94c3df2018-02-09 15:38:54 +0100517 elif "application/yaml" in cherrypy.request.headers["Content-Type"]:
518 error_text = "Invalid yaml format "
delacruzramob19cadc2019-10-08 10:18:02 +0200519 indata = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
gcalvinode4adfe2018-10-30 11:46:09 +0100520 cherrypy.request.headers.pop("Content-File-MD5", None)
tiernoc94c3df2018-02-09 15:38:54 +0100521 elif "application/binary" in cherrypy.request.headers["Content-Type"] or \
522 "application/gzip" in cherrypy.request.headers["Content-Type"] or \
tiernof27c79b2018-03-12 17:08:42 +0100523 "application/zip" in cherrypy.request.headers["Content-Type"] or \
524 "text/plain" in cherrypy.request.headers["Content-Type"]:
525 indata = cherrypy.request.body # .read()
tiernoc94c3df2018-02-09 15:38:54 +0100526 elif "multipart/form-data" in cherrypy.request.headers["Content-Type"]:
527 if "descriptor_file" in kwargs:
528 filecontent = kwargs.pop("descriptor_file")
529 if not filecontent.file:
530 raise NbiException("empty file or content", HTTPStatus.BAD_REQUEST)
tiernof27c79b2018-03-12 17:08:42 +0100531 indata = filecontent.file # .read()
tiernoc94c3df2018-02-09 15:38:54 +0100532 if filecontent.content_type.value:
533 cherrypy.request.headers["Content-Type"] = filecontent.content_type.value
534 else:
535 # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable,
536 # "Only 'Content-Type' of type 'application/json' or
537 # 'application/yaml' for input format are available")
538 error_text = "Invalid yaml format "
delacruzramob19cadc2019-10-08 10:18:02 +0200539 indata = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
gcalvinode4adfe2018-10-30 11:46:09 +0100540 cherrypy.request.headers.pop("Content-File-MD5", None)
tiernoc94c3df2018-02-09 15:38:54 +0100541 else:
542 error_text = "Invalid yaml format "
delacruzramob19cadc2019-10-08 10:18:02 +0200543 indata = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
gcalvinode4adfe2018-10-30 11:46:09 +0100544 cherrypy.request.headers.pop("Content-File-MD5", None)
tiernoc94c3df2018-02-09 15:38:54 +0100545 if not indata:
546 indata = {}
547
tiernoc94c3df2018-02-09 15:38:54 +0100548 format_yaml = False
549 if cherrypy.request.headers.get("Query-String-Format") == "yaml":
550 format_yaml = True
551
552 for k, v in kwargs.items():
553 if isinstance(v, str):
554 if v == "":
555 kwargs[k] = None
556 elif format_yaml:
557 try:
delacruzramob19cadc2019-10-08 10:18:02 +0200558 kwargs[k] = yaml.load(v, Loader=yaml.SafeLoader)
tiernoe1281182018-05-22 12:24:36 +0200559 except Exception:
tiernoc94c3df2018-02-09 15:38:54 +0100560 pass
561 elif k.endswith(".gt") or k.endswith(".lt") or k.endswith(".gte") or k.endswith(".lte"):
562 try:
563 kwargs[k] = int(v)
tiernoe1281182018-05-22 12:24:36 +0200564 except Exception:
tiernoc94c3df2018-02-09 15:38:54 +0100565 try:
566 kwargs[k] = float(v)
tiernoe1281182018-05-22 12:24:36 +0200567 except Exception:
tiernoc94c3df2018-02-09 15:38:54 +0100568 pass
569 elif v.find(",") > 0:
570 kwargs[k] = v.split(",")
571 elif isinstance(v, (list, tuple)):
572 for index in range(0, len(v)):
573 if v[index] == "":
574 v[index] = None
575 elif format_yaml:
576 try:
delacruzramob19cadc2019-10-08 10:18:02 +0200577 v[index] = yaml.load(v[index], Loader=yaml.SafeLoader)
tiernoe1281182018-05-22 12:24:36 +0200578 except Exception:
tiernoc94c3df2018-02-09 15:38:54 +0100579 pass
580
tiernof27c79b2018-03-12 17:08:42 +0100581 return indata
tiernoc94c3df2018-02-09 15:38:54 +0100582 except (ValueError, yaml.YAMLError) as exc:
583 raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
584 except KeyError as exc:
585 raise NbiException("Query string error: " + str(exc), HTTPStatus.BAD_REQUEST)
tiernob92094f2018-05-11 13:44:22 +0200586 except Exception as exc:
587 raise NbiException(error_text + str(exc), HTTPStatus.BAD_REQUEST)
tiernoc94c3df2018-02-09 15:38:54 +0100588
589 @staticmethod
tierno701018c2019-06-25 11:13:14 +0000590 def _format_out(data, token_info=None, _format=None):
tiernoc94c3df2018-02-09 15:38:54 +0100591 """
592 return string of dictionary data according to requested json, yaml, xml. By default json
tiernof27c79b2018-03-12 17:08:42 +0100593 :param data: response to be sent. Can be a dict, text or file
tierno701018c2019-06-25 11:13:14 +0000594 :param token_info: Contains among other username and project
tierno5792d7d2019-08-30 15:37:12 +0000595 :param _format: The format to be set as Content-Type if data is a file
tiernoc94c3df2018-02-09 15:38:54 +0100596 :return: None
597 """
tierno0f98af52018-03-19 10:28:22 +0100598 accept = cherrypy.request.headers.get("Accept")
tiernof27c79b2018-03-12 17:08:42 +0100599 if data is None:
tierno0f98af52018-03-19 10:28:22 +0100600 if accept and "text/html" in accept:
tierno701018c2019-06-25 11:13:14 +0000601 return html.format(data, cherrypy.request, cherrypy.response, token_info)
tierno09c073e2018-04-26 13:36:48 +0200602 # cherrypy.response.status = HTTPStatus.NO_CONTENT.value
tiernof27c79b2018-03-12 17:08:42 +0100603 return
604 elif hasattr(data, "read"): # file object
605 if _format:
606 cherrypy.response.headers["Content-Type"] = _format
607 elif "b" in data.mode: # binariy asssumig zip
608 cherrypy.response.headers["Content-Type"] = 'application/zip'
609 else:
610 cherrypy.response.headers["Content-Type"] = 'text/plain'
611 # TODO check that cherrypy close file. If not implement pending things to close per thread next
612 return data
tierno0f98af52018-03-19 10:28:22 +0100613 if accept:
tiernoc94c3df2018-02-09 15:38:54 +0100614 if "application/json" in accept:
615 cherrypy.response.headers["Content-Type"] = 'application/json; charset=utf-8'
616 a = json.dumps(data, indent=4) + "\n"
617 return a.encode("utf8")
618 elif "text/html" in accept:
tierno701018c2019-06-25 11:13:14 +0000619 return html.format(data, cherrypy.request, cherrypy.response, token_info)
tiernoc94c3df2018-02-09 15:38:54 +0100620
tiernof27c79b2018-03-12 17:08:42 +0100621 elif "application/yaml" in accept or "*/*" in accept or "text/plain" in accept:
tiernoc94c3df2018-02-09 15:38:54 +0100622 pass
tiernof717cbe2018-12-03 16:35:42 +0000623 # if there is not any valid accept, raise an error. But if response is already an error, format in yaml
624 elif cherrypy.response.status >= 400:
tiernoc94c3df2018-02-09 15:38:54 +0100625 raise cherrypy.HTTPError(HTTPStatus.NOT_ACCEPTABLE.value,
626 "Only 'Accept' of type 'application/json' or 'application/yaml' "
627 "for output format are available")
628 cherrypy.response.headers["Content-Type"] = 'application/yaml'
629 return yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False, tags=False,
630 encoding='utf-8', allow_unicode=True) # , canonical=True, default_style='"'
631
632 @cherrypy.expose
633 def index(self, *args, **kwargs):
tierno701018c2019-06-25 11:13:14 +0000634 token_info = None
tiernoc94c3df2018-02-09 15:38:54 +0100635 try:
636 if cherrypy.request.method == "GET":
tierno701018c2019-06-25 11:13:14 +0000637 token_info = self.authenticator.authorize()
delacruzramo01b15d32019-07-02 14:37:47 +0200638 outdata = token_info # Home page
tiernoc94c3df2018-02-09 15:38:54 +0100639 else:
640 raise cherrypy.HTTPError(HTTPStatus.METHOD_NOT_ALLOWED.value,
tierno2236d202018-05-16 19:05:16 +0200641 "Method {} not allowed for tokens".format(cherrypy.request.method))
tiernoc94c3df2018-02-09 15:38:54 +0100642
tierno701018c2019-06-25 11:13:14 +0000643 return self._format_out(outdata, token_info)
tiernoc94c3df2018-02-09 15:38:54 +0100644
Eduardo Sousa819d34c2018-07-31 01:20:02 +0100645 except (EngineException, AuthException) as e:
tierno701018c2019-06-25 11:13:14 +0000646 # cherrypy.log("index Exception {}".format(e))
tiernoc94c3df2018-02-09 15:38:54 +0100647 cherrypy.response.status = e.http_code.value
tierno701018c2019-06-25 11:13:14 +0000648 return self._format_out("Welcome to OSM!", token_info)
tiernoc94c3df2018-02-09 15:38:54 +0100649
650 @cherrypy.expose
tierno55945e72018-04-06 16:40:27 +0200651 def version(self, *args, **kwargs):
tiernodfe09572018-04-24 10:41:10 +0200652 # TODO consider to remove and provide version using the static version file
tierno55945e72018-04-06 16:40:27 +0200653 try:
654 if cherrypy.request.method != "GET":
655 raise NbiException("Only method GET is allowed", HTTPStatus.METHOD_NOT_ALLOWED)
656 elif args or kwargs:
657 raise NbiException("Invalid URL or query string for version", HTTPStatus.METHOD_NOT_ALLOWED)
tierno9c630112019-08-29 14:21:41 +0000658 # TODO include version of other modules, pick up from some kafka admin message
tierno5792d7d2019-08-30 15:37:12 +0000659 osm_nbi_version = {"version": nbi_version, "date": nbi_version_date}
660 return self._format_out(osm_nbi_version)
tierno55945e72018-04-06 16:40:27 +0200661 except NbiException as e:
662 cherrypy.response.status = e.http_code.value
663 problem_details = {
664 "code": e.http_code.name,
665 "status": e.http_code.value,
666 "detail": str(e),
667 }
668 return self._format_out(problem_details, None)
669
tierno12eac3c2020-03-19 23:22:08 +0000670 def domain(self):
671 try:
672 domains = {
673 "user_domain_name": cherrypy.tree.apps['/osm'].config["authentication"].get("user_domain_name"),
674 "project_domain_name": cherrypy.tree.apps['/osm'].config["authentication"].get("project_domain_name")}
675 return self._format_out(domains)
676 except NbiException as e:
677 cherrypy.response.status = e.http_code.value
678 problem_details = {
679 "code": e.http_code.name,
680 "status": e.http_code.value,
681 "detail": str(e),
682 }
683 return self._format_out(problem_details, None)
684
tiernoa5035702019-07-29 08:54:42 +0000685 @staticmethod
686 def _format_login(token_info):
687 """
688 Changes cherrypy.request.login to include username/project_name;session so that cherrypy access log will
689 log this information
690 :param token_info: Dictionary with token content
691 :return: None
692 """
693 cherrypy.request.login = token_info.get("username", "-")
694 if token_info.get("project_name"):
695 cherrypy.request.login += "/" + token_info["project_name"]
696 if token_info.get("id"):
697 cherrypy.request.login += ";session=" + token_info["id"][0:12]
698
tierno55945e72018-04-06 16:40:27 +0200699 @cherrypy.expose
tiernof27c79b2018-03-12 17:08:42 +0100700 def token(self, method, token_id=None, kwargs=None):
tierno701018c2019-06-25 11:13:14 +0000701 token_info = None
tiernoc94c3df2018-02-09 15:38:54 +0100702 # self.engine.load_dbase(cherrypy.request.app.config)
tiernof27c79b2018-03-12 17:08:42 +0100703 indata = self._format_in(kwargs)
704 if not isinstance(indata, dict):
705 raise NbiException("Expected application/yaml or application/json Content-Type", HTTPStatus.BAD_REQUEST)
tiernoa5035702019-07-29 08:54:42 +0000706
707 if method == "GET":
708 token_info = self.authenticator.authorize()
709 # for logging
710 self._format_login(token_info)
711 if token_id:
712 outdata = self.authenticator.get_token(token_info, token_id)
tiernoc94c3df2018-02-09 15:38:54 +0100713 else:
tiernoa5035702019-07-29 08:54:42 +0000714 outdata = self.authenticator.get_token_list(token_info)
715 elif method == "POST":
716 try:
717 token_info = self.authenticator.authorize()
718 except Exception:
719 token_info = None
720 if kwargs:
721 indata.update(kwargs)
722 # This is needed to log the user when authentication fails
723 cherrypy.request.login = "{}".format(indata.get("username", "-"))
724 outdata = token_info = self.authenticator.new_token(token_info, indata, cherrypy.request.remote)
725 cherrypy.session['Authorization'] = outdata["_id"]
726 self._set_location_header("admin", "v1", "tokens", outdata["_id"])
727 # for logging
728 self._format_login(token_info)
729
730 # cherrypy.response.cookie["Authorization"] = outdata["id"]
731 # cherrypy.response.cookie["Authorization"]['expires'] = 3600
732 elif method == "DELETE":
733 if not token_id and "id" in kwargs:
734 token_id = kwargs["id"]
735 elif not token_id:
736 token_info = self.authenticator.authorize()
737 # for logging
738 self._format_login(token_info)
739 token_id = token_info["_id"]
740 outdata = self.authenticator.del_token(token_id)
741 token_info = None
742 cherrypy.session['Authorization'] = "logout"
743 # cherrypy.response.cookie["Authorization"] = token_id
744 # cherrypy.response.cookie["Authorization"]['expires'] = 0
745 else:
746 raise NbiException("Method {} not allowed for token".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
747 return self._format_out(outdata, token_info)
tiernoc94c3df2018-02-09 15:38:54 +0100748
749 @cherrypy.expose
750 def test(self, *args, **kwargs):
tierno4836bac2020-01-15 14:41:48 +0000751 if not cherrypy.config.get("server.enable_test") or (isinstance(cherrypy.config["server.enable_test"], str) and
752 cherrypy.config["server.enable_test"].lower() == "false"):
753 cherrypy.response.status = HTTPStatus.METHOD_NOT_ALLOWED.value
754 return "test URL is disabled"
tiernoc94c3df2018-02-09 15:38:54 +0100755 thread_info = None
tiernof27c79b2018-03-12 17:08:42 +0100756 if args and args[0] == "help":
tiernof717cbe2018-12-03 16:35:42 +0000757 return "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nfs-clear[/folder]\nlogin\nlogin2\n"\
tiernoe1281182018-05-22 12:24:36 +0200758 "sleep/<time>\nmessage/topic\n</pre></html>"
tiernof27c79b2018-03-12 17:08:42 +0100759
760 elif args and args[0] == "init":
tiernoc94c3df2018-02-09 15:38:54 +0100761 try:
762 # self.engine.load_dbase(cherrypy.request.app.config)
763 self.engine.create_admin()
764 return "Done. User 'admin', password 'admin' created"
765 except Exception:
766 cherrypy.response.status = HTTPStatus.FORBIDDEN.value
767 return self._format_out("Database already initialized")
tiernof27c79b2018-03-12 17:08:42 +0100768 elif args and args[0] == "file":
769 return cherrypy.lib.static.serve_file(cherrypy.tree.apps['/osm'].config["storage"]["path"] + "/" + args[1],
770 "text/plain", "attachment")
771 elif args and args[0] == "file2":
772 f_path = cherrypy.tree.apps['/osm'].config["storage"]["path"] + "/" + args[1]
773 f = open(f_path, "r")
774 cherrypy.response.headers["Content-type"] = "text/plain"
tiernof27c79b2018-03-12 17:08:42 +0100775 return f
tierno55945e72018-04-06 16:40:27 +0200776
tiernof27c79b2018-03-12 17:08:42 +0100777 elif len(args) == 2 and args[0] == "db-clear":
tiernof717cbe2018-12-03 16:35:42 +0000778 deleted_info = self.engine.db.del_list(args[1], kwargs)
779 return "{} {} deleted\n".format(deleted_info["deleted"], args[1])
780 elif len(args) and args[0] == "fs-clear":
781 if len(args) >= 2:
782 folders = (args[1],)
783 else:
784 folders = self.engine.fs.dir_ls(".")
785 for folder in folders:
786 self.engine.fs.file_delete(folder)
787 return ",".join(folders) + " folders deleted\n"
tiernoc94c3df2018-02-09 15:38:54 +0100788 elif args and args[0] == "login":
789 if not cherrypy.request.headers.get("Authorization"):
790 cherrypy.response.headers["WWW-Authenticate"] = 'Basic realm="Access to OSM site", charset="UTF-8"'
791 cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value
792 elif args and args[0] == "login2":
793 if not cherrypy.request.headers.get("Authorization"):
794 cherrypy.response.headers["WWW-Authenticate"] = 'Bearer realm="Access to OSM site"'
795 cherrypy.response.status = HTTPStatus.UNAUTHORIZED.value
796 elif args and args[0] == "sleep":
797 sleep_time = 5
798 try:
799 sleep_time = int(args[1])
800 except Exception:
801 cherrypy.response.status = HTTPStatus.FORBIDDEN.value
802 return self._format_out("Database already initialized")
803 thread_info = cherrypy.thread_data
804 print(thread_info)
805 time.sleep(sleep_time)
806 # thread_info
807 elif len(args) >= 2 and args[0] == "message":
tiernob24258a2018-10-04 18:39:49 +0200808 main_topic = args[1]
809 return_text = "<html><pre>{} ->\n".format(main_topic)
tiernoc94c3df2018-02-09 15:38:54 +0100810 try:
tierno55945e72018-04-06 16:40:27 +0200811 if cherrypy.request.method == 'POST':
delacruzramob19cadc2019-10-08 10:18:02 +0200812 to_send = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
tierno55945e72018-04-06 16:40:27 +0200813 for k, v in to_send.items():
tiernob24258a2018-10-04 18:39:49 +0200814 self.engine.msg.write(main_topic, k, v)
tierno55945e72018-04-06 16:40:27 +0200815 return_text += " {}: {}\n".format(k, v)
816 elif cherrypy.request.method == 'GET':
817 for k, v in kwargs.items():
tiernof0ad3352020-05-12 14:32:37 +0000818 v_dict = yaml.load(v, Loader=yaml.SafeLoader)
819 self.engine.msg.write(main_topic, k, v_dict)
820 return_text += " {}: {}\n".format(k, v_dict)
tiernoc94c3df2018-02-09 15:38:54 +0100821 except Exception as e:
tierno55945e72018-04-06 16:40:27 +0200822 return_text += "Error: " + str(e)
823 return_text += "</pre></html>\n"
824 return return_text
tiernoc94c3df2018-02-09 15:38:54 +0100825
826 return_text = (
827 "<html><pre>\nheaders:\n args: {}\n".format(args) +
828 " kwargs: {}\n".format(kwargs) +
829 " headers: {}\n".format(cherrypy.request.headers) +
830 " path_info: {}\n".format(cherrypy.request.path_info) +
831 " query_string: {}\n".format(cherrypy.request.query_string) +
832 " session: {}\n".format(cherrypy.session) +
833 " cookie: {}\n".format(cherrypy.request.cookie) +
834 " method: {}\n".format(cherrypy.request.method) +
tiernof717cbe2018-12-03 16:35:42 +0000835 " session: {}\n".format(cherrypy.session.get('fieldname')) +
tiernoc94c3df2018-02-09 15:38:54 +0100836 " body:\n")
837 return_text += " length: {}\n".format(cherrypy.request.body.length)
838 if cherrypy.request.body.length:
839 return_text += " content: {}\n".format(
840 str(cherrypy.request.body.read(int(cherrypy.request.headers.get('Content-Length', 0)))))
841 if thread_info:
842 return_text += "thread: {}\n".format(thread_info)
843 return_text += "</pre></html>"
844 return return_text
845
tierno701018c2019-06-25 11:13:14 +0000846 @staticmethod
847 def _check_valid_url_method(method, *args):
tiernof27c79b2018-03-12 17:08:42 +0100848 if len(args) < 3:
tiernob24258a2018-10-04 18:39:49 +0200849 raise NbiException("URL must contain at least 'main_topic/version/topic'", HTTPStatus.METHOD_NOT_ALLOWED)
tiernof27c79b2018-03-12 17:08:42 +0100850
tierno701018c2019-06-25 11:13:14 +0000851 reference = valid_url_methods
tiernof27c79b2018-03-12 17:08:42 +0100852 for arg in args:
853 if arg is None:
854 break
855 if not isinstance(reference, dict):
856 raise NbiException("URL contains unexpected extra items '{}'".format(arg),
857 HTTPStatus.METHOD_NOT_ALLOWED)
858
859 if arg in reference:
860 reference = reference[arg]
861 elif "<ID>" in reference:
862 reference = reference["<ID>"]
863 elif "*" in reference:
864 reference = reference["*"]
865 break
866 else:
867 raise NbiException("Unexpected URL item {}".format(arg), HTTPStatus.METHOD_NOT_ALLOWED)
868 if "TODO" in reference and method in reference["TODO"]:
869 raise NbiException("Method {} not supported yet for this URL".format(method), HTTPStatus.NOT_IMPLEMENTED)
tierno2236d202018-05-16 19:05:16 +0200870 elif "METHODS" in reference and method not in reference["METHODS"]:
tiernof27c79b2018-03-12 17:08:42 +0100871 raise NbiException("Method {} not supported for this URL".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
tierno701018c2019-06-25 11:13:14 +0000872 return reference["ROLE_PERMISSION"] + method.lower()
tiernof27c79b2018-03-12 17:08:42 +0100873
874 @staticmethod
tiernob24258a2018-10-04 18:39:49 +0200875 def _set_location_header(main_topic, version, topic, id):
tiernof27c79b2018-03-12 17:08:42 +0100876 """
877 Insert response header Location with the URL of created item base on URL params
tiernob24258a2018-10-04 18:39:49 +0200878 :param main_topic:
tiernof27c79b2018-03-12 17:08:42 +0100879 :param version:
tiernob24258a2018-10-04 18:39:49 +0200880 :param topic:
tiernof27c79b2018-03-12 17:08:42 +0100881 :param id:
882 :return: None
883 """
884 # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT
tiernob24258a2018-10-04 18:39:49 +0200885 cherrypy.response.headers["Location"] = "/osm/{}/{}/{}/{}".format(main_topic, version, topic, id)
tiernof27c79b2018-03-12 17:08:42 +0100886 return
887
tierno65ca36d2019-02-12 19:27:52 +0100888 @staticmethod
tierno701018c2019-06-25 11:13:14 +0000889 def _extract_query_string_operations(kwargs, method):
890 """
891
892 :param kwargs:
893 :return:
894 """
895 query_string_operations = []
896 if kwargs:
897 for qs in ("FORCE", "PUBLIC", "ADMIN", "SET_PROJECT"):
898 if qs in kwargs and kwargs[qs].lower() != "false":
899 query_string_operations.append(qs.lower() + ":" + method.lower())
900 return query_string_operations
901
902 @staticmethod
903 def _manage_admin_query(token_info, kwargs, method, _id):
tierno65ca36d2019-02-12 19:27:52 +0100904 """
905 Processes the administrator query inputs (if any) of FORCE, ADMIN, PUBLIC, SET_PROJECT
906 Check that users has rights to use them and returs the admin_query
tierno701018c2019-06-25 11:13:14 +0000907 :param token_info: token_info rights obtained by token
tierno65ca36d2019-02-12 19:27:52 +0100908 :param kwargs: query string input.
909 :param method: http method: GET, POSST, PUT, ...
910 :param _id:
911 :return: admin_query dictionary with keys:
912 public: True, False or None
913 force: True or False
914 project_id: tuple with projects used for accessing an element
915 set_project: tuple with projects that a created element will belong to
916 method: show, list, delete, write
917 """
tierno701018c2019-06-25 11:13:14 +0000918 admin_query = {"force": False, "project_id": (token_info["project_id"], ), "username": token_info["username"],
delacruzramo029405d2019-09-26 10:52:56 +0200919 "admin": token_info["admin"], "public": None,
920 "allow_show_user_project_role": token_info["allow_show_user_project_role"]}
tierno65ca36d2019-02-12 19:27:52 +0100921 if kwargs:
922 # FORCE
923 if "FORCE" in kwargs:
924 if kwargs["FORCE"].lower() != "false": # if None or True set force to True
925 admin_query["force"] = True
926 del kwargs["FORCE"]
927 # PUBLIC
928 if "PUBLIC" in kwargs:
929 if kwargs["PUBLIC"].lower() != "false": # if None or True set public to True
930 admin_query["public"] = True
931 else:
932 admin_query["public"] = False
933 del kwargs["PUBLIC"]
934 # ADMIN
935 if "ADMIN" in kwargs:
936 behave_as = kwargs.pop("ADMIN")
937 if behave_as.lower() != "false":
tierno701018c2019-06-25 11:13:14 +0000938 if not token_info["admin"]:
tierno65ca36d2019-02-12 19:27:52 +0100939 raise NbiException("Only admin projects can use 'ADMIN' query string", HTTPStatus.UNAUTHORIZED)
940 if not behave_as or behave_as.lower() == "true": # convert True, None to empty list
941 admin_query["project_id"] = ()
942 elif isinstance(behave_as, (list, tuple)):
943 admin_query["project_id"] = behave_as
944 else: # isinstance(behave_as, str)
945 admin_query["project_id"] = (behave_as, )
946 if "SET_PROJECT" in kwargs:
947 set_project = kwargs.pop("SET_PROJECT")
948 if not set_project:
949 admin_query["set_project"] = list(admin_query["project_id"])
950 else:
951 if isinstance(set_project, str):
952 set_project = (set_project, )
953 if admin_query["project_id"]:
954 for p in set_project:
955 if p not in admin_query["project_id"]:
956 raise NbiException("Unauthorized for 'SET_PROJECT={p}'. Try with 'ADMIN=True' or "
957 "'ADMIN='{p}'".format(p=p), HTTPStatus.UNAUTHORIZED)
958 admin_query["set_project"] = set_project
959
960 # PROJECT_READ
961 # if "PROJECT_READ" in kwargs:
962 # admin_query["project"] = kwargs.pop("project")
tierno701018c2019-06-25 11:13:14 +0000963 # if admin_query["project"] == token_info["project_id"]:
tierno65ca36d2019-02-12 19:27:52 +0100964 if method == "GET":
965 if _id:
966 admin_query["method"] = "show"
967 else:
968 admin_query["method"] = "list"
969 elif method == "DELETE":
970 admin_query["method"] = "delete"
971 else:
972 admin_query["method"] = "write"
973 return admin_query
974
tiernoc94c3df2018-02-09 15:38:54 +0100975 @cherrypy.expose
tiernob24258a2018-10-04 18:39:49 +0200976 def default(self, main_topic=None, version=None, topic=None, _id=None, item=None, *args, **kwargs):
tierno701018c2019-06-25 11:13:14 +0000977 token_info = None
tiernof27c79b2018-03-12 17:08:42 +0100978 outdata = None
979 _format = None
tierno0f98af52018-03-19 10:28:22 +0100980 method = "DONE"
tiernob24258a2018-10-04 18:39:49 +0200981 engine_topic = None
tiernodb9dc582018-06-20 17:27:29 +0200982 rollback = []
tierno701018c2019-06-25 11:13:14 +0000983 engine_session = None
tiernoc94c3df2018-02-09 15:38:54 +0100984 try:
tiernob24258a2018-10-04 18:39:49 +0200985 if not main_topic or not version or not topic:
986 raise NbiException("URL must contain at least 'main_topic/version/topic'",
987 HTTPStatus.METHOD_NOT_ALLOWED)
vijay.r35ef2f72019-04-30 17:55:49 +0530988 if main_topic not in ("admin", "vnfpkgm", "nsd", "nslcm", "pdu", "nst", "nsilcm", "nspm"):
tiernob24258a2018-10-04 18:39:49 +0200989 raise NbiException("URL main_topic '{}' not supported".format(main_topic),
990 HTTPStatus.METHOD_NOT_ALLOWED)
tiernoc94c3df2018-02-09 15:38:54 +0100991 if version != 'v1':
992 raise NbiException("URL version '{}' not supported".format(version), HTTPStatus.METHOD_NOT_ALLOWED)
993
tiernof27c79b2018-03-12 17:08:42 +0100994 if kwargs and "METHOD" in kwargs and kwargs["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH"):
995 method = kwargs.pop("METHOD")
996 else:
997 method = cherrypy.request.method
tierno65ca36d2019-02-12 19:27:52 +0100998
tierno701018c2019-06-25 11:13:14 +0000999 role_permission = self._check_valid_url_method(method, main_topic, version, topic, _id, item, *args)
1000 query_string_operations = self._extract_query_string_operations(kwargs, method)
tiernob24258a2018-10-04 18:39:49 +02001001 if main_topic == "admin" and topic == "tokens":
tiernof27c79b2018-03-12 17:08:42 +01001002 return self.token(method, _id, kwargs)
delacruzramo029405d2019-09-26 10:52:56 +02001003 token_info = self.authenticator.authorize(role_permission, query_string_operations, _id)
tierno12eac3c2020-03-19 23:22:08 +00001004 if main_topic == "admin" and topic == "domains":
1005 return self.domain()
tierno701018c2019-06-25 11:13:14 +00001006 engine_session = self._manage_admin_query(token_info, kwargs, method, _id)
tiernof27c79b2018-03-12 17:08:42 +01001007 indata = self._format_in(kwargs)
tiernob24258a2018-10-04 18:39:49 +02001008 engine_topic = topic
1009 if topic == "subscriptions":
1010 engine_topic = main_topic + "_" + topic
vijay.r35ef2f72019-04-30 17:55:49 +05301011 if item and topic != "pm_jobs":
tiernob24258a2018-10-04 18:39:49 +02001012 engine_topic = item
tiernoc94c3df2018-02-09 15:38:54 +01001013
tiernob24258a2018-10-04 18:39:49 +02001014 if main_topic == "nsd":
1015 engine_topic = "nsds"
1016 elif main_topic == "vnfpkgm":
1017 engine_topic = "vnfds"
delacruzramo271d2002019-12-02 21:00:37 +01001018 if topic == "vnfpkg_op_occs":
1019 engine_topic = "vnfpkgops"
1020 if topic == "vnf_packages" and item == "action":
1021 engine_topic = "vnfpkgops"
tiernob24258a2018-10-04 18:39:49 +02001022 elif main_topic == "nslcm":
1023 engine_topic = "nsrs"
1024 if topic == "ns_lcm_op_occs":
1025 engine_topic = "nslcmops"
1026 if topic == "vnfrs" or topic == "vnf_instances":
1027 engine_topic = "vnfrs"
garciadeblas9750c5a2018-10-15 16:20:35 +02001028 elif main_topic == "nst":
1029 engine_topic = "nsts"
1030 elif main_topic == "nsilcm":
1031 engine_topic = "nsis"
1032 if topic == "nsi_lcm_op_occs":
delacruzramoc061f562019-04-05 11:00:02 +02001033 engine_topic = "nsilcmops"
tiernob24258a2018-10-04 18:39:49 +02001034 elif main_topic == "pdu":
1035 engine_topic = "pdus"
tierno65ca36d2019-02-12 19:27:52 +01001036 if engine_topic == "vims": # TODO this is for backward compatibility, it will be removed in the future
tiernob24258a2018-10-04 18:39:49 +02001037 engine_topic = "vim_accounts"
tiernoc94c3df2018-02-09 15:38:54 +01001038
1039 if method == "GET":
Felipe Vicens07f31722018-10-29 15:16:44 +01001040 if item in ("nsd_content", "package_content", "artifacts", "vnfd", "nsd", "nst", "nst_content"):
garciadeblas9750c5a2018-10-15 16:20:35 +02001041 if item in ("vnfd", "nsd", "nst"):
tiernof27c79b2018-03-12 17:08:42 +01001042 path = "$DESCRIPTOR"
1043 elif args:
1044 path = args
tiernob24258a2018-10-04 18:39:49 +02001045 elif item == "artifacts":
tiernof27c79b2018-03-12 17:08:42 +01001046 path = ()
1047 else:
1048 path = None
tierno701018c2019-06-25 11:13:14 +00001049 file, _format = self.engine.get_file(engine_session, engine_topic, _id, path,
tierno2236d202018-05-16 19:05:16 +02001050 cherrypy.request.headers.get("Accept"))
tiernof27c79b2018-03-12 17:08:42 +01001051 outdata = file
1052 elif not _id:
tierno701018c2019-06-25 11:13:14 +00001053 outdata = self.engine.get_item_list(engine_session, engine_topic, kwargs)
tiernoc94c3df2018-02-09 15:38:54 +01001054 else:
vijay.r35ef2f72019-04-30 17:55:49 +05301055 if item == "reports":
1056 # TODO check that project_id (_id in this context) has permissions
1057 _id = args[0]
tierno701018c2019-06-25 11:13:14 +00001058 outdata = self.engine.get_item(engine_session, engine_topic, _id)
delacruzramo271d2002019-12-02 21:00:37 +01001059
tiernof27c79b2018-03-12 17:08:42 +01001060 elif method == "POST":
tiernobdebce92019-07-01 15:36:49 +00001061 cherrypy.response.status = HTTPStatus.CREATED.value
garciadeblas9750c5a2018-10-15 16:20:35 +02001062 if topic in ("ns_descriptors_content", "vnf_packages_content", "netslice_templates_content"):
tiernof27c79b2018-03-12 17:08:42 +01001063 _id = cherrypy.request.headers.get("Transaction-Id")
1064 if not _id:
tiernobdebce92019-07-01 15:36:49 +00001065 _id, _ = self.engine.new_item(rollback, engine_session, engine_topic, {}, None,
1066 cherrypy.request.headers)
tierno701018c2019-06-25 11:13:14 +00001067 completed = self.engine.upload_content(engine_session, engine_topic, _id, indata, kwargs,
tierno65ca36d2019-02-12 19:27:52 +01001068 cherrypy.request.headers)
tiernof27c79b2018-03-12 17:08:42 +01001069 if completed:
tiernob24258a2018-10-04 18:39:49 +02001070 self._set_location_header(main_topic, version, topic, _id)
tiernof27c79b2018-03-12 17:08:42 +01001071 else:
1072 cherrypy.response.headers["Transaction-Id"] = _id
1073 outdata = {"id": _id}
tiernob24258a2018-10-04 18:39:49 +02001074 elif topic == "ns_instances_content":
1075 # creates NSR
tiernobdebce92019-07-01 15:36:49 +00001076 _id, _ = self.engine.new_item(rollback, engine_session, engine_topic, indata, kwargs)
tiernob24258a2018-10-04 18:39:49 +02001077 # creates nslcmop
1078 indata["lcmOperationType"] = "instantiate"
1079 indata["nsInstanceId"] = _id
tiernobdebce92019-07-01 15:36:49 +00001080 nslcmop_id, _ = self.engine.new_item(rollback, engine_session, "nslcmops", indata, None)
tiernob24258a2018-10-04 18:39:49 +02001081 self._set_location_header(main_topic, version, topic, _id)
kuuse078f55e2019-05-16 19:24:21 +02001082 outdata = {"id": _id, "nslcmop_id": nslcmop_id}
tiernob24258a2018-10-04 18:39:49 +02001083 elif topic == "ns_instances" and item:
1084 indata["lcmOperationType"] = item
1085 indata["nsInstanceId"] = _id
tiernobdebce92019-07-01 15:36:49 +00001086 _id, _ = self.engine.new_item(rollback, engine_session, "nslcmops", indata, kwargs)
tiernob24258a2018-10-04 18:39:49 +02001087 self._set_location_header(main_topic, version, "ns_lcm_op_occs", _id)
tierno65acb4d2018-04-06 16:42:40 +02001088 outdata = {"id": _id}
1089 cherrypy.response.status = HTTPStatus.ACCEPTED.value
garciadeblas9750c5a2018-10-15 16:20:35 +02001090 elif topic == "netslice_instances_content":
Felipe Vicens07f31722018-10-29 15:16:44 +01001091 # creates NetSlice_Instance_record (NSIR)
tiernobdebce92019-07-01 15:36:49 +00001092 _id, _ = self.engine.new_item(rollback, engine_session, engine_topic, indata, kwargs)
Felipe Vicens07f31722018-10-29 15:16:44 +01001093 self._set_location_header(main_topic, version, topic, _id)
garciadeblas9750c5a2018-10-15 16:20:35 +02001094 indata["lcmOperationType"] = "instantiate"
Felipe Vicens126af572019-06-05 19:13:04 +02001095 indata["netsliceInstanceId"] = _id
tiernobdebce92019-07-01 15:36:49 +00001096 nsilcmop_id, _ = self.engine.new_item(rollback, engine_session, "nsilcmops", indata, kwargs)
kuuse078f55e2019-05-16 19:24:21 +02001097 outdata = {"id": _id, "nsilcmop_id": nsilcmop_id}
garciadeblas9750c5a2018-10-15 16:20:35 +02001098 elif topic == "netslice_instances" and item:
1099 indata["lcmOperationType"] = item
Felipe Vicens126af572019-06-05 19:13:04 +02001100 indata["netsliceInstanceId"] = _id
tiernobdebce92019-07-01 15:36:49 +00001101 _id, _ = self.engine.new_item(rollback, engine_session, "nsilcmops", indata, kwargs)
garciadeblas9750c5a2018-10-15 16:20:35 +02001102 self._set_location_header(main_topic, version, "nsi_lcm_op_occs", _id)
1103 outdata = {"id": _id}
1104 cherrypy.response.status = HTTPStatus.ACCEPTED.value
delacruzramo271d2002019-12-02 21:00:37 +01001105 elif topic == "vnf_packages" and item == "action":
1106 indata["lcmOperationType"] = item
1107 indata["vnfPkgId"] = _id
1108 _id, _ = self.engine.new_item(rollback, engine_session, "vnfpkgops", indata, kwargs)
1109 self._set_location_header(main_topic, version, "vnfpkg_op_occs", _id)
1110 outdata = {"id": _id}
1111 cherrypy.response.status = HTTPStatus.ACCEPTED.value
tiernof27c79b2018-03-12 17:08:42 +01001112 else:
tiernobdebce92019-07-01 15:36:49 +00001113 _id, op_id = self.engine.new_item(rollback, engine_session, engine_topic, indata, kwargs,
1114 cherrypy.request.headers)
tiernob24258a2018-10-04 18:39:49 +02001115 self._set_location_header(main_topic, version, topic, _id)
tiernof27c79b2018-03-12 17:08:42 +01001116 outdata = {"id": _id}
tiernobdebce92019-07-01 15:36:49 +00001117 if op_id:
1118 outdata["op_id"] = op_id
1119 cherrypy.response.status = HTTPStatus.ACCEPTED.value
tiernob24258a2018-10-04 18:39:49 +02001120 # TODO form NsdInfo when topic in ("ns_descriptors", "vnf_packages")
tierno09c073e2018-04-26 13:36:48 +02001121
tiernoc94c3df2018-02-09 15:38:54 +01001122 elif method == "DELETE":
1123 if not _id:
tierno701018c2019-06-25 11:13:14 +00001124 outdata = self.engine.del_item_list(engine_session, engine_topic, kwargs)
tierno09c073e2018-04-26 13:36:48 +02001125 cherrypy.response.status = HTTPStatus.OK.value
tiernoc94c3df2018-02-09 15:38:54 +01001126 else: # len(args) > 1
tierno22577432020-04-08 15:16:57 +00001127 # for NS NSI generate an operation
1128 op_id = None
tierno701018c2019-06-25 11:13:14 +00001129 if topic == "ns_instances_content" and not engine_session["force"]:
tiernob24258a2018-10-04 18:39:49 +02001130 nslcmop_desc = {
1131 "lcmOperationType": "terminate",
1132 "nsInstanceId": _id,
1133 "autoremove": True
1134 }
tierno22577432020-04-08 15:16:57 +00001135 op_id, _ = self.engine.new_item(rollback, engine_session, "nslcmops", nslcmop_desc, kwargs)
1136 if op_id:
1137 outdata = {"_id": op_id}
tierno701018c2019-06-25 11:13:14 +00001138 elif topic == "netslice_instances_content" and not engine_session["force"]:
garciadeblas9750c5a2018-10-15 16:20:35 +02001139 nsilcmop_desc = {
1140 "lcmOperationType": "terminate",
Felipe Vicens126af572019-06-05 19:13:04 +02001141 "netsliceInstanceId": _id,
garciadeblas9750c5a2018-10-15 16:20:35 +02001142 "autoremove": True
1143 }
tierno22577432020-04-08 15:16:57 +00001144 op_id, _ = self.engine.new_item(rollback, engine_session, "nsilcmops", nsilcmop_desc, None)
1145 if op_id:
1146 outdata = {"_id": op_id}
1147 # if there is not any deletion in process, delete
1148 if not op_id:
1149 op_id = self.engine.del_item(engine_session, engine_topic, _id)
1150 if op_id:
1151 outdata = {"op_id": op_id}
1152 cherrypy.response.status = HTTPStatus.ACCEPTED.value if op_id else HTTPStatus.NO_CONTENT.value
tierno09c073e2018-04-26 13:36:48 +02001153
tierno7ae10112018-05-18 14:36:02 +02001154 elif method in ("PUT", "PATCH"):
tiernobdebce92019-07-01 15:36:49 +00001155 op_id = None
tierno701018c2019-06-25 11:13:14 +00001156 if not indata and not kwargs and not engine_session.get("set_project"):
tiernoc94c3df2018-02-09 15:38:54 +01001157 raise NbiException("Nothing to update. Provide payload and/or query string",
1158 HTTPStatus.BAD_REQUEST)
garciadeblas9750c5a2018-10-15 16:20:35 +02001159 if item in ("nsd_content", "package_content", "nst_content") and method == "PUT":
tierno701018c2019-06-25 11:13:14 +00001160 completed = self.engine.upload_content(engine_session, engine_topic, _id, indata, kwargs,
tierno65ca36d2019-02-12 19:27:52 +01001161 cherrypy.request.headers)
tiernof27c79b2018-03-12 17:08:42 +01001162 if not completed:
1163 cherrypy.response.headers["Transaction-Id"] = id
tiernof27c79b2018-03-12 17:08:42 +01001164 else:
tiernobdebce92019-07-01 15:36:49 +00001165 op_id = self.engine.edit_item(engine_session, engine_topic, _id, indata, kwargs)
1166
1167 if op_id:
1168 cherrypy.response.status = HTTPStatus.ACCEPTED.value
1169 outdata = {"op_id": op_id}
1170 else:
1171 cherrypy.response.status = HTTPStatus.NO_CONTENT.value
1172 outdata = None
tiernoc94c3df2018-02-09 15:38:54 +01001173 else:
1174 raise NbiException("Method {} not allowed".format(method), HTTPStatus.METHOD_NOT_ALLOWED)
tiernoa6bb45d2019-06-14 09:45:39 +00001175
1176 # if Role information changes, it is needed to reload the information of roles
1177 if topic == "roles" and method != "GET":
1178 self.authenticator.load_operation_to_allowed_roles()
delacruzramoad682a52019-12-10 16:26:34 +01001179
1180 if topic == "projects" and method == "DELETE" \
1181 or topic in ["users", "roles"] and method in ["PUT", "PATCH", "DELETE"]:
1182 self.authenticator.remove_token_from_cache()
1183
tierno701018c2019-06-25 11:13:14 +00001184 return self._format_out(outdata, token_info, _format)
tiernob24258a2018-10-04 18:39:49 +02001185 except Exception as e:
tierno36ec8602018-11-02 17:27:11 +01001186 if isinstance(e, (NbiException, EngineException, DbException, FsException, MsgException, AuthException,
delacruzramo01b15d32019-07-02 14:37:47 +02001187 ValidationError, AuthconnException)):
tiernob24258a2018-10-04 18:39:49 +02001188 http_code_value = cherrypy.response.status = e.http_code.value
1189 http_code_name = e.http_code.name
1190 cherrypy.log("Exception {}".format(e))
1191 else:
1192 http_code_value = cherrypy.response.status = HTTPStatus.BAD_REQUEST.value # INTERNAL_SERVER_ERROR
Felipe Vicense36ab852018-11-23 14:12:09 +01001193 cherrypy.log("CRITICAL: Exception {}".format(e), traceback=True)
tiernob24258a2018-10-04 18:39:49 +02001194 http_code_name = HTTPStatus.BAD_REQUEST.name
tierno3ace63c2018-05-03 17:51:43 +02001195 if hasattr(outdata, "close"): # is an open file
1196 outdata.close()
tiernob24258a2018-10-04 18:39:49 +02001197 error_text = str(e)
1198 rollback.reverse()
tiernodb9dc582018-06-20 17:27:29 +02001199 for rollback_item in rollback:
tierno3ace63c2018-05-03 17:51:43 +02001200 try:
tiernocc103432018-10-19 14:10:35 +02001201 if rollback_item.get("operation") == "set":
1202 self.engine.db.set_one(rollback_item["topic"], {"_id": rollback_item["_id"]},
1203 rollback_item["content"], fail_on_empty=False)
1204 else:
tiernoe8631782018-12-21 13:31:52 +00001205 self.engine.db.del_one(rollback_item["topic"], {"_id": rollback_item["_id"]},
1206 fail_on_empty=False)
tierno3ace63c2018-05-03 17:51:43 +02001207 except Exception as e2:
tiernob24258a2018-10-04 18:39:49 +02001208 rollback_error_text = "Rollback Exception {}: {}".format(rollback_item, e2)
1209 cherrypy.log(rollback_error_text)
1210 error_text += ". " + rollback_error_text
1211 # if isinstance(e, MsgException):
1212 # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
1213 # engine_topic[:-1], method, error_text)
tiernoc94c3df2018-02-09 15:38:54 +01001214 problem_details = {
tiernob24258a2018-10-04 18:39:49 +02001215 "code": http_code_name,
1216 "status": http_code_value,
1217 "detail": error_text,
tiernoc94c3df2018-02-09 15:38:54 +01001218 }
tierno701018c2019-06-25 11:13:14 +00001219 return self._format_out(problem_details, token_info)
tiernoc94c3df2018-02-09 15:38:54 +01001220 # raise cherrypy.HTTPError(e.http_code.value, str(e))
tiernoa5035702019-07-29 08:54:42 +00001221 finally:
1222 if token_info:
1223 self._format_login(token_info)
1224 if method in ("PUT", "PATCH", "POST") and isinstance(outdata, dict):
1225 for logging_id in ("id", "op_id", "nsilcmop_id", "nslcmop_id"):
1226 if outdata.get(logging_id):
1227 cherrypy.request.login += ";{}={}".format(logging_id, outdata[logging_id][:36])
tiernoc94c3df2018-02-09 15:38:54 +01001228
1229
tiernoc94c3df2018-02-09 15:38:54 +01001230def _start_service():
1231 """
1232 Callback function called when cherrypy.engine starts
1233 Override configuration with env variables
1234 Set database, storage, message configuration
1235 Init database with admin/admin user password
1236 """
tierno932499c2019-01-28 17:28:10 +00001237 global nbi_server
1238 global subscription_thread
tiernoc94c3df2018-02-09 15:38:54 +01001239 cherrypy.log.error("Starting osm_nbi")
1240 # update general cherrypy configuration
1241 update_dict = {}
1242
1243 engine_config = cherrypy.tree.apps['/osm'].config
1244 for k, v in environ.items():
1245 if not k.startswith("OSMNBI_"):
1246 continue
tiernoe1281182018-05-22 12:24:36 +02001247 k1, _, k2 = k[7:].lower().partition("_")
tiernoc94c3df2018-02-09 15:38:54 +01001248 if not k2:
1249 continue
1250 try:
1251 # update static configuration
1252 if k == 'OSMNBI_STATIC_DIR':
1253 engine_config["/static"]['tools.staticdir.dir'] = v
1254 engine_config["/static"]['tools.staticdir.on'] = True
1255 elif k == 'OSMNBI_SOCKET_PORT' or k == 'OSMNBI_SERVER_PORT':
1256 update_dict['server.socket_port'] = int(v)
1257 elif k == 'OSMNBI_SOCKET_HOST' or k == 'OSMNBI_SERVER_HOST':
1258 update_dict['server.socket_host'] = v
tiernof5298be2018-05-16 14:43:57 +02001259 elif k1 in ("server", "test", "auth", "log"):
1260 update_dict[k1 + '.' + k2] = v
Eduardo Sousa819d34c2018-07-31 01:20:02 +01001261 elif k1 in ("message", "database", "storage", "authentication"):
tiernof5298be2018-05-16 14:43:57 +02001262 # k2 = k2.replace('_', '.')
Eduardo Sousa819d34c2018-07-31 01:20:02 +01001263 if k2 in ("port", "db_port"):
tiernoc94c3df2018-02-09 15:38:54 +01001264 engine_config[k1][k2] = int(v)
1265 else:
1266 engine_config[k1][k2] = v
Eduardo Sousa819d34c2018-07-31 01:20:02 +01001267
tiernoc94c3df2018-02-09 15:38:54 +01001268 except ValueError as e:
1269 cherrypy.log.error("Ignoring environ '{}': " + str(e))
1270 except Exception as e:
1271 cherrypy.log.warn("skipping environ '{}' on exception '{}'".format(k, e))
1272
1273 if update_dict:
1274 cherrypy.config.update(update_dict)
tiernof5298be2018-05-16 14:43:57 +02001275 engine_config["global"].update(update_dict)
tiernoc94c3df2018-02-09 15:38:54 +01001276
1277 # logging cherrypy
1278 log_format_simple = "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
1279 log_formatter_simple = logging.Formatter(log_format_simple, datefmt='%Y-%m-%dT%H:%M:%S')
1280 logger_server = logging.getLogger("cherrypy.error")
1281 logger_access = logging.getLogger("cherrypy.access")
1282 logger_cherry = logging.getLogger("cherrypy")
1283 logger_nbi = logging.getLogger("nbi")
1284
tiernof5298be2018-05-16 14:43:57 +02001285 if "log.file" in engine_config["global"]:
1286 file_handler = logging.handlers.RotatingFileHandler(engine_config["global"]["log.file"],
tiernoc94c3df2018-02-09 15:38:54 +01001287 maxBytes=100e6, backupCount=9, delay=0)
1288 file_handler.setFormatter(log_formatter_simple)
1289 logger_cherry.addHandler(file_handler)
1290 logger_nbi.addHandler(file_handler)
tiernob24258a2018-10-04 18:39:49 +02001291 # log always to standard output
1292 for format_, logger in {"nbi.server %(filename)s:%(lineno)s": logger_server,
1293 "nbi.access %(filename)s:%(lineno)s": logger_access,
1294 "%(name)s %(filename)s:%(lineno)s": logger_nbi
1295 }.items():
1296 log_format_cherry = "%(asctime)s %(levelname)s {} %(message)s".format(format_)
1297 log_formatter_cherry = logging.Formatter(log_format_cherry, datefmt='%Y-%m-%dT%H:%M:%S')
1298 str_handler = logging.StreamHandler()
1299 str_handler.setFormatter(log_formatter_cherry)
1300 logger.addHandler(str_handler)
tiernoc94c3df2018-02-09 15:38:54 +01001301
tiernof5298be2018-05-16 14:43:57 +02001302 if engine_config["global"].get("log.level"):
1303 logger_cherry.setLevel(engine_config["global"]["log.level"])
1304 logger_nbi.setLevel(engine_config["global"]["log.level"])
tiernoc94c3df2018-02-09 15:38:54 +01001305
1306 # logging other modules
1307 for k1, logname in {"message": "nbi.msg", "database": "nbi.db", "storage": "nbi.fs"}.items():
1308 engine_config[k1]["logger_name"] = logname
1309 logger_module = logging.getLogger(logname)
1310 if "logfile" in engine_config[k1]:
1311 file_handler = logging.handlers.RotatingFileHandler(engine_config[k1]["logfile"],
tierno2236d202018-05-16 19:05:16 +02001312 maxBytes=100e6, backupCount=9, delay=0)
tiernoc94c3df2018-02-09 15:38:54 +01001313 file_handler.setFormatter(log_formatter_simple)
1314 logger_module.addHandler(file_handler)
1315 if "loglevel" in engine_config[k1]:
1316 logger_module.setLevel(engine_config[k1]["loglevel"])
1317 # TODO add more entries, e.g.: storage
1318 cherrypy.tree.apps['/osm'].root.engine.start(engine_config)
Eduardo Sousa819d34c2018-07-31 01:20:02 +01001319 cherrypy.tree.apps['/osm'].root.authenticator.start(engine_config)
tiernod985a8d2018-10-19 14:12:28 +02001320 cherrypy.tree.apps['/osm'].root.engine.init_db(target_version=database_version)
1321 cherrypy.tree.apps['/osm'].root.authenticator.init_db(target_version=auth_database_version)
tiernobee508e2019-01-21 11:21:49 +00001322
tierno932499c2019-01-28 17:28:10 +00001323 # start subscriptions thread:
1324 subscription_thread = SubscriptionThread(config=engine_config, engine=nbi_server.engine)
1325 subscription_thread.start()
1326 # Do not capture except SubscriptionException
1327
tiernob2e48bd2020-02-04 15:47:18 +00001328 backend = engine_config["authentication"]["backend"]
1329 cherrypy.log.error("Starting OSM NBI Version '{} {}' with '{}' authentication backend"
1330 .format(nbi_version, nbi_version_date, backend))
tiernoc94c3df2018-02-09 15:38:54 +01001331
1332
1333def _stop_service():
1334 """
1335 Callback function called when cherrypy.engine stops
1336 TODO: Ending database connections.
1337 """
tierno932499c2019-01-28 17:28:10 +00001338 global subscription_thread
tierno65ca36d2019-02-12 19:27:52 +01001339 if subscription_thread:
1340 subscription_thread.terminate()
tierno932499c2019-01-28 17:28:10 +00001341 subscription_thread = None
tiernoc94c3df2018-02-09 15:38:54 +01001342 cherrypy.tree.apps['/osm'].root.engine.stop()
1343 cherrypy.log.error("Stopping osm_nbi")
1344
tierno2236d202018-05-16 19:05:16 +02001345
tiernof5298be2018-05-16 14:43:57 +02001346def nbi(config_file):
tierno932499c2019-01-28 17:28:10 +00001347 global nbi_server
tiernoc94c3df2018-02-09 15:38:54 +01001348 # conf = {
1349 # '/': {
1350 # #'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
1351 # 'tools.sessions.on': True,
1352 # 'tools.response_headers.on': True,
1353 # # 'tools.response_headers.headers': [('Content-Type', 'text/plain')],
1354 # }
1355 # }
1356 # cherrypy.Server.ssl_module = 'builtin'
1357 # cherrypy.Server.ssl_certificate = "http/cert.pem"
1358 # cherrypy.Server.ssl_private_key = "http/privkey.pem"
1359 # cherrypy.Server.thread_pool = 10
1360 # cherrypy.config.update({'Server.socket_port': config["port"], 'Server.socket_host': config["host"]})
1361
1362 # cherrypy.config.update({'tools.auth_basic.on': True,
1363 # 'tools.auth_basic.realm': 'localhost',
1364 # 'tools.auth_basic.checkpassword': validate_password})
tierno932499c2019-01-28 17:28:10 +00001365 nbi_server = Server()
tiernoc94c3df2018-02-09 15:38:54 +01001366 cherrypy.engine.subscribe('start', _start_service)
1367 cherrypy.engine.subscribe('stop', _stop_service)
tierno932499c2019-01-28 17:28:10 +00001368 cherrypy.quickstart(nbi_server, '/osm', config_file)
tiernof5298be2018-05-16 14:43:57 +02001369
1370
1371def usage():
1372 print("""Usage: {} [options]
1373 -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg)
1374 -h|--help: shows this help
1375 """.format(sys.argv[0]))
tierno2236d202018-05-16 19:05:16 +02001376 # --log-socket-host HOST: send logs to this host")
1377 # --log-socket-port PORT: send logs using this port (default: 9022)")
tiernoc94c3df2018-02-09 15:38:54 +01001378
1379
1380if __name__ == '__main__':
tiernof5298be2018-05-16 14:43:57 +02001381 try:
1382 # load parameters and configuration
1383 opts, args = getopt.getopt(sys.argv[1:], "hvc:", ["config=", "help"])
1384 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
1385 config_file = None
1386 for o, a in opts:
1387 if o in ("-h", "--help"):
1388 usage()
1389 sys.exit()
1390 elif o in ("-c", "--config"):
1391 config_file = a
1392 # elif o == "--log-socket-port":
1393 # log_socket_port = a
1394 # elif o == "--log-socket-host":
1395 # log_socket_host = a
1396 # elif o == "--log-file":
1397 # log_file = a
1398 else:
1399 assert False, "Unhandled option"
1400 if config_file:
1401 if not path.isfile(config_file):
1402 print("configuration file '{}' that not exist".format(config_file), file=sys.stderr)
1403 exit(1)
1404 else:
1405 for config_file in (__file__[:__file__.rfind(".")] + ".cfg", "./nbi.cfg", "/etc/osm/nbi.cfg"):
1406 if path.isfile(config_file):
1407 break
1408 else:
1409 print("No configuration file 'nbi.cfg' found neither at local folder nor at /etc/osm/", file=sys.stderr)
1410 exit(1)
1411 nbi(config_file)
1412 except getopt.GetoptError as e:
1413 print(str(e), file=sys.stderr)
1414 # usage()
1415 exit(1)