-- 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 validate = require 'mc.validate'
local utils = require 'mc.utils'
local mdb = require 'mc.mdb'

local UBPort = {}

---@class UBPort.LinkStatusFromSMC
---@field LinkStatusFromSMC integer
local TLinkStatusFromSMC = {}
TLinkStatusFromSMC.__index = TLinkStatusFromSMC
TLinkStatusFromSMC.group = {}

local function TLinkStatusFromSMC_from_obj(obj)
    return setmetatable(obj, TLinkStatusFromSMC)
end

function TLinkStatusFromSMC.new(LinkStatusFromSMC)
    return TLinkStatusFromSMC_from_obj({LinkStatusFromSMC = LinkStatusFromSMC or 255})
end
---@param obj UBPort.LinkStatusFromSMC
function TLinkStatusFromSMC:init_from_obj(obj)
    self.LinkStatusFromSMC = obj.LinkStatusFromSMC or 255
end

function TLinkStatusFromSMC:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TLinkStatusFromSMC.group)
end

TLinkStatusFromSMC.from_obj = TLinkStatusFromSMC_from_obj

TLinkStatusFromSMC.proto_property = {'LinkStatusFromSMC'}

TLinkStatusFromSMC.default = {0}

TLinkStatusFromSMC.struct = {{name = 'LinkStatusFromSMC', is_array = false, struct = nil}}

function TLinkStatusFromSMC:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'LinkStatusFromSMC', self.LinkStatusFromSMC, 'uint8', true, errs, need_convert)

    TLinkStatusFromSMC:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TLinkStatusFromSMC.proto_property, errs, need_convert)
    return self
end

function TLinkStatusFromSMC:unpack(_)
    return self.LinkStatusFromSMC
end

UBPort.LinkStatusFromSMC = TLinkStatusFromSMC

---@class UBPort.PortId
---@field PortId integer
local TPortId = {}
TPortId.__index = TPortId
TPortId.group = {}

local function TPortId_from_obj(obj)
    return setmetatable(obj, TPortId)
end

function TPortId.new(PortId)
    return TPortId_from_obj({PortId = PortId or 1})
end
---@param obj UBPort.PortId
function TPortId:init_from_obj(obj)
    self.PortId = obj.PortId or 1
end

function TPortId:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TPortId.group)
end

TPortId.from_obj = TPortId_from_obj

TPortId.proto_property = {'PortId'}

TPortId.default = {0}

TPortId.struct = {{name = 'PortId', is_array = false, struct = nil}}

function TPortId:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'PortId', self.PortId, 'uint8', true, errs, need_convert)

    TPortId:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TPortId.proto_property, errs, need_convert)
    return self
end

function TPortId:unpack(_)
    return self.PortId
end

UBPort.PortId = TPortId

---@class UBPort.LinkStatus
---@field LinkStatus string
local TLinkStatus = {}
TLinkStatus.__index = TLinkStatus
TLinkStatus.group = {}

local function TLinkStatus_from_obj(obj)
    return setmetatable(obj, TLinkStatus)
end

function TLinkStatus.new(LinkStatus)
    return TLinkStatus_from_obj({LinkStatus = LinkStatus or [=[nolink]=]})
end
---@param obj UBPort.LinkStatus
function TLinkStatus:init_from_obj(obj)
    self.LinkStatus = obj.LinkStatus or [=[nolink]=]
end

function TLinkStatus:remove_error_props(errs, obj)
    utils.remove_obj_error_property(obj, errs, TLinkStatus.group)
end

TLinkStatus.from_obj = TLinkStatus_from_obj

TLinkStatus.proto_property = {'LinkStatus'}

TLinkStatus.default = {''}

TLinkStatus.struct = {{name = 'LinkStatus', is_array = false, struct = nil}}

function TLinkStatus:validate(prefix, errs, need_convert)
    prefix = prefix or ''

    validate.Optional(prefix .. 'LinkStatus', self.LinkStatus, 'string', true, errs, need_convert)

    if self.LinkStatus ~= nil then
        validate.Enum(prefix .. 'LinkStatus', self.LinkStatus, '', {'nolink', 'linkup', 'linkdown'}, errs, need_convert)
    end

    TLinkStatus:remove_error_props(errs, self)
    validate.CheckUnknowProperty(self, TLinkStatus.proto_property, errs, need_convert)
    return self
end

function TLinkStatus:unpack(_)
    return self.LinkStatus
end

UBPort.LinkStatus = TLinkStatus

UBPort.interface = mdb.register_interface('bmc.kepler.Chassis.UBPort', {
    LinkStatus = {'s', {'EMIT_CHANGE'}, true, 'nolink'},
    PortId = {'y', {}, true, 1},
    LinkStatusFromSMC = {'y', {}, true, 255}
}, {}, {})

return UBPort
