-- Copyright (c) 2024 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--         http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

-- Description: test the external IPMI and RPC interfaces of sensor in multihost.
local skynet = require 'skynet'
local log = require 'mc.logging'
local ctx = require 'mc.context'
local mdb = require 'mc.mdb'
local ipmitool = require 'test_ipmitool'
local common = require 'test_sensor_common'

local multihost_interface = {}
multihost_interface.__index = multihost_interface

function multihost_interface.enter_multihost(bus)
    log:info('== enter multihost ...')
    -- 获取connector
    local ret, connector = pcall(mdb.get_object, bus, '/bmc/kepler/Connector/Connector_Sensor_0_2_01',
    'bmc.kepler.Connector')
    assert(ret)
    assert(connector)

    connector.Presence = 1
    skynet.sleep(1000)  -- 等待对象加载
end

function multihost_interface.exit_multihost(bus)
    log:info('== exit multihost ...')
    -- 获取connector
    local ret, connector = pcall(mdb.get_object, bus, '/bmc/kepler/Connector/Connector_Sensor_0_2_01',
    'bmc.kepler.Connector')
    assert(ret)
    assert(connector)

    connector.Presence = 0
    skynet.sleep(200)  -- 等待对象卸载
end

function multihost_interface.test_ipmi_sdr_list(bus)
    log:info('== test multihost ipmitool sdr list start ...')
    -- 模拟带内查询，查询host1
    local sdr_count = ipmitool.sdr_list(bus, 1)
    assert(sdr_count == 9, 'actual: '.. sdr_count)

    -- 模拟其他通道查询
    sdr_count = ipmitool.sdr_list(bus)
    assert(sdr_count == 14, 'actual: '.. sdr_count)
end

function multihost_interface.test_ipmi_add_sel(bus)
    log:info('== test multihost ipmitool add sel start ...')
    -- 构造参数
    local data = {}
    data.sel_type = 2
    data.timestamp = 1770516666
    data.generate_id = 0
    data.msg_version = 0
    data.sensor_type = 3
    data.sensor_no = 2
    data.event_type = 0
    data.event_dir = 0
    data.data1 = 0
    data.data2 = 0
    data.data3 = 0

    -- 主机2发送add_sel IPMI命令
    ipmitool.add_sel(bus, data, 2)
    -- 利用GetSel接口获取事件用于校验
    local total, res = multihost_interface.get_sel(bus, 0, 10)
    assert(total == tonumber(res[1].RecordId),'actual: '..tonumber(res[1].RecordId))
    assert(tonumber(res[1].SystemId) == 2,'actual: '..tonumber(res[1].SystemId))

    -- 主机1发送add_sel IPMI命令
    ipmitool.add_sel(bus, data, 1)
    -- 利用GetSel接口获取事件用于校验
    total, res = multihost_interface.get_sel(bus, 0, 10)
    assert(total == tonumber(res[1].RecordId),'actual: '..tonumber(res[1].RecordId))
    assert(tonumber(res[1].SystemId) == 1,'actual: '..tonumber(res[1].SystemId))
end

function multihost_interface.test_add_oem_sel(bus)
    log:info('== test multihost add oem sel start ...')
    -- 构造参数
    local record_type = 2
    local timestamp = 1770514222
    local manufacturer_id = 2011
    local sel_datas = '222222'
    -- AddOemSel 方法
    local mobj = mdb.get_object(bus, '/bmc/kepler/Chassis/1/Sensors', 'bmc.kepler.Chassis.Sensors')
    local record_id = mobj:AddOemSel_PACKED(ctx.new(),record_type, timestamp, manufacturer_id, sel_datas):unpack()
    -- 利用GetSel接口获取事件用于校验
    local total, res = multihost_interface.get_sel(bus, 0, 10)
    assert(record_id == total,'actual: '..record_id)
    assert(tonumber(res[1].SystemId) == 0,'actual: '..tonumber(res[1].SystemId))
end

function multihost_interface.get_sel(bus, start, count)
    local mobj = mdb.get_object(bus, '/bmc/kepler/Chassis/1/Sensors', 'bmc.kepler.Chassis.Sensors')
    local _, total, list = pcall(function()
        return mobj:GetSel_PACKED(ctx.new(), start, count):unpack()
    end)
    local res = common.format_sel(list)

    return total, res
end


function multihost_interface.test_get_sensor_list(bus)
    log:info('== test multihost get sensor list start ...')
    local expected_res = {
        {SystemId = 1, SensorName = 'Inlet Temp'}, -- DT中为SystemId为1的公共传感器
        {SystemId = 1, SensorName = 'Power'},
        {SystemId = 1, SensorName = 'SYS 12V_2'},
        {SystemId = 1, SensorName = 'Boot Error'},
        {SystemId = 1, SensorName = 'Mngmnt Health'},
        {SystemId = 1, SensorName = 'Sel Status'},
        {SystemId = 2, SensorName = 'Inlet Temp'},
        {SystemId = 2, SensorName = 'Power'},
        {SystemId = 2, SensorName = 'SYS 12V_2'},
        {SystemId = 2, SensorName = 'Mngmnt Health'},
        {SystemId = 2, SensorName = 'Sel Status'}
    }

    local mobj = mdb.get_object(bus, '/bmc/kepler/Chassis/1/Sensors', 'bmc.kepler.Chassis.Sensors')
    local rsp = mobj:GetSensorList_PACKED(ctx.new())
    assert(#rsp.SensorList == 11, 'actual: ' .. tostring(#rsp.SensorList))
    for i, sensor in ipairs(rsp.SensorList) do
        assert(sensor.SystemId == expected_res[i].SystemId, 'sensor' .. i .. ' actual: ' .. sensor.SystemId)
        assert(sensor.SensorName == expected_res[i].SensorName, 'sensor' .. i .. ' actual: ' .. sensor.SensorName)
    end
end

function multihost_interface.test_entry(bus)
    log:info('================ test multihost interface start ================')
    multihost_interface.enter_multihost(bus)
    multihost_interface.test_ipmi_sdr_list(bus)
    multihost_interface.test_get_sensor_list(bus)
    multihost_interface.test_ipmi_add_sel(bus)
    multihost_interface.test_add_oem_sel(bus)
    multihost_interface.exit_multihost(bus)
    log:info('================ test multihost interface complete ================')
end

return multihost_interface