26c67daa295c1ccb969ce72ddc0a7969d5fc1261
[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.adapter.ocloud_repository import OcloudRepository\r
18 \r
19 \r
20 class InvalidOcloudState(Exception):\r
21     pass\r
22 \r
23 \r
24 class BaseWatcher(object):\r
25     def __init__(self, client: BaseClient) -> None:\r
26         super().__init__()\r
27         self._client = client\r
28 \r
29     def targetname(self) -> str:\r
30         return self._targetname()\r
31 \r
32     def probe(self):\r
33         self._probe()\r
34 \r
35     def _probe(self):\r
36         raise NotImplementedError\r
37 \r
38     def _targetname(self):\r
39         raise NotImplementedError\r
40 \r
41 \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
46         self._repo = repo\r
47 \r
48     def _targetname(self):\r
49         return "ocloud"\r
50 \r
51     def _probe(self):\r
52         ocloudmodel = self._client.get(None)\r
53         if ocloudmodel:\r
54             self._compare_and_update(ocloudmodel)\r
55 \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
63         else:\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
68 \r
69 \r
70 class DmsWatcher(BaseWatcher):\r
71     def __init__(self, client: BaseClient) -> None:\r
72         super().__init__(client)\r
73 \r
74     def _targetname(self):\r
75         return "dms"\r
76 \r
77 \r
78 class ResourcePoolWatcher(BaseWatcher):\r
79     def __init__(self) -> None:\r
80         super().__init__()\r
81 \r
82     def _targetname(self):\r
83         return "ocloud"\r
84 \r
85 \r
86 class ResourceWatcher(BaseWatcher):\r
87     def __init__(self) -> None:\r
88         super().__init__()\r
89 \r
90     def _targetname(self):\r
91         return "resource"\r