1 # Copyright (C) 2021 Wind River Systems, Inc.
\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
7 # http://www.apache.org/licenses/LICENSE-2.0
\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
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
26 class BaseWatcher(object):
\r
27 def __init__(self, client: BaseClient,
\r
28 bus: MessageBus) -> None:
\r
30 self._client = client
\r
32 # self._uow = bus.uow
\r
34 def targetname(self) -> str:
\r
35 return self._targetname()
\r
37 def probe(self, parent: commands.UpdateStxObject = None):
\r
39 cmds = self._probe(parent.data if parent else None)
\r
41 self._bus.handle(cmd)
\r
43 # return self._probe(parent)
\r
45 except Exception as ex:
\r
46 logger.warning("Failed to probe resource due to: " + str(ex))
\r
49 def _probe(self, parent: object = None) -> commands.UpdateStxObject:
\r
50 raise NotImplementedError
\r
52 def _targetname(self):
\r
53 raise NotImplementedError
\r
55 # def _compare_and_update(self, newmodel: StxGenericModel) -> bool:
\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
69 # node to organize watchers in tree hierachy
\r
70 class WatcherTree(object):
\r
71 def __init__(self, watcher: BaseWatcher) -> None:
\r
73 self.watcher = watcher
\r
76 def addchild(self, watcher: BaseWatcher) -> object:
\r
77 child = WatcherTree(watcher)
\r
78 self.children[watcher.targetname()] = child
\r
81 def removechild(self, targetname: str) -> object:
\r
82 return self.children.pop(targetname)
\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
96 for res in resources:
\r
97 for targetname in self.children.keys():
\r
98 self.children[targetname].probe(res, childdepth)
\r