From 32492d843d878a98c0402a48c773e5cea644aca3 Mon Sep 17 00:00:00 2001
From: armingol <pablo.armingolrobles@telefonica.com>
Date: Fri, 21 Jun 2024 13:33:07 +0200
Subject: [PATCH] Inventory NBI in Webuo

---
 .../ietf_hardware/HardwareMultipleDevices.py  | 36 ++++++++++++++++
 .../nbi_plugins/ietf_hardware/YangHandler.py  | 41 +++----------------
 .../nbi_plugins/ietf_hardware/__init__.py     |  8 +++-
 .../nbi_plugins/tfs_api/Resources.py          |  9 ----
 .../nbi_plugins/tfs_api/__init__.py           | 10 +++--
 5 files changed, 54 insertions(+), 50 deletions(-)
 create mode 100644 src/nbi/service/rest_server/nbi_plugins/ietf_hardware/HardwareMultipleDevices.py

diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/HardwareMultipleDevices.py b/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/HardwareMultipleDevices.py
new file mode 100644
index 000000000..5258455e5
--- /dev/null
+++ b/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/HardwareMultipleDevices.py
@@ -0,0 +1,36 @@
+import logging
+from flask import request
+from flask.json import jsonify
+from flask_restful import Resource
+from common.proto.context_pb2 import Empty
+from context.client.ContextClient import ContextClient
+from ..tools.Authentication import HTTP_AUTH
+from ..tools.HttpStatusCodes import HTTP_OK, HTTP_SERVERERROR
+from .YangHandler import YangHandler
+
+LOGGER = logging.getLogger(__name__)
+
+class HardwareMultipleDevices(Resource):
+    @HTTP_AUTH.login_required
+    def get(self):
+       
+        LOGGER.debug('Request: {:s}'.format(str(request)))
+
+        try:
+            context_client = ContextClient()
+            list_devices = context_client.ListDevices(Empty())
+            LOGGER.info('Request: {:s}'.format(str(list_devices)))
+            hardware_list_reply = []
+            yang_handler = YangHandler()
+            for device in list_devices.devices:
+                hardware_reply = yang_handler.compose(device)
+                hardware_list_reply.append(hardware_reply)
+            
+            yang_handler.destroy()
+            response = jsonify(hardware_list_reply)
+            response.status_code = HTTP_OK
+        except Exception as e: # pylint: disable=broad-except
+            MSG = 'Something went wrong Retrieving Hardware of Devices({:s})'
+            response = jsonify({'error': str(e)})
+            response.status_code = HTTP_SERVERERROR
+        return response
\ No newline at end of file
diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/YangHandler.py b/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/YangHandler.py
index 88c9887c0..f0212b01f 100644
--- a/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/YangHandler.py
+++ b/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/YangHandler.py
@@ -98,8 +98,12 @@ class YangHandler:
             component_new.create_path('serial-num', attributes["serial-num"])
             component_new.create_path('mfg-name', attributes["mfg-name"])
             if attributes["id"]:
-                component_new.create_path('parent-rel-pos', attributes["id"])
-            
+                try:
+                    parent_rel_pos = int(attributes["id"].replace("\"", ""))
+                    component_new.create_path('parent-rel-pos', parent_rel_pos)
+                except ValueError:
+                    continue
+
             component_new.create_path('uri', component.name)
    
 
@@ -113,39 +117,6 @@ class YangHandler:
             component_new.create_path('contains-child', contains_child)
 
         return hardware.print_mem('json')
-                                    
-
-
-        '''# example methods (based on openconfig, to be adapted):
-        #str_path = '/interfaces/interface[name={:s}]'.format(if_name)
-        if_name = 'my-if'
-        interfaces = self._yang_context.create_data_path('/openconfig-interfaces:interfaces')
-        my_if = interfaces.create_path('interface[name="{:s}"]'.format(if_name))
-        my_if.create_path('config/name', if_name)
-        my_if.create_path('config/enabled', True)
-
-        my_subifs = my_if.create_path('subinterfaces')
-
-        subif_index = 3
-        my_subif = my_subifs.create_path('subinterface[index="{:d}"]'.format(subif_index))
-        my_subif.create_path('config/index', subif_index)
-        my_subif.create_path('config/enabled', True)
-
-        vlan_id = 123
-        my_subif_vlan = my_subif.create_path('openconfig-vlan:vlan')
-        my_subif_vlan.create_path('match/single-tagged/config/vlan-id', vlan_id)
-
-        my_subif_ipv4 = my_subif.create_path('openconfig-if-ip:ipv4')
-        my_subif_ipv4.create_path('config/enabled', True)
-
-        my_subif_ipv4_addrs = my_subif_ipv4.create_path('addresses')
-        my_ipv4_addr_ip = '10.0.1.10'
-        my_ipv4_addr_prefix = 24
-        my_subif_ipv4_addr = my_subif_ipv4_addrs.create_path('address[ip="{:s}"]'.format(my_ipv4_addr_ip))
-        my_subif_ipv4_addr.create_path('config/ip', my_ipv4_addr_ip)
-        my_subif_ipv4_addr.create_path('config/prefix-length', my_ipv4_addr_prefix)
-
-        return my_if.print_mem('json')'''
     
 
     def destroy(self) -> None:
