59a708791a8d5e25c52f3b500464067036f86d17
[dcaegen2/platform.git] / oti / event-handler / otihandler / __main__.py
1 # ================================================================================
2 # Copyright (c) 2019-2020 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
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
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16
17 """run as server: python -m otihandler"""
18
19 import logging
20 import os
21 import sys
22
23 from otihandler.config import Config
24 from otihandler.onap.audit import Audit
25 from otihandler.web_server import DTIWeb
26 from otihandler.dbclient import DaoBase
27
28
29 class LogWriter(object):
30     """redirect the standard out + err to the logger"""
31
32     def __init__(self, logger_func):
33         self.logger_func = logger_func
34
35     def write(self, log_line):
36         """actual writer to be used in place of stdout or stderr"""
37
38         log_line = log_line.rstrip()
39         if log_line:
40             self.logger_func(log_line)
41
42     def flush(self):
43         """no real flushing of the buffer"""
44
45         pass
46
47
48 def run_event_handler():
49     """main run function for event_handler"""
50
51     Config.load_from_file()
52     # Config.discover()
53
54     logger = logging.getLogger("event_handler")
55     sys.stdout = LogWriter(logger.info)
56     sys.stderr = LogWriter(logger.error)
57
58     logger.info("========== run_event_handler ==========")
59     app_version = os.getenv("APP_VER")
60     logger.info("app_version %s", app_version)
61     Audit.init(Config.get_system_name(), app_version, Config.LOGGER_CONFIG_FILE_PATH)
62
63     logger.info("starting event_handler with config:")
64     logger.info(Audit.log_json_dumps(Config.config))
65
66     audit = Audit(req_message="start event_handler")
67
68     audit = Audit(req_message="DB init start")
69     DaoBase.init_db(os.environ.get("DB_CONN_URL"))
70
71     DTIWeb.run_forever(audit)
72
73 if __name__ == "__main__":
74     run_event_handler()