Update accelerator resource 67/9667/5
authorBin Yang <bin.yang@windriver.com>
Wed, 16 Nov 2022 11:21:59 +0000 (19:21 +0800)
committerBin Yang <bin.yang@windriver.com>
Thu, 17 Nov 2022 01:01:57 +0000 (09:01 +0800)
enrich description field

Issue-ID: INF-358

Signed-off-by: Bin Yang <bin.yang@windriver.com>
Change-Id: I53981730863a434c2751b62eac2476f6d496bb97

o2common/views/flask_restx_fields.py [new file with mode: 0644]
o2ims/adapter/orm.py
o2ims/domain/ocloud.py
o2ims/service/auditor/pserver_acc_handler.py
o2ims/views/ocloud_dto.py

diff --git a/o2common/views/flask_restx_fields.py b/o2common/views/flask_restx_fields.py
new file mode 100644 (file)
index 0000000..0337239
--- /dev/null
@@ -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
index f7ea107..e8914e1 100644 (file)
@@ -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(
index bdc07d9..b7c82b5 100644 (file)
@@ -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
index fc289ee..c3c78ba 100644 (file)
@@ -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
index 2bc836d..870e7bd 100644 (file)
@@ -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(