Fix INF-371 inventoryChange notification of the resourceType, resourcePool, Dms
[pti/o2.git] / o2ims / domain / stx_object.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # from dataclasses import dataclass
16 import datetime
17 import json
18 from o2common.domain.base import AgRoot
19
20 from o2ims.domain.resource_type import ResourceTypeEnum, MismatchedModel
21 from o2common.helper import o2logging
22 logger = o2logging.get_logger(__name__)
23
24
25 class StxGenericModel(AgRoot):
26     def __init__(self, type: ResourceTypeEnum,
27                  api_response: dict = None, content_hash=None) -> None:
28         super().__init__()
29         if api_response:
30             self.id = str(api_response.uuid)
31             self.type = type
32             self.name = api_response.name
33             self.updatetime = datetime.datetime.strptime(
34                 api_response.updated_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
35                 if api_response.updated_at else None
36             self.createtime = datetime.datetime.strptime(
37                 api_response.created_at.split('.')[0], "%Y-%m-%dT%H:%M:%S") \
38                 if api_response.created_at else None
39             self.hash = content_hash
40             if not self.hash:
41                 if hasattr(api_response, 'filtered'):
42                     self.filtered = api_response.filtered
43                     self.hash = str(hash((self.id, str(self.filtered))))
44                 else:
45                     self.hash = str(hash((self.id, self.updatetime)))
46             self.content = json.dumps(api_response.to_dict())
47             if ResourceTypeEnum.RESOURCE_POOL == type:
48                 self.res_pool_id = self.id
49
50     def is_outdated(self, newmodel) -> bool:
51         # return self.updatetime < newmodel.updatetime
52         # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)
53         return self.hash != newmodel.hash
54
55     def update_by(self, newmodel) -> None:
56         if self.id != newmodel.id:
57             raise MismatchedModel("Mismatched model")
58         self.name = newmodel.name
59         self.createtime = newmodel.createtime
60         self.updatetime = newmodel.updatetime
61         self.content = newmodel.content