-- 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 memory_data_keeping = class(base)

function memory_data_keeping:ctor(bus)
    self.obj_name = "Memory"
    -- 监听的资源树路径
    self.paths_table = {
        "/bmc/kepler/Systems/:SystemId/Memory/:ID"
    }
    -- 监听的接口
    self.interfaces_table = {
        ["bmc.kepler.Systems.Memory"] = {
            [enums.data_keeping_config.FOREACH_FUNC] = client.ForeachMemoryObjects,
            [enums.data_keeping_config.PROPERTIES] = {
                [props.MEMORY.ORIGINAL_PART_NUMBER] = enums.data_keeping_log.LOG
            }
        }
    }

    self.loaded_memory_part_number = {}
    self.memory_path_map = {}
end

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

function memory_data_keeping:action_after_obj_added(path, interface)
    if self.memory_path_map[path] then
        log:error("Duplicated memory obj(path:%s)", path)
        return
    end
    local part_number = self.objs[path][interface][props.MEMORY.ORIGINAL_PART_NUMBER]
    self.memory_path_map[path] = part_number

    self.loaded_memory_part_number[part_number] = (not self.loaded_memory_part_number[part_number]) and
        1 or (self.loaded_memory_part_number[part_number] + 1)

    log:notice("Add memory obj(path:%s), name: %s, cur number:%u",
        path, part_number, self.loaded_memory_part_number[part_number])
end

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

    local part_number = self.objs[path][interface][props.MEMORY.ORIGINAL_PART_NUMBER]
    self.memory_path_map[path] = nil

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

    log:notice("Delete memory obj(path:%s), cur number:%u", path,
        self.loaded_memory_part_number[part_number])
end

function memory_data_keeping:action_after_property_changed(path, interface, prop, value)
    if prop ~= props.MEMORY.ORIGINAL_PART_NUMBER then
        return
    end
    local pre_memory_part_number = self.memory_path_map[path]
    if self.loaded_memory_part_number[pre_memory_part_number] >= 1 then
        self.loaded_memory_part_number[pre_memory_part_number] = self.loaded_memory_part_number[pre_memory_part_number] - 1
        log:notice("Update the number of memory obj(name:%s), cur number: %s", pre_memory_part_number,
            self.loaded_memory_part_number[pre_memory_part_number])
    else
        log:error("The number of memory obj(name:%s) less then 1", pre_memory_part_number)
    end

    self.loaded_memory_part_number[value] = (not self.loaded_memory_part_number[value]) and
        1 or (self.loaded_memory_part_number[value] + 1)
    self.memory_path_map[path] = value

    log:notice("Update memory obj(path:%s), pre name: %s, name: %s, cur number:%s",
        path, pre_memory_part_number, value, self.loaded_memory_part_number[value])
end

function memory_data_keeping:get_memory_part_number()
    return self.loaded_memory_part_number
end

return singleton(memory_data_keeping)