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