mirror of
https://github.com/ntop/ntopng.git
synced 2026-04-30 07:59:35 +00:00
Additional methods for pools and interface pools lua classes
This commit is contained in:
parent
a9040f1057
commit
efbb84a892
2 changed files with 157 additions and 30 deletions
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
require "lua_utils"
|
||||
local user_scripts = require "user_scripts"
|
||||
local json = require "dkjson"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
|
|
@ -13,19 +14,15 @@ local base_pools = {}
|
|||
|
||||
-- ##############################################
|
||||
|
||||
local base_pools_prefix = "ntopng.pools"
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:create(args)
|
||||
if args then
|
||||
-- We're being sub-classed
|
||||
if not args.key or not args.name or not args.members or not args.configset_id then
|
||||
if not args.key then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
local this = args or {key = "base", members = {}, configset_id = 0}
|
||||
local this = args or {key = "base"}
|
||||
|
||||
setmetatable(this, self)
|
||||
self.__index = self
|
||||
|
|
@ -35,14 +32,47 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:_get_pools_prefix_key()
|
||||
local key = string.format("ntopng.pools.%s_pools", self.key)
|
||||
-- e.g.:
|
||||
-- ntopng.pools.interface_pools
|
||||
-- ntopng.pools.snmp_device_pools
|
||||
-- ntopng.pools.network_pools
|
||||
|
||||
return key
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:_get_pool_ids_key()
|
||||
return string.format("%s.%s_pools.pool_ids", base_pools_prefix, self.key)
|
||||
local key = string.format("%s.pool_ids", self:_get_pools_prefix_key())
|
||||
-- e.g.:
|
||||
-- ntopng.pools.interface_pools.pool_ids
|
||||
|
||||
return key
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:_get_next_pool_id_key()
|
||||
return string.format("%s.%s_pools.next_pool_id", base_pools_prefix, self.key)
|
||||
local key = string.format("%s.next_pool_id", self:_get_pools_prefix_key())
|
||||
-- e.g.:
|
||||
-- ntopng.pools.interface_pools.next_pool_id
|
||||
|
||||
return key
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:_get_pool_details_key(pool_id)
|
||||
if not pool_id then
|
||||
-- A pool id is always needed
|
||||
return nil
|
||||
end
|
||||
|
||||
local key = string.format("%s.pool_id_%d.next_pool_id", self:_get_pools_prefix_key(), pool_id)
|
||||
tprint(key)
|
||||
return key
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
|
@ -61,31 +91,135 @@ end
|
|||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:_persist()
|
||||
io.write("persist")
|
||||
-- @brief Persist pool details to disk. Possibly assign a pool id
|
||||
-- @param pool_id The pool_id of the pool which needs to be persisted. If nil, a new pool id is assigned
|
||||
function base_pools:_persist(pool_id, name, members, configset_id)
|
||||
-- self:cleanup()
|
||||
|
||||
-- LOCK
|
||||
local pool_details_key = self:_get_pool_details_key(pool_id)
|
||||
local pool_details = {
|
||||
name = name,
|
||||
members = members,
|
||||
configset_id = configset_id
|
||||
}
|
||||
ntop.setCache(pool_details_key, json.encode(pool_details))
|
||||
|
||||
-- Check if duplicate names exist
|
||||
-- Check is members are valid and do not belong to any other pool
|
||||
-- Check if the configset_id is valid
|
||||
-- Return the assigned pool_id
|
||||
return pool_id
|
||||
end
|
||||
|
||||
-- Now that everything is ok, the id can be assigned
|
||||
local pool_id = self:_assign_pool_id()
|
||||
-- ##############################################
|
||||
|
||||
-- UNLOCK
|
||||
function base_pools:add_pool(name, members, configset_id)
|
||||
local pool_id
|
||||
|
||||
-- Set the pool id to the current instance
|
||||
self.pool_id = pool_id
|
||||
return true
|
||||
-- TODO: LOCK
|
||||
|
||||
if name and members and configset_id then
|
||||
-- Check if duplicate names exist
|
||||
-- Check if members are valid and do not belong to any other pool
|
||||
-- Check if the configset_id is valid
|
||||
|
||||
if true then -- TODO: implement checks
|
||||
-- All the checks have succeeded
|
||||
-- Now that everything is ok, the id can be assigned
|
||||
pool_id = self:_assign_pool_id()
|
||||
|
||||
-- Data can now be persisted with the new pool_id
|
||||
self:_persist(pool_id, name, members, configset_id)
|
||||
end
|
||||
end
|
||||
|
||||
-- TODO: UNLOCK
|
||||
|
||||
return pool_id
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:edit_pool(pool_id, new_name, new_members, new_configset_id)
|
||||
local ret = false
|
||||
|
||||
-- TODO: LOCK
|
||||
|
||||
-- Make sure the pool exists
|
||||
local cur_pool_details = self:get_pool(pool_id)
|
||||
|
||||
-- If here, pool_id has been found
|
||||
if cur_pool_details and new_name and new_members and new_configset_id then
|
||||
-- Check if new_name is not the name of any other existing pool
|
||||
-- Check if none of new_members belongs to any other exsiting pool
|
||||
-- Check if the configset_id is valid
|
||||
|
||||
if true then -- TODO: implement checks
|
||||
-- If here, all checks are valid and the pool can be edited
|
||||
self:_persist(pool_id, new_name, new_members, new_configset_id)
|
||||
|
||||
-- Pool edited successfully
|
||||
ret = true
|
||||
end
|
||||
end
|
||||
|
||||
-- TODO: UNLOCK
|
||||
|
||||
return ret
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:delete_pool(pool_id)
|
||||
local ret = false
|
||||
|
||||
-- TODO: LOCK
|
||||
|
||||
-- Make sure the pool exists
|
||||
local cur_pool_details = self:get_pool(pool_id)
|
||||
|
||||
if cur_pool_details then
|
||||
-- Remove the key with all the pool details (e.g., with members, and configset_id)
|
||||
ntop.delCache(self:_get_pool_details_key(pool_id))
|
||||
|
||||
-- Remove the pool_id from the set of all currently existing pool ids
|
||||
ntop.delMembersCache(self:_get_pool_ids_key(), string.format("%d", pool_id))
|
||||
end
|
||||
|
||||
-- TODO: UNLOCK
|
||||
|
||||
return ret
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:get_pool(pool_id)
|
||||
local pool_details
|
||||
local pool_details_key = self:_get_pool_details_key(pool_id)
|
||||
|
||||
-- Attempt at retrieving the pool details key and at decoding it from JSON
|
||||
if pool_details_key then
|
||||
local pool_details_str = ntop.getCache(pool_details_key)
|
||||
pool_details = json.decode(pool_details_str)
|
||||
end
|
||||
|
||||
-- Upon success, pool details are returned, otherwise nil
|
||||
return pool_details
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
||||
function base_pools:cleanup()
|
||||
-- TODO: LOCK
|
||||
|
||||
-- Delete pool details
|
||||
local cur_pool_ids = ntop.getMembersCache(self:_get_pool_ids_key())
|
||||
for _, pool_id in pairs(cur_pool_ids) do
|
||||
ntop.delCache(self:_get_pool_details_key(pool_id))
|
||||
end
|
||||
|
||||
-- Delete pool ids
|
||||
ntop.delCache(self:_get_pool_ids_key())
|
||||
ntop.delCache(self:_get_next_pool_id_key())
|
||||
|
||||
-- TODO: UNLOCK
|
||||
end
|
||||
|
||||
-- ##############################################
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue