Add capabilities of the DMS query
[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.filtered = getattr(api_response, 'filtered', None)
40             self.hash = content_hash or str(
41                 hash((self.id, str(self.filtered)
42                       if self.filtered else self.updatetime)))
43
44             def handle_non_serializable(obj):
45                 return repr(obj)
46             self.content = json.dumps(
47                 vars(api_response), default=handle_non_serializable)
48
49             if ResourceTypeEnum.RESOURCE_POOL == type:
50                 self.res_pool_id = self.id
51
52     def is_outdated(self, newmodel) -> bool:
53         # return self.updatetime < newmodel.updatetime
54         # logger.warning("hash1: " + self.hash + " vs hash2: " + newmodel.hash)
55         return self.hash != newmodel.hash
56
57     def update_by(self, newmodel) -> None:
58         if self.id != newmodel.id:
59             raise MismatchedModel("Mismatched model")
60         self.name = newmodel.name
61         self.createtime = newmodel.createtime
62         self.updatetime = newmodel.updatetime
63         self.content = newmodel.content