Add o2dms api endpoint
[pti/o2.git] / o2app / entrypoints / resource_watcher.py
diff --git a/o2app/entrypoints/resource_watcher.py b/o2app/entrypoints/resource_watcher.py
new file mode 100644 (file)
index 0000000..036596c
--- /dev/null
@@ -0,0 +1,83 @@
+# Copyright (C) 2021 Wind River Systems, Inc.\r
+#\r
+#  Licensed under the Apache License, Version 2.0 (the "License");\r
+#  you may not use this file except in compliance with the License.\r
+#  You may obtain a copy of the License at\r
+#\r
+#      http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+#  Unless required by applicable law or agreed to in writing, software\r
+#  distributed under the License is distributed on an "AS IS" BASIS,\r
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+#  See the License for the specific language governing permissions and\r
+#  limitations under the License.\r
+\r
+import cotyledon\r
+\r
+from o2app import bootstrap\r
+from o2common.service.watcher.base import WatcherTree\r
+\r
+from o2ims.service.watcher.worker import PollWorker\r
+from o2ims.service.watcher.ocloud_watcher import OcloudWatcher\r
+from o2ims.service.watcher.ocloud_watcher import DmsWatcher\r
+from o2ims.service.watcher.resourcepool_watcher import ResourcePoolWatcher\r
+from o2ims.adapter.clients.ocloud_sa_client import StxSaDmsClient\r
+from o2ims.adapter.clients.ocloud_sa_client import StxSaOcloudClient\r
+from o2ims.adapter.clients.ocloud_sa_client import StxSaResourcePoolClient\r
+\r
+from o2ims.service.watcher.pserver_watcher import PServerWatcher\r
+from o2ims.adapter.clients.ocloud_sa_client import StxPserverClient\r
+\r
+from o2ims.service.watcher.pserver_cpu_watcher import PServerCpuWatcher\r
+from o2ims.adapter.clients.ocloud_sa_client import StxCpuClient\r
+\r
+from o2common.helper import o2logging\r
+logger = o2logging.get_logger(__name__)\r
+\r
+# r = redis.Redis(**config.get_redis_host_and_port())\r
+\r
+\r
+class WatcherService(cotyledon.Service):\r
+    def __init__(self, worker_id, args=None) -> None:\r
+        super().__init__(worker_id)\r
+        self.args = args\r
+        self.bus = bootstrap.bootstrap()\r
+        self.worker = PollWorker()\r
+\r
+    def run(self):\r
+        try:\r
+            root = WatcherTree(OcloudWatcher(\r
+                StxSaOcloudClient(), self.bus))\r
+            root.addchild(\r
+                DmsWatcher(StxSaDmsClient(), self.bus))\r
+\r
+            child_respool = root.addchild(\r
+                ResourcePoolWatcher(StxSaResourcePoolClient(),\r
+                                    self.bus))\r
+            child_pserver = child_respool.addchild(\r
+                PServerWatcher(StxPserverClient(), self.bus))\r
+            child_pserver.addchild(\r
+                PServerCpuWatcher(StxCpuClient(), self.bus))\r
+\r
+            self.worker.add_watcher(root)\r
+\r
+            self.worker.start()\r
+        except Exception as ex:\r
+            logger.warning("WorkerService Exception:" + str(ex))\r
+        finally:\r
+            self.worker.stop()\r
+\r
+\r
+def start_watchers(sm: cotyledon.ServiceManager = None):\r
+    watchersm = sm if sm else cotyledon.ServiceManager()\r
+    watchersm.add(WatcherService, workers=1, args=())\r
+    watchersm.run()\r
+\r
+\r
+def main():\r
+    logger.info("Resource watcher starting")\r
+    start_watchers()\r
+\r
+\r
+if __name__ == "__main__":\r
+    main()\r