Add o2dms api endpoint
[pti/o2.git] / o2app / 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
19 from o2common.adapter.notifications import AbstractNotifications,\
20     SmoO2Notifications
21 from o2common.adapter import redis_eventpublisher
22 from o2common.service import unit_of_work
23
24 from o2app.service import handlers, messagebus
25 from o2app.adapter.unit_of_work import SqlAlchemyUnitOfWork
26
27 from o2ims.adapter import orm as o2ims_orm
28 from o2dms.adapter import orm as o2dms_orm
29
30 from o2common.helper import o2logging
31 logger = o2logging.get_logger(__name__)
32
33
34 @retry(tries=100, delay=2, backoff=1)
35 def wait_for_db_ready(engine):
36     # wait for db up
37     logger.info("Wait for DB ready ...")
38     engine.connect()
39     logger.info("DB is ready")
40
41
42 def bootstrap(
43     start_orm: bool = True,
44     uow: unit_of_work.AbstractUnitOfWork = SqlAlchemyUnitOfWork(),
45     notifications: AbstractNotifications = None,
46     publish: Callable = redis_eventpublisher.publish,
47 ) -> messagebus.MessageBus:
48
49     if notifications is None:
50         notifications = SmoO2Notifications()
51
52     if start_orm:
53         with uow:
54             # get default engine if uow is by default
55             engine = uow.session.get_bind()
56             wait_for_db_ready(engine)
57             o2ims_orm.start_o2ims_mappers(engine)
58             o2dms_orm.start_o2dms_mappers(engine)
59
60     dependencies = {"uow": uow, "notifications": notifications,
61                     "publish": publish}
62     injected_event_handlers = {
63         event_type: [
64             inject_dependencies(handler, dependencies)
65             for handler in event_handlers
66         ]
67         for event_type, event_handlers in handlers.EVENT_HANDLERS.items()
68     }
69     injected_command_handlers = {
70         command_type: inject_dependencies(handler, dependencies)
71         for command_type, handler in handlers.COMMAND_HANDLERS.items()
72     }
73
74     return messagebus.MessageBus(
75         uow=uow,
76         event_handlers=injected_event_handlers,
77         command_handlers=injected_command_handlers,
78     )
79
80
81 def inject_dependencies(handler, dependencies):
82     params = inspect.signature(handler).parameters
83     deps = {
84         name: dependency
85         for name, dependency in dependencies.items()
86         if name in params
87     }
88     return lambda message: handler(message, **deps)