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 dataclasses import dataclass
\r
18 from o2common.domain.base import AgRoot
\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
25 class StxGenericModel(AgRoot):
\r
26 def __init__(self, type: ResourceTypeEnum,
\r
27 api_response: dict = None, content_hash=None) -> None:
\r
30 self.id = str(api_response.uuid)
\r
32 self.updatetime = datetime.datetime.strptime(
\r
33 api_response.updated_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
\r
34 if api_response.updated_at else None
\r
35 self.createtime = datetime.datetime.strptime(
\r
36 api_response.created_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
\r
37 if api_response.created_at else None
\r
38 self.name = api_response.name
\r
39 self.hash = content_hash if content_hash \
\r
40 else str(hash((self.id, self.updatetime)))
\r
41 self.content = json.dumps(api_response.to_dict())
\r
43 def is_outdated(self, newmodel) -> bool:
\r
44 # return self.updatetime < newmodel.updatetime
\r
45 # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)
\r
46 return self.hash != newmodel.hash
\r
48 def update_by(self, newmodel) -> None:
\r
49 if self.id != newmodel.id:
\r
50 raise MismatchedModel("Mismatched model")
\r
51 self.name = newmodel.name
\r
52 self.createtime = newmodel.createtime
\r
53 self.updatetime = newmodel.updatetime
\r
54 self.content = newmodel.content
\r