Convert file endlines to Unix (LF)
[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.adapter.clients.ocloud_sa_client import StxSaDmsClient
25 from o2ims.adapter.clients.ocloud_sa_client import StxSaOcloudClient
26 from o2ims.adapter.clients.ocloud_sa_client import StxSaResourcePoolClient
27
28 from o2ims.service.watcher.pserver_watcher import PServerWatcher
29 from o2ims.adapter.clients.ocloud_sa_client import StxPserverClient
30
31 from o2ims.service.watcher.pserver_cpu_watcher import PServerCpuWatcher
32 from o2ims.adapter.clients.ocloud_sa_client import StxCpuClient
33
34 from o2ims.service.watcher.pserver_mem_watcher import PServerMemWatcher
35 from o2ims.adapter.clients.ocloud_sa_client import StxMemClient
36
37 from o2ims.service.watcher.pserver_if_watcher import PServerIfWatcher
38 from o2ims.adapter.clients.ocloud_sa_client import StxIfClient
39
40 from o2ims.service.watcher.pserver_port_watcher import PServerIfPortWatcher
41 from o2ims.adapter.clients.ocloud_sa_client import StxIfPortClient
42
43 from o2ims.service.watcher.pserver_eth_watcher import PServerEthWatcher
44 from o2ims.adapter.clients.ocloud_sa_client import StxEthClient
45
46 from o2common.helper import o2logging
47 logger = o2logging.get_logger(__name__)
48
49 # r = redis.Redis(**config.get_redis_host_and_port())
50
51
52 class WatcherService(cotyledon.Service):
53     def __init__(self, worker_id, args=None) -> None:
54         super().__init__(worker_id)
55         self.args = args
56         self.bus = bootstrap.bootstrap()
57         self.worker = PollWorker(bus=self.bus)
58
59     def run(self):
60         try:
61             root = WatcherTree(OcloudWatcher(
62                 StxSaOcloudClient(), self.bus))
63             root.addchild(
64                 DmsWatcher(StxSaDmsClient(), self.bus))
65
66             child_respool = root.addchild(
67                 ResourcePoolWatcher(StxSaResourcePoolClient(),
68                                     self.bus))
69             child_pserver = child_respool.addchild(
70                 PServerWatcher(StxPserverClient(), self.bus))
71             child_pserver.addchild(
72                 PServerCpuWatcher(StxCpuClient(), self.bus))
73             child_pserver.addchild(
74                 PServerMemWatcher(StxMemClient(), self.bus))
75             child_pserver.addchild(
76                 PServerEthWatcher(StxEthClient(), self.bus))
77             child_if = child_pserver.addchild(
78                 PServerIfWatcher(StxIfClient(), self.bus))
79             # child_if.addchild(
80             #     PServerIfPortWatcher(StxIfPortClient(), self.bus))
81
82             self.worker.add_watcher(root)
83
84             self.worker.start()
85         except Exception as ex:
86             logger.warning("WorkerService Exception:" + str(ex))
87         finally:
88             self.worker.stop()
89
90
91 def start_watchers(sm: cotyledon.ServiceManager = None):
92     watchersm = sm if sm else cotyledon.ServiceManager()
93     watchersm.add(WatcherService, workers=1, args=())
94     watchersm.run()
95
96
97 def main():
98     logger.info("Resource watcher starting")
99     start_watchers()
100
101
102 if __name__ == "__main__":
103     main()