5e4d660bc3ee7e4140b7311e64ef181a739c2f6c
[pti/o2.git] / o2ims / domain / stx_object.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 dataclasses import dataclass\r
16 import datetime\r
17 import json\r
18 from o2common.domain.base import AgRoot\r
19 \r
20 from o2ims.domain.resource_type import ResourceTypeEnum, MismatchedModel\r
21 from o2common.helper import o2logging\r
22 logger = o2logging.get_logger(__name__)\r
23 \r
24 \r
25 class StxGenericModel(AgRoot):\r
26     def __init__(self, type: ResourceTypeEnum,\r
27                  api_response: dict = None, content_hash=None) -> None:\r
28         if api_response:\r
29             self.id = str(api_response.uuid)\r
30             self.type = type\r
31             self.updatetime = datetime.datetime.strptime(\r
32                 api_response.updated_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \\r
33                 if api_response.updated_at else None\r
34             self.createtime = datetime.datetime.strptime(\r
35                 api_response.created_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \\r
36                 if api_response.created_at else None\r
37             self.name = api_response.name\r
38             self.hash = content_hash if content_hash \\r
39                 else str(hash((self.id, self.updatetime)))\r
40             self.content = json.dumps(api_response.to_dict())\r
41 \r
42     def is_outdated(self, newmodel) -> bool:\r
43         # return self.updatetime < newmodel.updatetime\r
44         # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)\r
45         return self.hash != newmodel.hash\r
46 \r
47     def update_by(self, newmodel) -> None:\r
48         if self.id != newmodel.id:\r
49             raise MismatchedModel("Mismatched model")\r
50         self.name = newmodel.name\r
51         self.createtime = newmodel.createtime\r
52         self.updatetime = newmodel.updatetime\r
53         self.content = newmodel.content\r