From d12267b1c22b1deee5171a19e2799de22bf37bb4 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Thu, 18 Jun 2015 13:31:40 +0200 Subject: [PATCH] MT#12025 move NGCPConfig to new config.lua file Change-Id: I5ce296dd8cd77317b642e0b5988487bf8922d3b3 (cherry picked from commit 9afb7095d74d6c51887359e9fa2f20630fd858fa) --- ngcp/config.lua | 119 +++++++++++++++++++++++++++++++++++++++++++ ngcp/ngcp.lua | 100 +----------------------------------- tests/ngcp_dp.lua | 1 + tests/ngcp_pp.lua | 3 +- tests/ngcp_pprof.lua | 1 + tests/ngcp_up.lua | 1 + 6 files changed, 125 insertions(+), 100 deletions(-) create mode 100644 ngcp/config.lua diff --git a/ngcp/config.lua b/ngcp/config.lua new file mode 100644 index 0000000..f66207e --- /dev/null +++ b/ngcp/config.lua @@ -0,0 +1,119 @@ +-- +-- Copyright 2015 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". +-- + +-- load drivers +local driver = require "luasql.mysql" +if not luasql then + luasql = driver +end +-- class NGCPConfig +NGCPConfig = { + __class__ = 'NGCPConfig' +} +NGCPConfig_MT = { __index = NGCPConfig } + + function NGCPConfig:new() + local t = { + db_host = "127.0.0.1", + db_port = 3306, + db_username = "kamailio", + db_pass = "somepasswd", + db_database = "kamailio", + default = { + contract = { + }, + peer = { + sst_enable = "yes", + sst_expires = 300, + sst_min_timer = 90, + sst_max_timer = 7200, + sst_refresh_method = "UPDATE_FALLBACK_INVITE", + outbound_from_user = "npn", + inbound_upn = "from_user", + inbound_npn = "from_user", + inbound_uprn = "from_user", + ip_header = "P-NGCP-Src-Ip", + }, + dom = { + sst_enable = "yes", + sst_expires = 300, + sst_min_timer = 90, + sst_max_timer = 7200, + sst_refresh_method = "UPDATE_FALLBACK_INVITE", + outbound_from_user = "npn", + inbound_upn = "from_user", + inbound_uprn = "from_user", + ip_header = "P-NGCP-Src-Ip", + }, + -- just for prefs that are only on usr level + usr = { + account_id = 0, + ext_subscriber_id = "", + ext_contract_id = "", + ringtimeout = 180, + } + } + } + setmetatable( t, NGCPConfig_MT ) + return t + end + + local function check_connection(c) + local cur = c:execute("SELECT 1") + local row = cur:fetch() + local result = false + if cur:numrows() == 1 then + result = true + end + cur:close() + return result + end + + function NGCPConfig:getDBConnection() + if not self.env then + self.env = assert (luasql.mysql()) + end + if self.con then + local ok,err = pcall(check_connection, self.con) + if not ok then + self.con = nil + sr.log("dbg", "lost database connection. Reconnecting") + end + end + if not self.con then + sr.log("dbg","connecting to mysql") + self.con = self.env:connect( self.db_database, + self.db_username, self.db_pass, self.db_host, self.db_port) + end + return self.con + end + + function NGCPConfig:get_defaults(vtype) + local k,v + local defs = {} + + if self.default[vtype] then + for k,v in pairs(self.default[vtype]) do + defs[k] = v + end + end + return defs + end +-- class \ No newline at end of file diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index b0e35e3..8b7d37b 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -23,105 +23,7 @@ require 'ngcp.pp' require 'ngcp.dp' require 'ngcp.up' require 'ngcp.rp' --- load drivers -local driver = require "luasql.mysql" -if not luasql then - luasql = driver -end --- class NGCPConfig -NGCPConfig = { - __class__ = 'NGCPConfig' -} -NGCPConfig_MT = { __index = NGCPConfig } - - function NGCPConfig:new() - local t = { - db_host = "127.0.0.1", - db_port = 3306, - db_username = "kamailio", - db_pass = "somepasswd", - db_database = "kamailio", - default = { - contract = { - }, - peer = { - sst_enable = "yes", - sst_expires = 300, - sst_min_timer = 90, - sst_max_timer = 7200, - sst_refresh_method = "UPDATE_FALLBACK_INVITE", - outbound_from_user = "npn", - inbound_upn = "from_user", - inbound_npn = "from_user", - inbound_uprn = "from_user", - ip_header = "P-NGCP-Src-Ip", - }, - dom = { - sst_enable = "yes", - sst_expires = 300, - sst_min_timer = 90, - sst_max_timer = 7200, - sst_refresh_method = "UPDATE_FALLBACK_INVITE", - outbound_from_user = "npn", - inbound_upn = "from_user", - inbound_uprn = "from_user", - ip_header = "P-NGCP-Src-Ip", - }, - -- just for prefs that are only on usr level - usr = { - account_id = 0, - ext_subscriber_id = "", - ext_contract_id = "", - ringtimeout = 180, - } - } - } - setmetatable( t, NGCPConfig_MT ) - return t - end - - local function check_connection(c) - local cur = c:execute("SELECT 1") - local row = cur:fetch() - local result = false - if cur:numrows() == 1 then - result = true - end - cur:close() - return result - end - - function NGCPConfig:getDBConnection() - if not self.env then - self.env = assert (luasql.mysql()) - end - if self.con then - local ok,err = pcall(check_connection, self.con) - if not ok then - self.con = nil - sr.log("dbg", "lost database connection. Reconnecting") - end - end - if not self.con then - sr.log("dbg","connecting to mysql") - self.con = self.env:connect( self.db_database, - self.db_username, self.db_pass, self.db_host, self.db_port) - end - return self.con - end - - function NGCPConfig:get_defaults(vtype) - local k,v - local defs = {} - - if self.default[vtype] then - for k,v in pairs(self.default[vtype]) do - defs[k] = v - end - end - return defs - end --- class +require 'ngcp.config' -- class NGCP NGCP = { diff --git a/tests/ngcp_dp.lua b/tests/ngcp_dp.lua index e9feb74..22e3112 100644 --- a/tests/ngcp_dp.lua +++ b/tests/ngcp_dp.lua @@ -39,6 +39,7 @@ package.preload['luasql.mysql'] = function () return env end end +require 'ngcp.config' require 'ngcp.dp' TestNGCPDomainPrefs = {} --class diff --git a/tests/ngcp_pp.lua b/tests/ngcp_pp.lua index 0d9101a..eff5b0b 100644 --- a/tests/ngcp_pp.lua +++ b/tests/ngcp_pp.lua @@ -39,7 +39,8 @@ package.preload['luasql.mysql'] = function () return env end end -require 'ngcp.dp' +require 'ngcp.config' +require 'ngcp.rp' TestNGCPPeerPrefs = {} --class diff --git a/tests/ngcp_pprof.lua b/tests/ngcp_pprof.lua index 25caa8e..5b0f7f9 100644 --- a/tests/ngcp_pprof.lua +++ b/tests/ngcp_pprof.lua @@ -39,6 +39,7 @@ package.preload['luasql.mysql'] = function () return env end end +require 'ngcp.config' require 'ngcp.pprof' TestNGCPProfilePrefs = {} --class diff --git a/tests/ngcp_up.lua b/tests/ngcp_up.lua index 8f956ba..1af95d3 100644 --- a/tests/ngcp_up.lua +++ b/tests/ngcp_up.lua @@ -40,6 +40,7 @@ package.preload['luasql.mysql'] = function () end end +require 'ngcp.config' require 'ngcp.up' TestNGCPUserPrefs = {} --class