alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 1 | # 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 Shatov | f53e5e7 | 2018-01-11 11:15:56 -0500 | [diff] [blame] | 19 | |
| 20 | """run as server: python -m policyhandler/policy_handler""" |
| 21 | |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 22 | import os |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 23 | import sys |
| 24 | import logging |
| 25 | |
| 26 | from policyhandler.config import Config |
| 27 | from policyhandler.onap.audit import Audit |
| 28 | from policyhandler.web_server import PolicyWeb |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 29 | from policyhandler.policy_receiver import PolicyReceiver |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 30 | |
| 31 | class 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 | |
| 46 | def 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 Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 56 | 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_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 59 | |
| 60 | logger.info("starting policy_handler with config:") |
| 61 | logger.info(Audit.log_json_dumps(Config.config)) |
| 62 | |
Alex Shatov | 1369bea | 2018-01-10 11:00:50 -0500 | [diff] [blame] | 63 | audit = Audit(req_message="start policy handler") |
| 64 | PolicyReceiver.run(audit) |
| 65 | PolicyWeb.run_forever(audit) |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 66 | |
alex_sh | 9d980ce | 2017-08-23 17:30:56 -0400 | [diff] [blame] | 67 | if __name__ == "__main__": |
Alex Shatov | 42a989a | 2017-09-12 13:00:25 -0400 | [diff] [blame] | 68 | run_policy_handler() |