Commit 32492d84 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

Inventory NBI in Webuo

parent 5bfe1990
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
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
+6 −35
Original line number Diff line number Diff line
@@ -98,7 +98,11 @@ 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)
   
@@ -115,38 +119,5 @@ class YangHandler:
        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:
        self._yang_context.destroy()
 No newline at end of file
+6 −2
Original line number Diff line number Diff line
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
+0 −9
Original line number Diff line number Diff line
@@ -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):
+6 −4
Original line number Diff line number Diff line
@@ -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'),