1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# ============LICENSE_START===================================================
# Copyright (C) 2019-2020 Nordix Foundation.
# ============================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=====================================================
import sys
from signal import signal, SIGTERM
import mod.aai_client as aai
import mod.pmsh_logging as logger
from mod import db, create_app, launch_api_server
from mod.aai_event_handler import process_aai_events
from mod.config_handler import ConfigHandler
from mod.exit_handler import ExitHandler
from mod.pmsh_utils import AppConfig, PeriodicTask
from mod.policy_response_handler import PolicyResponseHandler
from mod.subscription import Subscription, AdministrativeState
from mod.subscription_handler import SubscriptionHandler
def main():
try:
config_handler = ConfigHandler()
config = config_handler.get_config()
app_conf = AppConfig(**config['config'])
app = create_app()
app.app_context().push()
db.create_all(app=app)
sub, nfs = aai.get_pmsh_subscription_data(config)
policy_mr_pub = app_conf.get_mr_pub('policy_pm_publisher')
policy_mr_sub = app_conf.get_mr_sub('policy_pm_subscriber')
mr_aai_event_sub = app_conf.get_mr_sub('aai_subscriber')
subscription_in_db = Subscription.get(sub.subscriptionName)
administrative_state = subscription_in_db.status if subscription_in_db \
else AdministrativeState.LOCKED.value
aai_event_thread = PeriodicTask(10, process_aai_events,
args=(mr_aai_event_sub,
sub, policy_mr_pub, app, app_conf))
subscription_handler = SubscriptionHandler(config_handler, administrative_state,
policy_mr_pub, app, app_conf, aai_event_thread)
policy_response_handler = PolicyResponseHandler(policy_mr_sub, sub.subscriptionName, app)
subscription_handler_thread = PeriodicTask(30, subscription_handler.execute)
policy_response_handler_thread = PeriodicTask(5, policy_response_handler.poll_policy_topic)
subscription_handler_thread.start()
policy_response_handler_thread.start()
periodic_tasks = [aai_event_thread, subscription_handler_thread,
policy_response_handler_thread]
signal(SIGTERM, ExitHandler(periodic_tasks=periodic_tasks,
subscription_handler=subscription_handler))
launch_api_server(app_conf)
except Exception as e:
logger.debug(f'Failed to Init PMSH: {e}')
sys.exit(e)
if __name__ == '__main__':
main()
|