99d5595d209ba41e4354b718137b6155a8b847d6
[pti/o2.git] / o2app / entrypoints / resource_watcher.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 import cotyledon
16
17 from o2app import bootstrap
18 from o2common.service.watcher.base import WatcherTree
19 from o2common.service.watcher.worker import PollWorker
20
21 from o2ims.service.watcher.ocloud_watcher import OcloudWatcher
22 from o2ims.service.watcher.ocloud_watcher import DmsWatcher
23 from o2ims.service.watcher.resourcepool_watcher import ResourcePoolWatcher
24 from o2ims.service.watcher.alarm_watcher import AlarmWatcher
25
26 from o2ims.adapter.clients.ocloud_client import StxDmsClient
27 from o2ims.adapter.clients.ocloud_client import StxOcloudClient
28 from o2ims.adapter.clients.ocloud_client import StxResourcePoolClient
29 from o2ims.adapter.clients.fault_client import StxAlarmClient
30
31 from o2ims.service.watcher.pserver_watcher import PServerWatcher
32 from o2ims.adapter.clients.ocloud_client import StxPserverClient
33
34 from o2ims.service.watcher.pserver_cpu_watcher import PServerCpuWatcher
35 from o2ims.adapter.clients.ocloud_client import StxCpuClient
36
37 from o2ims.service.watcher.pserver_mem_watcher import PServerMemWatcher
38 from o2ims.adapter.clients.ocloud_client import StxMemClient
39
40 from o2ims.service.watcher.pserver_if_watcher import PServerIfWatcher
41 from o2ims.adapter.clients.ocloud_client import StxIfClient
42
43 from o2ims.service.watcher.pserver_port_watcher import PServerIfPortWatcher
44 from o2ims.adapter.clients.ocloud_client import StxIfPortClient
45
46 from o2ims.service.watcher.pserver_eth_watcher import PServerEthWatcher
47 from o2ims.adapter.clients.ocloud_client import StxEthClient
48
49 from o2common.helper import o2logging
50 logger = o2logging.get_logger(__name__)
51
52 # r = redis.Redis(**config.get_redis_host_and_port())
53
54
55 class WatcherService(cotyledon.Service):
56     def __init__(self, worker_id, args=None) -> None:
57         super().__init__(worker_id)
58         self.args = args
59         self.bus = bootstrap.bootstrap()
60         self.worker = PollWorker(bus=self.bus)
61
62     def run(self):
63         try:
64             root = WatcherTree(OcloudWatcher(
65                 StxOcloudClient(), self.bus))
66             root.addchild(
67                 DmsWatcher(StxDmsClient(), self.bus))
68             # root.addchild(
69             #     AlarmWatcher(StxFaultClient(), self.bus))
70
71             child_respool = root.addchild(
72                 ResourcePoolWatcher(StxResourcePoolClient(),
73                                     self.bus))
74             child_pserver = child_respool.addchild(
75                 PServerWatcher(StxPserverClient(), self.bus))
76             child_pserver.addchild(
77                 PServerCpuWatcher(StxCpuClient(), self.bus))
78             child_pserver.addchild(
79                 PServerMemWatcher(StxMemClient(), self.bus))
80             child_pserver.addchild(
81                 PServerEthWatcher(StxEthClient(), self.bus))
82             child_if = child_pserver.addchild(
83                 PServerIfWatcher(StxIfClient(), self.bus))
84             # child_if.addchild(
85             #     PServerIfPortWatcher(StxIfPortClient(), self.bus))
86
87             self.worker.add_watcher(root)
88
89             # Add Alarm watch
90             root = WatcherTree(
91                 AlarmWatcher(StxAlarmClient(self.bus.uow), self.bus))
92             self.worker.add_watcher(root)
93
94             self.worker.start()
95         except Exception as ex:
96             logger.warning("WorkerService Exception:" + str(ex))
97         finally:
98             self.worker.stop()
99
100
101 def start_watchers(sm: cotyledon.ServiceManager = None):
102     watchersm = sm if sm else cotyledon.ServiceManager()
103     watchersm.add(WatcherService, workers=1, args=())
104     watchersm.run()
105
106
107 def main():
108     logger.info("Resource watcher starting")
109     start_watchers()
110
111
112 if __name__ == "__main__":
113     main()