Skip to content

Commit

Permalink
added methods for janusagent configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gezimbll authored and danbogos committed Apr 9, 2024
1 parent 8836337 commit c5c8ca5
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
16 changes: 15 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (cfg *CGRConfig) loadFromJSONCfg(jsnCfg *CgrJsonCfg) (err error) {
cfg.loadMailerCfg, cfg.loadSureTaxCfg, cfg.loadDispatcherSCfg,
cfg.loadLoaderCgrCfg, cfg.loadMigratorCgrCfg, cfg.loadTLSCgrCfg,
cfg.loadAnalyzerCgrCfg, cfg.loadApierCfg, cfg.loadErsCfg, cfg.loadEesCfg,
cfg.loadSIPAgentCfg, cfg.loadRegistrarCCfg,
cfg.loadSIPAgentCfg, cfg.loadRegistrarCCfg, cfg.loadJanusAgentCfg,
cfg.loadConfigSCfg, cfg.loadAPIBanCgrCfg, cfg.loadSentryPeerCgrCfg, cfg.loadCoreSCfg} {
if err = loadFunc(jsnCfg); err != nil {
return
Expand Down Expand Up @@ -801,6 +801,14 @@ func (cfg *CGRConfig) loadSIPAgentCfg(jsnCfg *CgrJsonCfg) (err error) {
return cfg.sipAgentCfg.loadFromJSONCfg(jsnSIPAgentCfg, cfg.generalCfg.RSRSep)
}

func (cfg *CGRConfig) loadJanusAgentCfg(jsnCfg *CgrJsonCfg) (err error) {
var jsnJanusAgentCfg *JanusAgentJsonCfg
if jsnJanusAgentCfg, err = jsnCfg.JanusAgentCfgJson(); err != nil {
return
}
return cfg.janusAgentCfg.loadFromJSONCfg(jsnJanusAgentCfg, cfg.generalCfg.RSRSep)
}

// loadTemplateSCfg loads the Template section of the configuration
func (cfg *CGRConfig) loadTemplateSCfg(jsnCfg *CgrJsonCfg) (err error) {
var jsnTemplateCfg map[string][]*FcTemplateJsonCfg
Expand Down Expand Up @@ -1241,6 +1249,7 @@ func (cfg *CGRConfig) getLoadFunctions() map[string]func(*CgrJsonCfg) error {
ApierS: cfg.loadApierCfg,
RPCConnsJsonName: cfg.loadRPCConns,
SIPAgentJson: cfg.loadSIPAgentCfg,
JanusAgentJson: cfg.loadJanusAgentCfg,
TemplatesJson: cfg.loadTemplateSCfg,
ConfigSJson: cfg.loadConfigSCfg,
APIBanCfgJson: cfg.loadAPIBanCgrCfg,
Expand Down Expand Up @@ -1481,6 +1490,8 @@ func (cfg *CGRConfig) reloadSections(sections ...string) {
cfg.rldChans[THRESHOLDS_JSON] <- struct{}{}
case RouteSJson:
cfg.rldChans[RouteSJson] <- struct{}{}
case JanusAgentJson:
cfg.rldChans[JanusAgentJson] <- struct{}{}
case LoaderJson:
cfg.rldChans[LoaderJson] <- struct{}{}
case DispatcherSJson:
Expand Down Expand Up @@ -1823,6 +1834,8 @@ func (cfg *CGRConfig) V1GetConfigAsJSON(ctx *context.Context, args *SectionWithA
mp = cfg.RadiusAgentCfg().AsMapInterface(cfg.GeneralCfg().RSRSep)
case DNSAgentJson:
mp = cfg.DNSAgentCfg().AsMapInterface(cfg.GeneralCfg().RSRSep)
case JanusAgentJson:
mp = cfg.JanusAgentCfg().AsMapInterface(cfg.GeneralCfg().RSRSep)
case ATTRIBUTE_JSN:
mp = cfg.AttributeSCfg().AsMapInterface()
case ChargerSCfgJson:
Expand Down Expand Up @@ -1944,6 +1957,7 @@ func (cfg *CGRConfig) Clone() (cln *CGRConfig) {
cdrsCfg: cfg.cdrsCfg.Clone(),
sessionSCfg: cfg.sessionSCfg.Clone(),
fsAgentCfg: cfg.fsAgentCfg.Clone(),
janusAgentCfg: cfg.janusAgentCfg.Clone(),
kamAgentCfg: cfg.kamAgentCfg.Clone(),
asteriskAgentCfg: cfg.asteriskAgentCfg.Clone(),
diameterAgentCfg: cfg.diameterAgentCfg.Clone(),
Expand Down
14 changes: 13 additions & 1 deletion config/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var (
sortedCfgSections = []string{GENERAL_JSN, RPCConnsJsonName, DATADB_JSN, STORDB_JSN, LISTEN_JSN, TlsCfgJson, HTTP_JSN, SCHEDULER_JSN,
CACHE_JSN, FilterSjsn, RALS_JSN, CDRS_JSN, ERsJson, SessionSJson, AsteriskAgentJSN, FreeSWITCHAgentJSN,
KamailioAgentJSN, DA_JSN, RA_JSN, HttpAgentJson, DNSAgentJson, ATTRIBUTE_JSN, ChargerSCfgJson, RESOURCES_JSON, STATS_JSON,
THRESHOLDS_JSON, RouteSJson, LoaderJson, MAILER_JSN, SURETAX_JSON, CgrLoaderCfgJson, CgrMigratorCfgJson, DispatcherSJson,
THRESHOLDS_JSON, RouteSJson, LoaderJson, MAILER_JSN, SURETAX_JSON, CgrLoaderCfgJson, CgrMigratorCfgJson, DispatcherSJson, JanusAgentJson,
AnalyzerCfgJson, ApierS, EEsJson, SIPAgentJson, RegistrarCJson, TemplatesJson, ConfigSJson, APIBanCfgJson, SentryPeerCfgJson, CoreSCfgJson}
)

Expand Down Expand Up @@ -525,6 +525,18 @@ func (jsnCfg CgrJsonCfg) SIPAgentJsonCfg() (*SIPAgentJsonCfg, error) {
return sipAgnt, nil
}

func (jsnCfg CgrJsonCfg) JanusAgentCfgJson() (*JanusAgentJsonCfg, error) {
raw, haskey := jsnCfg[JanusAgentJson]
if !haskey {
return nil, nil
}
cfg := new(JanusAgentJsonCfg)
if err := json.Unmarshal(*raw, cfg); err != nil {
return nil, err
}
return cfg, nil
}

func (jsnCfg CgrJsonCfg) TemplateSJsonCfg() (map[string][]*FcTemplateJsonCfg, error) {
rawCfg, hasKey := jsnCfg[TemplatesJson]
if !hasKey {
Expand Down
103 changes: 103 additions & 0 deletions config/janusagntcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,113 @@ along with this program. If not, see <http://www.gnu.org/licenses/>

package config

import (
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
)

// JanusAgentCfg the config for an Janus Agent
type JanusAgentCfg struct {
Enabled bool
URL string
SessionSConns []string
RequestProcessors []*RequestProcessor
}

func (jaCfg *JanusAgentCfg) loadFromJSONCfg(jsnCfg *JanusAgentJsonCfg, separator string) (err error) {
if jsnCfg == nil {
return
}

if jsnCfg.Enabled != nil {
jaCfg.Enabled = *jsnCfg.Enabled
}

if jsnCfg.Url != nil {
jaCfg.URL = *jsnCfg.Url
}

if jsnCfg.Sessions_conns != nil {
jaCfg.SessionSConns = make([]string, len(*jsnCfg.Sessions_conns))
for idx, connID := range *jsnCfg.Sessions_conns {
jaCfg.SessionSConns[idx] = connID

if connID == utils.MetaInternal || connID == rpcclient.BiRPCInternal {
jaCfg.SessionSConns[idx] = utils.ConcatenatedKey(connID, utils.MetaSessionS)
}
}
}

if jsnCfg.RequestProcessors != nil {
for _, reqProcJsn := range *jsnCfg.RequestProcessors {
rp := new(RequestProcessor)
var haveID bool
for _, rpSet := range jaCfg.RequestProcessors {
if reqProcJsn.ID != nil && rpSet.ID == *reqProcJsn.ID {
rp = rpSet
haveID = true
break
}
}

if err = rp.loadFromJSONCfg(reqProcJsn, separator); err != nil {
return
}
if !haveID {
jaCfg.RequestProcessors = append(jaCfg.RequestProcessors, rp)
}

}
}
return
}

func (jaCfg *JanusAgentCfg) AsMapInterface(separator string) (initialMP map[string]any) {

initialMP = map[string]any{
utils.EnabledCfg: jaCfg.Enabled,
utils.URLCfg: jaCfg.URL,
}

if jaCfg.SessionSConns != nil {
sessionConns := make([]string, len(jaCfg.SessionSConns))

for i, item := range jaCfg.SessionSConns {
sessionConns[i] = item
if item == utils.ConcatenatedKey(utils.MetaInternal, utils.MetaSessionS) {
sessionConns[i] = utils.MetaInternal
} else if item == utils.ConcatenatedKey(rpcclient.BiRPCInternal, utils.MetaSessionS) {
sessionConns[i] = rpcclient.BiRPCInternal
}
}
initialMP[utils.SessionSConnsCfg] = sessionConns
}

requestProcessors := make([]map[string]any, len(jaCfg.RequestProcessors))
for i, item := range jaCfg.RequestProcessors {
requestProcessors[i] = item.AsMapInterface(separator)
}
initialMP[utils.RequestProcessorsCfg] = requestProcessors

return
}

func (jaCfg *JanusAgentCfg) Clone() *JanusAgentCfg {
cln := &JanusAgentCfg{
Enabled: jaCfg.Enabled,
URL: jaCfg.URL,
}

if jaCfg.SessionSConns != nil {
cln.SessionSConns = make([]string, len(jaCfg.SessionSConns))
copy(cln.SessionSConns, jaCfg.SessionSConns)
}

if jaCfg.RequestProcessors != nil {
cln.RequestProcessors = make([]*RequestProcessor, len(jaCfg.RequestProcessors))
for i, rp := range jaCfg.RequestProcessors {
cln.RequestProcessors[i] = rp.Clone()
}
}
return cln
}
7 changes: 7 additions & 0 deletions config/libconfig_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,13 @@ type SIPAgentJsonCfg struct {
Request_processors *[]*ReqProcessorJsnCfg
}

type JanusAgentJsonCfg struct {
Enabled *bool `json:"enabled"`
Url *string `json:"url"`
Sessions_conns *[]string `json:"sessions_conns"`
RequestProcessors *[]*ReqProcessorJsnCfg `json:"request_processors"`
}

type ConfigSCfgJson struct {
Enabled *bool
Url *string
Expand Down
2 changes: 2 additions & 0 deletions servmanager/servmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ func (srvMngr *ServiceManager) handleReload() {
go srvMngr.reloadService(utils.GlobalVarS)
case <-srvMngr.GetConfig().GetReloadChan(config.CoreSCfgJson):
go srvMngr.reloadService(utils.CoreS)
case <-srvMngr.GetConfig().GetReloadChan(config.JanusAgentJson):
go srvMngr.reloadService(utils.JanusAgent)
}
// handle RPC server
}
Expand Down

0 comments on commit c5c8ca5

Please sign in to comment.