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
21 from o2ims.domain.stx_object import StxGenericModel
22 from o2common.service.unit_of_work import AbstractUnitOfWork
23 from o2ims.domain.resource_type import MismatchedModel
24 from o2ims.domain.ocloud import Resource, ResourceType
26 from o2common.helper import o2logging
27 logger = o2logging.get_logger(__name__)
30 class InvalidResourceType(Exception):
34 def update_pserver_cpu(
35 cmd: commands.UpdatePserverCpu,
36 uow: AbstractUnitOfWork
40 p_resource = uow.resources.get(cmd.parentid)
41 resourcepool = uow.resource_pools.get(p_resource.resourcePoolId)
43 res = uow.session.execute(
45 SELECT "resourceTypeId", "oCloudId", "name"
47 WHERE "resourceTypeEnum" = :resource_type_enum
49 dict(resource_type_enum=stxobj.type.name)
53 resourcetype_id = str(uuid.uuid4())
54 uow.resource_types.add(ResourceType(
56 'pserver_cpu', stxobj.type,
57 resourcepool.oCloudId))
59 resourcetype_id = first['resourceTypeId']
61 resource = uow.resources.get(stxobj.id)
63 logger.info("add the cpu of pserver:" + stxobj.name
64 + " update_at: " + str(stxobj.updatetime)
65 + " id: " + str(stxobj.id)
66 + " hash: " + str(stxobj.hash))
67 localmodel = create_by(stxobj, p_resource, resourcetype_id)
68 uow.resources.add(localmodel)
70 logger.info("Add the cpu of pserver: " + stxobj.id
71 + ", name: " + stxobj.name)
74 if is_outdated(localmodel, stxobj):
75 logger.info("update cpu of pserver:" + stxobj.name
76 + " update_at: " + str(stxobj.updatetime)
77 + " id: " + str(stxobj.id)
78 + " hash: " + str(stxobj.hash))
79 update_by(localmodel, stxobj, p_resource)
80 uow.resources.update(localmodel)
82 logger.info("Update the cpu of pserver: " + stxobj.id
83 + ", name: " + stxobj.name)
87 def is_outdated(resource: Resource, stxobj: StxGenericModel):
88 return True if resource.hash != stxobj.hash else False
91 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
93 # content = json.loads(stxobj.content)
94 resourcetype_id = resourcetype_id
95 resourcepool_id = parent.resourcePoolId
96 parent_id = parent.resourceId
97 gAssetId = '' # TODO: global ID
98 description = "A CPU resource of the physical server"
99 resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
100 stxobj.name, parent_id, gAssetId, stxobj.content,
102 resource.createtime = stxobj.createtime
103 resource.updatetime = stxobj.updatetime
104 resource.hash = stxobj.hash
109 def update_by(target: Resource, stxobj: StxGenericModel,
110 parentid: str) -> None:
111 if target.resourceId != stxobj.id:
112 raise MismatchedModel("Mismatched Id")
113 target.createtime = stxobj.createtime
114 target.updatetime = stxobj.updatetime
115 target.hash = stxobj.hash
116 target.version_number = target.version_number + 1