From 471aa9a994930ae7b91ef2a926f635354802b543 Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Thu, 12 Jun 2014 21:04:40 +0200 Subject: [PATCH] MT#7471 Implement contract pref loading. --- ngcp/cp.lua | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ ngcp/ngcp.lua | 28 +++++++++-- ngcp/rp.lua | 26 ++++++++++- 3 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 ngcp/cp.lua diff --git a/ngcp/cp.lua b/ngcp/cp.lua new file mode 100644 index 0000000..8cbd5a2 --- /dev/null +++ b/ngcp/cp.lua @@ -0,0 +1,126 @@ +-- +-- Copyright 2013 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". +-- +require 'ngcp.utils' +require 'ngcp.pref' + +-- class NGCPContractPrefs +NGCPContractPrefs = { + __class__ = 'NGCPContractPrefs' +} +NGCPContractPrefs_MT = { __index = NGCPContractPrefs } + +NGCPContractPrefs_MT.__tostring = function () + local output = '' + local xavp = NGCPXAvp:new('caller','contract_prefs') + output = string.format("caller_contract_prefs:%s\n", tostring(xavp)) + xavp = NGCPXAvp:new('callee','contract_prefs') + output = output .. string.format("callee_contract_prefs:%s\n", tostring(xavp)) + return output + end + + function NGCPContractPrefs:new(config) + local t = { + config = config, + db_table = "contract_preferences" + } + -- creates xavp contract + NGCPPrefs.init("contract_prefs") + return setmetatable( t, NGCPContractPrefs_MT ) + end + + function NGCPContractPrefs:caller_load(contract) + if not contract then + return {} + end + return NGCPContractPrefs._load(self,"caller",contract) + end + + function NGCPContractPrefs:callee_load(contract) + if not contract then + return {} + end + return NGCPContractPrefs._load(self,"callee",contract) + end + + function NGCPContractPrefs:_defaults(level) + local defaults = self.config:get_defaults('contract') + local keys = {} + local k,_ + + if defaults then + for k,_ in pairs(defaults) do + table.insert(keys, k) + end + end + return keys, defaults + end + + function NGCPContractPrefs:_load(level, contract) + local con = self.config:getDBConnection() + local query = "SELECT * FROM " .. self.db_table .. " WHERE uuid ='" .. contract .."'" + local cur = con:execute(query) + local defaults + local keys + local result = {} + local row = cur:fetch({}, "a") + local k,v + local xavp + + keys, defaults = self:_defaults(level) + + if row then + while row do + --sr.log("info", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) + table.insert(result, row) + table.add(keys, row.attribute) + defaults[row.attribute] = nil + row = cur:fetch({}, "a") + end + else + sr.log("dbg", string.format("no results for query:%s", query)) + end + cur:close() + con:close() + + xavp = self:xavp(level, result) + for k,v in pairs(defaults) do + xavp(k, v) + end + + return keys + end + + function NGCPContractPrefs:xavp(level, l) + if level ~= 'caller' and level ~= 'callee' then + error(string.format("unknown level:%s. It has to be [caller|callee]", tostring(level))) + end + return NGCPXAvp:new(level,'contract_prefs', l) + end + + function NGCPContractPrefs:clean(vtype) + if not vtype then + NGCPContractPrefs:xavp('callee'):clean() + NGCPContractPrefs:xavp('caller'):clean() + else + NGCPContractPrefs:xavp(vtype):clean() + end + end +-- class +--EOF diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index dbe9b95..a5408a0 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -17,6 +17,7 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- +require 'ngcp.cp' require 'ngcp.pp' require 'ngcp.dp' require 'ngcp.up' @@ -40,6 +41,8 @@ NGCPConfig_MT = { __index = NGCPConfig } db_pass = "somepasswd", db_database = "kamailio", default = { + contract = { + }, peer = { sst_enable = "yes", sst_expires = 300, @@ -122,14 +125,31 @@ end config = NGCPConfig:new() } t.prefs = { - dom = NGCPDomainPrefs:new(t.config), - usr = NGCPUserPrefs:new(t.config), - peer = NGCPPeerPrefs:new(t.config), - real = NGCPRealPrefs:new(t.config), + dom = NGCPDomainPrefs:new(t.config), + usr = NGCPUserPrefs:new(t.config), + peer = NGCPPeerPrefs:new(t.config), + real = NGCPRealPrefs:new(t.config), + contract = NGCPContractPrefs:new(t.config), } return t end + function NGCP:caller_contract_load(contract) + local _,v, xvap + local keys = self.prefs.contract:caller_load(contract) + + self.prefs.real:caller_contract_load(keys) + return keys + end + + function NGCP:callee_contract_load(contract) + local _,v, xvap + local keys = self.prefs.contract:callee_load(contract) + + self.prefs.real:callee_contract_load(keys) + return keys + end + function NGCP:caller_peer_load(peer) local _,v, xvap local keys = self.prefs.peer:caller_load(peer) diff --git a/ngcp/rp.lua b/ngcp/rp.lua index 032e011..6cd1dd6 100644 --- a/ngcp/rp.lua +++ b/ngcp/rp.lua @@ -42,6 +42,14 @@ NGCPRealPrefs_MT.__tostring = function () return setmetatable( t, NGCPRealPrefs_MT ) end + function NGCPRealPrefs:caller_contract_load(keys) + return NGCPRealPrefs:_contract_load("caller", keys) + end + + function NGCPRealPrefs:callee_contract_load(keys) + return NGCPRealPrefs:_contract_load("callee", keys) + end + function NGCPRealPrefs:caller_peer_load(keys) return NGCPRealPrefs:_peer_load("caller", keys) end @@ -58,6 +66,22 @@ NGCPRealPrefs_MT.__tostring = function () return NGCPRealPrefs:_usr_load("callee", keys) end + function NGCPRealPrefs:_contract_load(level, keys) + local _,v + local xavp = { + peer = NGCPContractPrefs:xavp(level), + } + local contract_keys = {} + local values = sr.xavp.get(xavp.contract.name, 0, 0) + for _,v in pairs(keys) do + local value = values[v] + if value then + table.add(contract_keys, v) + end + end + return contract_keys + end + function NGCPRealPrefs:_peer_load(level, keys) local _,v local xavp = { @@ -119,4 +143,4 @@ NGCPRealPrefs_MT.__tostring = function () end end -- class ---EOF \ No newline at end of file +--EOF