Fix INF-328 and INF-373 the resource change and update issue
[pti/o2.git] / o2ims / service / auditor / pserver_res_handler.py
1 # Copyright (C) 2022 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 import json
15
16 from o2ims.domain import events
17 from o2ims.domain.stx_object import StxGenericModel
18 from o2ims.domain.subscription_obj import NotificationEventEnum
19 from o2ims.domain.resource_type import MismatchedModel
20 from o2ims.domain.ocloud import Resource
21
22 from o2common.helper import o2logging
23 logger = o2logging.get_logger(__name__)
24
25
26 class InvalidResourceType(Exception):
27     pass
28
29
30 def is_outdated(resource: Resource, stxobj: StxGenericModel):
31     return True if resource.hash != stxobj.hash else False
32
33
34 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
35         -> Resource:
36     resourcetype_id = resourcetype_id
37     resourcepool_id = parent.resourcePoolId
38     parent_id = parent.resourceId
39     gAssetId = ''  # TODO: global ID
40     extensions = json.dumps(stxobj.filtered)
41     description = ";".join([f"{k}:{v}" for k, v in stxobj.filtered.items()])
42     resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
43                         parent_id, gAssetId, stxobj.content, description,
44                         extensions)
45     resource.createtime = stxobj.createtime
46     resource.updatetime = stxobj.updatetime
47     resource.hash = stxobj.hash
48
49     resource.events.append(events.ResourceChanged(
50         id=stxobj.id,
51         resourcePoolId=resource.resourcePoolId,
52         notificationEventType=NotificationEventEnum.CREATE,
53         updatetime=stxobj.updatetime
54     ))
55
56     return resource
57
58
59 def update_by(target: Resource, stxobj: StxGenericModel, parent: Resource)\
60         -> None:
61     if target.resourceId != stxobj.id:
62         raise MismatchedModel("Mismatched Id")
63     target.updatetime = stxobj.updatetime
64     target.hash = stxobj.hash
65     target.elements = stxobj.content
66     target.extensions = json.dumps(stxobj.filtered)
67     target.description = ";".join(
68         [f"{k}:{v}" for k, v in stxobj.filtered.items()])
69     target.version_number = target.version_number + 1
70     target.events.append(events.ResourceChanged(
71         id=stxobj.id,
72         resourcePoolId=target.resourcePoolId,
73         notificationEventType=NotificationEventEnum.MODIFY,
74         updatetime=stxobj.updatetime
75     ))