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):
35 cmd: commands.UpdatePserver,
36 uow: AbstractUnitOfWork
40 resourcepool = uow.resource_pools.get(cmd.parentid)
42 # res = uow.session.execute(select(resourcetype).where(
43 # resourcetype.c.resourceTypeEnum == stxobj.type))
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', stxobj.type,
58 resourcepool.oCloudId))
60 resourcetype_id = first['resourceTypeId']
62 resource = uow.resources.get(stxobj.id)
64 logger.info("add pserver:" + stxobj.name
65 + " update_at: " + str(stxobj.updatetime)
66 + " id: " + str(stxobj.id)
67 + " hash: " + str(stxobj.hash))
68 localmodel = create_by(stxobj, cmd.parentid, resourcetype_id)
69 uow.resources.add(localmodel)
71 logger.info("Add the pserver: " + stxobj.id
72 + ", name: " + stxobj.name)
75 if is_outdated(localmodel, stxobj):
76 logger.info("update pserver:" + stxobj.name
77 + " update_at: " + str(stxobj.updatetime)
78 + " id: " + str(stxobj.id)
79 + " hash: " + str(stxobj.hash))
80 update_by(localmodel, stxobj, cmd.parentid)
81 uow.resources.update(localmodel)
83 logger.info("Update the 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, parentid: str, resourcetype_id: str) \
94 # content = json.loads(stxobj.content)
95 resourcetype_id = resourcetype_id
96 resourcepool_id = parentid
98 gAssetId = '' # TODO: global ID
99 description = "A physical server resource"
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