-- Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
-- 
-- this file licensed under the 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 utils = require 'basic_cooling.cooling_utils'
local client = require 'thermal_mgmt.client'
local enums = require 'basic_cooling.define.cooling_enums'
local props = require 'basic_cooling.define.cooling_properties'
local base = require 'basic_cooling.data_keeping.base'
local singleton = require 'mc.singleton'

local cpu_data_keeping = class(base)

function cpu_data_keeping:ctor(bus)
    self.obj_name = "CPU"
    -- 监听的资源树路径
    self.paths_table = {
        "/bmc/kepler/Systems/:SystemId/Processors/CPU/:ID"
    }
    -- 监听的接口
    self.interfaces_table = {
        ["bmc.kepler.Systems.Processor"] = {
            [enums.data_keeping_config.FOREACH_FUNC] = client.ForeachProcessorObjects,
            [enums.data_keeping_config.PROPERTIES] = {
                [props.CPU.MODEL] = enums.data_keeping_log.LOG
            }
        }
    }

    self.loaded_cpu_model = {}
    self.cpu_path_map = {}
end

function cpu_data_keeping:init()
    self.super.init(self)
end

function cpu_data_keeping:action_after_obj_added(path, interface)
    if self.cpu_path_map[path] then
        log:error("Duplicated CPU obj(path:%s)", path)
        return
    end
    local model = self.objs[path][interface][props.CPU.MODEL]
    self.cpu_path_map[path] = model

    self.loaded_cpu_model[model] = (not self.loaded_cpu_model[model]) and
        1 or (self.loaded_cpu_model[model] + 1)

    log:notice("Add CPU obj(path:%s), name: %s, cur number:%u",
        path, model, self.loaded_cpu_model[model])
end

function cpu_data_keeping:action_after_obj_removed(path, interface)
    if not self.cpu_path_map[path] then
        log:error("CPU obj(path:%s) not in table", path)
        return
    end

    local model = self.objs[path][interface][props.CPU.MODEL]
    self.cpu_path_map[path] = nil

    if self.loaded_cpu_model[model] >= 1 then
        self.loaded_cpu_model[model] = self.loaded_cpu_model[model] - 1
    else
        log:error("The number of CPU obj(name:%s) less then 1", model)
    end

    log:notice("Delete CPU obj(path:%s), cur number:%u", path,
        self.loaded_cpu_model[model])
end

function cpu_data_keeping:action_after_property_changed(path, interface, prop, value)
    if prop ~= props.CPU.MODEL then
        return
    end
    local pre_cpu_model = self.cpu_path_map[path]
    if self.loaded_cpu_model[pre_cpu_model] >= 1 then
        self.loaded_cpu_model[pre_cpu_model] = self.loaded_cpu_model[pre_cpu_model] - 1
        log:notice('Update the number of CPU obj(name:%s), cur number: %s', pre_cpu_model,
            self.loaded_cpu_model[pre_cpu_model])
    else
        log:error("The number of CPU obj(name:%s) less then 1", pre_cpu_model)
    end

    self.loaded_cpu_model[value] = (not self.loaded_cpu_model[value]) and
        1 or (self.loaded_cpu_model[value] + 1)
    self.cpu_path_map[path] = value

    log:notice("Update CPU obj(path:%s), pre name: %s, name: %s, cur number:%s",
        path, pre_cpu_model, value, self.loaded_cpu_model[value])
end

function cpu_data_keeping:get_cpu_model()
    return self.loaded_cpu_model
end

return singleton(cpu_data_keeping)