2 # -*- coding: utf-8 -*-
5 # Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
22 from codecs
import getreader
24 from http
import HTTPStatus
27 import logging
.handlers
28 from os
import environ
, path
33 from osm_common
.dbbase
import DbException
34 from osm_common
.fsbase
import FsException
35 from osm_common
.msgbase
import MsgException
36 from osm_ng_ro
import version
as ro_version
, version_date
as ro_version_date
37 import osm_ng_ro
.html_out
as html
38 from osm_ng_ro
.ns
import Ns
, NsException
39 from osm_ng_ro
.validation
import ValidationError
40 from osm_ng_ro
.vim_admin
import VimAdminThread
44 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
45 __version__
= "0.1." # file version, not NBI version
46 version_date
= "May 2020"
48 database_version
= "1.2"
49 auth_database_version
= "1.0"
50 ro_server
= None # instance of Server class
51 vim_admin_thread
= None # instance of VimAdminThread class
53 # vim_threads = None # instance of VimThread class
56 RO North Bound Interface
57 URL: /ro GET POST PUT DELETE PATCH
65 valid_query_string
= ("ADMIN", "SET_PROJECT", "FORCE", "PUBLIC")
66 # ^ Contains possible administrative query string words:
67 # ADMIN=True(by default)|Project|Project-list: See all elements, or elements of a project
68 # (not owned by my session project).
69 # PUBLIC=True(by default)|False: See/hide public elements. Set/Unset a topic to be public
70 # FORCE=True(by default)|False: Force edition/deletion operations
71 # SET_PROJECT=Project|Project-list: Add/Delete the topic to the projects portfolio
74 # contains allowed URL and methods, and the role_permission name
79 "ROLE_PERMISSION": "tokens:",
80 "<ID>": {"METHODS": ("DELETE",), "ROLE_PERMISSION": "tokens:id:"},
88 "ROLE_PERMISSION": "rebuild:",
91 "ROLE_PERMISSION": "rebuild:id:",
96 "ROLE_PERMISSION": "start:",
99 "ROLE_PERMISSION": "start:id:",
103 "METHODS": ("POST",),
104 "ROLE_PERMISSION": "stop:",
106 "METHODS": ("POST",),
107 "ROLE_PERMISSION": "stop:id:",
112 "ROLE_PERMISSION": "deploy:",
114 "METHODS": ("GET", "POST", "DELETE"),
115 "ROLE_PERMISSION": "deploy:id:",
118 "ROLE_PERMISSION": "deploy:id:id:",
120 "METHODS": ("POST",),
121 "ROLE_PERMISSION": "deploy:id:id:cancel",
129 "ROLE_PERMISSION": "recreate:id:",
132 "ROLE_PERMISSION": "recreate:id:id:",
139 "ROLE_PERMISSION": "migrate:id:",
142 "ROLE_PERMISSION": "migrate:id:id:",
151 class RoException(Exception):
152 def __init__(self
, message
, http_code
=HTTPStatus
.METHOD_NOT_ALLOWED
):
153 Exception.__init
__(self
, message
)
154 self
.http_code
= http_code
157 class AuthException(RoException
):
162 def __init__(self
, valid_url_methods
, valid_query_string
):
163 self
.valid_url_methods
= valid_url_methods
164 self
.valid_query_string
= valid_query_string
166 def authorize(self
, *args
, **kwargs
):
167 return {"token": "ok", "id": "ok"}
169 def new_token(self
, token_info
, indata
, remote
):
170 return {"token": "ok", "id": "ok", "remote": remote
}
172 def del_token(self
, token_id
):
175 def start(self
, engine_config
):
179 class Server(object):
181 # to decode bytes to str
182 reader
= getreader("utf-8")
186 self
.authenticator
= Authenticator(valid_url_methods
, valid_query_string
)
188 self
.map_operation
= {
189 "token:post": self
.new_token
,
190 "token:id:delete": self
.del_token
,
191 "deploy:get": self
.ns
.get_deploy
,
192 "deploy:id:get": self
.ns
.get_actions
,
193 "deploy:id:post": self
.ns
.deploy
,
194 "deploy:id:delete": self
.ns
.delete
,
195 "deploy:id:id:get": self
.ns
.status
,
196 "deploy:id:id:cancel:post": self
.ns
.cancel
,
197 "rebuild:id:post": self
.ns
.rebuild_start_stop
,
198 "start:id:post": self
.ns
.rebuild_start_stop
,
199 "stop:id:post": self
.ns
.rebuild_start_stop
,
200 "recreate:id:post": self
.ns
.recreate
,
201 "recreate:id:id:get": self
.ns
.recreate_status
,
202 "migrate:id:post": self
.ns
.migrate
,
205 def _format_in(self
, kwargs
):
209 if cherrypy
.request
.body
.length
:
210 error_text
= "Invalid input format "
212 if "Content-Type" in cherrypy
.request
.headers
:
213 if "application/json" in cherrypy
.request
.headers
["Content-Type"]:
214 error_text
= "Invalid json format "
215 indata
= json
.load(self
.reader(cherrypy
.request
.body
))
216 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
217 elif "application/yaml" in cherrypy
.request
.headers
["Content-Type"]:
218 error_text
= "Invalid yaml format "
220 cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
222 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
224 "application/binary" in cherrypy
.request
.headers
["Content-Type"]
225 or "application/gzip"
226 in cherrypy
.request
.headers
["Content-Type"]
227 or "application/zip" in cherrypy
.request
.headers
["Content-Type"]
228 or "text/plain" in cherrypy
.request
.headers
["Content-Type"]
230 indata
= cherrypy
.request
.body
# .read()
232 "multipart/form-data"
233 in cherrypy
.request
.headers
["Content-Type"]
235 if "descriptor_file" in kwargs
:
236 filecontent
= kwargs
.pop("descriptor_file")
238 if not filecontent
.file:
240 "empty file or content", HTTPStatus
.BAD_REQUEST
243 indata
= filecontent
.file # .read()
245 if filecontent
.content_type
.value
:
246 cherrypy
.request
.headers
[
248 ] = filecontent
.content_type
.value
250 # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable,
251 # "Only 'Content-Type' of type 'application/json' or
252 # 'application/yaml' for input format are available")
253 error_text
= "Invalid yaml format "
255 cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
257 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
259 error_text
= "Invalid yaml format "
260 indata
= yaml
.load(cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
)
261 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
267 if cherrypy
.request
.headers
.get("Query-String-Format") == "yaml":
270 for k
, v
in kwargs
.items():
271 if isinstance(v
, str):
276 kwargs
[k
] = yaml
.load(v
, Loader
=yaml
.SafeLoader
)
282 or k
.endswith(".gte")
283 or k
.endswith(".lte")
292 elif v
.find(",") > 0:
293 kwargs
[k
] = v
.split(",")
294 elif isinstance(v
, (list, tuple)):
295 for index
in range(0, len(v
)):
300 v
[index
] = yaml
.load(v
[index
], Loader
=yaml
.SafeLoader
)
305 except (ValueError, yaml
.YAMLError
) as exc
:
306 raise RoException(error_text
+ str(exc
), HTTPStatus
.BAD_REQUEST
)
307 except KeyError as exc
:
308 raise RoException("Query string error: " + str(exc
), HTTPStatus
.BAD_REQUEST
)
309 except Exception as exc
:
310 raise RoException(error_text
+ str(exc
), HTTPStatus
.BAD_REQUEST
)
313 def _format_out(data
, token_info
=None, _format
=None):
315 return string of dictionary data according to requested json, yaml, xml. By default json
316 :param data: response to be sent. Can be a dict, text or file
317 :param token_info: Contains among other username and project
318 :param _format: The format to be set as Content-Type if data is a file
321 accept
= cherrypy
.request
.headers
.get("Accept")
324 if accept
and "text/html" in accept
:
326 data
, cherrypy
.request
, cherrypy
.response
, token_info
329 # cherrypy.response.status = HTTPStatus.NO_CONTENT.value
331 elif hasattr(data
, "read"): # file object
333 cherrypy
.response
.headers
["Content-Type"] = _format
334 elif "b" in data
.mode
: # binariy asssumig zip
335 cherrypy
.response
.headers
["Content-Type"] = "application/zip"
337 cherrypy
.response
.headers
["Content-Type"] = "text/plain"
339 # TODO check that cherrypy close file. If not implement pending things to close per thread next
343 if "application/json" in accept
:
344 cherrypy
.response
.headers
[
346 ] = "application/json; charset=utf-8"
347 a
= json
.dumps(data
, indent
=4) + "\n"
349 return a
.encode("utf8")
350 elif "text/html" in accept
:
352 data
, cherrypy
.request
, cherrypy
.response
, token_info
355 "application/yaml" in accept
357 or "text/plain" in accept
360 # if there is not any valid accept, raise an error. But if response is already an error, format in yaml
361 elif cherrypy
.response
.status
>= 400:
362 raise cherrypy
.HTTPError(
363 HTTPStatus
.NOT_ACCEPTABLE
.value
,
364 "Only 'Accept' of type 'application/json' or 'application/yaml' "
365 "for output format are available",
368 cherrypy
.response
.headers
["Content-Type"] = "application/yaml"
370 return yaml
.safe_dump(
374 default_flow_style
=False,
378 ) # , canonical=True, default_style='"'
381 def index(self
, *args
, **kwargs
):
385 if cherrypy
.request
.method
== "GET":
386 token_info
= self
.authenticator
.authorize()
387 outdata
= token_info
# Home page
389 raise cherrypy
.HTTPError(
390 HTTPStatus
.METHOD_NOT_ALLOWED
.value
,
391 "Method {} not allowed for tokens".format(cherrypy
.request
.method
),
394 return self
._format
_out
(outdata
, token_info
)
395 except (NsException
, AuthException
) as e
:
396 # cherrypy.log("index Exception {}".format(e))
397 cherrypy
.response
.status
= e
.http_code
.value
399 return self
._format
_out
("Welcome to OSM!", token_info
)
402 def version(self
, *args
, **kwargs
):
403 # TODO consider to remove and provide version using the static version file
405 if cherrypy
.request
.method
!= "GET":
407 "Only method GET is allowed",
408 HTTPStatus
.METHOD_NOT_ALLOWED
,
412 "Invalid URL or query string for version",
413 HTTPStatus
.METHOD_NOT_ALLOWED
,
416 # TODO include version of other modules, pick up from some kafka admin message
417 osm_ng_ro_version
= {"version": ro_version
, "date": ro_version_date
}
419 return self
._format
_out
(osm_ng_ro_version
)
420 except RoException
as e
:
421 cherrypy
.response
.status
= e
.http_code
.value
423 "code": e
.http_code
.name
,
424 "status": e
.http_code
.value
,
428 return self
._format
_out
(problem_details
, None)
430 def new_token(self
, engine_session
, indata
, *args
, **kwargs
):
434 token_info
= self
.authenticator
.authorize()
439 indata
.update(kwargs
)
441 # This is needed to log the user when authentication fails
442 cherrypy
.request
.login
= "{}".format(indata
.get("username", "-"))
443 token_info
= self
.authenticator
.new_token(
444 token_info
, indata
, cherrypy
.request
.remote
446 cherrypy
.session
["Authorization"] = token_info
["id"]
447 self
._set
_location
_header
("admin", "v1", "tokens", token_info
["id"])
450 # cherrypy.response.cookie["Authorization"] = outdata["id"]
451 # cherrypy.response.cookie["Authorization"]['expires'] = 3600
453 return token_info
, token_info
["id"], True
455 def del_token(self
, engine_session
, indata
, version
, _id
, *args
, **kwargs
):
458 if not token_id
and "id" in kwargs
:
459 token_id
= kwargs
["id"]
461 token_info
= self
.authenticator
.authorize()
463 token_id
= token_info
["id"]
465 self
.authenticator
.del_token(token_id
)
467 cherrypy
.session
["Authorization"] = "logout"
468 # cherrypy.response.cookie["Authorization"] = token_id
469 # cherrypy.response.cookie["Authorization"]['expires'] = 0
471 return None, None, True
474 def test(self
, *args
, **kwargs
):
475 if not cherrypy
.config
.get("server.enable_test") or (
476 isinstance(cherrypy
.config
["server.enable_test"], str)
477 and cherrypy
.config
["server.enable_test"].lower() == "false"
479 cherrypy
.response
.status
= HTTPStatus
.METHOD_NOT_ALLOWED
.value
481 return "test URL is disabled"
485 if args
and args
[0] == "help":
487 "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nfs-clear[/folder]\nlogin\nlogin2\n"
488 "sleep/<time>\nmessage/topic\n</pre></html>"
490 elif args
and args
[0] == "init":
492 # self.ns.load_dbase(cherrypy.request.app.config)
493 self
.ns
.create_admin()
495 return "Done. User 'admin', password 'admin' created"
497 cherrypy
.response
.status
= HTTPStatus
.FORBIDDEN
.value
499 return self
._format
_out
("Database already initialized")
500 elif args
and args
[0] == "file":
501 return cherrypy
.lib
.static
.serve_file(
502 cherrypy
.tree
.apps
["/ro"].config
["storage"]["path"] + "/" + args
[1],
506 elif args
and args
[0] == "file2":
507 f_path
= cherrypy
.tree
.apps
["/ro"].config
["storage"]["path"] + "/" + args
[1]
508 f
= open(f_path
, "r")
509 cherrypy
.response
.headers
["Content-type"] = "text/plain"
512 elif len(args
) == 2 and args
[0] == "db-clear":
513 deleted_info
= self
.ns
.db
.del_list(args
[1], kwargs
)
514 return "{} {} deleted\n".format(deleted_info
["deleted"], args
[1])
515 elif len(args
) and args
[0] == "fs-clear":
519 folders
= self
.ns
.fs
.dir_ls(".")
521 for folder
in folders
:
522 self
.ns
.fs
.file_delete(folder
)
524 return ",".join(folders
) + " folders deleted\n"
525 elif args
and args
[0] == "login":
526 if not cherrypy
.request
.headers
.get("Authorization"):
527 cherrypy
.response
.headers
[
529 ] = 'Basic realm="Access to OSM site", charset="UTF-8"'
530 cherrypy
.response
.status
= HTTPStatus
.UNAUTHORIZED
.value
531 elif args
and args
[0] == "login2":
532 if not cherrypy
.request
.headers
.get("Authorization"):
533 cherrypy
.response
.headers
[
535 ] = 'Bearer realm="Access to OSM site"'
536 cherrypy
.response
.status
= HTTPStatus
.UNAUTHORIZED
.value
537 elif args
and args
[0] == "sleep":
541 sleep_time
= int(args
[1])
543 cherrypy
.response
.status
= HTTPStatus
.FORBIDDEN
.value
544 return self
._format
_out
("Database already initialized")
546 thread_info
= cherrypy
.thread_data
548 time
.sleep(sleep_time
)
550 elif len(args
) >= 2 and args
[0] == "message":
552 return_text
= "<html><pre>{} ->\n".format(main_topic
)
555 if cherrypy
.request
.method
== "POST":
556 to_send
= yaml
.load(cherrypy
.request
.body
, Loader
=yaml
.SafeLoader
)
557 for k
, v
in to_send
.items():
558 self
.ns
.msg
.write(main_topic
, k
, v
)
559 return_text
+= " {}: {}\n".format(k
, v
)
560 elif cherrypy
.request
.method
== "GET":
561 for k
, v
in kwargs
.items():
563 main_topic
, k
, yaml
.load(v
, Loader
=yaml
.SafeLoader
)
565 return_text
+= " {}: {}\n".format(
566 k
, yaml
.load(v
, Loader
=yaml
.SafeLoader
)
568 except Exception as e
:
569 return_text
+= "Error: " + str(e
)
571 return_text
+= "</pre></html>\n"
576 "<html><pre>\nheaders:\n args: {}\n".format(args
)
577 + " kwargs: {}\n".format(kwargs
)
578 + " headers: {}\n".format(cherrypy
.request
.headers
)
579 + " path_info: {}\n".format(cherrypy
.request
.path_info
)
580 + " query_string: {}\n".format(cherrypy
.request
.query_string
)
581 + " session: {}\n".format(cherrypy
.session
)
582 + " cookie: {}\n".format(cherrypy
.request
.cookie
)
583 + " method: {}\n".format(cherrypy
.request
.method
)
584 + " session: {}\n".format(cherrypy
.session
.get("fieldname"))
587 return_text
+= " length: {}\n".format(cherrypy
.request
.body
.length
)
589 if cherrypy
.request
.body
.length
:
590 return_text
+= " content: {}\n".format(
592 cherrypy
.request
.body
.read(
593 int(cherrypy
.request
.headers
.get("Content-Length", 0))
599 return_text
+= "thread: {}\n".format(thread_info
)
601 return_text
+= "</pre></html>"
606 def _check_valid_url_method(method
, *args
):
609 "URL must contain at least 'main_topic/version/topic'",
610 HTTPStatus
.METHOD_NOT_ALLOWED
,
613 reference
= valid_url_methods
618 if not isinstance(reference
, dict):
620 "URL contains unexpected extra items '{}'".format(arg
),
621 HTTPStatus
.METHOD_NOT_ALLOWED
,
625 reference
= reference
[arg
]
626 elif "<ID>" in reference
:
627 reference
= reference
["<ID>"]
628 elif "*" in reference
:
629 # reference = reference["*"]
633 "Unexpected URL item {}".format(arg
),
634 HTTPStatus
.METHOD_NOT_ALLOWED
,
637 if "TODO" in reference
and method
in reference
["TODO"]:
639 "Method {} not supported yet for this URL".format(method
),
640 HTTPStatus
.NOT_IMPLEMENTED
,
642 elif "METHODS" not in reference
or method
not in reference
["METHODS"]:
644 "Method {} not supported for this URL".format(method
),
645 HTTPStatus
.METHOD_NOT_ALLOWED
,
648 return reference
["ROLE_PERMISSION"] + method
.lower()
651 def _set_location_header(main_topic
, version
, topic
, id):
653 Insert response header Location with the URL of created item base on URL params
660 # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT
661 cherrypy
.response
.headers
["Location"] = "/ro/{}/{}/{}/{}".format(
662 main_topic
, version
, topic
, id
683 engine_session
= None
686 if not main_topic
or not version
or not topic
:
688 "URL must contain at least 'main_topic/version/topic'",
689 HTTPStatus
.METHOD_NOT_ALLOWED
,
692 if main_topic
not in (
697 "URL main_topic '{}' not supported".format(main_topic
),
698 HTTPStatus
.METHOD_NOT_ALLOWED
,
703 "URL version '{}' not supported".format(version
),
704 HTTPStatus
.METHOD_NOT_ALLOWED
,
709 and "METHOD" in kwargs
710 and kwargs
["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH")
712 method
= kwargs
.pop("METHOD")
714 method
= cherrypy
.request
.method
716 role_permission
= self
._check
_valid
_url
_method
(
717 method
, main_topic
, version
, topic
, _id
, _id2
, *args
, **kwargs
719 # skip token validation if requesting a token
720 indata
= self
._format
_in
(kwargs
)
722 if main_topic
!= "admin" or topic
!= "tokens":
723 token_info
= self
.authenticator
.authorize(role_permission
, _id
)
725 outdata
, created_id
, done
= self
.map_operation
[role_permission
](
726 engine_session
, indata
, version
, _id
, _id2
, *args
, *kwargs
730 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
732 cherrypy
.response
.status
= (
733 HTTPStatus
.ACCEPTED
.value
735 else HTTPStatus
.OK
.value
736 if outdata
is not None
737 else HTTPStatus
.NO_CONTENT
.value
740 return self
._format
_out
(outdata
, token_info
, _format
)
741 except Exception as e
:
754 http_code_value
= cherrypy
.response
.status
= e
.http_code
.value
755 http_code_name
= e
.http_code
.name
756 cherrypy
.log("Exception {}".format(e
))
759 cherrypy
.response
.status
760 ) = HTTPStatus
.BAD_REQUEST
.value
# INTERNAL_SERVER_ERROR
761 cherrypy
.log("CRITICAL: Exception {}".format(e
), traceback
=True)
762 http_code_name
= HTTPStatus
.BAD_REQUEST
.name
764 if hasattr(outdata
, "close"): # is an open file
770 for rollback_item
in rollback
:
772 if rollback_item
.get("operation") == "set":
774 rollback_item
["topic"],
775 {"_id": rollback_item
["_id"]},
776 rollback_item
["content"],
781 rollback_item
["topic"],
782 {"_id": rollback_item
["_id"]},
785 except Exception as e2
:
786 rollback_error_text
= "Rollback Exception {}: {}".format(
789 cherrypy
.log(rollback_error_text
)
790 error_text
+= ". " + rollback_error_text
792 # if isinstance(e, MsgException):
793 # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
794 # engine_topic[:-1], method, error_text)
796 "code": http_code_name
,
797 "status": http_code_value
,
798 "detail": error_text
,
801 return self
._format
_out
(problem_details
, token_info
)
802 # raise cherrypy.HTTPError(e.http_code.value, str(e))
805 if method
in ("PUT", "PATCH", "POST") and isinstance(outdata
, dict):
806 for logging_id
in ("id", "op_id", "nsilcmop_id", "nslcmop_id"):
807 if outdata
.get(logging_id
):
808 cherrypy
.request
.login
+= ";{}={}".format(
809 logging_id
, outdata
[logging_id
][:36]
813 def _start_service():
815 Callback function called when cherrypy.engine starts
816 Override configuration with env variables
817 Set database, storage, message configuration
818 Init database with admin/admin user password
820 global ro_server
, vim_admin_thread
822 cherrypy
.log
.error("Starting osm_ng_ro")
823 # update general cherrypy configuration
825 engine_config
= cherrypy
.tree
.apps
["/ro"].config
827 for k
, v
in environ
.items():
828 if not k
.startswith("OSMRO_"):
831 k1
, _
, k2
= k
[6:].lower().partition("_")
837 if k1
in ("server", "test", "auth", "log"):
838 # update [global] configuration
839 update_dict
[k1
+ "." + k2
] = yaml
.safe_load(v
)
841 # update [/static] configuration
842 engine_config
["/static"]["tools.staticdir." + k2
] = yaml
.safe_load(v
)
844 # update [/] configuration
845 engine_config
["/"]["tools." + k2
.replace("_", ".")] = yaml
.safe_load(v
)
846 elif k1
in ("message", "database", "storage", "authentication"):
847 engine_config
[k1
][k2
] = yaml
.safe_load(v
)
849 except Exception as e
:
850 raise RoException("Cannot load env '{}': {}".format(k
, e
))
853 cherrypy
.config
.update(update_dict
)
854 engine_config
["global"].update(update_dict
)
857 log_format_simple
= (
858 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
860 log_formatter_simple
= logging
.Formatter(
861 log_format_simple
, datefmt
="%Y-%m-%dT%H:%M:%S"
863 logger_server
= logging
.getLogger("cherrypy.error")
864 logger_access
= logging
.getLogger("cherrypy.access")
865 logger_cherry
= logging
.getLogger("cherrypy")
866 logger
= logging
.getLogger("ro")
868 if "log.file" in engine_config
["global"]:
869 file_handler
= logging
.handlers
.RotatingFileHandler(
870 engine_config
["global"]["log.file"], maxBytes
=100e6
, backupCount
=9, delay
=0
872 file_handler
.setFormatter(log_formatter_simple
)
873 logger_cherry
.addHandler(file_handler
)
874 logger
.addHandler(file_handler
)
876 # log always to standard output
877 for format_
, logger
in {
878 "ro.server %(filename)s:%(lineno)s": logger_server
,
879 "ro.access %(filename)s:%(lineno)s": logger_access
,
880 "%(name)s %(filename)s:%(lineno)s": logger
,
882 log_format_cherry
= "%(asctime)s %(levelname)s {} %(message)s".format(format_
)
883 log_formatter_cherry
= logging
.Formatter(
884 log_format_cherry
, datefmt
="%Y-%m-%dT%H:%M:%S"
886 str_handler
= logging
.StreamHandler()
887 str_handler
.setFormatter(log_formatter_cherry
)
888 logger
.addHandler(str_handler
)
890 if engine_config
["global"].get("log.level"):
891 logger_cherry
.setLevel(engine_config
["global"]["log.level"])
892 logger
.setLevel(engine_config
["global"]["log.level"])
894 # logging other modules
900 engine_config
[k1
]["logger_name"] = logname
901 logger_module
= logging
.getLogger(logname
)
903 if "logfile" in engine_config
[k1
]:
904 file_handler
= logging
.handlers
.RotatingFileHandler(
905 engine_config
[k1
]["logfile"], maxBytes
=100e6
, backupCount
=9, delay
=0
907 file_handler
.setFormatter(log_formatter_simple
)
908 logger_module
.addHandler(file_handler
)
910 if "loglevel" in engine_config
[k1
]:
911 logger_module
.setLevel(engine_config
[k1
]["loglevel"])
912 # TODO add more entries, e.g.: storage
914 engine_config
["assignment"] = {}
915 # ^ each VIM, SDNc will be assigned one worker id. Ns class will add items and VimThread will auto-assign
916 cherrypy
.tree
.apps
["/ro"].root
.ns
.start(engine_config
)
917 cherrypy
.tree
.apps
["/ro"].root
.authenticator
.start(engine_config
)
918 cherrypy
.tree
.apps
["/ro"].root
.ns
.init_db(target_version
=database_version
)
920 # # start subscriptions thread:
921 vim_admin_thread
= VimAdminThread(config
=engine_config
, engine
=ro_server
.ns
)
922 vim_admin_thread
.start()
923 # # Do not capture except SubscriptionException
925 # backend = engine_config["authentication"]["backend"]
926 # cherrypy.log.error("Starting OSM NBI Version '{} {}' with '{}' authentication backend"
927 # .format(ro_version, ro_version_date, backend))
932 Callback function called when cherrypy.engine stops
933 TODO: Ending database connections.
935 global vim_admin_thread
937 # terminate vim_admin_thread
939 vim_admin_thread
.terminate()
941 vim_admin_thread
= None
942 cherrypy
.tree
.apps
["/ro"].root
.ns
.stop()
943 cherrypy
.log
.error("Stopping osm_ng_ro")
946 def ro_main(config_file
):
950 cherrypy
.engine
.subscribe("start", _start_service
)
951 cherrypy
.engine
.subscribe("stop", _stop_service
)
952 cherrypy
.quickstart(ro_server
, "/ro", config_file
)
957 """Usage: {} [options]
958 -c|--config [configuration_file]: loads the configuration file (default: ./ro.cfg)
959 -h|--help: shows this help
964 # --log-socket-host HOST: send logs to this host")
965 # --log-socket-port PORT: send logs using this port (default: 9022)")
968 if __name__
== "__main__":
970 # load parameters and configuration
971 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvc:", ["config=", "help"])
972 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
976 if o
in ("-h", "--help"):
979 elif o
in ("-c", "--config"):
982 assert False, "Unhandled option"
985 if not path
.isfile(config_file
):
987 "configuration file '{}' that not exist".format(config_file
),
993 path
.dirname(__file__
) + "/ro.cfg",
997 if path
.isfile(config_file
):
1001 "No configuration file 'ro.cfg' found neither at local folder nor at /etc/osm/",
1006 ro_main(config_file
)
1007 except KeyboardInterrupt:
1008 print("KeyboardInterrupt. Finishing", file=sys
.stderr
)
1009 except getopt
.GetoptError
as e
:
1010 print(str(e
), file=sys
.stderr
)