Adjust API level on swagger and set API doc expansion by default
[pti/o2.git] / o2ims / bootstrap.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from retry import retry
16 import inspect
17 from typing import Callable
18 from o2ims.adapter import orm, redis_eventpublisher
19 from o2ims.adapter.notifications import AbstractNotifications,\
20     SmoO2Notifications
21
22 from o2ims.service import handlers, messagebus, unit_of_work
23 from o2ims.adapter.unit_of_work import SqlAlchemyUnitOfWork
24 from o2ims.adapter.clients import orm_stx
25 from o2common.helper import o2logging
26 logger = o2logging.get_logger(__name__)
27
28
29 @retry(tries=100, delay=2, backoff=1)
30 def wait_for_db_ready(engine):
31     # wait for db up
32     logger.info("Wait for DB ready ...")
33     engine.connect()
34     logger.info("DB is ready")
35
36
37 def bootstrap(
38     start_orm: bool = True,
39     uow: unit_of_work.AbstractUnitOfWork = SqlAlchemyUnitOfWork(),
40     notifications: AbstractNotifications = None,
41     publish: Callable = redis_eventpublisher.publish,
42 ) -> messagebus.MessageBus:
43
44     if notifications is None:
45         notifications = SmoO2Notifications()
46
47     if start_orm:
48         with uow:
49             # get default engine if uow is by default
50             engine = uow.session.get_bind()
51             wait_for_db_ready(engine)
52             orm.start_o2ims_mappers(engine)
53
54         orm_stx.start_o2ims_stx_mappers(uow)
55
56     dependencies = {"uow": uow, "notifications": notifications,
57                     "publish": publish}
58     injected_event_handlers = {
59         event_type: [
60             inject_dependencies(handler, dependencies)
61             for handler in event_handlers
62         ]
63         for event_type, event_handlers in handlers.EVENT_HANDLERS.items()
64     }
65     injected_command_handlers = {
66         command_type: inject_dependencies(handler, dependencies)
67         for command_type, handler in handlers.COMMAND_HANDLERS.items()
68     }
69
70     return messagebus.MessageBus(
71         uow=uow,
72         event_handlers=injected_event_handlers,
73         command_handlers=injected_command_handlers,
74     )
75
76
77 def inject_dependencies(handler, dependencies):
78     params = inspect.signature(handler).parameters
79     deps = {
80         name: dependency
81         for name, dependency in dependencies.items()
82         if name in params
83     }
84     return lambda message: handler(message, **deps)