diff --git a/ngcp/config.lua b/ngcp/config.lua index 3dae668..960cf5d 100644 --- a/ngcp/config.lua +++ b/ngcp/config.lua @@ -72,6 +72,11 @@ local NGCPConfig_MT = { __index = NGCPConfig } ext_contract_id = "", ringtimeout = 180, } + }, + -- blob prefs to be loaded automaticaly + blob_prefs = { + emergency_provider_info = true, + emergency_location_object = true, } } setmetatable( t, NGCPConfig_MT ) diff --git a/ngcp/pref.lua b/ngcp/pref.lua index 5075cbe..b3716c8 100644 --- a/ngcp/pref.lua +++ b/ngcp/pref.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013-2020 SipWise Team +-- Copyright 2013-2021 SipWise Team -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -25,6 +25,7 @@ local NGCPXAvp = require 'ngcp.xavp' local NGCPPrefs = utils.inheritsFrom() NGCPPrefs.__class__ = 'NGCPPrefs' NGCPPrefs.levels = {"caller", "callee"} +NGCPPrefs.query_blob = "SELECT * FROM provisioning.voip_%s_blob WHERE id = %s" function NGCPPrefs.__tostring(self) local output, msg = '', "%s_%s:%s\n" @@ -58,6 +59,37 @@ function NGCPPrefs:_defaults() return keys, defaults end +function NGCPPrefs:pref_is_blob(attribute, value_type) + local vtype = value_type + + if type(value_type) == "string" then + vtype = tonumber(value_type) + end + if vtype == 2 then + if self.config.blob_prefs[attribute] then return true end + KSR.dbg(string.format("skip load of blob value of attribute:%s\n", + attribute)) + end + return false +end + +function NGCPPrefs:_get_blob(blob_id) + local result + local con = assert (self.config:getDBConnection()) + local query = self.query_blob:format(self.db_table, blob_id) + local cur = assert (con:execute(query)) + local row = cur:fetch({}, "a") + + if utable.size(row) > 0 then + KSR.dbg(string.format("row content_type:%s\n", row.content_type)) + result = row.value + else + KSR.dbg(string.format("no results for query:%s\n", query)) + end + cur:close() + return result +end + function NGCPPrefs:_set_xavp(level, cur, query) local result = {} local row = cur:fetch({}, "a") @@ -66,8 +98,12 @@ function NGCPPrefs:_set_xavp(level, cur, query) if utable.size(row) > 0 then while utable.size(row) > 0 do - KSR.dbg(string.format("result:%s row:%s\n", - utable.tostring(result), utable.tostring(row))) + if self:pref_is_blob(row.attribute, row.type) then + row.value = self:_get_blob(row.value) + row.type = 0 + end + KSR.dbg(string.format("row attribute:%s\n", + tostring(row.attribute))) table.insert(result, row) utable.add(keys, row.attribute) defaults[row.attribute] = nil diff --git a/tests/ngcp_dp.lua b/tests/ngcp_dp.lua index 8d8cf37..8417d48 100644 --- a/tests/ngcp_dp.lua +++ b/tests/ngcp_dp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013-2020 SipWise Team +-- Copyright 2013-2021 SipWise Team -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ KSR = ksrMock.new() local mc,env,con local dp_vars = DPFetch:new() +local bp_vars = require 'tests_v.bp_vars' package.loaded.luasql = nil package.preload['luasql.mysql'] = function () @@ -186,5 +187,27 @@ TestNGCPDomainPrefs = {} --class caller_xavp("otherfoo","foo") lu.assertEquals(tostring(self.d), 'caller_dom_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_dom_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') end + + function TestNGCPDomainPrefs:test_caller_load_blob() + lu.assertNotNil(self.d.config) + con:execute("SELECT * FROM dom_preferences WHERE domain ='192.168.51.57'") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(dp_vars:val("d_192_168_51_57")) + con:execute("SELECT * FROM provisioning.voip_dom_preferences_blob WHERE id = 2") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(bp_vars[2]) + self.cur:close() + self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:close() + + mc:replay() + local keys = self.d:caller_load("192.168.51.57") + mc:verify() + + lu.assertTrue(utable.contains(keys, "emergency_provider_info")) + lu.assertStrMatches( + KSR.pv.get("$xavp(caller_dom_prefs=>emergency_provider_info)"), + '^<.xml.+' + ) + end + -- class TestNGCPDomainPrefs --EOF diff --git a/tests/ngcp_up.lua b/tests/ngcp_up.lua index 833ad5c..530b0c1 100644 --- a/tests/ngcp_up.lua +++ b/tests/ngcp_up.lua @@ -22,6 +22,7 @@ local lemock = require('lemock') local utils = require 'ngcp.utils' local utable = utils.table local UPFetch = require 'tests_v.up_vars' +local bp_vars = require 'tests_v.bp_vars' local ksrMock = require 'mocks.ksr' KSR = ksrMock.new() @@ -247,4 +248,39 @@ TestNGCPUserPrefs = {} --class lu.assertEquals(self.d:__tostring(), expected) lu.assertEquals(tostring(self.d), expected) end + + function TestNGCPUserPrefs:test_callee_load_blob() + lu.assertNotNil(self.d.config) + con:execute("SELECT * FROM usr_preferences WHERE uuid ='b3431971-79ab-466e-adbb-81d92a9c94fa' ORDER BY id DESC") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(up_vars:val("b3431971_79ab_466e_adbb_81d92a9c94fa")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(up_vars:val("b3431971_79ab_466e_adbb_81d92a9c94fa")) + con:execute("SELECT * FROM provisioning.voip_usr_preferences_blob WHERE id = 1") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(bp_vars[1]) + self.cur:close() + self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:close() + + mc:replay() + local keys = self.d:callee_load("b3431971-79ab-466e-adbb-81d92a9c94fa") + mc:verify() + + local lkeys = { + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "emergency_location_format", + "emergency_location_object" + } + + lu.assertItemsEquals(keys, lkeys) + lu.assertEquals( + KSR.pv.get("$xavp(callee_usr_prefs=>emergency_location_format)"), + "PIDF-LO" + ) + lu.assertStrMatches( + KSR.pv.get("$xavp(callee_usr_prefs=>emergency_location_object)"), + '^<.xml.+' + ) + end -- class TestNGCPUserPrefs diff --git a/tests_v/bp_vars.lua b/tests_v/bp_vars.lua new file mode 100644 index 0000000..4dd1f16 --- /dev/null +++ b/tests_v/bp_vars.lua @@ -0,0 +1,100 @@ +-- +-- Copyright 2021 SipWise Team +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- + +local bp_vars = { + { + id = 1, + preference_id = 14, + content_type = 'application/xml', + value = [[ + + + + + + + + 32.86726 -97.16054 + + + + + false + + 2010-11-14T20:00:00Z + + + 802.11 + + mac:1234567890ab + 2010-11-04T20:57:29Z + + + + + + US + Texas + Colleyville + Treemont + Circle + 3913 + 1 + Haley's Place + 76034 + + + + false + + 2010-11-14T20:00:00Z + + + triangulation + + 2010-11-04T12:28:04Z + + +]] + }, + { + id = 2, + preference_id = 2, + content_type = 'application/xml', + value = [[ + + + Telekom Deutschland + D124 + sip:+492281234567@t-mobile.de;user=phone + BNetzA + +]] + } +} + +return bp_vars diff --git a/tests_v/dp_vars.lua b/tests_v/dp_vars.lua index f1fe68b..c41d5f7 100644 --- a/tests_v/dp_vars.lua +++ b/tests_v/dp_vars.lua @@ -152,6 +152,18 @@ local dp_vars = { value = "4", last_modified = "1900-01-01 00:00:01" } + }, + d_192_168_51_57 = { + { + id = 14, + uuid = "", + username = "0", + domain = "192.168.51.57", + attribute = "emergency_provider_info", + type = 2, + value = 2, + last_modified = "1900-01-01 00:00:01" + }, } } diff --git a/tests_v/up_vars.lua b/tests_v/up_vars.lua index c291c8d..3a450ee 100644 --- a/tests_v/up_vars.lua +++ b/tests_v/up_vars.lua @@ -133,6 +133,28 @@ local up_vars = { value = "no", last_modified = "1900-01-01 00:00:01" }, + }, + b3431971_79ab_466e_adbb_81d92a9c94fa = { + { + id = 1, + uuid = "b3431971-79ab-466e-adbb-81d92a9c94fa", + username = "testuser3", + domain = "192.168.51.57", + attribute = "emergency_location_format", + type = 0, + value = "PIDF-LO", + last_modified = "1900-01-01 00:00:01" + }, + { + id = 2, + uuid = "b3431971-79ab-466e-adbb-81d92a9c94fa", + username = "testuser3", + domain = "192.168.51.57", + attribute = "emergency_location_object", + type = 2, + value = 1, + last_modified = "1900-01-01 00:00:01" + }, } }