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
19 from o2ims.domain.resource_type import ResourceTypeEnum
\r
21 logger = logging.getLogger(__name__)
\r
24 class MismatchedModel(Exception):
\r
28 class StxGenericModel:
\r
29 def __init__(self, type: ResourceTypeEnum,
\r
30 api_response: dict = None, content_hash=None) -> None:
\r
32 self.id = api_response.uuid
\r
34 self.updatetime = datetime.datetime.strptime(
\r
35 api_response.updated_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
\r
36 if api_response.updated_at else None
\r
37 self.createtime = datetime.datetime.strptime(
\r
38 api_response.created_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
\r
39 if api_response.created_at else None
\r
40 self.name = api_response.name
\r
41 self.hash = content_hash if content_hash \
\r
42 else str(hash((self.id, self.updatetime)))
\r
43 self.content = json.dumps(api_response.to_dict())
\r
45 def is_outdated(self, newmodel) -> bool:
\r
46 # return self.updatetime < newmodel.updatetime
\r
47 # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)
\r
48 return self.hash != newmodel.hash
\r
50 def update_by(self, newmodel) -> None:
\r
51 if self.id != newmodel.id:
\r
52 raise MismatchedModel("Mismatched model")
\r
53 self.name = newmodel.name
\r
54 self.createtime = newmodel.createtime
\r
55 self.updatetime = newmodel.updatetime
\r
56 self.content = newmodel.content
\r