From 0e091264c642fda732263c197914155d3f53d6a2 Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Fri, 12 Dec 2025 11:53:22 +0100 Subject: [PATCH] MT#63923 Add support for loading based on prefs group Add support of loading based on the name of the preferences group (e.g. "Media Codec Transcoding Options") For that to work extend functionality on levels of: - root of the framework (NGCP) - peers prefrences handling (NGCPPeerPrefs) - real preferences handling (NGCPRealPrefs) Then in the kamailio script use it like: lua_run("ngcp_caller_peer_load_group", "$avp(s:peer_id)", "Media Codec Transcoding Options") Change-Id: I3b4bca6fc0959550366fc18268f7e355571385cb --- ngcp/ngcp.lua | 12 ++++++++++ ngcp/pp.lua | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ ngcp/rp.lua | 8 +++++++ 3 files changed, 82 insertions(+) diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index 5c70f03..aa2b6c0 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -98,6 +98,18 @@ function NGCP:callee_peer_load(peer) return keys end +function NGCP:caller_peer_load_group(peer, group_name) + local keys = self.prefs.peer:caller_load_group(peer, group_name) + self.prefs.real:caller_peer_load_group(keys) + return keys +end + +function NGCP:callee_peer_load_group(peer, group_name) + local keys = self.prefs.peer:callee_load_group(peer, group_name) + self.prefs.real:callee_peer_load_group(keys) + return keys +end + function NGCP:caller_usr_load(uuid, domain) local keys = { domain = self.prefs.dom:caller_load(domain), diff --git a/ngcp/pp.lua b/ngcp/pp.lua index e1c9bcc..d619ae6 100644 --- a/ngcp/pp.lua +++ b/ngcp/pp.lua @@ -28,6 +28,23 @@ NGCPPeerPrefs.__class__ = 'NGCPPeerPrefs' NGCPPeerPrefs.group = "peer_prefs" NGCPPeerPrefs.db_table = "peer_preferences" NGCPPeerPrefs.query = "SELECT * FROM %s WHERE uuid = '%s'" +-- joins three tables: +-- *kamailio.peer_preferences +-- *provisioning.voip_preferences +-- *provisioning.voip_preference_groups +-- links peers attributes to the preferences list, +-- to the preferences group id +NGCPPeerPrefs.group_query = [[ +SELECT kp.* + FROM kamailio.peer_preferences AS kp + JOIN provisioning.voip_preferences AS vp + ON vp.attribute = kp.attribute + JOIN provisioning.voip_preference_groups AS vpg + ON vpg.id = vp.voip_preference_groups_id + WHERE kp.uuid = '%s' AND vpg.id = %s +]] +NGCPPeerPrefs.select_id_query = "SELECT id FROM provisioning.voip_preference_groups WHERE name = '%s'" + -- luacheck: globals KSR function NGCPPeerPrefs:new(config) local instance = NGCPPeerPrefs:create() @@ -46,5 +63,50 @@ function NGCPPeerPrefs:clean(vtype) end end +function NGCPPeerPrefs:get_pref_group_id(name) + local con = assert(self.config:getDBConnection()) + local query = self.select_id_query:format(name) + local cur = assert(con:execute(query)) + local row = cur:fetch({}, "a") + cur:close() + + if row and row.id then + return tonumber(row.id) + end + + KSR.err(string.format("[NGCP] preference group '%s' not found\n", name)) + return nil +end + +function NGCPPeerPrefs:load_group(level, uuid, group_id) + local con = assert(self.config:getDBConnection()) + local query = self.group_query:format(uuid, group_id) + local cur = assert(con:execute(query)) + + return self:_set_xavp(level, cur, query) +end + +function NGCPPeerPrefs:caller_load_group(uuid, name) + local group_id = self:get_pref_group_id(name) + if not group_id then + KSR.err("[NGCP] Cannot load group '%s', skipping\n", name) + return {} + end + + if not uuid or uuid == '' then return {} end + return self:load_group("caller", uuid, group_id) +end + +function NGCPPeerPrefs:callee_load_group(uuid, name) + local group_id = self:get_pref_group_id(name) + if not group_id then + KSR.err("[NGCP] Cannot load group '%s', skipping\n", name) + return {} + end + + if not uuid or uuid == '' then return {} end + return self:load_group("callee", uuid, group_id) +end + -- class return NGCPPeerPrefs diff --git a/ngcp/rp.lua b/ngcp/rp.lua index 7ee0561..3c3fa7a 100644 --- a/ngcp/rp.lua +++ b/ngcp/rp.lua @@ -63,6 +63,14 @@ function NGCPRealPrefs:callee_peer_load(keys) return self:_peer_load("callee", keys) end +function NGCPRealPrefs:caller_peer_load_group(keys) + return self:_peer_load("caller", keys) +end + +function NGCPRealPrefs:callee_peer_load_group(keys) + return self:_peer_load("callee", keys) +end + function NGCPRealPrefs:caller_usr_load(keys) return self:_usr_load("caller", keys) end