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_acc(
36 cmd: commands.UpdatePserverAcc,
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", "name"
48 WHERE "resourceTypeEnum" = :resource_type_enum
50 dict(resource_type_enum=stxobj.type.name)
54 res_type_name = 'pserver_acc'
55 resourcetype_id = str(uuid.uuid3(
56 uuid.NAMESPACE_URL, res_type_name))
57 res_type = ResourceType(
59 res_type_name, stxobj.type,
60 description='An Accelerator resource type of Physical Server')
61 dict_id = str(uuid.uuid3(
63 str(f"{res_type_name}_alarmdictionary")))
64 alarm_dictionary = uow.alarm_dictionaries.get(dict_id)
66 res_type.alarmDictionary = alarm_dictionary
67 uow.resource_types.add(res_type)
69 resourcetype_id = first['resourceTypeId']
71 resource = uow.resources.get(stxobj.id)
73 logger.info("add the accelerator of pserver:" + stxobj.name
74 + " update_at: " + str(stxobj.updatetime)
75 + " id: " + str(stxobj.id)
76 + " hash: " + str(stxobj.hash))
77 localmodel = create_by(stxobj, p_resource, resourcetype_id)
78 uow.resources.add(localmodel)
80 logger.info("Add the accelerator of pserver: " + stxobj.id
81 + ", name: " + stxobj.name)
84 if is_outdated(localmodel, stxobj):
85 logger.info("update accelerator of pserver:" + stxobj.name
86 + " update_at: " + str(stxobj.updatetime)
87 + " id: " + str(stxobj.id)
88 + " hash: " + str(stxobj.hash))
89 update_by(localmodel, stxobj, p_resource)
90 uow.resources.update(localmodel)
92 logger.info("Update the accelerator of pserver: " + stxobj.id
93 + ", name: " + stxobj.name)
97 def is_outdated(resource: Resource, stxobj: StxGenericModel):
98 return True if resource.hash != stxobj.hash else False
101 def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\
103 # content = json.loads(stxobj.content)
104 resourcetype_id = resourcetype_id
105 resourcepool_id = parent.resourcePoolId
106 parent_id = parent.resourceId
107 gAssetId = '' # TODO: global ID
108 # description = "%s : An Accelerator resource of the physical server"\
110 content = json.loads(stxobj.content)
112 "name", "pdevice", "pciaddr", "pvendor_id", "pvendor",
113 "pclass_id", "pclass", "psvendor", "psdevice",
114 "sriov_totalvfs", "sriov_numvfs", "numa_node"
117 filter(lambda item: item[0] in selected_keys, content.items()))
118 extensions = json.dumps(filtered)
119 description = ";".join([f"{k}:{v}" for k, v in filtered.items()])
120 resource = Resource(stxobj.id, resourcetype_id, resourcepool_id,
121 stxobj.name, parent_id, gAssetId, stxobj.content,
122 description, extensions)
123 resource.createtime = stxobj.createtime
124 resource.updatetime = stxobj.updatetime
125 resource.hash = stxobj.hash
130 def update_by(target: Resource, stxobj: StxGenericModel,
131 parentid: str) -> None:
132 if target.resourceId != stxobj.id:
133 raise MismatchedModel("Mismatched Id")
134 target.createtime = stxobj.createtime
135 target.updatetime = stxobj.updatetime
136 target.hash = stxobj.hash
137 target.version_number = target.version_number + 1
138 target.events.append(events.ResourceChanged(
140 resourcePoolId=target.resourcePoolId,
141 notificationEventType=NotificationEventEnum.MODIFY,
142 updatetime=stxobj.updatetime