Refactor code layout
[pti/o2.git] / o2app / entrypoints / resource_watcher.py
1 # Copyright (C) 2021 Wind River Systems, Inc.\r
2 #\r
3 #  Licensed under the Apache License, Version 2.0 (the "License");\r
4 #  you may not use this file except in compliance with the License.\r
5 #  You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 #  Unless required by applicable law or agreed to in writing, software\r
10 #  distributed under the License is distributed on an "AS IS" BASIS,\r
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 #  See the License for the specific language governing permissions and\r
13 #  limitations under the License.\r
14 \r
15 import cotyledon\r
16 \r
17 from o2app import bootstrap\r
18 from o2common.service.watcher.base import WatcherTree\r
19 from o2common.service.watcher.worker import PollWorker\r
20 \r
21 from o2ims.service.watcher.ocloud_watcher import OcloudWatcher\r
22 from o2ims.service.watcher.ocloud_watcher import DmsWatcher\r
23 from o2ims.service.watcher.resourcepool_watcher import ResourcePoolWatcher\r
24 from o2ims.adapter.clients.ocloud_sa_client import StxSaDmsClient\r
25 from o2ims.adapter.clients.ocloud_sa_client import StxSaOcloudClient\r
26 from o2ims.adapter.clients.ocloud_sa_client import StxSaResourcePoolClient\r
27 \r
28 from o2ims.service.watcher.pserver_watcher import PServerWatcher\r
29 from o2ims.adapter.clients.ocloud_sa_client import StxPserverClient\r
30 \r
31 from o2ims.service.watcher.pserver_cpu_watcher import PServerCpuWatcher\r
32 from o2ims.adapter.clients.ocloud_sa_client import StxCpuClient\r
33 \r
34 from o2common.helper import o2logging\r
35 logger = o2logging.get_logger(__name__)\r
36 \r
37 # r = redis.Redis(**config.get_redis_host_and_port())\r
38 \r
39 \r
40 class WatcherService(cotyledon.Service):\r
41     def __init__(self, worker_id, args=None) -> None:\r
42         super().__init__(worker_id)\r
43         self.args = args\r
44         self.bus = bootstrap.bootstrap()\r
45         self.worker = PollWorker()\r
46 \r
47     def run(self):\r
48         try:\r
49             root = WatcherTree(OcloudWatcher(\r
50                 StxSaOcloudClient(), self.bus))\r
51             root.addchild(\r
52                 DmsWatcher(StxSaDmsClient(), self.bus))\r
53 \r
54             child_respool = root.addchild(\r
55                 ResourcePoolWatcher(StxSaResourcePoolClient(),\r
56                                     self.bus))\r
57             child_pserver = child_respool.addchild(\r
58                 PServerWatcher(StxPserverClient(), self.bus))\r
59             child_pserver.addchild(\r
60                 PServerCpuWatcher(StxCpuClient(), self.bus))\r
61 \r
62             self.worker.add_watcher(root)\r
63 \r
64             self.worker.start()\r
65         except Exception as ex:\r
66             logger.warning("WorkerService Exception:" + str(ex))\r
67         finally:\r
68             self.worker.stop()\r
69 \r
70 \r
71 def start_watchers(sm: cotyledon.ServiceManager = None):\r
72     watchersm = sm if sm else cotyledon.ServiceManager()\r
73     watchersm.add(WatcherService, workers=1, args=())\r
74     watchersm.run()\r
75 \r
76 \r
77 def main():\r
78     logger.info("Resource watcher starting")\r
79     start_watchers()\r
80 \r
81 \r
82 if __name__ == "__main__":\r
83     main()\r