-- 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.

local class = require 'mc.class'
local log = require 'mc.logging'
local mdb_config_manage = require 'mc.mdb.micro_component.config_manage'
local json = require 'cjson'
local business_config = require 'config_mgmt.business_config'
local server = class()

function server:init()
    self:register()
end

function server:ctor(db, bus, data)
    self.db = db
    self.bus = bus
    self.data = data
end

-- 配置导入
function server:on_import(ctx, data_str, data_type)
    local data
    if data_str then
        data = json.decode(data_str)
    end
    if not data or not data.ConfigData or type(data.ConfigData) ~= 'table' then
        log:error('Import data [%s] is invalid', data_str)
        return
    end
    business_config.on_import(self, ctx, data.ConfigData)
end

-- 配置导出
function server:on_export(ctx, type)
    local data = business_config.on_export(self, ctx)
    return json.encode({ ConfigData = data })
end

function server:register()
    mdb_config_manage.on_import(function(...) return self:on_import(...) end)
    mdb_config_manage.on_export(function(...) return self:on_export(...) end)
end

return server
