INF-303 Add Infrastructure Monitoring Fault Service; INF-305 update inventory api...
[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._tags = None
31         # self._uow = bus.uow
32
33     def targetname(self) -> str:
34         return self._targetname()
35
36     def probe(self, parent: commands.Command = None, tags: object = None):
37         try:
38             cmds = self._probe(
39                 parent.data if parent else None, tags)
40             for cmd in cmds:
41                 self._bus.handle(cmd)
42
43             # return self._probe(parent)
44             return cmds
45         except Exception as ex:
46             logger.warning("Failed to probe %s watcher due to: %s - %s" %
47                            (self._targetname(), type(ex), str(ex)))
48             return []
49
50     def _probe(self, parent: object = None, tags: object = None) \
51             -> commands.Command:
52         raise NotImplementedError
53
54     def _targetname(self):
55         raise NotImplementedError
56
57     # def _compare_and_update(self, newmodel: StxGenericModel) -> bool:
58     #     with self._uow:
59     #         # localmodel = self._uow.stxobjects.get(ocloudmodel.id)
60     #         localmodel = self._uow.stxobjects.get(str(newmodel.id))
61     #         if not localmodel:
62     #             logger.info("add entry:" + newmodel.name)
63     #             self._uow.stxobjects.add(newmodel)
64     #         elif localmodel.is_outdated(newmodel):
65     #             logger.info("update entry:" + newmodel.name)
66     #             localmodel.update_by(newmodel)
67     #             self._uow.stxobjects.update(localmodel)
68     #         self._uow.commit()
69
70
71 # node to organize watchers in tree hierachy
72 class WatcherTree(object):
73     def __init__(self, watcher: BaseWatcher) -> None:
74         super().__init__()
75         self.watcher = watcher
76         self.children = {}
77         self.tags = None
78
79     def addchild(self, watcher: BaseWatcher) -> object:
80         child = WatcherTree(watcher)
81         self.children[watcher.targetname()] = child
82         return child
83
84     def removechild(self, targetname: str) -> object:
85         return self.children.pop(targetname)
86
87     # probe all resources by parent, depth = 0 for indefinite recursive
88     def probe(self, parentresource=None, depth: int = 0, tags: object = None):
89         logger.debug("probe resources with watcher: "
90                      + self.watcher.targetname())
91         childdepth = depth - 1 if depth > 0 else 0
92         resources = self.watcher.probe(parentresource, tags)
93         logger.debug("probe returns " + str(len(resources)) + " resources")
94         if self.watcher._tags is not None:
95             tags = self.watcher._tags
96
97         if depth == 1:
98             # stop recursive
99             return
100
101         for res in resources:
102             for targetname in self.children.keys():
103                 self.children[targetname].probe(res, childdepth, tags)