b5570548f0dc164bc130117f39c8844174f7f01a
[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 o2ims.service.watcher.pserver_dev_watcher import PServerDevWatcher
50 # from o2ims.adapter.clients.ocloud_client import StxDevClient
51 from o2ims.service.watcher.pserver_acc_watcher import PServerAccWatcher
52 from o2ims.adapter.clients.ocloud_client import StxAccClient
53
54 from o2ims.service.watcher.agg_compute_watcher import ComputeAggWatcher
55 from o2ims.service.watcher.agg_network_watcher import NetworkAggWatcher
56 from o2ims.service.watcher.agg_storage_watcher import StorageAggWatcher
57 from o2ims.service.watcher.agg_undefined_watcher import UndefinedAggWatcher
58 from o2ims.adapter.clients.aggregate_client import ComputeAggClient, \
59     NetworkAggClient, StorageAggClient, UndefinedAggClient
60
61 from o2common.helper import o2logging
62 logger = o2logging.get_logger(__name__)
63
64 # r = redis.Redis(**config.get_redis_host_and_port())
65
66
67 class WatcherService(cotyledon.Service):
68     def __init__(self, worker_id, args=None) -> None:
69         super().__init__(worker_id)
70         self.args = args
71         self.bus = bootstrap.bootstrap()
72         self.worker = PollWorker(bus=self.bus)
73
74     def run(self):
75         try:
76             root = WatcherTree(OcloudWatcher(
77                 StxOcloudClient(), self.bus))
78             root.addchild(
79                 DmsWatcher(StxDmsClient(), self.bus))
80             # root.addchild(
81             #     AlarmWatcher(StxFaultClient(), self.bus))
82
83             child_respool = root.addchild(
84                 ResourcePoolWatcher(StxResourcePoolClient(),
85                                     self.bus))
86
87             # Add Aggregate watch
88             child_respool.addchild(
89                 ComputeAggWatcher(ComputeAggClient(), self.bus))
90             child_respool.addchild(
91                 NetworkAggWatcher(NetworkAggClient(), self.bus))
92             child_respool.addchild(
93                 StorageAggWatcher(StorageAggClient(), self.bus))
94             child_respool.addchild(
95                 UndefinedAggWatcher(UndefinedAggClient(), self.bus))
96
97             # Add Resource watch
98             child_pserver = child_respool.addchild(
99                 PServerWatcher(StxPserverClient(), self.bus))
100             child_pserver.addchild(
101                 PServerCpuWatcher(StxCpuClient(), self.bus))
102             child_pserver.addchild(
103                 PServerMemWatcher(StxMemClient(), self.bus))
104             child_pserver.addchild(
105                 PServerEthWatcher(StxEthClient(), self.bus))
106             child_pserver.addchild(
107                 PServerIfWatcher(StxIfClient(), self.bus))
108             # child_if.addchild(
109             #     PServerIfPortWatcher(StxIfPortClient(), self.bus))
110             # child_pserver.addchild(
111             #     PServerDevWatcher(StxDevClient(), self.bus))
112             child_pserver.addchild(
113                 PServerAccWatcher(StxAccClient(), self.bus))
114
115             # Add Alarm watch
116             child_respool.addchild(
117                 AlarmWatcher(StxAlarmClient(self.bus.uow), self.bus))
118
119             self.worker.add_watcher(root)
120
121             self.worker.start()
122         except Exception as ex:
123             logger.warning("WorkerService Exception:" + str(ex))
124         finally:
125             self.worker.stop()
126
127
128 def start_watchers(sm: cotyledon.ServiceManager = None):
129     watchersm = sm if sm else cotyledon.ServiceManager()
130     watchersm.add(WatcherService, workers=1, args=())
131     watchersm.run()
132
133
134 def main():
135     logger.info("Resource watcher starting")
136     start_watchers()
137
138
139 if __name__ == "__main__":
140     main()