Exemplo n.º 1
0
def get_network_device_function(uuid, device_id, device_function_id):
    """Get the Redfish NetworkDeviceFunction for a given UUID and device_id.

        Return NetworkDeviceFunction Redfish JSON for a given hardware UUID
        and device_id.

        Parameters:
            uuid: the UUID of the server_hardware
            device_id: The id of the network device
            device_function_id: The id of the device function

        Returns:
            JSON: Redfish json with NetworkDeviceFunction

        Exceptions:
            When hardware is not found calls abort(404)
            When other errors occur calls abort(500)

    """
    try:
        # Initial validation of device_id
        device_id_validation = int(device_id)

        server_hardware = g.oneview_client.server_hardware.get(uuid)

        # Final validation of device_id
        if device_id_validation - 1 < 0 or (device_id_validation - 1) >= \
            len(server_hardware["portMap"]["deviceSlots"]):
            abort(status.HTTP_404_NOT_FOUND,
                  "Network interface id {} not found.".format(device_id))

        ndf = NetworkDeviceFunction(
            device_id, device_function_id, server_hardware)

        json_str = ndf.serialize()

        return Response(
            response=json_str,
            status=status.HTTP_200_OK,
            mimetype="application/json")
    except ValueError:
        # Failed to convert device_id to int
        logging.exception(
            "Failed to convert device id {} to integer.".format(device_id))
        abort(status.HTTP_404_NOT_FOUND, "Network interface not found")
    except HPOneViewException as e:
        # In case of error log exception and abort
        logging.exception(e)
        if e.oneview_response['errorCode'] == "RESOURCE_NOT_FOUND":
            abort(status.HTTP_404_NOT_FOUND, "Server hardware not found")
        else:
            abort(status.HTTP_500_INTERNAL_SERVER_ERROR)
    def test_serialize(self):
        # Tests the serialize function result against known result

        try:
            network_device_function = \
                NetworkDeviceFunction(
                    self.device_id,
                    self.device_function_id,
                    self.server_hardware)
        except Exception as e:
            self.fail("Failed to instantiate NetworkDeviceFunction class."
                      " Error: {}".format(e))

        try:
            result = json.loads(network_device_function.serialize())
        except Exception as e:
            self.fail("Failed to serialize. Error: ".format(e))

        self.assertEqual(self.network_device_function_mockup, result)
    def test_class_instantiation(self):
        # Tests if class is correctly instantiated and validated

        try:
            network_device_function = \
                NetworkDeviceFunction(
                    self.device_id,
                    self.device_function_id,
                    self.server_hardware)
        except Exception as e:
            self.fail("Failed to instantiate NetworkDeviceFunction class."
                      " Error: {}".format(e))
        self.assertIsInstance(network_device_function, NetworkDeviceFunction)
    def test_port_type_fibre_channel_exception(self):
        # Tests if class with an invalid device_function_id

        try:
            server_hardware = deepcopy(self.server_hardware)
            server_hardware["portMap"]["deviceSlots"][2]["physicalPorts"][0]["type"]\
                = "FibreChannel"
            NetworkDeviceFunction(self.device_id, self.device_function_id,
                                  server_hardware)
        except OneViewRedfishInvalidAttributeValueException as e:
            self.assertIsInstance(
                e, OneViewRedfishInvalidAttributeValueException)
            self.assertEqual(e.msg, "FibreChannel not implemented")
    def test_invalid_device_function_id(self):
        # Tests if class with an invalid device_function_id

        try:
            network_device_function = NetworkDeviceFunction(
                self.device_id, "invalid_device_id", self.server_hardware)
        except OneViewRedfishResourceNotFoundError as e:
            self.assertIsInstance(e, OneViewRedfishResourceNotFoundError)
        except Exception as e:
            self.fail("Failed to instantiate NetworkDeviceFunction class."
                      " Error: {}".format(e))
        else:
            self.fail("Class instantiated with invalid parameters."
                      " Error: {}".format(network_device_function))