2 # -*- coding: utf-8 -*-
4 # 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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
21 import osm_nbi
.html_out
as html
23 import logging
.handlers
27 from osm_nbi
.authconn
import AuthException
, AuthconnException
28 from osm_nbi
.auth
import Authenticator
29 from osm_nbi
.engine
import Engine
, EngineException
30 from osm_nbi
.subscriptions
import SubscriptionThread
31 from osm_nbi
.validation
import ValidationError
32 from osm_common
.dbbase
import DbException
33 from osm_common
.fsbase
import FsException
34 from osm_common
.msgbase
import MsgException
35 from http
import HTTPStatus
36 from codecs
import getreader
37 from os
import environ
, path
38 from osm_nbi
import version
as nbi_version
, version_date
as nbi_version_date
40 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
42 __version__
= "0.1.3" # file version, not NBI version
43 version_date
= "Aug 2019"
45 database_version
= "1.2"
46 auth_database_version
= "1.0"
47 nbi_server
= None # instance of Server class
48 subscription_thread
= None # instance of SubscriptionThread class
51 North Bound Interface (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented)
52 URL: /osm GET POST PUT DELETE PATCH
54 /ns_descriptors_content O O
60 /artifacts[/<artifactPath>] O
68 /vnf_packages_content O O
72 /package_content O5 O5
75 /artifacts[/<artifactPath>] O5
80 /ns_instances_content O O
93 /vnf_instances (also vnfrs for compatibility) O
109 /vim_accounts (also vims for compatibility) O O
123 /netslice_templates_content O O
125 /netslice_templates O O
129 /artifacts[/<artifactPath>] O
131 /<subscriptionId> X X
134 /netslice_instances_content O O
135 /<SliceInstanceId> O O
136 /netslice_instances O O
137 /<SliceInstanceId> O O
142 /<nsiLcmOpOccId> O O O
144 /<subscriptionId> X X
147 Follows SOL005 section 4.3.2 It contains extra METHOD to override http method, FORCE to force.
148 simpleFilterExpr := <attrName>["."<attrName>]*["."<op>]"="<value>[","<value>]*
149 filterExpr := <simpleFilterExpr>["&"<simpleFilterExpr>]*
150 op := "eq" | "neq" (or "ne") | "gt" | "lt" | "gte" | "lte" | "cont" | "ncont"
152 For filtering inside array, it must select the element of the array, or add ANYINDEX to apply the filtering over any
153 item of the array, that is, pass if any item of the array pass the filter.
154 It allows both ne and neq for not equal
155 TODO: 4.3.3 Attribute selectors
156 all_fields, fields=x,y,.., exclude_default, exclude_fields=x,y,...
157 (none) … same as “exclude_default”
158 all_fields … all attributes.
159 fields=<list> … all attributes except all complex attributes with minimum cardinality of zero that are not
160 conditionally mandatory, and that are not provided in <list>.
161 exclude_fields=<list> … all attributes except those complex attributes with a minimum cardinality of zero that
162 are not conditionally mandatory, and that are provided in <list>.
163 exclude_default … all attributes except those complex attributes with a minimum cardinality of zero that are not
164 conditionally mandatory, and that are part of the "default exclude set" defined in the present specification for
165 the particular resource
166 exclude_default and include=<list> … all attributes except those complex attributes with a minimum cardinality
167 of zero that are not conditionally mandatory and that are part of the "default exclude set" defined in the
168 present specification for the particular resource, but that are not part of <list>
169 Additionally it admits some administrator values:
170 FORCE: To force operations skipping dependency checkings
171 ADMIN: To act as an administrator or a different project
172 PUBLIC: To get public descriptors or set a descriptor as public
173 SET_PROJECT: To make a descriptor available for other project
175 Header field name Reference Example Descriptions
176 Accept IETF RFC 7231 [19] application/json Content-Types that are acceptable for the response.
177 This header field shall be present if the response is expected to have a non-empty message body.
178 Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the request.
179 This header field shall be present if the request has a non-empty message body.
180 Authorization IETF RFC 7235 [22] Bearer mF_9.B5f-4.1JqM The authorization token for the request.
181 Details are specified in clause 4.5.3.
182 Range IETF RFC 7233 [21] 1000-2000 Requested range of bytes from a file
183 Header field name Reference Example Descriptions
184 Content-Type IETF RFC 7231 [19] application/json The MIME type of the body of the response.
185 This header field shall be present if the response has a non-empty message body.
186 Location IETF RFC 7231 [19] http://www.example.com/vnflcm/v1/vnf_instances/123 Used in redirection, or when a
187 new resource has been created.
188 This header field shall be present if the response status code is 201 or 3xx.
189 In the present document this header field is also used if the response status code is 202 and a new resource was
191 WWW-Authenticate IETF RFC 7235 [22] Bearer realm="example" Challenge if the corresponding HTTP request has not
192 provided authorization, or error details if the corresponding HTTP request has provided an invalid authorization
194 Accept-Ranges IETF RFC 7233 [21] bytes Used by the Server to signal whether or not it supports ranges for
196 Content-Range IETF RFC 7233 [21] bytes 21010-47021/ 47022 Signals the byte range that is contained in the
197 response, and the total length of the file.
198 Retry-After IETF RFC 7231 [19] Fri, 31 Dec 1999 23:59:59 GMT
201 valid_query_string
= ("ADMIN", "SET_PROJECT", "FORCE", "PUBLIC")
202 # ^ Contains possible administrative query string words:
203 # ADMIN=True(by default)|Project|Project-list: See all elements, or elements of a project
204 # (not owned by my session project).
205 # PUBLIC=True(by default)|False: See/hide public elements. Set/Unset a topic to be public
206 # FORCE=True(by default)|False: Force edition/deletion operations
207 # SET_PROJECT=Project|Project-list: Add/Delete the topic to the projects portfolio
209 valid_url_methods
= {
210 # contains allowed URL and methods, and the role_permission name
214 "METHODS": ("GET", "POST", "DELETE"),
215 "ROLE_PERMISSION": "tokens:",
216 "<ID>": {"METHODS": ("GET", "DELETE"), "ROLE_PERMISSION": "tokens:id:"},
219 "METHODS": ("GET", "POST"),
220 "ROLE_PERMISSION": "users:",
222 "METHODS": ("GET", "DELETE", "PATCH"),
223 "ROLE_PERMISSION": "users:id:",
227 "METHODS": ("GET", "POST"),
228 "ROLE_PERMISSION": "projects:",
230 "METHODS": ("GET", "DELETE", "PATCH"),
231 "ROLE_PERMISSION": "projects:id:",
235 "METHODS": ("GET", "POST"),
236 "ROLE_PERMISSION": "roles:",
238 "METHODS": ("GET", "DELETE", "PATCH"),
239 "ROLE_PERMISSION": "roles:id:",
243 "METHODS": ("GET", "POST"),
244 "ROLE_PERMISSION": "vims:",
246 "METHODS": ("GET", "DELETE", "PATCH"),
247 "ROLE_PERMISSION": "vims:id:",
251 "METHODS": ("GET", "POST"),
252 "ROLE_PERMISSION": "vim_accounts:",
254 "METHODS": ("GET", "DELETE", "PATCH"),
255 "ROLE_PERMISSION": "vim_accounts:id:",
259 "METHODS": ("GET", "POST"),
260 "ROLE_PERMISSION": "wim_accounts:",
262 "METHODS": ("GET", "DELETE", "PATCH"),
263 "ROLE_PERMISSION": "wim_accounts:id:",
267 "METHODS": ("GET", "POST"),
268 "ROLE_PERMISSION": "sdn_controllers:",
270 "METHODS": ("GET", "DELETE", "PATCH"),
271 "ROLE_PERMISSION": "sdn_controllers:id:",
275 "METHODS": ("GET", "POST"),
276 "ROLE_PERMISSION": "k8sclusters:",
278 "METHODS": ("GET", "DELETE", "PATCH"),
279 "ROLE_PERMISSION": "k8sclusters:id:",
283 "METHODS": ("GET", "POST"),
284 "ROLE_PERMISSION": "vca:",
286 "METHODS": ("GET", "DELETE", "PATCH"),
287 "ROLE_PERMISSION": "vca:id:",
291 "METHODS": ("GET", "POST"),
292 "ROLE_PERMISSION": "k8srepos:",
294 "METHODS": ("GET", "DELETE"),
295 "ROLE_PERMISSION": "k8srepos:id:",
299 "METHODS": ("GET", "POST"),
300 "ROLE_PERMISSION": "osmrepos:",
302 "METHODS": ("GET", "DELETE", "PATCH"),
303 "ROLE_PERMISSION": "osmrepos:id:",
308 "ROLE_PERMISSION": "domains:",
315 "METHODS": ("GET", "POST"),
316 "ROLE_PERMISSION": "pduds:",
318 "METHODS": ("GET", "POST", "DELETE", "PATCH", "PUT"),
319 "ROLE_PERMISSION": "pduds:id:",
326 "ns_descriptors_content": {
327 "METHODS": ("GET", "POST"),
328 "ROLE_PERMISSION": "nsds:",
330 "METHODS": ("GET", "PUT", "DELETE"),
331 "ROLE_PERMISSION": "nsds:id:",
335 "METHODS": ("GET", "POST"),
336 "ROLE_PERMISSION": "nsds:",
338 "METHODS": ("GET", "DELETE", "PATCH"),
339 "ROLE_PERMISSION": "nsds:id:",
341 "METHODS": ("GET", "PUT"),
342 "ROLE_PERMISSION": "nsds:id:content:",
345 "METHODS": ("GET",), # descriptor inside package
346 "ROLE_PERMISSION": "nsds:id:content:",
350 "ROLE_PERMISSION": "nsds:id:nsd_artifact:",
356 "TODO": ("GET", "POST"),
358 "TODO": ("GET", "DELETE", "PATCH"),
359 "pnfd_content": {"TODO": ("GET", "PUT")},
363 "TODO": ("GET", "POST"),
364 "<ID>": {"TODO": ("GET", "DELETE")},
370 "vnf_packages_content": {
371 "METHODS": ("GET", "POST"),
372 "ROLE_PERMISSION": "vnfds:",
374 "METHODS": ("GET", "PUT", "DELETE"),
375 "ROLE_PERMISSION": "vnfds:id:",
379 "METHODS": ("GET", "POST"),
380 "ROLE_PERMISSION": "vnfds:",
382 "METHODS": ("GET", "DELETE", "PATCH"), # GET: vnfPkgInfo
383 "ROLE_PERMISSION": "vnfds:id:",
385 "METHODS": ("GET", "PUT"), # package
386 "ROLE_PERMISSION": "vnfds:id:",
390 "ROLE_PERMISSION": "vnfds:id:upload:",
394 "METHODS": ("GET",), # descriptor inside package
395 "ROLE_PERMISSION": "vnfds:id:content:",
399 "ROLE_PERMISSION": "vnfds:id:vnfd_artifact:",
403 "METHODS": ("POST",),
404 "ROLE_PERMISSION": "vnfds:id:action:",
409 "TODO": ("GET", "POST"),
410 "<ID>": {"TODO": ("GET", "DELETE")},
414 "ROLE_PERMISSION": "vnfds:vnfpkgops:",
415 "<ID>": {"METHODS": ("GET",), "ROLE_PERMISSION": "vnfds:vnfpkgops:id:"},
421 "ns_instances_content": {
422 "METHODS": ("GET", "POST"),
423 "ROLE_PERMISSION": "ns_instances:",
425 "METHODS": ("GET", "DELETE"),
426 "ROLE_PERMISSION": "ns_instances:id:",
430 "METHODS": ("GET", "POST"),
431 "ROLE_PERMISSION": "ns_instances:",
433 "METHODS": ("GET", "DELETE"),
434 "ROLE_PERMISSION": "ns_instances:id:",
436 "METHODS": ("POST",),
437 "ROLE_PERMISSION": "ns_instances:id:scale:",
440 "METHODS": ("POST",),
441 "ROLE_PERMISSION": "ns_instances:id:terminate:",
444 "METHODS": ("POST",),
445 "ROLE_PERMISSION": "ns_instances:id:instantiate:",
448 "METHODS": ("POST",),
449 "ROLE_PERMISSION": "ns_instances:id:action:",
452 "METHODS": ("POST",),
453 "ROLE_PERMISSION": "ns_instances:id:update:",
459 "ROLE_PERMISSION": "ns_instances:opps:",
462 "ROLE_PERMISSION": "ns_instances:opps:id:",
467 "ROLE_PERMISSION": "vnf_instances:",
468 "<ID>": {"METHODS": ("GET",), "ROLE_PERMISSION": "vnf_instances:id:"},
472 "ROLE_PERMISSION": "vnf_instances:",
473 "<ID>": {"METHODS": ("GET",), "ROLE_PERMISSION": "vnf_instances:id:"},
476 "METHODS": ("GET", "POST"),
477 "ROLE_PERMISSION": "ns_subscriptions:",
479 "METHODS": ("GET", "DELETE"),
480 "ROLE_PERMISSION": "ns_subscriptions:id:",
487 "vnf_instances": {"METHODS": ("GET", "POST"),
488 "ROLE_PERMISSION": "vnflcm_instances:",
489 "<ID>": {"METHODS": ("GET", "DELETE"),
490 "ROLE_PERMISSION": "vnflcm_instances:id:",
491 "scale": {"METHODS": ("POST",),
492 "ROLE_PERMISSION": "vnflcm_instances:id:scale:"
494 "terminate": {"METHODS": ("POST",),
495 "ROLE_PERMISSION": "vnflcm_instances:id:terminate:"
497 "instantiate": {"METHODS": ("POST",),
498 "ROLE_PERMISSION": "vnflcm_instances:id:instantiate:"
502 "vnf_lcm_op_occs": {"METHODS": ("GET",),
503 "ROLE_PERMISSION": "vnf_instances:opps:",
504 "<ID>": {"METHODS": ("GET",),
505 "ROLE_PERMISSION": "vnf_instances:opps:id:"
512 "netslice_templates_content": {
513 "METHODS": ("GET", "POST"),
514 "ROLE_PERMISSION": "slice_templates:",
516 "METHODS": ("GET", "PUT", "DELETE"),
517 "ROLE_PERMISSION": "slice_templates:id:",
520 "netslice_templates": {
521 "METHODS": ("GET", "POST"),
522 "ROLE_PERMISSION": "slice_templates:",
524 "METHODS": ("GET", "DELETE"),
526 "ROLE_PERMISSION": "slice_templates:id:",
528 "METHODS": ("GET", "PUT"),
529 "ROLE_PERMISSION": "slice_templates:id:content:",
532 "METHODS": ("GET",), # descriptor inside package
533 "ROLE_PERMISSION": "slice_templates:id:content:",
537 "ROLE_PERMISSION": "slice_templates:id:content:",
543 "TODO": ("GET", "POST"),
544 "<ID>": {"TODO": ("GET", "DELETE")},
550 "netslice_instances_content": {
551 "METHODS": ("GET", "POST"),
552 "ROLE_PERMISSION": "slice_instances:",
554 "METHODS": ("GET", "DELETE"),
555 "ROLE_PERMISSION": "slice_instances:id:",
558 "netslice_instances": {
559 "METHODS": ("GET", "POST"),
560 "ROLE_PERMISSION": "slice_instances:",
562 "METHODS": ("GET", "DELETE"),
563 "ROLE_PERMISSION": "slice_instances:id:",
565 "METHODS": ("POST",),
566 "ROLE_PERMISSION": "slice_instances:id:terminate:",
569 "METHODS": ("POST",),
570 "ROLE_PERMISSION": "slice_instances:id:instantiate:",
573 "METHODS": ("POST",),
574 "ROLE_PERMISSION": "slice_instances:id:action:",
580 "ROLE_PERMISSION": "slice_instances:opps:",
583 "ROLE_PERMISSION": "slice_instances:opps:id:",
595 "ROLE_PERMISSION": "reports:id:",
604 "alarms": {"METHODS": ("GET", "PATCH"),
605 "ROLE_PERMISSION": "alarms:",
606 "<ID>": {"METHODS": ("GET", "PATCH"),
607 "ROLE_PERMISSION": "alarms:id:",
615 class NbiException(Exception):
616 def __init__(self
, message
, http_code
=HTTPStatus
.METHOD_NOT_ALLOWED
):
617 Exception.__init
__(self
, message
)
618 self
.http_code
= http_code
621 class Server(object):
623 # to decode bytes to str
624 reader
= getreader("utf-8")
628 self
.authenticator
= Authenticator(valid_url_methods
, valid_query_string
)
629 self
.engine
= Engine(self
.authenticator
)
631 def _format_in(self
, kwargs
):
634 if cherrypy
.request
.body
.length
:
635 error_text
= "Invalid input format "
637 if "Content-Type" in cherrypy
.request
.headers
:
638 if "application/json" in cherrypy
.request
.headers
["Content-Type"]:
639 error_text
= "Invalid json format "
640 indata
= json
.load(self
.reader(cherrypy
.request
.body
))
641 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
642 elif "application/yaml" in cherrypy
.request
.headers
["Content-Type"]:
643 error_text
= "Invalid yaml format "
645 cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
647 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
649 "application/binary" in cherrypy
.request
.headers
["Content-Type"]
650 or "application/gzip"
651 in cherrypy
.request
.headers
["Content-Type"]
652 or "application/zip" in cherrypy
.request
.headers
["Content-Type"]
653 or "text/plain" in cherrypy
.request
.headers
["Content-Type"]
655 indata
= cherrypy
.request
.body
# .read()
657 "multipart/form-data"
658 in cherrypy
.request
.headers
["Content-Type"]
660 if "descriptor_file" in kwargs
:
661 filecontent
= kwargs
.pop("descriptor_file")
662 if not filecontent
.file:
664 "empty file or content", HTTPStatus
.BAD_REQUEST
666 indata
= filecontent
.file # .read()
667 if filecontent
.content_type
.value
:
668 cherrypy
.request
.headers
[
670 ] = filecontent
.content_type
.value
672 # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable,
673 # "Only 'Content-Type' of type 'application/json' or
674 # 'application/yaml' for input format are available")
675 error_text
= "Invalid yaml format "
677 cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
679 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
681 error_text
= "Invalid yaml format "
682 indata
= yaml
.load(cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
)
683 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
688 if cherrypy
.request
.headers
.get("Query-String-Format") == "yaml":
691 for k
, v
in kwargs
.items():
692 if isinstance(v
, str):
697 kwargs
[k
] = yaml
.load(v
, Loader
=yaml
.SafeLoader
)
703 or k
.endswith(".gte")
704 or k
.endswith(".lte")
713 elif v
.find(",") > 0:
714 kwargs
[k
] = v
.split(",")
715 elif isinstance(v
, (list, tuple)):
716 for index
in range(0, len(v
)):
721 v
[index
] = yaml
.load(v
[index
], Loader
=yaml
.SafeLoader
)
726 except (ValueError, yaml
.YAMLError
) as exc
:
727 raise NbiException(error_text
+ str(exc
), HTTPStatus
.BAD_REQUEST
)
728 except KeyError as exc
:
730 "Query string error: " + str(exc
), HTTPStatus
.BAD_REQUEST
732 except Exception as exc
:
733 raise NbiException(error_text
+ str(exc
), HTTPStatus
.BAD_REQUEST
)
736 def _format_out(data
, token_info
=None, _format
=None):
738 return string of dictionary data according to requested json, yaml, xml. By default json
739 :param data: response to be sent. Can be a dict, text or file
740 :param token_info: Contains among other username and project
741 :param _format: The format to be set as Content-Type if data is a file
744 accept
= cherrypy
.request
.headers
.get("Accept")
746 if accept
and "text/html" in accept
:
748 data
, cherrypy
.request
, cherrypy
.response
, token_info
750 # cherrypy.response.status = HTTPStatus.NO_CONTENT.value
752 elif hasattr(data
, "read"): # file object
754 cherrypy
.response
.headers
["Content-Type"] = _format
755 elif "b" in data
.mode
: # binariy asssumig zip
756 cherrypy
.response
.headers
["Content-Type"] = "application/zip"
758 cherrypy
.response
.headers
["Content-Type"] = "text/plain"
759 # TODO check that cherrypy close file. If not implement pending things to close per thread next
762 if "text/html" in accept
:
764 data
, cherrypy
.request
, cherrypy
.response
, token_info
766 elif "application/yaml" in accept
or "*/*" in accept
:
768 elif "application/json" in accept
or (
769 cherrypy
.response
.status
and cherrypy
.response
.status
>= 300
771 cherrypy
.response
.headers
[
773 ] = "application/json; charset=utf-8"
774 a
= json
.dumps(data
, indent
=4) + "\n"
775 return a
.encode("utf8")
776 cherrypy
.response
.headers
["Content-Type"] = "application/yaml"
777 return yaml
.safe_dump(
781 default_flow_style
=False,
785 ) # , canonical=True, default_style='"'
788 def index(self
, *args
, **kwargs
):
791 if cherrypy
.request
.method
== "GET":
792 token_info
= self
.authenticator
.authorize()
793 outdata
= token_info
# Home page
795 raise cherrypy
.HTTPError(
796 HTTPStatus
.METHOD_NOT_ALLOWED
.value
,
797 "Method {} not allowed for tokens".format(cherrypy
.request
.method
),
800 return self
._format
_out
(outdata
, token_info
)
802 except (EngineException
, AuthException
) as e
:
803 # cherrypy.log("index Exception {}".format(e))
804 cherrypy
.response
.status
= e
.http_code
.value
805 return self
._format
_out
("Welcome to OSM!", token_info
)
808 def version(self
, *args
, **kwargs
):
809 # TODO consider to remove and provide version using the static version file
811 if cherrypy
.request
.method
!= "GET":
813 "Only method GET is allowed", HTTPStatus
.METHOD_NOT_ALLOWED
817 "Invalid URL or query string for version",
818 HTTPStatus
.METHOD_NOT_ALLOWED
,
820 # TODO include version of other modules, pick up from some kafka admin message
821 osm_nbi_version
= {"version": nbi_version
, "date": nbi_version_date
}
822 return self
._format
_out
(osm_nbi_version
)
823 except NbiException
as e
:
824 cherrypy
.response
.status
= e
.http_code
.value
826 "code": e
.http_code
.name
,
827 "status": e
.http_code
.value
,
830 return self
._format
_out
(problem_details
, None)
835 "user_domain_name": cherrypy
.tree
.apps
["/osm"]
836 .config
["authentication"]
837 .get("user_domain_name"),
838 "project_domain_name": cherrypy
.tree
.apps
["/osm"]
839 .config
["authentication"]
840 .get("project_domain_name"),
842 return self
._format
_out
(domains
)
843 except NbiException
as e
:
844 cherrypy
.response
.status
= e
.http_code
.value
846 "code": e
.http_code
.name
,
847 "status": e
.http_code
.value
,
850 return self
._format
_out
(problem_details
, None)
853 def _format_login(token_info
):
855 Changes cherrypy.request.login to include username/project_name;session so that cherrypy access log will
857 :param token_info: Dictionary with token content
860 cherrypy
.request
.login
= token_info
.get("username", "-")
861 if token_info
.get("project_name"):
862 cherrypy
.request
.login
+= "/" + token_info
["project_name"]
863 if token_info
.get("id"):
864 cherrypy
.request
.login
+= ";session=" + token_info
["id"][0:12]
866 # NS Fault Management
868 def nsfm(self
, version
=None, topic
=None, uuid
=None, project_name
=None, ns_id
=None, *args
, **kwargs
):
869 if topic
== 'alarms':
871 method
= cherrypy
.request
.method
872 role_permission
= self
._check
_valid
_url
_method
(method
, "nsfm", version
, topic
, None, None, *args
)
873 query_string_operations
= self
._extract
_query
_string
_operations
(kwargs
, method
)
875 self
.authenticator
.authorize(role_permission
, query_string_operations
, None)
877 # to handle get request
878 if cherrypy
.request
.method
== 'GET':
879 # if request is on basis of uuid
880 if uuid
and uuid
!= 'None':
882 alarm
= self
.engine
.db
.get_one("alarms", {"uuid": uuid
})
883 alarm_action
= self
.engine
.db
.get_one("alarms_action", {"uuid": uuid
})
884 alarm
.update(alarm_action
)
885 vnf
= self
.engine
.db
.get_one("vnfrs", {"nsr-id-ref": alarm
["tags"]["ns_id"]})
886 alarm
["vnf-id"] = vnf
["_id"]
887 return self
._format
_out
(str(alarm
))
889 return self
._format
_out
("Please provide valid alarm uuid")
890 elif ns_id
and ns_id
!= 'None':
891 # if request is on basis of ns_id
893 alarms
= self
.engine
.db
.get_list("alarms", {"tags.ns_id": ns_id
})
895 alarm_action
= self
.engine
.db
.get_one("alarms_action", {"uuid": alarm
['uuid']})
896 alarm
.update(alarm_action
)
897 return self
._format
_out
(str(alarms
))
899 return self
._format
_out
("Please provide valid ns id")
901 # to return only alarm which are related to given project
902 project
= self
.engine
.db
.get_one("projects", {"name": project_name
})
903 project_id
= project
.get('_id')
904 ns_list
= self
.engine
.db
.get_list("nsrs", {"_admin.projects_read": project_id
})
907 ns_ids
.append(ns
.get("_id"))
908 alarms
= self
.engine
.db
.get_list("alarms")
909 alarm_list
= [alarm
for alarm
in alarms
if alarm
["tags"]["ns_id"] in ns_ids
]
910 for alrm
in alarm_list
:
911 action
= self
.engine
.db
.get_one("alarms_action", {"uuid": alrm
.get("uuid")})
913 return self
._format
_out
(str(alarm_list
))
914 # to handle patch request for alarm update
915 elif cherrypy
.request
.method
== 'PATCH':
916 data
= yaml
.load(cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
)
918 # check if uuid is valid
919 self
.engine
.db
.get_one("alarms", {"uuid": data
.get("uuid")})
921 return self
._format
_out
("Please provide valid alarm uuid.")
922 if data
.get("is_enable") is not None:
923 if data
.get("is_enable"):
926 alarm_status
= 'disabled'
927 self
.engine
.db
.set_one("alarms", {"uuid": data
.get("uuid")},
928 {"alarm_status": alarm_status
})
930 self
.engine
.db
.set_one("alarms", {"uuid": data
.get("uuid")},
931 {"threshold": data
.get("threshold")})
932 return self
._format
_out
("Alarm updated")
933 except Exception as e
:
934 cherrypy
.response
.status
= e
.http_code
.value
935 if isinstance(e
, (NbiException
, EngineException
, DbException
, FsException
, MsgException
, AuthException
,
936 ValidationError
, AuthconnException
)):
937 http_code_value
= cherrypy
.response
.status
= e
.http_code
.value
938 http_code_name
= e
.http_code
.name
939 cherrypy
.log("Exception {}".format(e
))
941 http_code_value
= cherrypy
.response
.status
= HTTPStatus
.BAD_REQUEST
.value
# INTERNAL_SERVER_ERROR
942 cherrypy
.log("CRITICAL: Exception {}".format(e
), traceback
=True)
943 http_code_name
= HTTPStatus
.BAD_REQUEST
.name
945 "code": http_code_name
,
946 "status": http_code_value
,
949 return self
._format
_out
(problem_details
)
952 def token(self
, method
, token_id
=None, kwargs
=None):
954 # self.engine.load_dbase(cherrypy.request.app.config)
955 indata
= self
._format
_in
(kwargs
)
956 if not isinstance(indata
, dict):
958 "Expected application/yaml or application/json Content-Type",
959 HTTPStatus
.BAD_REQUEST
,
963 token_info
= self
.authenticator
.authorize()
965 self
._format
_login
(token_info
)
967 outdata
= self
.authenticator
.get_token(token_info
, token_id
)
969 outdata
= self
.authenticator
.get_token_list(token_info
)
970 elif method
== "POST":
972 token_info
= self
.authenticator
.authorize()
976 indata
.update(kwargs
)
977 # This is needed to log the user when authentication fails
978 cherrypy
.request
.login
= "{}".format(indata
.get("username", "-"))
979 outdata
= token_info
= self
.authenticator
.new_token(
980 token_info
, indata
, cherrypy
.request
.remote
982 cherrypy
.session
["Authorization"] = outdata
["_id"]
983 self
._set
_location
_header
("admin", "v1", "tokens", outdata
["_id"])
985 self
._format
_login
(token_info
)
986 # password expiry check
987 if self
.authenticator
.check_password_expiry(outdata
):
988 outdata
= {"id": outdata
["id"],
989 "message": "change_password",
990 "user_id": outdata
["user_id"]
992 # cherrypy.response.cookie["Authorization"] = outdata["id"]
993 # cherrypy.response.cookie["Authorization"]['expires'] = 3600
994 elif method
== "DELETE":
995 if not token_id
and "id" in kwargs
:
996 token_id
= kwargs
["id"]
998 token_info
= self
.authenticator
.authorize()
1000 self
._format
_login
(token_info
)
1001 token_id
= token_info
["_id"]
1002 outdata
= self
.authenticator
.del_token(token_id
)
1004 cherrypy
.session
["Authorization"] = "logout"
1005 # cherrypy.response.cookie["Authorization"] = token_id
1006 # cherrypy.response.cookie["Authorization"]['expires'] = 0
1009 "Method {} not allowed for token".format(method
),
1010 HTTPStatus
.METHOD_NOT_ALLOWED
,
1012 return self
._format
_out
(outdata
, token_info
)
1015 def test(self
, *args
, **kwargs
):
1016 if not cherrypy
.config
.get("server.enable_test") or (
1017 isinstance(cherrypy
.config
["server.enable_test"], str)
1018 and cherrypy
.config
["server.enable_test"].lower() == "false"
1020 cherrypy
.response
.status
= HTTPStatus
.METHOD_NOT_ALLOWED
.value
1021 return "test URL is disabled"
1023 if args
and args
[0] == "help":
1025 "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nfs-clear[/folder]\nlogin\nlogin2\n"
1026 "sleep/<time>\nmessage/topic\n</pre></html>"
1029 elif args
and args
[0] == "init":
1031 # self.engine.load_dbase(cherrypy.request.app.config)
1032 self
.engine
.create_admin()
1033 return "Done. User 'admin', password 'admin' created"
1035 cherrypy
.response
.status
= HTTPStatus
.FORBIDDEN
.value
1036 return self
._format
_out
("Database already initialized")
1037 elif args
and args
[0] == "file":
1038 return cherrypy
.lib
.static
.serve_file(
1039 cherrypy
.tree
.apps
["/osm"].config
["storage"]["path"] + "/" + args
[1],
1043 elif args
and args
[0] == "file2":
1045 cherrypy
.tree
.apps
["/osm"].config
["storage"]["path"] + "/" + args
[1]
1047 f
= open(f_path
, "r")
1048 cherrypy
.response
.headers
["Content-type"] = "text/plain"
1051 elif len(args
) == 2 and args
[0] == "db-clear":
1052 deleted_info
= self
.engine
.db
.del_list(args
[1], kwargs
)
1053 return "{} {} deleted\n".format(deleted_info
["deleted"], args
[1])
1054 elif len(args
) and args
[0] == "fs-clear":
1056 folders
= (args
[1],)
1058 folders
= self
.engine
.fs
.dir_ls(".")
1059 for folder
in folders
:
1060 self
.engine
.fs
.file_delete(folder
)
1061 return ",".join(folders
) + " folders deleted\n"
1062 elif args
and args
[0] == "login":
1063 if not cherrypy
.request
.headers
.get("Authorization"):
1064 cherrypy
.response
.headers
[
1066 ] = 'Basic realm="Access to OSM site", charset="UTF-8"'
1067 cherrypy
.response
.status
= HTTPStatus
.UNAUTHORIZED
.value
1068 elif args
and args
[0] == "login2":
1069 if not cherrypy
.request
.headers
.get("Authorization"):
1070 cherrypy
.response
.headers
[
1072 ] = 'Bearer realm="Access to OSM site"'
1073 cherrypy
.response
.status
= HTTPStatus
.UNAUTHORIZED
.value
1074 elif args
and args
[0] == "sleep":
1077 sleep_time
= int(args
[1])
1079 cherrypy
.response
.status
= HTTPStatus
.FORBIDDEN
.value
1080 return self
._format
_out
("Database already initialized")
1081 thread_info
= cherrypy
.thread_data
1083 time
.sleep(sleep_time
)
1085 elif len(args
) >= 2 and args
[0] == "message":
1086 main_topic
= args
[1]
1087 return_text
= "<html><pre>{} ->\n".format(main_topic
)
1089 if cherrypy
.request
.method
== "POST":
1090 to_send
= yaml
.load(cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
)
1091 for k
, v
in to_send
.items():
1092 self
.engine
.msg
.write(main_topic
, k
, v
)
1093 return_text
+= " {}: {}\n".format(k
, v
)
1094 elif cherrypy
.request
.method
== "GET":
1095 for k
, v
in kwargs
.items():
1096 v_dict
= yaml
.load(v
, Loader
=yaml
.SafeLoader
)
1097 self
.engine
.msg
.write(main_topic
, k
, v_dict
)
1098 return_text
+= " {}: {}\n".format(k
, v_dict
)
1099 except Exception as e
:
1100 return_text
+= "Error: " + str(e
)
1101 return_text
+= "</pre></html>\n"
1105 "<html><pre>\nheaders:\n args: {}\n".format(args
)
1106 + " kwargs: {}\n".format(kwargs
)
1107 + " headers: {}\n".format(cherrypy
.request
.headers
)
1108 + " path_info: {}\n".format(cherrypy
.request
.path_info
)
1109 + " query_string: {}\n".format(cherrypy
.request
.query_string
)
1110 + " session: {}\n".format(cherrypy
.session
)
1111 + " cookie: {}\n".format(cherrypy
.request
.cookie
)
1112 + " method: {}\n".format(cherrypy
.request
.method
)
1113 + " session: {}\n".format(cherrypy
.session
.get("fieldname"))
1116 return_text
+= " length: {}\n".format(cherrypy
.request
.body
.length
)
1117 if cherrypy
.request
.body
.length
:
1118 return_text
+= " content: {}\n".format(
1120 cherrypy
.request
.body
.read(
1121 int(cherrypy
.request
.headers
.get("Content-Length", 0))
1126 return_text
+= "thread: {}\n".format(thread_info
)
1127 return_text
+= "</pre></html>"
1131 def _check_valid_url_method(method
, *args
):
1134 "URL must contain at least 'main_topic/version/topic'",
1135 HTTPStatus
.METHOD_NOT_ALLOWED
,
1138 reference
= valid_url_methods
1142 if not isinstance(reference
, dict):
1144 "URL contains unexpected extra items '{}'".format(arg
),
1145 HTTPStatus
.METHOD_NOT_ALLOWED
,
1148 if arg
in reference
:
1149 reference
= reference
[arg
]
1150 elif "<ID>" in reference
:
1151 reference
= reference
["<ID>"]
1152 elif "*" in reference
:
1153 # if there is content
1155 reference
= reference
["*"]
1159 "Unexpected URL item {}".format(arg
), HTTPStatus
.METHOD_NOT_ALLOWED
1161 if "TODO" in reference
and method
in reference
["TODO"]:
1163 "Method {} not supported yet for this URL".format(method
),
1164 HTTPStatus
.NOT_IMPLEMENTED
,
1166 elif "METHODS" in reference
and method
not in reference
["METHODS"]:
1168 "Method {} not supported for this URL".format(method
),
1169 HTTPStatus
.METHOD_NOT_ALLOWED
,
1171 return reference
["ROLE_PERMISSION"] + method
.lower()
1174 def _set_location_header(main_topic
, version
, topic
, id):
1176 Insert response header Location with the URL of created item base on URL params
1183 # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT
1184 cherrypy
.response
.headers
["Location"] = "/osm/{}/{}/{}/{}".format(
1185 main_topic
, version
, topic
, id
1190 def _extract_query_string_operations(kwargs
, method
):
1196 query_string_operations
= []
1198 for qs
in ("FORCE", "PUBLIC", "ADMIN", "SET_PROJECT"):
1199 if qs
in kwargs
and kwargs
[qs
].lower() != "false":
1200 query_string_operations
.append(qs
.lower() + ":" + method
.lower())
1201 return query_string_operations
1204 def _manage_admin_query(token_info
, kwargs
, method
, _id
):
1206 Processes the administrator query inputs (if any) of FORCE, ADMIN, PUBLIC, SET_PROJECT
1207 Check that users has rights to use them and returs the admin_query
1208 :param token_info: token_info rights obtained by token
1209 :param kwargs: query string input.
1210 :param method: http method: GET, POSST, PUT, ...
1212 :return: admin_query dictionary with keys:
1213 public: True, False or None
1214 force: True or False
1215 project_id: tuple with projects used for accessing an element
1216 set_project: tuple with projects that a created element will belong to
1217 method: show, list, delete, write
1221 "project_id": (token_info
["project_id"],),
1222 "username": token_info
["username"],
1223 "admin": token_info
["admin"],
1225 "allow_show_user_project_role": token_info
["allow_show_user_project_role"],
1229 if "FORCE" in kwargs
:
1231 kwargs
["FORCE"].lower() != "false"
1232 ): # if None or True set force to True
1233 admin_query
["force"] = True
1236 if "PUBLIC" in kwargs
:
1238 kwargs
["PUBLIC"].lower() != "false"
1239 ): # if None or True set public to True
1240 admin_query
["public"] = True
1242 admin_query
["public"] = False
1243 del kwargs
["PUBLIC"]
1245 if "ADMIN" in kwargs
:
1246 behave_as
= kwargs
.pop("ADMIN")
1247 if behave_as
.lower() != "false":
1248 if not token_info
["admin"]:
1250 "Only admin projects can use 'ADMIN' query string",
1251 HTTPStatus
.UNAUTHORIZED
,
1254 not behave_as
or behave_as
.lower() == "true"
1255 ): # convert True, None to empty list
1256 admin_query
["project_id"] = ()
1257 elif isinstance(behave_as
, (list, tuple)):
1258 admin_query
["project_id"] = behave_as
1259 else: # isinstance(behave_as, str)
1260 admin_query
["project_id"] = (behave_as
,)
1261 if "SET_PROJECT" in kwargs
:
1262 set_project
= kwargs
.pop("SET_PROJECT")
1264 admin_query
["set_project"] = list(admin_query
["project_id"])
1266 if isinstance(set_project
, str):
1267 set_project
= (set_project
,)
1268 if admin_query
["project_id"]:
1269 for p
in set_project
:
1270 if p
not in admin_query
["project_id"]:
1272 "Unauthorized for 'SET_PROJECT={p}'. Try with 'ADMIN=True' or "
1273 "'ADMIN='{p}'".format(p
=p
),
1274 HTTPStatus
.UNAUTHORIZED
,
1276 admin_query
["set_project"] = set_project
1279 # if "PROJECT_READ" in kwargs:
1280 # admin_query["project"] = kwargs.pop("project")
1281 # if admin_query["project"] == token_info["project_id"]:
1284 admin_query
["method"] = "show"
1286 admin_query
["method"] = "list"
1287 elif method
== "DELETE":
1288 admin_query
["method"] = "delete"
1290 admin_query
["method"] = "write"
1310 engine_session
= None
1312 if not main_topic
or not version
or not topic
:
1314 "URL must contain at least 'main_topic/version/topic'",
1315 HTTPStatus
.METHOD_NOT_ALLOWED
,
1317 if main_topic
not in (
1329 "URL main_topic '{}' not supported".format(main_topic
),
1330 HTTPStatus
.METHOD_NOT_ALLOWED
,
1334 "URL version '{}' not supported".format(version
),
1335 HTTPStatus
.METHOD_NOT_ALLOWED
,
1340 and "METHOD" in kwargs
1341 and kwargs
["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH")
1343 method
= kwargs
.pop("METHOD")
1345 method
= cherrypy
.request
.method
1347 role_permission
= self
._check
_valid
_url
_method
(
1348 method
, main_topic
, version
, topic
, _id
, item
, *args
1350 query_string_operations
= self
._extract
_query
_string
_operations
(
1353 if main_topic
== "admin" and topic
== "tokens":
1354 return self
.token(method
, _id
, kwargs
)
1355 token_info
= self
.authenticator
.authorize(
1356 role_permission
, query_string_operations
, _id
1358 if main_topic
== "admin" and topic
== "domains":
1359 return self
.domain()
1360 engine_session
= self
._manage
_admin
_query
(token_info
, kwargs
, method
, _id
)
1361 indata
= self
._format
_in
(kwargs
)
1362 engine_topic
= topic
1364 if item
and topic
!= "pm_jobs":
1367 if main_topic
== "nsd":
1368 engine_topic
= "nsds"
1369 elif main_topic
== "vnfpkgm":
1370 engine_topic
= "vnfds"
1371 if topic
== "vnfpkg_op_occs":
1372 engine_topic
= "vnfpkgops"
1373 if topic
== "vnf_packages" and item
== "action":
1374 engine_topic
= "vnfpkgops"
1375 elif main_topic
== "nslcm":
1376 engine_topic
= "nsrs"
1377 if topic
== "ns_lcm_op_occs":
1378 engine_topic
= "nslcmops"
1379 if topic
== "vnfrs" or topic
== "vnf_instances":
1380 engine_topic
= "vnfrs"
1381 elif main_topic
== "vnflcm":
1382 if topic
== "vnf_lcm_op_occs":
1383 engine_topic
= "vnflcmops"
1384 elif main_topic
== "nst":
1385 engine_topic
= "nsts"
1386 elif main_topic
== "nsilcm":
1387 engine_topic
= "nsis"
1388 if topic
== "nsi_lcm_op_occs":
1389 engine_topic
= "nsilcmops"
1390 elif main_topic
== "pdu":
1391 engine_topic
= "pdus"
1393 engine_topic
== "vims"
1394 ): # TODO this is for backward compatibility, it will be removed in the future
1395 engine_topic
= "vim_accounts"
1397 if topic
== "subscriptions":
1398 engine_topic
= main_topic
+ "_" + topic
1410 if item
in ("vnfd", "nsd", "nst"):
1411 path
= "$DESCRIPTOR"
1414 elif item
== "artifacts":
1418 file, _format
= self
.engine
.get_file(
1423 cherrypy
.request
.headers
.get("Accept"),
1427 outdata
= self
.engine
.get_item_list(
1428 engine_session
, engine_topic
, kwargs
, api_req
=True
1431 if item
== "reports":
1432 # TODO check that project_id (_id in this context) has permissions
1435 if "vcaStatusRefresh" in kwargs
:
1436 filter_q
= {"vcaStatusRefresh": kwargs
["vcaStatusRefresh"]}
1437 outdata
= self
.engine
.get_item(engine_session
, engine_topic
, _id
, filter_q
, True)
1439 elif method
== "POST":
1440 cherrypy
.response
.status
= HTTPStatus
.CREATED
.value
1442 "ns_descriptors_content",
1443 "vnf_packages_content",
1444 "netslice_templates_content",
1446 _id
= cherrypy
.request
.headers
.get("Transaction-Id")
1448 _id
, _
= self
.engine
.new_item(
1454 cherrypy
.request
.headers
,
1456 completed
= self
.engine
.upload_content(
1462 cherrypy
.request
.headers
,
1465 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
1467 cherrypy
.response
.headers
["Transaction-Id"] = _id
1468 outdata
= {"id": _id
}
1469 elif topic
== "ns_instances_content":
1471 _id
, _
= self
.engine
.new_item(
1472 rollback
, engine_session
, engine_topic
, indata
, kwargs
1475 indata
["lcmOperationType"] = "instantiate"
1476 indata
["nsInstanceId"] = _id
1477 nslcmop_id
, _
= self
.engine
.new_item(
1478 rollback
, engine_session
, "nslcmops", indata
, None
1480 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
1481 outdata
= {"id": _id
, "nslcmop_id": nslcmop_id
}
1482 elif topic
== "ns_instances" and item
:
1483 indata
["lcmOperationType"] = item
1484 indata
["nsInstanceId"] = _id
1485 _id
, _
= self
.engine
.new_item(
1486 rollback
, engine_session
, "nslcmops", indata
, kwargs
1488 self
._set
_location
_header
(
1489 main_topic
, version
, "ns_lcm_op_occs", _id
1491 outdata
= {"id": _id
}
1492 cherrypy
.response
.status
= HTTPStatus
.ACCEPTED
.value
1493 elif topic
== "netslice_instances_content":
1494 # creates NetSlice_Instance_record (NSIR)
1495 _id
, _
= self
.engine
.new_item(
1496 rollback
, engine_session
, engine_topic
, indata
, kwargs
1498 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
1499 indata
["lcmOperationType"] = "instantiate"
1500 indata
["netsliceInstanceId"] = _id
1501 nsilcmop_id
, _
= self
.engine
.new_item(
1502 rollback
, engine_session
, "nsilcmops", indata
, kwargs
1504 outdata
= {"id": _id
, "nsilcmop_id": nsilcmop_id
}
1505 elif topic
== "netslice_instances" and item
:
1506 indata
["lcmOperationType"] = item
1507 indata
["netsliceInstanceId"] = _id
1508 _id
, _
= self
.engine
.new_item(
1509 rollback
, engine_session
, "nsilcmops", indata
, kwargs
1511 self
._set
_location
_header
(
1512 main_topic
, version
, "nsi_lcm_op_occs", _id
1514 outdata
= {"id": _id
}
1515 cherrypy
.response
.status
= HTTPStatus
.ACCEPTED
.value
1516 elif topic
== "vnf_packages" and item
== "action":
1517 indata
["lcmOperationType"] = item
1518 indata
["vnfPkgId"] = _id
1519 _id
, _
= self
.engine
.new_item(
1520 rollback
, engine_session
, "vnfpkgops", indata
, kwargs
1522 self
._set
_location
_header
(
1523 main_topic
, version
, "vnfpkg_op_occs", _id
1525 outdata
= {"id": _id
}
1526 cherrypy
.response
.status
= HTTPStatus
.ACCEPTED
.value
1527 elif topic
== "subscriptions":
1528 _id
, _
= self
.engine
.new_item(
1529 rollback
, engine_session
, engine_topic
, indata
, kwargs
1531 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
1533 link
["self"] = cherrypy
.response
.headers
["Location"]
1536 "filter": indata
["filter"],
1537 "callbackUri": indata
["CallbackUri"],
1540 cherrypy
.response
.status
= HTTPStatus
.CREATED
.value
1541 elif topic
== "vnf_instances" and item
:
1542 indata
["lcmOperationType"] = item
1543 indata
["vnfInstanceId"] = _id
1544 _id
, _
= self
.engine
.new_item(rollback
, engine_session
, "vnflcmops", indata
, kwargs
)
1545 self
._set
_location
_header
(main_topic
, version
, "vnf_lcm_op_occs", _id
)
1546 outdata
= {"id": _id
}
1547 cherrypy
.response
.status
= HTTPStatus
.ACCEPTED
.value
1549 _id
, op_id
= self
.engine
.new_item(
1555 cherrypy
.request
.headers
,
1557 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
1558 outdata
= {"id": _id
}
1560 outdata
["op_id"] = op_id
1561 cherrypy
.response
.status
= HTTPStatus
.ACCEPTED
.value
1562 # TODO form NsdInfo when topic in ("ns_descriptors", "vnf_packages")
1564 elif method
== "DELETE":
1566 outdata
= self
.engine
.del_item_list(
1567 engine_session
, engine_topic
, kwargs
1569 cherrypy
.response
.status
= HTTPStatus
.OK
.value
1570 else: # len(args) > 1
1571 # for NS NSI generate an operation
1573 if topic
== "ns_instances_content" and not engine_session
["force"]:
1575 "lcmOperationType": "terminate",
1576 "nsInstanceId": _id
,
1579 op_id
, _
= self
.engine
.new_item(
1580 rollback
, engine_session
, "nslcmops", nslcmop_desc
, kwargs
1583 outdata
= {"_id": op_id
}
1585 topic
== "netslice_instances_content"
1586 and not engine_session
["force"]
1589 "lcmOperationType": "terminate",
1590 "netsliceInstanceId": _id
,
1593 op_id
, _
= self
.engine
.new_item(
1594 rollback
, engine_session
, "nsilcmops", nsilcmop_desc
, None
1597 outdata
= {"_id": op_id
}
1598 # if there is not any deletion in process, delete
1600 op_id
= self
.engine
.del_item(engine_session
, engine_topic
, _id
)
1602 outdata
= {"op_id": op_id
}
1603 cherrypy
.response
.status
= (
1604 HTTPStatus
.ACCEPTED
.value
1606 else HTTPStatus
.NO_CONTENT
.value
1609 elif method
in ("PUT", "PATCH"):
1611 if not indata
and not kwargs
and not engine_session
.get("set_project"):
1613 "Nothing to update. Provide payload and/or query string",
1614 HTTPStatus
.BAD_REQUEST
,
1617 item
in ("nsd_content", "package_content", "nst_content")
1620 completed
= self
.engine
.upload_content(
1626 cherrypy
.request
.headers
,
1629 cherrypy
.response
.headers
["Transaction-Id"] = id
1631 op_id
= self
.engine
.edit_item(
1632 engine_session
, engine_topic
, _id
, indata
, kwargs
1636 cherrypy
.response
.status
= HTTPStatus
.ACCEPTED
.value
1637 outdata
= {"op_id": op_id
}
1639 cherrypy
.response
.status
= HTTPStatus
.NO_CONTENT
.value
1643 "Method {} not allowed".format(method
),
1644 HTTPStatus
.METHOD_NOT_ALLOWED
,
1647 # if Role information changes, it is needed to reload the information of roles
1648 if topic
== "roles" and method
!= "GET":
1649 self
.authenticator
.load_operation_to_allowed_roles()
1653 and method
== "DELETE"
1654 or topic
in ["users", "roles"]
1655 and method
in ["PUT", "PATCH", "DELETE"]
1657 self
.authenticator
.remove_token_from_cache()
1659 return self
._format
_out
(outdata
, token_info
, _format
)
1660 except Exception as e
:
1674 http_code_value
= cherrypy
.response
.status
= e
.http_code
.value
1675 http_code_name
= e
.http_code
.name
1676 cherrypy
.log("Exception {}".format(e
))
1679 cherrypy
.response
.status
1680 ) = HTTPStatus
.BAD_REQUEST
.value
# INTERNAL_SERVER_ERROR
1681 cherrypy
.log("CRITICAL: Exception {}".format(e
), traceback
=True)
1682 http_code_name
= HTTPStatus
.BAD_REQUEST
.name
1683 if hasattr(outdata
, "close"): # is an open file
1687 for rollback_item
in rollback
:
1689 if rollback_item
.get("operation") == "set":
1690 self
.engine
.db
.set_one(
1691 rollback_item
["topic"],
1692 {"_id": rollback_item
["_id"]},
1693 rollback_item
["content"],
1694 fail_on_empty
=False,
1696 elif rollback_item
.get("operation") == "del_list":
1697 self
.engine
.db
.del_list(
1698 rollback_item
["topic"],
1699 rollback_item
["filter"],
1700 fail_on_empty
=False,
1703 self
.engine
.db
.del_one(
1704 rollback_item
["topic"],
1705 {"_id": rollback_item
["_id"]},
1706 fail_on_empty
=False,
1708 except Exception as e2
:
1709 rollback_error_text
= "Rollback Exception {}: {}".format(
1712 cherrypy
.log(rollback_error_text
)
1713 error_text
+= ". " + rollback_error_text
1714 # if isinstance(e, MsgException):
1715 # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
1716 # engine_topic[:-1], method, error_text)
1718 "code": http_code_name
,
1719 "status": http_code_value
,
1720 "detail": error_text
,
1722 return self
._format
_out
(problem_details
, token_info
)
1723 # raise cherrypy.HTTPError(e.http_code.value, str(e))
1726 self
._format
_login
(token_info
)
1727 if method
in ("PUT", "PATCH", "POST") and isinstance(outdata
, dict):
1728 for logging_id
in ("id", "op_id", "nsilcmop_id", "nslcmop_id"):
1729 if outdata
.get(logging_id
):
1730 cherrypy
.request
.login
+= ";{}={}".format(
1731 logging_id
, outdata
[logging_id
][:36]
1735 def _start_service():
1737 Callback function called when cherrypy.engine starts
1738 Override configuration with env variables
1739 Set database, storage, message configuration
1740 Init database with admin/admin user password
1743 global subscription_thread
1744 cherrypy
.log
.error("Starting osm_nbi")
1745 # update general cherrypy configuration
1748 engine_config
= cherrypy
.tree
.apps
["/osm"].config
1749 for k
, v
in environ
.items():
1750 if not k
.startswith("OSMNBI_"):
1752 k1
, _
, k2
= k
[7:].lower().partition("_")
1756 # update static configuration
1757 if k
== "OSMNBI_STATIC_DIR":
1758 engine_config
["/static"]["tools.staticdir.dir"] = v
1759 engine_config
["/static"]["tools.staticdir.on"] = True
1760 elif k
== "OSMNBI_SOCKET_PORT" or k
== "OSMNBI_SERVER_PORT":
1761 update_dict
["server.socket_port"] = int(v
)
1762 elif k
== "OSMNBI_SOCKET_HOST" or k
== "OSMNBI_SERVER_HOST":
1763 update_dict
["server.socket_host"] = v
1764 elif k1
in ("server", "test", "auth", "log"):
1765 update_dict
[k1
+ "." + k2
] = v
1766 elif k1
in ("message", "database", "storage", "authentication"):
1767 # k2 = k2.replace('_', '.')
1768 if k2
in ("port", "db_port"):
1769 engine_config
[k1
][k2
] = int(v
)
1771 engine_config
[k1
][k2
] = v
1773 except ValueError as e
:
1774 cherrypy
.log
.error("Ignoring environ '{}': " + str(e
))
1775 except Exception as e
:
1776 cherrypy
.log
.warn("skipping environ '{}' on exception '{}'".format(k
, e
))
1779 cherrypy
.config
.update(update_dict
)
1780 engine_config
["global"].update(update_dict
)
1783 log_format_simple
= (
1784 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
1786 log_formatter_simple
= logging
.Formatter(
1787 log_format_simple
, datefmt
="%Y-%m-%dT%H:%M:%S"
1789 logger_server
= logging
.getLogger("cherrypy.error")
1790 logger_access
= logging
.getLogger("cherrypy.access")
1791 logger_cherry
= logging
.getLogger("cherrypy")
1792 logger_nbi
= logging
.getLogger("nbi")
1794 if "log.file" in engine_config
["global"]:
1795 file_handler
= logging
.handlers
.RotatingFileHandler(
1796 engine_config
["global"]["log.file"], maxBytes
=100e6
, backupCount
=9, delay
=0
1798 file_handler
.setFormatter(log_formatter_simple
)
1799 logger_cherry
.addHandler(file_handler
)
1800 logger_nbi
.addHandler(file_handler
)
1801 # log always to standard output
1802 for format_
, logger
in {
1803 "nbi.server %(filename)s:%(lineno)s": logger_server
,
1804 "nbi.access %(filename)s:%(lineno)s": logger_access
,
1805 "%(name)s %(filename)s:%(lineno)s": logger_nbi
,
1807 log_format_cherry
= "%(asctime)s %(levelname)s {} %(message)s".format(format_
)
1808 log_formatter_cherry
= logging
.Formatter(
1809 log_format_cherry
, datefmt
="%Y-%m-%dT%H:%M:%S"
1811 str_handler
= logging
.StreamHandler()
1812 str_handler
.setFormatter(log_formatter_cherry
)
1813 logger
.addHandler(str_handler
)
1815 if engine_config
["global"].get("log.level"):
1816 logger_cherry
.setLevel(engine_config
["global"]["log.level"])
1817 logger_nbi
.setLevel(engine_config
["global"]["log.level"])
1819 # logging other modules
1820 for k1
, logname
in {
1821 "message": "nbi.msg",
1822 "database": "nbi.db",
1823 "storage": "nbi.fs",
1825 engine_config
[k1
]["logger_name"] = logname
1826 logger_module
= logging
.getLogger(logname
)
1827 if "logfile" in engine_config
[k1
]:
1828 file_handler
= logging
.handlers
.RotatingFileHandler(
1829 engine_config
[k1
]["logfile"], maxBytes
=100e6
, backupCount
=9, delay
=0
1831 file_handler
.setFormatter(log_formatter_simple
)
1832 logger_module
.addHandler(file_handler
)
1833 if "loglevel" in engine_config
[k1
]:
1834 logger_module
.setLevel(engine_config
[k1
]["loglevel"])
1835 # TODO add more entries, e.g.: storage
1836 cherrypy
.tree
.apps
["/osm"].root
.engine
.start(engine_config
)
1837 cherrypy
.tree
.apps
["/osm"].root
.authenticator
.start(engine_config
)
1838 cherrypy
.tree
.apps
["/osm"].root
.engine
.init_db(target_version
=database_version
)
1839 cherrypy
.tree
.apps
["/osm"].root
.authenticator
.init_db(
1840 target_version
=auth_database_version
1843 # start subscriptions thread:
1844 subscription_thread
= SubscriptionThread(
1845 config
=engine_config
, engine
=nbi_server
.engine
1847 subscription_thread
.start()
1848 # Do not capture except SubscriptionException
1850 backend
= engine_config
["authentication"]["backend"]
1852 "Starting OSM NBI Version '{} {}' with '{}' authentication backend".format(
1853 nbi_version
, nbi_version_date
, backend
1858 def _stop_service():
1860 Callback function called when cherrypy.engine stops
1861 TODO: Ending database connections.
1863 global subscription_thread
1864 if subscription_thread
:
1865 subscription_thread
.terminate()
1866 subscription_thread
= None
1867 cherrypy
.tree
.apps
["/osm"].root
.engine
.stop()
1868 cherrypy
.log
.error("Stopping osm_nbi")
1871 def nbi(config_file
):
1875 # #'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
1876 # 'tools.sessions.on': True,
1877 # 'tools.response_headers.on': True,
1878 # # 'tools.response_headers.headers': [('Content-Type', 'text/plain')],
1881 # cherrypy.Server.ssl_module = 'builtin'
1882 # cherrypy.Server.ssl_certificate = "http/cert.pem"
1883 # cherrypy.Server.ssl_private_key = "http/privkey.pem"
1884 # cherrypy.Server.thread_pool = 10
1885 # cherrypy.config.update({'Server.socket_port': config["port"], 'Server.socket_host': config["host"]})
1887 # cherrypy.config.update({'tools.auth_basic.on': True,
1888 # 'tools.auth_basic.realm': 'localhost',
1889 # 'tools.auth_basic.checkpassword': validate_password})
1890 nbi_server
= Server()
1891 cherrypy
.engine
.subscribe("start", _start_service
)
1892 cherrypy
.engine
.subscribe("stop", _stop_service
)
1893 cherrypy
.quickstart(nbi_server
, "/osm", config_file
)
1898 """Usage: {} [options]
1899 -c|--config [configuration_file]: loads the configuration file (default: ./nbi.cfg)
1900 -h|--help: shows this help
1905 # --log-socket-host HOST: send logs to this host")
1906 # --log-socket-port PORT: send logs using this port (default: 9022)")
1909 if __name__
== "__main__":
1911 # load parameters and configuration
1912 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvc:", ["config=", "help"])
1913 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
1916 if o
in ("-h", "--help"):
1919 elif o
in ("-c", "--config"):
1921 # elif o == "--log-socket-port":
1922 # log_socket_port = a
1923 # elif o == "--log-socket-host":
1924 # log_socket_host = a
1925 # elif o == "--log-file":
1928 assert False, "Unhandled option"
1930 if not path
.isfile(config_file
):
1932 "configuration file '{}' that not exist".format(config_file
),
1937 for config_file
in (
1938 __file__
[: __file__
.rfind(".")] + ".cfg",
1942 if path
.isfile(config_file
):
1946 "No configuration file 'nbi.cfg' found neither at local folder nor at /etc/osm/",
1951 except getopt
.GetoptError
as e
:
1952 print(str(e
), file=sys
.stderr
)