-- Copyright (c) 2026 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.

local UB_MODEL = "NPU_UB"

local netcard_slot_cmp = function (netcard_1, netcard_2)
    if netcard_1.PcieSlotId > netcard_2.PcieSlotId then
        return false
    end
    if netcard_1.PcieSlotId == netcard_2.PcieSlotId and netcard_1.Type >= netcard_2.Type then
        return false
    end
    return true
end

local netcard_table = cjson.json_object_new_array()
for i in pairs(Input) do
    if Input[i].Model == UB_MODEL then
        netcard_table[#netcard_table + 1] = Input[i]
    end
end

for i = 1, #netcard_table, 1 do
    for j = 1, #netcard_table - i, 1 do
        if not netcard_slot_cmp(netcard_table[j], netcard_table[j + 1]) then
            netcard_table[j], netcard_table[j + 1] = netcard_table[j + 1], netcard_table[j]
        end
    end
end

local result = cjson.json_object_new_array()
for i = 1, #netcard_table, 1 do
    local ub_card = cjson.json_object_new_object()
    ub_card.AdapterId = netcard_table[i].AdapterId
    ub_card.Name = netcard_table[i].Name
    result[#result + 1] = ub_card
end

return result