Merge "Refactor and bug fixes"
[dcaegen2/services.git] / components / pm-subscription-handler / pmsh_service / pmsh_service_main.py
1 # ============LICENSE_START===================================================
2 #  Copyright (C) 2019-2020 Nordix Foundation.
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 #
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
18 import sys
19 from signal import signal, SIGTERM
20
21 from mod import db, create_app, launch_api_server, logger
22 from mod.aai_event_handler import process_aai_events
23 from mod.exit_handler import ExitHandler
24 from mod.pmsh_utils import AppConfig, PeriodicTask
25 from mod.policy_response_handler import PolicyResponseHandler
26 from mod.subscription_handler import SubscriptionHandler
27
28
29 def main():
30     try:
31         try:
32             app = create_app()
33             app.app_context().push()
34             db.create_all(app=app)
35             app_conf = AppConfig()
36             policy_mr_pub = app_conf.get_mr_pub('policy_pm_publisher')
37             policy_mr_sub = app_conf.get_mr_sub('policy_pm_subscriber')
38             aai_event_mr_sub = app_conf.get_mr_sub('aai_subscriber')
39         except Exception as e:
40             logger.error(f'Failed to get config and create application: {e}', exc_info=True)
41             sys.exit(e)
42
43         app_conf_thread = PeriodicTask(10, app_conf.refresh_config)
44         app_conf_thread.start()
45
46         policy_response_handler = PolicyResponseHandler(policy_mr_sub, app_conf, app)
47         policy_response_handler_thread = PeriodicTask(25, policy_response_handler.poll_policy_topic)
48
49         aai_event_thread = PeriodicTask(20, process_aai_events,
50                                         args=(aai_event_mr_sub, policy_mr_pub, app, app_conf))
51
52         subscription_handler = SubscriptionHandler(policy_mr_pub, app, app_conf, aai_event_thread,
53                                                    policy_response_handler_thread)
54
55         subscription_handler_thread = PeriodicTask(30, subscription_handler.execute)
56         subscription_handler_thread.start()
57
58         periodic_tasks = [app_conf_thread, aai_event_thread, subscription_handler_thread,
59                           policy_response_handler_thread]
60
61         signal(SIGTERM, ExitHandler(periodic_tasks=periodic_tasks,
62                                     app_conf=app_conf, subscription_handler=subscription_handler))
63         launch_api_server(app_conf)
64
65     except Exception as e:
66         logger.error(f'Failed to initialise PMSH: {e}', exc_info=True)
67         sys.exit(e)
68
69
70 if __name__ == '__main__':
71     main()