blob: f838d34f2b68ef5d73ca6a3d536dd8cda84105b0 [file] [log] [blame]
alex_sh9d980ce2017-08-23 17:30:56 -04001# org.onap.dcae
2# ================================================================================
3# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
4# ================================================================================
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16# ============LICENSE_END=========================================================
17#
18# ECOMP is a trademark and service mark of AT&T Intellectual Property.
Alex Shatovf53e5e72018-01-11 11:15:56 -050019
20"""run as server: python -m policyhandler/policy_handler"""
21
Alex Shatov1369bea2018-01-10 11:00:50 -050022import os
alex_sh9d980ce2017-08-23 17:30:56 -040023import sys
24import logging
25
26from policyhandler.config import Config
27from policyhandler.onap.audit import Audit
28from policyhandler.web_server import PolicyWeb
Alex Shatov1369bea2018-01-10 11:00:50 -050029from policyhandler.policy_receiver import PolicyReceiver
alex_sh9d980ce2017-08-23 17:30:56 -040030
31class LogWriter(object):
32 """redirect the standard out + err to the logger"""
33 def __init__(self, logger_func):
34 self.logger_func = logger_func
35
36 def write(self, log_line):
37 """actual writer to be used in place of stdout or stderr"""
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 pass
45
46def run_policy_handler():
47 """main run function for policy-handler"""
48 Config.load_from_file()
49 Config.discover()
50
51 logger = logging.getLogger("policy_handler")
52 sys.stdout = LogWriter(logger.info)
53 sys.stderr = LogWriter(logger.error)
54
55 logger.info("========== run_policy_handler ==========")
Alex Shatov1369bea2018-01-10 11:00:50 -050056 policy_handler_version = os.getenv("APP_VER")
57 logger.info("policy_handler_version %s", policy_handler_version)
58 Audit.init(Config.get_system_name(), policy_handler_version, Config.LOGGER_CONFIG_FILE_PATH)
alex_sh9d980ce2017-08-23 17:30:56 -040059
60 logger.info("starting policy_handler with config:")
61 logger.info(Audit.log_json_dumps(Config.config))
62
Alex Shatov1369bea2018-01-10 11:00:50 -050063 audit = Audit(req_message="start policy handler")
64 PolicyReceiver.run(audit)
65 PolicyWeb.run_forever(audit)
alex_sh9d980ce2017-08-23 17:30:56 -040066
alex_sh9d980ce2017-08-23 17:30:56 -040067if __name__ == "__main__":
Alex Shatov42a989a2017-09-12 13:00:25 -040068 run_policy_handler()