Commit c50c2323 authored by Pablo Armingol's avatar Pablo Armingol
Browse files

Refactor SetEndpoint method in QKDServiceHandler to improve readability and error handling

parent 472a60be
Loading
Loading
Loading
Loading
+161 −161
Original line number Diff line number Diff line
@@ -65,30 +65,30 @@ class QKDServiceHandler(_ServiceHandler):
        context_uuid = self.__service.service_id.context_id.context_uuid.uuid

        results = []
        # try:
        try:

        #     if len(endpoints) > 4:
        #         is_virtual = True
        #     else:
        #         is_virtual = False
            if len(endpoints) > 4:
                is_virtual = True
            else:
                is_virtual = False

        #     devices    = []
        #     qkdn_ids   = []
        #     interfaces = []
        #     links      = []
            devices    = []
            qkdn_ids   = []
            interfaces = []
            links      = []

        #     # Optare: First a big iteration through all devices is done in order to obtain all information needed for the whole operation
        #     # Optare: This is a way to minimize time of operation. Otherwise it would require many O(N) operations. This way we can reduce it to one
            # Optare: First a big iteration through all devices is done in order to obtain all information needed for the whole operation
            # Optare: This is a way to minimize time of operation. Otherwise it would require many O(N) operations. This way we can reduce it to one

        #     # Populate devices and QKDN ids
        #     for idx, endpoint in enumerate(endpoints[::2]):
        #         device_uuid, endpoint_left_uuid = get_device_endpoint_uuids(endpoint)
        #         _, endpoint_right_uuid = get_device_endpoint_uuids(endpoints[2 * idx + 1])
            # Populate devices and QKDN ids
            for idx, endpoint in enumerate(endpoints[::2]):
                device_uuid, endpoint_left_uuid = get_device_endpoint_uuids(endpoint)
                _, endpoint_right_uuid = get_device_endpoint_uuids(endpoints[2 * idx + 1])

        #         device = self.__task_executor.get_device(DeviceId(**json_device_id(device_uuid)))
        #         devices.append(device)
        #         interfaces.append([0,0])
        #         links.append([])
                device = self.__task_executor.get_device(DeviceId(**json_device_id(device_uuid)))
                devices.append(device)
                interfaces.append([0,0])
                links.append([])

                endpoint_left =  get_endpoint_name_by_uuid(device, endpoint_left_uuid) if idx > 0 else None
                endpoint_right = get_endpoint_name_by_uuid(device, endpoint_right_uuid) if 2 * idx + 2 < len(endpoints) else None
