From f9746f13be900f5ad7e9e2a9dbf7527e74b5d428 Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Wed, 16 Nov 2022 19:21:59 +0800 Subject: [PATCH] Update accelerator resource enrich description field Issue-ID: INF-358 Signed-off-by: Bin Yang Change-Id: I53981730863a434c2751b62eac2476f6d496bb97 --- o2common/views/flask_restx_fields.py | 35 ++++++++++++++++++++++++++++ o2ims/adapter/orm.py | 6 ++--- o2ims/domain/ocloud.py | 4 ++-- o2ims/service/auditor/pserver_acc_handler.py | 18 ++++++++++---- o2ims/views/ocloud_dto.py | 9 +++++-- 5 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 o2common/views/flask_restx_fields.py diff --git a/o2common/views/flask_restx_fields.py b/o2common/views/flask_restx_fields.py new file mode 100644 index 0000000..0337239 --- /dev/null +++ b/o2common/views/flask_restx_fields.py @@ -0,0 +1,35 @@ +# Copyright (C) 2022 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from flask_restx import fields +import json + +from o2common.helper import o2logging +logger = o2logging.get_logger(__name__) + + +class Json2Dict(fields.Raw): + + def format(self, value): + value2 = None + try: + value2 = json.loads(value) if value else None + except Exception as ex: + logger.warning( + f"Failed to loads json string: {value}, exception: {str(ex)}") + value2 = value + return value2 diff --git a/o2ims/adapter/orm.py b/o2ims/adapter/orm.py index f7ea107..e8914e1 100644 --- a/o2ims/adapter/orm.py +++ b/o2ims/adapter/orm.py @@ -117,9 +117,9 @@ resource = Table( Column("name", String(255)), Column("globalAssetId", String(255)), Column("parentId", String(255)), - Column("description", String(255)), - Column("elements", Text()) - # Column("extensions", String(1024)) + Column("description", String()), + Column("elements", Text()), + Column("extensions", String()) ) deploymentmanager = Table( diff --git a/o2ims/domain/ocloud.py b/o2ims/domain/ocloud.py index bdc07d9..b7c82b5 100644 --- a/o2ims/domain/ocloud.py +++ b/o2ims/domain/ocloud.py @@ -116,7 +116,7 @@ class Resource(AgRoot, Serializer): def __init__(self, resourceId: str, resourceTypeId: str, resourcePoolId: str, name: str, parentId: str = '', gAssetId: str = '', elements: str = '', - description: str = '') -> None: + description: str = '', extensions: str = '') -> None: super().__init__() self.resourceId = resourceId self.description = description @@ -124,7 +124,7 @@ class Resource(AgRoot, Serializer): self.globalAssetId = gAssetId self.resourcePoolId = resourcePoolId self.elements = elements - self.extensions = [] + self.extensions = extensions self.name = name self.parentId = parentId diff --git a/o2ims/service/auditor/pserver_acc_handler.py b/o2ims/service/auditor/pserver_acc_handler.py index fc289ee..c3c78ba 100644 --- a/o2ims/service/auditor/pserver_acc_handler.py +++ b/o2ims/service/auditor/pserver_acc_handler.py @@ -15,7 +15,7 @@ # pylint: disable=unused-argument from __future__ import annotations import uuid -# import json +import json from o2ims.domain import commands, events from o2ims.domain.stx_object import StxGenericModel @@ -99,11 +99,21 @@ def create_by(stxobj: StxGenericModel, parent: Resource, resourcetype_id: str)\ resourcepool_id = parent.resourcePoolId parent_id = parent.resourceId gAssetId = '' # TODO: global ID - description = "%s : An Accelerator resource of the physical server"\ - % stxobj.name + # description = "%s : An Accelerator resource of the physical server"\ + # % stxobj.name + content = json.loads(stxobj.content) + selected_keys = [ + "name", "pdevice", "pciaddr", "pvendor_id", "pvendor", + "pclass_id", "pclass", "psvendor", "psdevice", + "sriov_totalvfs", "sriov_numvfs", "numa_node" + ] + filtered = dict( + filter(lambda item: item[0] in selected_keys, content.items())) + extensions = json.dumps(filtered) + description = ";".join([f"{k}:{v}" for k, v in filtered.items()]) resource = Resource(stxobj.id, resourcetype_id, resourcepool_id, stxobj.name, parent_id, gAssetId, stxobj.content, - description) + description, extensions) resource.createtime = stxobj.createtime resource.updatetime = stxobj.updatetime resource.hash = stxobj.hash diff --git a/o2ims/views/ocloud_dto.py b/o2ims/views/ocloud_dto.py index 2bc836d..870e7bd 100644 --- a/o2ims/views/ocloud_dto.py +++ b/o2ims/views/ocloud_dto.py @@ -15,6 +15,7 @@ from flask_restx import fields from o2ims.views.api_ns import api_ims_inventory as api_ims_inventory_v1 +from o2common.views.flask_restx_fields import Json2Dict class OcloudDTO: @@ -107,7 +108,9 @@ class ResourceDTO: 'parentId': fields.String, 'description': fields.String, # 'elements': fields.String, - 'extensions': fields.String + # 'extensions': fields.String + 'extensions': Json2Dict(attribute='extensions') + # 'extensions': fields.Raw(attribute='extensions') }, mask='{resourceId,resourcePoolId,resourceTypeId,description,parentId}' ) @@ -123,7 +126,9 @@ class ResourceDTO: 'parentId': fields.String, 'description': fields.String, # 'elements': fields.String, - 'extensions': fields.String + # 'extensions': fields.String + 'extensions': Json2Dict(attribute='extensions') + # 'extensions': fields.Raw(attribute='extensions') } if iteration_number: resource_json_mapping['elements'] = fields.List( -- 2.16.6