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 o2ims.service.client.base_client import BaseClient
\r
16 from o2ims.domain.stx_object import StxGenericModel
\r
17 from o2ims.adapter.ocloud_repository import OcloudRepository
\r
20 class InvalidOcloudState(Exception):
\r
24 class BaseWatcher(object):
\r
25 def __init__(self, client: BaseClient) -> None:
\r
27 self._client = client
\r
29 def targetname(self) -> str:
\r
30 return self._targetname()
\r
36 raise NotImplementedError
\r
38 def _targetname(self):
\r
39 raise NotImplementedError
\r
42 class OcloudWather(BaseWatcher):
\r
43 def __init__(self, ocloud_client: BaseClient,
\r
44 repo: OcloudRepository) -> None:
\r
45 super().__init__(ocloud_client)
\r
48 def _targetname(self):
\r
52 ocloudmodel = self._client.get(None)
\r
54 self._compare_and_update(ocloudmodel)
\r
56 def _compare_and_update(self, ocloudmodel: StxGenericModel) -> bool:
\r
57 # localmodel = self._repo.get(ocloudmodel.id)
\r
58 oclouds = self._repo.list()
\r
59 if len(oclouds) > 1:
\r
60 raise InvalidOcloudState("More than 1 ocloud is found")
\r
61 if len(oclouds) == 0:
\r
62 self._repo.add(ocloudmodel)
\r
64 localmodel = oclouds.pop()
\r
65 if localmodel.is_outdated(ocloudmodel):
\r
66 localmodel.update_by(ocloudmodel)
\r
67 self._repo.update(localmodel)
\r
70 class DmsWatcher(BaseWatcher):
\r
71 def __init__(self, client: BaseClient) -> None:
\r
72 super().__init__(client)
\r
74 def _targetname(self):
\r
78 class ResourcePoolWatcher(BaseWatcher):
\r
79 def __init__(self) -> None:
\r
82 def _targetname(self):
\r
86 class ResourceWatcher(BaseWatcher):
\r
87 def __init__(self) -> None:
\r
90 def _targetname(self):
\r