@@ -96,199 +96,199 @@ class QKDServiceHandler(_ServiceHandler):
                for config_rule in device.device_config.config_rules:
                    resource_key = config_rule.custom.resource_key

        #             if resource_key == '__node__':
        #                 value = json.loads(config_rule.custom.resource_value)
        #                 qkdn_ids.append(value['qkdn_id'])
                    if resource_key == '__node__':
                        value = json.loads(config_rule.custom.resource_value)
                        qkdn_ids.append(value['qkdn_id'])

        #             elif resource_key.startswith('/interface'):
        #                 value = json.loads(config_rule.custom.resource_value)
        #                 try:
        #                     endpoint_str = value['qkdi_att_point']['uuid']
        #                     LOGGER.info("A: " + str(endpoint_str) + "....." + str(endpoint_left) + "....." + str(endpoint_right))
                    elif resource_key.startswith('/interface'):
                        value = json.loads(config_rule.custom.resource_value)
                        try:
                            endpoint_str = value['qkdi_att_point']['uuid']
                            LOGGER.info("A: " + str(endpoint_str) + "....." + str(endpoint_left) + "....." + str(endpoint_right))

        #                     if endpoint_str == endpoint_left:
        #                         interfaces[idx][0] = value['qkdi_id']
        #                     elif endpoint_str == endpoint_right:
        #                         interfaces[idx][1] = value['qkdi_id']
        #                 except KeyError:
        #                     pass
                            if endpoint_str == endpoint_left:
                                interfaces[idx][0] = value['qkdi_id']
                            elif endpoint_str == endpoint_right:
                                interfaces[idx][1] = value['qkdi_id']
                        except KeyError:
                            pass

        #             elif resource_key.startswith('/link'):
        #                 value = json.loads(config_rule.custom.resource_value)
        #                 links[idx].append(( value['uuid'],
        #                     (value['src_qkdn_id'], value['src_interface_id']),
        #                     (value['dst_qkdn_id'], value['dst_interface_id'])
        #                 ))
                    elif resource_key.startswith('/link'):
                        value = json.loads(config_rule.custom.resource_value)
                        links[idx].append(( value['uuid'],
                            (value['src_qkdn_id'], value['src_interface_id']),
                            (value['dst_qkdn_id'], value['dst_interface_id'])
                        ))


        #     LOGGER.info("IFs: " + str(interfaces))
        #     LOGGER.info("Links: " + str(links))
        #     LOGGER.info("context_: " + context_uuid)
            LOGGER.info("IFs: " + str(interfaces))
            LOGGER.info("Links: " + str(links))
            LOGGER.info("context_: " + context_uuid)


        #     # Optare: From here now is where the work is really done. It iterates over every device in use for the service (ordered)
            # Optare: From here now is where the work is really done. It iterates over every device in use for the service (ordered)

        #     src_device_uuid, src_endpoint_uuid = get_device_endpoint_uuids(endpoints[0])
        #     src_device = devices[0]
        #     src_endpoint = get_endpoint_matching(src_device, src_endpoint_uuid)
            src_device_uuid, src_endpoint_uuid = get_device_endpoint_uuids(endpoints[0])
            src_device = devices[0]
            src_endpoint = get_endpoint_matching(src_device, src_endpoint_uuid)

        #     dst_device_uuid, dst_endpoint_uuid = get_device_endpoint_uuids(endpoints[-1])
        #     dst_device = devices[-1]
        #     dst_endpoint = get_endpoint_matching(dst_device, dst_endpoint_uuid)
            dst_device_uuid, dst_endpoint_uuid = get_device_endpoint_uuids(endpoints[-1])
            dst_device = devices[-1]
            dst_endpoint = get_endpoint_matching(dst_device, dst_endpoint_uuid)

        #     src_qkdn_id = qkdn_ids[0]
        #     dst_qkdn_id = qkdn_ids[-1]
            src_qkdn_id = qkdn_ids[0]
            dst_qkdn_id = qkdn_ids[-1]

        #     src_interface_id = interfaces[0][1]
        #     dst_interface_id = interfaces[-1][0]
            src_interface_id = interfaces[0][1]
            dst_interface_id = interfaces[-1][0]

        #     service_qkdl_id_src_dst = str(uuid.uuid4())
        #     service_qkdl_id_dst_src = str(uuid.uuid4())
            service_qkdl_id_src_dst = str(uuid.uuid4())
            service_qkdl_id_dst_src = str(uuid.uuid4())


        #     for idx, device in enumerate(devices):
            for idx, device in enumerate(devices):

        #         # Even though we always create them together. There is a chance the admin deletes one of the rules manually
        #         phys_qkdl_id_right = None if idx == (len(devices) - 1) else '' # None == impossible
        #         phys_qkdl_id_left = None if idx == 0 else '' # None == impossible
                # Even though we always create them together. There is a chance the admin deletes one of the rules manually
                phys_qkdl_id_right = None if idx == (len(devices) - 1) else '' # None == impossible
                phys_qkdl_id_left = None if idx == 0 else '' # None == impossible

        #         for link_uuid, link_src, link_dst in links[idx]:
        #             qkdn_link_src, qkdn_interface_src = link_src
        #             qkdn_link_dst, qkdn_interface_dst = link_dst
                for link_uuid, link_src, link_dst in links[idx]:
                    qkdn_link_src, qkdn_interface_src = link_src
                    qkdn_link_dst, qkdn_interface_dst = link_dst

        #             if phys_qkdl_id_right == '' and \
        #                 qkdn_link_src == qkdn_ids[idx] and qkdn_interface_src == interfaces[idx][1] and \
        #                 qkdn_link_dst[idx+1] and qkdn_interface_dst == interfaces[idx+1][0]:
        #                 phys_qkdl_id_right = link_uuid
                    if phys_qkdl_id_right == '' and \
                        qkdn_link_src == qkdn_ids[idx] and qkdn_interface_src == interfaces[idx][1] and \
                        qkdn_link_dst[idx+1] and qkdn_interface_dst == interfaces[idx+1][0]:
                        phys_qkdl_id_right = link_uuid


        #             if phys_qkdl_id_left == '' and \
        #                 qkdn_link_src == qkdn_ids[idx] and qkdn_interface_src == interfaces[idx][0] and \
        #                 qkdn_link_dst[idx-1] and qkdn_interface_dst == interfaces[idx-1][1]:
        #                 phys_qkdl_id_left = link_uuid
                    if phys_qkdl_id_left == '' and \
                        qkdn_link_src == qkdn_ids[idx] and qkdn_interface_src == interfaces[idx][0] and \
                        qkdn_link_dst[idx-1] and qkdn_interface_dst == interfaces[idx-1][1]:
                        phys_qkdl_id_left = link_uuid


        #         # Optare: Before adding information to config_rules you have to delete the list first otherwise old content will be called again
        #         del device.device_config.config_rules[:]
                # Optare: Before adding information to config_rules you have to delete the list first otherwise old content will be called again
                del device.device_config.config_rules[:]


        #         if phys_qkdl_id_right:
        #             if not is_virtual:
        #                 service_qkdl_id_src_dst = phys_qkdl_id_right
                if phys_qkdl_id_right:
                    if not is_virtual:
                        service_qkdl_id_src_dst = phys_qkdl_id_right

        #         elif phys_qkdl_id_right == '':
        #             qkdl_id_src_dst = str(uuid.uuid4()) if is_virtual else service_qkdl_id_src_dst
                elif phys_qkdl_id_right == '':
                    qkdl_id_src_dst = str(uuid.uuid4()) if is_virtual else service_qkdl_id_src_dst

        #             json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(qkdl_id_src_dst), {
        #                 'uuid'               : qkdl_id_src_dst,
        #                 'type'               : 'DIRECT',
        #                 'src_qkdn_id'        : qkdn_ids[idx],
        #                 'src_interface_id'   : interfaces[idx][1],
        #                 'dst_qkdn_id'        : qkdn_ids[idx+1],
        #                 'dst_interface_id'   : interfaces[idx+1][0],
        #             })
                    json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(qkdl_id_src_dst), {
                        'uuid'               : qkdl_id_src_dst,
                        'type'               : 'DIRECT',
                        'src_qkdn_id'        : qkdn_ids[idx],
                        'src_interface_id'   : interfaces[idx][1],
                        'dst_qkdn_id'        : qkdn_ids[idx+1],
                        'dst_interface_id'   : interfaces[idx+1][0],
                    })

        #             device.device_config.config_rules.append(ConfigRule(**json_config_rule))
                    device.device_config.config_rules.append(ConfigRule(**json_config_rule))


        #         if phys_qkdl_id_left:
        #             if not is_virtual:
        #                 service_qkdl_id_dst_src = phys_qkdl_id_left
                if phys_qkdl_id_left:
                    if not is_virtual:
                        service_qkdl_id_dst_src = phys_qkdl_id_left

        #         elif phys_qkdl_id_left == '':
        #             qkdl_id_dst_src = str(uuid.uuid4()) if is_virtual else service_qkdl_id_dst_src
                elif phys_qkdl_id_left == '':
                    qkdl_id_dst_src = str(uuid.uuid4()) if is_virtual else service_qkdl_id_dst_src

        #             json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(qkdl_id_dst_src), {
        #                 'uuid'               : qkdl_id_dst_src,
        #                 'type'               : 'DIRECT',
        #                 'src_qkdn_id'        : qkdn_ids[idx],
        #                 'src_interface_id'   : interfaces[idx][0],
        #                 'dst_qkdn_id'        : qkdn_ids[idx-1],
        #                 'dst_interface_id'   : interfaces[idx-1][1],
        #             })
                    json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(qkdl_id_dst_src), {
                        'uuid'               : qkdl_id_dst_src,
                        'type'               : 'DIRECT',
                        'src_qkdn_id'        : qkdn_ids[idx],
                        'src_interface_id'   : interfaces[idx][0],
                        'dst_qkdn_id'        : qkdn_ids[idx-1],
                        'dst_interface_id'   : interfaces[idx-1][1],
                    })

        #             device.device_config.config_rules.append(ConfigRule(**json_config_rule))
                    device.device_config.config_rules.append(ConfigRule(**json_config_rule))



        #         if is_virtual:
        #             if idx < len(qkdn_ids) - 1:
        #                 json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(service_qkdl_id_src_dst), {
        #                     'uuid'               : service_qkdl_id_src_dst,
        #                     'type'               : 'VIRTUAL',
        #                     'src_qkdn_id'        : src_qkdn_id,
        #                     'src_interface_id'   : src_interface_id,
        #                     'dst_qkdn_id'        : dst_qkdn_id,
        #                     'dst_interface_id'   : dst_interface_id,
        #                     'virt_prev_hop'      : qkdn_ids[idx-1] if idx > 0 else None,
        #                     'virt_next_hops'     : qkdn_ids[idx+1:],
        #                     'virt_bandwidth'     : 0,
        #                 })
                if is_virtual:
                    if idx < len(qkdn_ids) - 1:
                        json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(service_qkdl_id_src_dst), {
                            'uuid'               : service_qkdl_id_src_dst,
                            'type'               : 'VIRTUAL',
                            'src_qkdn_id'        : src_qkdn_id,
                            'src_interface_id'   : src_interface_id,
                            'dst_qkdn_id'        : dst_qkdn_id,
                            'dst_interface_id'   : dst_interface_id,
                            'virt_prev_hop'      : qkdn_ids[idx-1] if idx > 0 else None,
                            'virt_next_hops'     : qkdn_ids[idx+1:],
                            'virt_bandwidth'     : 0,
                        })

        #                 device.device_config.config_rules.append(ConfigRule(**json_config_rule))
                        device.device_config.config_rules.append(ConfigRule(**json_config_rule))

        #             if idx > 0:
        #                 json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(service_qkdl_id_dst_src), {
        #                     'uuid'               : service_qkdl_id_dst_src,
        #                     'type'               : 'VIRTUAL',
        #                     'src_qkdn_id'        : dst_qkdn_id,
        #                     'src_interface_id'   : dst_interface_id,
        #                     'dst_qkdn_id'        : src_qkdn_id,
        #                     'dst_interface_id'   : src_interface_id,
        #                     'virt_prev_hop'      : qkdn_ids[idx+1] if idx < len(qkdn_ids) - 1 else None,
        #                     'virt_next_hops'     : qkdn_ids[idx-1::-1],
        #                     'virt_bandwidth'     : 0,
        #                 })
                    if idx > 0:
                        json_config_rule = json_config_rule_set('/link/link[{:s}]'.format(service_qkdl_id_dst_src), {
                            'uuid'               : service_qkdl_id_dst_src,
                            'type'               : 'VIRTUAL',
                            'src_qkdn_id'        : dst_qkdn_id,
                            'src_interface_id'   : dst_interface_id,
                            'dst_qkdn_id'        : src_qkdn_id,
                            'dst_interface_id'   : src_interface_id,
                            'virt_prev_hop'      : qkdn_ids[idx+1] if idx < len(qkdn_ids) - 1 else None,
                            'virt_next_hops'     : qkdn_ids[idx-1::-1],
                            'virt_bandwidth'     : 0,
                        })

        #                 device.device_config.config_rules.append(ConfigRule(**json_config_rule))
                        device.device_config.config_rules.append(ConfigRule(**json_config_rule))



        #         json_config_rule = json_config_rule_set('/services/service[{:s}]'.format(service_uuid), {
        #             'uuid'               : service_uuid,
        #             'qkdl_id_src_dst'    : service_qkdl_id_src_dst,
        #             'qkdl_id_dst_src'    : service_qkdl_id_dst_src,
        #         })
                json_config_rule = json_config_rule_set('/services/service[{:s}]'.format(service_uuid), {
                    'uuid'               : service_uuid,
                    'qkdl_id_src_dst'    : service_qkdl_id_src_dst,
                    'qkdl_id_dst_src'    : service_qkdl_id_dst_src,
                })

        #         device.device_config.config_rules.append(ConfigRule(**json_config_rule))
        #         self.__task_executor.configure_device(device)
                device.device_config.config_rules.append(ConfigRule(**json_config_rule))
                self.__task_executor.configure_device(device)


        #     if is_virtual:
            if is_virtual:

        #         # Register App
        #         internal_app_src_dst = {
        #             'app_id': {'context_id': {'context_uuid': {'uuid': context_uuid}}, 'app_uuid': {'uuid': str(uuid.uuid4())}},
        #             'app_status': QKDAppStatusEnum.QKDAPPSTATUS_ON,
        #             'app_type': QKDAppTypesEnum.QKDAPPTYPES_INTERNAL,
        #             'server_app_id': '',
        #             'client_app_id': [],
        #             'backing_qkdl_id': [{'qkdl_uuid': {'uuid': service_qkdl_id_src_dst}}],
        #             'local_device_id': src_device.device_id,
        #             'remote_device_id': dst_device.device_id,
        #         }
                # Register App
                internal_app_src_dst = {
                    'app_id': {'context_id': {'context_uuid': {'uuid': context_uuid}}, 'app_uuid': {'uuid': str(uuid.uuid4())}},
                    'app_status': QKDAppStatusEnum.QKDAPPSTATUS_ON,
                    'app_type': QKDAppTypesEnum.QKDAPPTYPES_INTERNAL,
                    'server_app_id': '',
                    'client_app_id': [],
                    'backing_qkdl_id': [{'qkdl_uuid': {'uuid': service_qkdl_id_src_dst}}],
                    'local_device_id': src_device.device_id,
                    'remote_device_id': dst_device.device_id,
                }

                self.__task_executor.register_qkd_app(App(**internal_app_src_dst))


        #         # Register App
        #         internal_app_dst_src = {
        #             'app_id': {'context_id': {'context_uuid': {'uuid': context_uuid}}, 'app_uuid': {'uuid': str(uuid.uuid4())}},
        #             'app_status': QKDAppStatusEnum.QKDAPPSTATUS_ON,
        #             'app_type': QKDAppTypesEnum.QKDAPPTYPES_INTERNAL,
        #             'server_app_id': '',
        #             'client_app_id': [],
        #             'backing_qkdl_id': [{'qkdl_uuid': {'uuid': service_qkdl_id_dst_src}}],
        #             'local_device_id': dst_device.device_id,
        #             'remote_device_id': src_device.device_id,
        #         }
                # Register App
                internal_app_dst_src = {
                    'app_id': {'context_id': {'context_uuid': {'uuid': context_uuid}}, 'app_uuid': {'uuid': str(uuid.uuid4())}},
                    'app_status': QKDAppStatusEnum.QKDAPPSTATUS_ON,
                    'app_type': QKDAppTypesEnum.QKDAPPTYPES_INTERNAL,
                    'server_app_id': '',
                    'client_app_id': [],
                    'backing_qkdl_id': [{'qkdl_uuid': {'uuid': service_qkdl_id_dst_src}}],
                    'local_device_id': dst_device.device_id,
                    'remote_device_id': src_device.device_id,
                }

                self.__task_executor.register_qkd_app(App(**internal_app_dst_src))

        #     results.append(True)
        # except Exception as e: # pylint: disable=broad-except
        #     LOGGER.exception('Unable to SetEndpoint for Service({:s})'.format(str(service_uuid)))
        #     results.append(e)
            results.append(True)
        except Exception as e: # pylint: disable=broad-except
            LOGGER.exception('Unable to SetEndpoint for Service({:s})'.format(str(service_uuid)))
            results.append(e)

        return results