1 # Copyright (C) 2021 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # from dataclasses import dataclass
18 from o2common.domain.base import AgRoot
20 from o2ims.domain.resource_type import ResourceTypeEnum, MismatchedModel
21 from o2common.helper import o2logging
22 logger = o2logging.get_logger(__name__)
25 class StxGenericModel(AgRoot):
26 def __init__(self, type: ResourceTypeEnum,
27 api_response: dict = None, content_hash=None) -> None:
30 self.id = str(api_response.uuid)
32 self.updatetime = datetime.datetime.strptime(
33 api_response.updated_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
34 if api_response.updated_at else None
35 self.createtime = datetime.datetime.strptime(
36 api_response.created_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
37 if api_response.created_at else None
38 self.name = api_response.name
39 self.hash = content_hash if content_hash \
40 else str(hash((self.id, self.updatetime)))
41 self.content = json.dumps(api_response.to_dict())
42 if ResourceTypeEnum.RESOURCE_POOL == type:
43 self.res_pool_id = self.id
45 def is_outdated(self, newmodel) -> bool:
46 # return self.updatetime < newmodel.updatetime
47 # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)
48 return self.hash != newmodel.hash
50 def update_by(self, newmodel) -> None:
51 if self.id != newmodel.id:
52 raise MismatchedModel("Mismatched model")
53 self.name = newmodel.name
54 self.createtime = newmodel.createtime
55 self.updatetime = newmodel.updatetime
56 self.content = newmodel.content