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 # pylint: disable=unused-argument
16 from __future__ import annotations
20 from o2ims.domain import commands, events
21 from o2ims.domain.stx_object import StxGenericModel
22 from o2ims.domain.subscription_obj import NotificationEventEnum
23 from o2common.service.unit_of_work import AbstractUnitOfWork
24 from o2ims.domain.resource_type import MismatchedModel
25 from o2ims.domain.ocloud import Resource, ResourceType
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
31 class InvalidResourceType(Exception):
35 def update_pserver_mem(
36 cmd: commands.UpdatePserverMem,
37 uow: AbstractUnitOfWork
41 p_resource = uow.resources.get(cmd.parentid)
42 resourcepool = uow.resource_pools.get(p_resource.resourcePoolId)
44 res = uow.session.execute(
46 SELECT "resourceTypeId", "oCloudId", "name"
48 WHERE "resourceTypeEnum" = :resource_type_enum
50 dict(resource_type_enum=stxobj.type.name)
54 resourcetype_id = str(uuid.uuid4())
55 uow.resource_types.add(ResourceType(
57 'pserver_mem', stxobj.type,
58 resourcepool.oCloudId))
60 resourcetype_id = first['resourceTypeId']
62 resource = uow.resources.get(stxobj.id)
64 logger.info("add the memory of pserver:" + stxobj.name
65 + " update_at: " + str(stxobj.updatetime)
66 + " id: " + str(stxobj.id)
67 + " hash: " + str(stxobj.hash))
68 localmodel = create_by(stxobj, p_resource, resourcetype_id)
69 uow.resources.add(localmodel)
71 logger.info("Add the memory of pserver: " + stxobj.id
72 + ", name: " + stxobj.name)
75 if is_outdated(localmodel, stxobj):
76 logger.info("update memory of pserver:" + stxobj.name
77 + " update_at: " + str(stxobj.updatetime)
78 + " id: " + str(stxobj.id)
79 + " hash: " + str(stxobj.hash))
80 update_by(localmodel, stxobj, p_resource)
81 uow.resources.update(localmodel)
83 logger.info("Update the memory of pserver: " + stxobj.id
84 + ", name: " + stxobj.name)
88 def is_outdated(resource: Resource, stxobj: StxGenericModel):
89 return True if resource.hash != stxobj.hash else False
92 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
94 # content = json.loads(stxobj.content)
95 resourcetype_id = resourcetype_id
96 resourcepool_id = parent.resourcePoolId
97 parent_id = parent.resourceId
98 gAssetId = '' # TODO: global ID
99 description = "A memory resource of the physical server"
100 resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
101 stxobj.name, parent_id, gAssetId, stxobj.content,
103 resource.createtime = stxobj.createtime
104 resource.updatetime = stxobj.updatetime
105 resource.hash = stxobj.hash
110 def update_by(target: Resource, stxobj: StxGenericModel,
111 parentid: str) -> None:
112 if target.resourceId != stxobj.id:
113 raise MismatchedModel("Mismatched Id")
114 target.createtime = stxobj.createtime
115 target.updatetime = stxobj.updatetime
116 target.hash = stxobj.hash
117 target.version_number = target.version_number + 1
118 target.events.append(events.ResourceChanged(
120 resourcePoolId=target.resourcePoolId,
121 notificationEventType=NotificationEventEnum.MODIFY,
122 updatetime=stxobj.updatetime