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