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