0daf8d43a7fbf37721a1354e3bef2c71d648972f
[pti/o2.git] / o2common / service / watcher / base.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 # from logging import exception\r
16 # from cgtsclient import exc\r
17 from o2ims.service.client.base_client import BaseClient\r
18 # from o2ims.domain.stx_object import StxGenericModel\r
19 # from o2ims.service.unit_of_work import AbstractUnitOfWork\r
20 from o2ims.domain import commands\r
21 from o2ims.service.messagebus import MessageBus\r
22 from o2common.helper import o2logging\r
23 logger = o2logging.get_logger(__name__)\r
24 \r
25 \r
26 class BaseWatcher(object):\r
27     def __init__(self, client: BaseClient,\r
28                  bus: MessageBus) -> None:\r
29         super().__init__()\r
30         self._client = client\r
31         self._bus = bus\r
32         # self._uow = bus.uow\r
33 \r
34     def targetname(self) -> str:\r
35         return self._targetname()\r
36 \r
37     def probe(self, parent: commands.UpdateStxObject = None):\r
38         try:\r
39             cmds = self._probe(parent.data if parent else None)\r
40             for cmd in cmds:\r
41                 self._bus.handle(cmd)\r
42 \r
43             # return self._probe(parent)\r
44             return cmds\r
45         except Exception as ex:\r
46             logger.warning("Failed to probe resource due to: " + str(ex))\r
47             return []\r
48 \r
49     def _probe(self, parent: object = None) -> commands.UpdateStxObject:\r
50         raise NotImplementedError\r
51 \r
52     def _targetname(self):\r
53         raise NotImplementedError\r
54 \r
55     # def _compare_and_update(self, newmodel: StxGenericModel) -> bool:\r
56     #     with self._uow:\r
57     #         # localmodel = self._uow.stxobjects.get(ocloudmodel.id)\r
58     #         localmodel = self._uow.stxobjects.get(str(newmodel.id))\r
59     #         if not localmodel:\r
60     #             logger.info("add entry:" + newmodel.name)\r
61     #             self._uow.stxobjects.add(newmodel)\r
62     #         elif localmodel.is_outdated(newmodel):\r
63     #             logger.info("update entry:" + newmodel.name)\r
64     #             localmodel.update_by(newmodel)\r
65     #             self._uow.stxobjects.update(localmodel)\r
66     #         self._uow.commit()\r
67 \r
68 \r
69 # node to organize watchers in tree hierachy\r
70 class WatcherTree(object):\r
71     def __init__(self, watcher: BaseWatcher) -> None:\r
72         super().__init__()\r
73         self.watcher = watcher\r
74         self.children = {}\r
75 \r
76     def addchild(self, watcher: BaseWatcher) -> object:\r
77         child = WatcherTree(watcher)\r
78         self.children[watcher.targetname()] = child\r
79         return child\r
80 \r
81     def removechild(self, targetname: str) -> object:\r
82         return self.children.pop(targetname)\r
83 \r
84     # probe all resources by parent, depth = 0 for indefinite recursive\r
85     def probe(self, parentresource=None, depth: int = 0):\r
86         logger.debug("probe resources with watcher: "\r
87                      + self.watcher.targetname())\r
88         childdepth = depth - 1 if depth > 0 else 0\r
89         resources = self.watcher.probe(parentresource)\r
90         logger.debug("probe returns " + str(len(resources)) + " resources")\r
91 \r
92         if depth == 1:\r
93             # stop recursive\r
94             return\r
95 \r
96         for res in resources:\r
97             for targetname in self.children.keys():\r
98                 self.children[targetname].probe(res, childdepth)\r