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:",
149 "ROLE_PERMISSION": "verticalscale:id:",
152 "ROLE_PERMISSION": "verticalscale:id:id:",
161 class RoException(Exception):
162 def __init__(self
, message
, http_code
=HTTPStatus
.METHOD_NOT_ALLOWED
):
163 Exception.__init
__(self
, message
)
164 self
.http_code
= http_code
167 class AuthException(RoException
):
172 def __init__(self
, valid_url_methods
, valid_query_string
):
173 self
.valid_url_methods
= valid_url_methods
174 self
.valid_query_string
= valid_query_string
176 def authorize(self
, *args
, **kwargs
):
177 return {"token": "ok", "id": "ok"}
179 def new_token(self
, token_info
, indata
, remote
):
180 return {"token": "ok", "id": "ok", "remote": remote
}
182 def del_token(self
, token_id
):
185 def start(self
, engine_config
):
189 class Server(object):
191 # to decode bytes to str
192 reader
= getreader("utf-8")
196 self
.authenticator
= Authenticator(valid_url_methods
, valid_query_string
)
198 self
.map_operation
= {
199 "token:post": self
.new_token
,
200 "token:id:delete": self
.del_token
,
201 "deploy:get": self
.ns
.get_deploy
,
202 "deploy:id:get": self
.ns
.get_actions
,
203 "deploy:id:post": self
.ns
.deploy
,
204 "deploy:id:delete": self
.ns
.delete
,
205 "deploy:id:id:get": self
.ns
.status
,
206 "deploy:id:id:cancel:post": self
.ns
.cancel
,
207 "rebuild:id:post": self
.ns
.rebuild_start_stop
,
208 "start:id:post": self
.ns
.rebuild_start_stop
,
209 "stop:id:post": self
.ns
.rebuild_start_stop
,
210 "recreate:id:post": self
.ns
.recreate
,
211 "recreate:id:id:get": self
.ns
.recreate_status
,
212 "migrate:id:post": self
.ns
.migrate
,
213 "verticalscale:id:post": self
.ns
.verticalscale
,
216 def _format_in(self
, kwargs
):
220 if cherrypy
.request
.body
.length
:
221 error_text
= "Invalid input format "
223 if "Content-Type" in cherrypy
.request
.headers
:
224 if "application/json" in cherrypy
.request
.headers
["Content-Type"]:
225 error_text
= "Invalid json format "
226 indata
= json
.load(self
.reader(cherrypy
.request
.body
))
227 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
228 elif "application/yaml" in cherrypy
.request
.headers
["Content-Type"]:
229 error_text
= "Invalid yaml format "
230 indata
= yaml
.safe_load(cherrypy
.request
.body
)
231 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
233 "application/binary" in cherrypy
.request
.headers
["Content-Type"]
234 or "application/gzip"
235 in cherrypy
.request
.headers
["Content-Type"]
236 or "application/zip" in cherrypy
.request
.headers
["Content-Type"]
237 or "text/plain" in cherrypy
.request
.headers
["Content-Type"]
239 indata
= cherrypy
.request
.body
# .read()
241 "multipart/form-data"
242 in cherrypy
.request
.headers
["Content-Type"]
244 if "descriptor_file" in kwargs
:
245 filecontent
= kwargs
.pop("descriptor_file")
247 if not filecontent
.file:
249 "empty file or content", HTTPStatus
.BAD_REQUEST
252 indata
= filecontent
.file # .read()
254 if filecontent
.content_type
.value
:
255 cherrypy
.request
.headers
[
257 ] = filecontent
.content_type
.value
259 # raise cherrypy.HTTPError(HTTPStatus.Not_Acceptable,
260 # "Only 'Content-Type' of type 'application/json' or
261 # 'application/yaml' for input format are available")
262 error_text
= "Invalid yaml format "
263 indata
= yaml
.safe_load(cherrypy
.request
.body
)
264 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
266 error_text
= "Invalid yaml format "
267 indata
= yaml
.safe_load(cherrypy
.request
.body
)
268 cherrypy
.request
.headers
.pop("Content-File-MD5", None)
274 if cherrypy
.request
.headers
.get("Query-String-Format") == "yaml":
277 for k
, v
in kwargs
.items():
278 if isinstance(v
, str):
283 kwargs
[k
] = yaml
.safe_load(v
)
284 except Exception as yaml_error
:
286 f
"{yaml_error} occured while parsing the yaml"
291 or k
.endswith(".gte")
292 or k
.endswith(".lte")
299 except Exception as keyword_error
:
301 f
"{keyword_error} occured while getting the keyword arguments"
303 elif v
.find(",") > 0:
304 kwargs
[k
] = v
.split(",")
305 elif isinstance(v
, (list, tuple)):
306 for index
in range(0, len(v
)):
311 v
[index
] = yaml
.safe_load(v
[index
])
312 except Exception as error
:
314 f
"{error} occured while parsing the yaml"
318 except (ValueError, yaml
.YAMLError
) as exc
:
319 raise RoException(error_text
+ str(exc
), HTTPStatus
.BAD_REQUEST
)
320 except KeyError as exc
:
321 raise RoException("Query string error: " + str(exc
), HTTPStatus
.BAD_REQUEST
)
322 except Exception as exc
:
323 raise RoException(error_text
+ str(exc
), HTTPStatus
.BAD_REQUEST
)
326 def _format_out(data
, token_info
=None, _format
=None):
328 return string of dictionary data according to requested json, yaml, xml. By default json
329 :param data: response to be sent. Can be a dict, text or file
330 :param token_info: Contains among other username and project
331 :param _format: The format to be set as Content-Type if data is a file
334 accept
= cherrypy
.request
.headers
.get("Accept")
337 if accept
and "text/html" in accept
:
339 data
, cherrypy
.request
, cherrypy
.response
, token_info
342 # cherrypy.response.status = HTTPStatus.NO_CONTENT.value
344 elif hasattr(data
, "read"): # file object
346 cherrypy
.response
.headers
["Content-Type"] = _format
347 elif "b" in data
.mode
: # binariy asssumig zip
348 cherrypy
.response
.headers
["Content-Type"] = "application/zip"
350 cherrypy
.response
.headers
["Content-Type"] = "text/plain"
352 # TODO check that cherrypy close file. If not implement pending things to close per thread next
356 if "application/json" in accept
:
357 cherrypy
.response
.headers
[
359 ] = "application/json; charset=utf-8"
360 a
= json
.dumps(data
, indent
=4) + "\n"
362 return a
.encode("utf8")
363 elif "text/html" in accept
:
365 data
, cherrypy
.request
, cherrypy
.response
, token_info
368 "application/yaml" in accept
370 or "text/plain" in accept
373 # if there is not any valid accept, raise an error. But if response is already an error, format in yaml
374 elif cherrypy
.response
.status
>= 400:
375 raise cherrypy
.HTTPError(
376 HTTPStatus
.NOT_ACCEPTABLE
.value
,
377 "Only 'Accept' of type 'application/json' or 'application/yaml' "
378 "for output format are available",
381 cherrypy
.response
.headers
["Content-Type"] = "application/yaml"
383 return yaml
.safe_dump(
387 default_flow_style
=False,
391 ) # , canonical=True, default_style='"'
394 def index(self
, *args
, **kwargs
):
398 if cherrypy
.request
.method
== "GET":
399 token_info
= self
.authenticator
.authorize()
400 outdata
= token_info
# Home page
402 raise cherrypy
.HTTPError(
403 HTTPStatus
.METHOD_NOT_ALLOWED
.value
,
404 "Method {} not allowed for tokens".format(cherrypy
.request
.method
),
407 return self
._format
_out
(outdata
, token_info
)
408 except (NsException
, AuthException
) as e
:
409 # cherrypy.log("index Exception {}".format(e))
410 cherrypy
.response
.status
= e
.http_code
.value
412 return self
._format
_out
("Welcome to OSM!", token_info
)
415 def version(self
, *args
, **kwargs
):
416 # TODO consider to remove and provide version using the static version file
418 if cherrypy
.request
.method
!= "GET":
420 "Only method GET is allowed",
421 HTTPStatus
.METHOD_NOT_ALLOWED
,
425 "Invalid URL or query string for version",
426 HTTPStatus
.METHOD_NOT_ALLOWED
,
429 # TODO include version of other modules, pick up from some kafka admin message
430 osm_ng_ro_version
= {"version": ro_version
, "date": ro_version_date
}
432 return self
._format
_out
(osm_ng_ro_version
)
433 except RoException
as e
:
434 cherrypy
.response
.status
= e
.http_code
.value
436 "code": e
.http_code
.name
,
437 "status": e
.http_code
.value
,
441 return self
._format
_out
(problem_details
, None)
443 def new_token(self
, engine_session
, indata
, *args
, **kwargs
):
447 token_info
= self
.authenticator
.authorize()
452 indata
.update(kwargs
)
454 # This is needed to log the user when authentication fails
455 cherrypy
.request
.login
= "{}".format(indata
.get("username", "-"))
456 token_info
= self
.authenticator
.new_token(
457 token_info
, indata
, cherrypy
.request
.remote
459 cherrypy
.session
["Authorization"] = token_info
["id"]
460 self
._set
_location
_header
("admin", "v1", "tokens", token_info
["id"])
463 # cherrypy.response.cookie["Authorization"] = outdata["id"]
464 # cherrypy.response.cookie["Authorization"]['expires'] = 3600
466 return token_info
, token_info
["id"], True
468 def del_token(self
, engine_session
, indata
, version
, _id
, *args
, **kwargs
):
471 if not token_id
and "id" in kwargs
:
472 token_id
= kwargs
["id"]
474 token_info
= self
.authenticator
.authorize()
476 token_id
= token_info
["id"]
478 self
.authenticator
.del_token(token_id
)
480 cherrypy
.session
["Authorization"] = "logout"
481 # cherrypy.response.cookie["Authorization"] = token_id
482 # cherrypy.response.cookie["Authorization"]['expires'] = 0
484 return None, None, True
487 def test(self
, *args
, **kwargs
):
488 if not cherrypy
.config
.get("server.enable_test") or (
489 isinstance(cherrypy
.config
["server.enable_test"], str)
490 and cherrypy
.config
["server.enable_test"].lower() == "false"
492 cherrypy
.response
.status
= HTTPStatus
.METHOD_NOT_ALLOWED
.value
494 return "test URL is disabled"
498 if args
and args
[0] == "help":
500 "<html><pre>\ninit\nfile/<name> download file\ndb-clear/table\nfs-clear[/folder]\nlogin\nlogin2\n"
501 "sleep/<time>\nmessage/topic\n</pre></html>"
503 elif args
and args
[0] == "init":
505 # self.ns.load_dbase(cherrypy.request.app.config)
506 self
.ns
.create_admin()
508 return "Done. User 'admin', password 'admin' created"
510 cherrypy
.response
.status
= HTTPStatus
.FORBIDDEN
.value
512 return self
._format
_out
("Database already initialized")
513 elif args
and args
[0] == "file":
514 return cherrypy
.lib
.static
.serve_file(
515 cherrypy
.tree
.apps
["/ro"].config
["storage"]["path"] + "/" + args
[1],
519 elif args
and args
[0] == "file2":
520 f_path
= cherrypy
.tree
.apps
["/ro"].config
["storage"]["path"] + "/" + args
[1]
521 f
= open(f_path
, "r")
522 cherrypy
.response
.headers
["Content-type"] = "text/plain"
525 elif len(args
) == 2 and args
[0] == "db-clear":
526 deleted_info
= self
.ns
.db
.del_list(args
[1], kwargs
)
527 return "{} {} deleted\n".format(deleted_info
["deleted"], args
[1])
528 elif len(args
) and args
[0] == "fs-clear":
532 folders
= self
.ns
.fs
.dir_ls(".")
534 for folder
in folders
:
535 self
.ns
.fs
.file_delete(folder
)
537 return ",".join(folders
) + " folders deleted\n"
538 elif args
and args
[0] == "login":
539 if not cherrypy
.request
.headers
.get("Authorization"):
540 cherrypy
.response
.headers
[
542 ] = 'Basic realm="Access to OSM site", charset="UTF-8"'
543 cherrypy
.response
.status
= HTTPStatus
.UNAUTHORIZED
.value
544 elif args
and args
[0] == "login2":
545 if not cherrypy
.request
.headers
.get("Authorization"):
546 cherrypy
.response
.headers
[
548 ] = 'Bearer realm="Access to OSM site"'
549 cherrypy
.response
.status
= HTTPStatus
.UNAUTHORIZED
.value
550 elif args
and args
[0] == "sleep":
554 sleep_time
= int(args
[1])
556 cherrypy
.response
.status
= HTTPStatus
.FORBIDDEN
.value
557 return self
._format
_out
("Database already initialized")
559 thread_info
= cherrypy
.thread_data
561 time
.sleep(sleep_time
)
563 elif len(args
) >= 2 and args
[0] == "message":
565 return_text
= "<html><pre>{} ->\n".format(main_topic
)
568 if cherrypy
.request
.method
== "POST":
569 to_send
= yaml
.safe_load(cherrypy
.request
.body
)
570 for k
, v
in to_send
.items():
571 self
.ns
.msg
.write(main_topic
, k
, v
)
572 return_text
+= " {}: {}\n".format(k
, v
)
573 elif cherrypy
.request
.method
== "GET":
574 for k
, v
in kwargs
.items():
575 self
.ns
.msg
.write(main_topic
, k
, yaml
.safe_load(v
))
576 return_text
+= " {}: {}\n".format(k
, yaml
.safe_load(v
))
577 except Exception as e
:
578 return_text
+= "Error: " + str(e
)
580 return_text
+= "</pre></html>\n"
585 "<html><pre>\nheaders:\n args: {}\n".format(args
)
586 + " kwargs: {}\n".format(kwargs
)
587 + " headers: {}\n".format(cherrypy
.request
.headers
)
588 + " path_info: {}\n".format(cherrypy
.request
.path_info
)
589 + " query_string: {}\n".format(cherrypy
.request
.query_string
)
590 + " session: {}\n".format(cherrypy
.session
)
591 + " cookie: {}\n".format(cherrypy
.request
.cookie
)
592 + " method: {}\n".format(cherrypy
.request
.method
)
593 + " session: {}\n".format(cherrypy
.session
.get("fieldname"))
596 return_text
+= " length: {}\n".format(cherrypy
.request
.body
.length
)
598 if cherrypy
.request
.body
.length
:
599 return_text
+= " content: {}\n".format(
601 cherrypy
.request
.body
.read(
602 int(cherrypy
.request
.headers
.get("Content-Length", 0))
608 return_text
+= "thread: {}\n".format(thread_info
)
610 return_text
+= "</pre></html>"
615 def _check_valid_url_method(method
, *args
):
618 "URL must contain at least 'main_topic/version/topic'",
619 HTTPStatus
.METHOD_NOT_ALLOWED
,
622 reference
= valid_url_methods
627 if not isinstance(reference
, dict):
629 "URL contains unexpected extra items '{}'".format(arg
),
630 HTTPStatus
.METHOD_NOT_ALLOWED
,
634 reference
= reference
[arg
]
635 elif "<ID>" in reference
:
636 reference
= reference
["<ID>"]
637 elif "*" in reference
:
638 # reference = reference["*"]
642 "Unexpected URL item {}".format(arg
),
643 HTTPStatus
.METHOD_NOT_ALLOWED
,
646 if "TODO" in reference
and method
in reference
["TODO"]:
648 "Method {} not supported yet for this URL".format(method
),
649 HTTPStatus
.NOT_IMPLEMENTED
,
651 elif "METHODS" not in reference
or method
not in reference
["METHODS"]:
653 "Method {} not supported for this URL".format(method
),
654 HTTPStatus
.METHOD_NOT_ALLOWED
,
657 return reference
["ROLE_PERMISSION"] + method
.lower()
660 def _set_location_header(main_topic
, version
, topic
, id):
662 Insert response header Location with the URL of created item base on URL params
669 # Use cherrypy.request.base for absoluted path and make use of request.header HOST just in case behind aNAT
670 cherrypy
.response
.headers
["Location"] = "/ro/{}/{}/{}/{}".format(
671 main_topic
, version
, topic
, id
692 engine_session
= None
695 if not main_topic
or not version
or not topic
:
697 "URL must contain at least 'main_topic/version/topic'",
698 HTTPStatus
.METHOD_NOT_ALLOWED
,
701 if main_topic
not in (
706 "URL main_topic '{}' not supported".format(main_topic
),
707 HTTPStatus
.METHOD_NOT_ALLOWED
,
712 "URL version '{}' not supported".format(version
),
713 HTTPStatus
.METHOD_NOT_ALLOWED
,
718 and "METHOD" in kwargs
719 and kwargs
["METHOD"] in ("PUT", "POST", "DELETE", "GET", "PATCH")
721 method
= kwargs
.pop("METHOD")
723 method
= cherrypy
.request
.method
725 role_permission
= self
._check
_valid
_url
_method
(
726 method
, main_topic
, version
, topic
, _id
, _id2
, *args
, **kwargs
728 # skip token validation if requesting a token
729 indata
= self
._format
_in
(kwargs
)
731 if main_topic
!= "admin" or topic
!= "tokens":
732 token_info
= self
.authenticator
.authorize(role_permission
, _id
)
734 outdata
, created_id
, done
= self
.map_operation
[role_permission
](
735 engine_session
, indata
, version
, _id
, _id2
, *args
, *kwargs
739 self
._set
_location
_header
(main_topic
, version
, topic
, _id
)
741 cherrypy
.response
.status
= (
742 HTTPStatus
.ACCEPTED
.value
744 else HTTPStatus
.OK
.value
745 if outdata
is not None
746 else HTTPStatus
.NO_CONTENT
.value
749 return self
._format
_out
(outdata
, token_info
, _format
)
750 except Exception as e
:
763 http_code_value
= cherrypy
.response
.status
= e
.http_code
.value
764 http_code_name
= e
.http_code
.name
765 cherrypy
.log("Exception {}".format(e
))
768 cherrypy
.response
.status
769 ) = HTTPStatus
.BAD_REQUEST
.value
# INTERNAL_SERVER_ERROR
770 cherrypy
.log("CRITICAL: Exception {}".format(e
), traceback
=True)
771 http_code_name
= HTTPStatus
.BAD_REQUEST
.name
773 if hasattr(outdata
, "close"): # is an open file
779 for rollback_item
in rollback
:
781 if rollback_item
.get("operation") == "set":
783 rollback_item
["topic"],
784 {"_id": rollback_item
["_id"]},
785 rollback_item
["content"],
790 rollback_item
["topic"],
791 {"_id": rollback_item
["_id"]},
794 except Exception as e2
:
795 rollback_error_text
= "Rollback Exception {}: {}".format(
798 cherrypy
.log(rollback_error_text
)
799 error_text
+= ". " + rollback_error_text
801 # if isinstance(e, MsgException):
802 # error_text = "{} has been '{}' but other modules cannot be informed because an error on bus".format(
803 # engine_topic[:-1], method, error_text)
805 "code": http_code_name
,
806 "status": http_code_value
,
807 "detail": error_text
,
810 return self
._format
_out
(problem_details
, token_info
)
811 # raise cherrypy.HTTPError(e.http_code.value, str(e))
814 if method
in ("PUT", "PATCH", "POST") and isinstance(outdata
, dict):
815 for logging_id
in ("id", "op_id", "nsilcmop_id", "nslcmop_id"):
816 if outdata
.get(logging_id
):
817 cherrypy
.request
.login
+= ";{}={}".format(
818 logging_id
, outdata
[logging_id
][:36]
822 def _start_service():
824 Callback function called when cherrypy.engine starts
825 Override configuration with env variables
826 Set database, storage, message configuration
827 Init database with admin/admin user password
829 global ro_server
, vim_admin_thread
831 cherrypy
.log
.error("Starting osm_ng_ro")
832 # update general cherrypy configuration
834 engine_config
= cherrypy
.tree
.apps
["/ro"].config
836 for k
, v
in environ
.items():
837 if not k
.startswith("OSMRO_"):
840 k1
, _
, k2
= k
[6:].lower().partition("_")
846 if k1
in ("server", "test", "auth", "log"):
847 # update [global] configuration
848 update_dict
[k1
+ "." + k2
] = yaml
.safe_load(v
)
850 # update [/static] configuration
851 engine_config
["/static"]["tools.staticdir." + k2
] = yaml
.safe_load(v
)
853 # update [/] configuration
854 engine_config
["/"]["tools." + k2
.replace("_", ".")] = yaml
.safe_load(v
)
855 elif k1
in ("message", "database", "storage", "authentication", "period"):
856 engine_config
[k1
][k2
] = yaml
.safe_load(v
)
858 except Exception as e
:
859 raise RoException("Cannot load env '{}': {}".format(k
, e
))
862 cherrypy
.config
.update(update_dict
)
863 engine_config
["global"].update(update_dict
)
866 log_format_simple
= (
867 "%(asctime)s %(levelname)s %(name)s %(filename)s:%(lineno)s %(message)s"
869 log_formatter_simple
= logging
.Formatter(
870 log_format_simple
, datefmt
="%Y-%m-%dT%H:%M:%S"
872 logger_server
= logging
.getLogger("cherrypy.error")
873 logger_access
= logging
.getLogger("cherrypy.access")
874 logger_cherry
= logging
.getLogger("cherrypy")
875 logger
= logging
.getLogger("ro")
877 if "log.file" in engine_config
["global"]:
878 file_handler
= logging
.handlers
.RotatingFileHandler(
879 engine_config
["global"]["log.file"], maxBytes
=100e6
, backupCount
=9, delay
=0
881 file_handler
.setFormatter(log_formatter_simple
)
882 logger_cherry
.addHandler(file_handler
)
883 logger
.addHandler(file_handler
)
885 # log always to standard output
886 for format_
, logger
in {
887 "ro.server %(filename)s:%(lineno)s": logger_server
,
888 "ro.access %(filename)s:%(lineno)s": logger_access
,
889 "%(name)s %(filename)s:%(lineno)s": logger
,
891 log_format_cherry
= "%(asctime)s %(levelname)s {} %(message)s".format(format_
)
892 log_formatter_cherry
= logging
.Formatter(
893 log_format_cherry
, datefmt
="%Y-%m-%dT%H:%M:%S"
895 str_handler
= logging
.StreamHandler()
896 str_handler
.setFormatter(log_formatter_cherry
)
897 logger
.addHandler(str_handler
)
899 if engine_config
["global"].get("log.level"):
900 logger_cherry
.setLevel(engine_config
["global"]["log.level"])
901 logger
.setLevel(engine_config
["global"]["log.level"])
903 # logging other modules
909 engine_config
[k1
]["logger_name"] = logname
910 logger_module
= logging
.getLogger(logname
)
912 if "logfile" in engine_config
[k1
]:
913 file_handler
= logging
.handlers
.RotatingFileHandler(
914 engine_config
[k1
]["logfile"], maxBytes
=100e6
, backupCount
=9, delay
=0
916 file_handler
.setFormatter(log_formatter_simple
)
917 logger_module
.addHandler(file_handler
)
919 if "loglevel" in engine_config
[k1
]:
920 logger_module
.setLevel(engine_config
[k1
]["loglevel"])
921 # TODO add more entries, e.g.: storage
923 engine_config
["assignment"] = {}
924 # ^ each VIM, SDNc will be assigned one worker id. Ns class will add items and VimThread will auto-assign
925 cherrypy
.tree
.apps
["/ro"].root
.ns
.start(engine_config
)
926 cherrypy
.tree
.apps
["/ro"].root
.authenticator
.start(engine_config
)
927 cherrypy
.tree
.apps
["/ro"].root
.ns
.init_db(target_version
=database_version
)
929 # # start subscriptions thread:
930 vim_admin_thread
= VimAdminThread(config
=engine_config
, engine
=ro_server
.ns
)
931 vim_admin_thread
.start()
932 # # Do not capture except SubscriptionException
934 # backend = engine_config["authentication"]["backend"]
935 # cherrypy.log.error("Starting OSM NBI Version '{} {}' with '{}' authentication backend"
936 # .format(ro_version, ro_version_date, backend))
941 Callback function called when cherrypy.engine stops
942 TODO: Ending database connections.
944 global vim_admin_thread
946 # terminate vim_admin_thread
948 vim_admin_thread
.terminate()
950 vim_admin_thread
= None
951 cherrypy
.tree
.apps
["/ro"].root
.ns
.stop()
952 cherrypy
.log
.error("Stopping osm_ng_ro")
955 def ro_main(config_file
):
959 cherrypy
.engine
.subscribe("start", _start_service
)
960 cherrypy
.engine
.subscribe("stop", _stop_service
)
961 cherrypy
.quickstart(ro_server
, "/ro", config_file
)
966 """Usage: {} [options]
967 -c|--config [configuration_file]: loads the configuration file (default: ./ro.cfg)
968 -h|--help: shows this help
973 # --log-socket-host HOST: send logs to this host")
974 # --log-socket-port PORT: send logs using this port (default: 9022)")
977 if __name__
== "__main__":
979 # load parameters and configuration
980 opts
, args
= getopt
.getopt(sys
.argv
[1:], "hvc:", ["config=", "help"])
981 # TODO add "log-socket-host=", "log-socket-port=", "log-file="
985 if o
in ("-h", "--help"):
988 elif o
in ("-c", "--config"):
991 raise ValueError("Unhandled option")
994 if not path
.isfile(config_file
):
996 "configuration file '{}' that not exist".format(config_file
),
1001 for config_file
in (
1002 path
.dirname(__file__
) + "/ro.cfg",
1006 if path
.isfile(config_file
):
1010 "No configuration file 'ro.cfg' found neither at local folder nor at /etc/osm/",
1015 ro_main(config_file
)
1016 except KeyboardInterrupt:
1017 print("KeyboardInterrupt. Finishing", file=sys
.stderr
)
1018 except getopt
.GetoptError
as e
:
1019 print(str(e
), file=sys
.stderr
)