diff --git a/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/__init__.py b/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/__init__.py
index 7f4e219ff..2b1621a2a 100644
--- a/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/__init__.py
+++ b/src/nbi/service/rest_server/nbi_plugins/ietf_hardware/__init__.py
@@ -1,7 +1,11 @@
 from nbi.service.rest_server.nbi_plugins.ietf_hardware.Hardware import Hardware
+from nbi.service.rest_server.nbi_plugins.ietf_hardware.HardwareMultipleDevices import HardwareMultipleDevices
 from nbi.service.rest_server.RestServer import RestServer
 
-URL_PREFIX = "/restconf/data/device=<path:device_uuid>/ietf-hardware:hardware"
+URL_PREFIX_device = "/restconf/data/device=<path:device_uuid>/ietf-hardware:hardware"
+URL_PREFIX_hardware = "/restconf/data/ietf-hardware:hardware"
 
 def register_ietf_hardware(rest_server: RestServer):
-    rest_server.add_resource(Hardware, URL_PREFIX)
\ No newline at end of file
+    rest_server.add_resource(Hardware, URL_PREFIX_device)
+    rest_server.add_resource(HardwareMultipleDevices, URL_PREFIX_hardware)
+    
\ No newline at end of file
diff --git a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py
index b5fd18971..9311c915a 100644
--- a/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py
+++ b/src/nbi/service/rest_server/nbi_plugins/tfs_api/Resources.py
@@ -176,15 +176,6 @@ class Devices(_Resource):
 class Device(_Resource):
     def get(self, device_uuid : str):
         return format_grpc_to_json(self.client.GetDevice(grpc_device_id(device_uuid)))
-class Deviceshw(_Resource):
-    def get(self, device_uuid : str):
-        device =format_grpc_to_json(self.client.GetDevice(grpc_device_id(device_uuid)))
-        yang_handler = YangHandler('ietf-hardware')
-
-        hardware_reply = yang_handler.compose(device)
-        device = jsonify(hardware_reply)
-
-        return device
     
 class LinkIds(_Resource):
     def get(self):
diff --git a/src/nbi/service/rest_server/nbi_plugins/tfs_api/__init__.py b/src/nbi/service/rest_server/nbi_plugins/tfs_api/__init__.py
index cbb38e6f2..569c26c4e 100644
--- a/src/nbi/service/rest_server/nbi_plugins/tfs_api/__init__.py
+++ b/src/nbi/service/rest_server/nbi_plugins/tfs_api/__init__.py
@@ -13,10 +13,11 @@
 # limitations under the License.
 
 from nbi.service.rest_server.RestServer import RestServer
+from nbi.service.rest_server.nbi_plugins.ietf_hardware import Hardware, HardwareMultipleDevices
 from .Resources import (
     Connection, ConnectionIds, Connections,
     Context, ContextIds, Contexts,
-    Device, DeviceIds, Devices, Deviceshw,
+    Device, DeviceIds, Devices,
     DummyContexts,
     Link, LinkIds, Links,
     PolicyRule, PolicyRuleIds, PolicyRules,
@@ -48,10 +49,11 @@ RESOURCES = [
     ('api.slice',          Slice,         '/context/<path:context_uuid>/slice/<path:slice_uuid>'),
 
     ('api.device_ids',     DeviceIds,     '/device_ids'),
-    ('api.devices',        Devices,       '/devices'),
-    ('api.device',         Device,        '/device/<path:device_uuid>'),
+    '''('api.devices',        Devices,       '/devices'),'''
+    '''('api.device',         Device,        '/device/<path:device_uuid>'),'''
+    ('api.devices',        HardwareMultipleDevices,       '/devices'),
+    ('api.device',         Hardware,        '/device/<path:device_uuid>'),
     
-    ('api.deviceshw',        Deviceshw,       '/device/<path:device_uuid>/hardware'),
     
     ('api.link_ids',       LinkIds,       '/link_ids'),
     ('api.links',          Links,         '/links'),
-- 
GitLab