1 # Copyright (C) 2022 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.
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
22 from o2common.helper import o2logging
23 logger = o2logging.get_logger(__name__)
26 class InvalidResourceType(Exception):
30 def is_outdated(resource: Resource, stxobj: StxGenericModel):
31 return True if resource.hash != stxobj.hash else False
34 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
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,
45 resource.createtime = stxobj.createtime
46 resource.updatetime = stxobj.updatetime
47 resource.hash = stxobj.hash
49 resource.events.append(events.ResourceChanged(
51 resourcePoolId=resource.resourcePoolId,
52 notificationEventType=NotificationEventEnum.CREATE,
53 updatetime=stxobj.updatetime
59 def update_by(target: Resource, stxobj: StxGenericModel, parent: Resource)\
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(
72 resourcePoolId=target.resourcePoolId,
73 notificationEventType=NotificationEventEnum.MODIFY,
74 updatetime=stxobj.updatetime