From e9ba47beabb0563c6b805e34b4eec0ac2d3f8709 Mon Sep 17 00:00:00 2001 From: DanB Date: Tue, 5 Aug 2014 18:17:10 +0200 Subject: [PATCH] Individual configuration in sm/freeswitch for capturing extra fields --- config/config.go | 10 ++++++++++ config/config_test.go | 2 ++ config/test_data.txt | 1 + data/conf/cgrates.cfg | 1 + sessionmanager/event.go | 2 ++ sessionmanager/fsevent_test.go | 11 +++++++++++ sessionmanager/osipsevent.go | 15 ++++++++------- sessionmanager/osipsevent_test.go | 4 ++-- 8 files changed, 37 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 1d12ba650..4a1f1a45a 100644 --- a/config/config.go +++ b/config/config.go @@ -127,6 +127,7 @@ type CGRConfig struct { FSLowBalanceAnnFile string // File to be played when low balance is reached FSEmptyBalanceContext string // If defined, call will be transfered to this context on empty balance FSEmptyBalanceAnnFile string // File to be played before disconnecting prepaid calls (applies only if no context defined) + FSCdrExtraFields []*utils.RSRField // Extra fields to store in CDRs in case of processing them OsipsListenUdp string // Address where to listen for event datagrams coming from OpenSIPS OsipsMiAddr string // Adress where to reach OpenSIPS mi_datagram module OsipsEvSubscInterval time.Duration // Refresh event subscription at this interval @@ -229,6 +230,7 @@ func (self *CGRConfig) setDefaults() error { self.FSLowBalanceAnnFile = "" self.FSEmptyBalanceContext = "" self.FSEmptyBalanceAnnFile = "" + self.FSCdrExtraFields = []*utils.RSRField{} self.OsipsListenUdp = "127.0.0.1:2020" self.OsipsMiAddr = "127.0.0.1:8020" self.OsipsEvSubscInterval = time.Duration(60) * time.Second @@ -599,6 +601,14 @@ func loadConfig(c *conf.ConfigFile) (*CGRConfig, error) { if hasOpt = c.HasOption("freeswitch", "empty_balance_ann_file"); hasOpt { cfg.FSEmptyBalanceAnnFile, _ = c.GetString("freeswitch", "empty_balance_ann_file") } + if hasOpt = c.HasOption("freeswitch", "cdr_extra_fields"); hasOpt { + extraFieldsStr, _ := c.GetString("freeswitch", "cdr_extra_fields") + if extraFields, err := utils.ParseRSRFields(extraFieldsStr, utils.FIELDS_SEP); err != nil { + return nil, err + } else { + cfg.FSCdrExtraFields = extraFields + } + } if hasOpt = c.HasOption("opensips", "listen_udp"); hasOpt { cfg.OsipsListenUdp, _ = c.GetString("opensips", "listen_udp") } diff --git a/config/config_test.go b/config/config_test.go index 3a287ab29..a59dd0bb3 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -129,6 +129,7 @@ func TestDefaults(t *testing.T) { eCfg.FSLowBalanceAnnFile = "" eCfg.FSEmptyBalanceContext = "" eCfg.FSEmptyBalanceAnnFile = "" + eCfg.FSCdrExtraFields = []*utils.RSRField{} eCfg.OsipsListenUdp = "127.0.0.1:2020" eCfg.OsipsMiAddr = "127.0.0.1:8020" eCfg.OsipsEvSubscInterval = time.Duration(60) * time.Second @@ -286,6 +287,7 @@ func TestConfigFromFile(t *testing.T) { eCfg.FSLowBalanceAnnFile = "test" eCfg.FSEmptyBalanceContext = "test" eCfg.FSEmptyBalanceAnnFile = "test" + eCfg.FSCdrExtraFields = []*utils.RSRField{&utils.RSRField{Id: "test"}} eCfg.OsipsListenUdp = "test" eCfg.OsipsMiAddr = "test" eCfg.OsipsEvSubscInterval = time.Duration(99) * time.Second diff --git a/config/test_data.txt b/config/test_data.txt index b405bcc9a..23e6d26c1 100644 --- a/config/test_data.txt +++ b/config/test_data.txt @@ -129,6 +129,7 @@ min_dur_low_balance = 99 # Threshold which will trigger low balance warnin low_balance_ann_file = test # File to be played when low balance is reached empty_balance_context = test # If defined, call will be transfered to this context on empty balance empty_balance_ann_file = test # File to be played before disconnecting prepaid calls (applies only if no context defined) +cdr_extra_fields = test # Extra fields to store in CDRs in case of processing them [opensips] listen_udp = test # Address where to listen for event datagrams coming from OpenSIPS diff --git a/data/conf/cgrates.cfg b/data/conf/cgrates.cfg index ac074fb2f..88e042b0d 100644 --- a/data/conf/cgrates.cfg +++ b/data/conf/cgrates.cfg @@ -135,6 +135,7 @@ # low_balance_ann_file = # File to be played when low balance is reached for prepaid calls # empty_balance_context = # If defined, prepaid calls will be transfered to this context on empty balance # empty_balance_ann_file = # File to be played before disconnecting prepaid calls on empty balance (applies only if no context defined) +# cdr_extra_fields = # Extra fields to store in CDRs in case of processing them [opensips] # listen_udp = 127.0.0.1:2020 # Address where to listen for datagram events coming from OpenSIPS diff --git a/sessionmanager/event.go b/sessionmanager/event.go index 5e36d6be7..914c60f73 100644 --- a/sessionmanager/event.go +++ b/sessionmanager/event.go @@ -40,6 +40,8 @@ type Event interface { GetAnswerTime(string) (time.Time, error) GetEndTime() (time.Time, error) GetDuration(string) (time.Duration, error) + GetOriginatorIP(string) string + GetExtraFields() map[string]string MissingParameter() bool ParseEventValue(*utils.RSRField) string PassesFieldFilter(*utils.RSRField) (bool, string) diff --git a/sessionmanager/fsevent_test.go b/sessionmanager/fsevent_test.go index c6e82a5c8..a194ddef1 100644 --- a/sessionmanager/fsevent_test.go +++ b/sessionmanager/fsevent_test.go @@ -627,3 +627,14 @@ func TestFsEvAsStoredCdr(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", eStoredCdr, storedCdr) } } + +func TestFsEvGetExtraFields(t *testing.T) { + cfg, _ = config.NewDefaultCGRConfig() + cfg.FSCdrExtraFields = []*utils.RSRField{&utils.RSRField{Id: "Channel-Read-Codec-Name"}, &utils.RSRField{Id: "Channel-Write-Codec-Name"}, &utils.RSRField{Id: "NonExistingHeader"}} + config.SetCgrConfig(cfg) + ev := new(FSEvent).New(hangupEv) + expectedExtraFields := map[string]string{"Channel-Read-Codec-Name": "G722", "Channel-Write-Codec-Name": "G722", "NonExistingHeader": ""} + if extraFields := ev.GetExtraFields(); !reflect.DeepEqual(extraFields, extraFields) { + t.Errorf("Expecting: %+v, received: %+v", expectedExtraFields, extraFields) + } +} diff --git a/sessionmanager/osipsevent.go b/sessionmanager/osipsevent.go index 67c80d4cb..21342f5e9 100644 --- a/sessionmanager/osipsevent.go +++ b/sessionmanager/osipsevent.go @@ -161,6 +161,12 @@ func (osipsev *OsipsEvent) GetDuration(fieldName string) (time.Duration, error) } return utils.ParseDurationWithSecs(durStr) } +func (osipsEv *OsipsEvent) GetOriginatorIP(fieldName string) string { + if osipsEv.osipsEvent == nil || osipsEv.osipsEvent.OriginatorAddress == nil { + return "" + } + return osipsEv.osipsEvent.OriginatorAddress.IP.String() +} func (osipsev *OsipsEvent) MissingParameter() bool { return len(osipsev.GetUUID()) == 0 || len(osipsev.GetAccount(utils.META_DEFAULT)) == 0 || @@ -184,18 +190,13 @@ func (osipsev *OsipsEvent) GetExtraFields() map[string]string { } return extraFields } -func (osipsEv *OsipsEvent) GetOriginatorIP() string { - if osipsEv.osipsEvent == nil || osipsEv.osipsEvent.OriginatorAddress == nil { - return "" - } - return osipsEv.osipsEvent.OriginatorAddress.IP.String() -} + func (osipsEv *OsipsEvent) AsStoredCdr() *utils.StoredCdr { storCdr := new(utils.StoredCdr) storCdr.CgrId = osipsEv.GetCgrId() storCdr.TOR = utils.VOICE storCdr.AccId = osipsEv.GetUUID() - storCdr.CdrHost = osipsEv.GetOriginatorIP() + storCdr.CdrHost = osipsEv.GetOriginatorIP(utils.META_DEFAULT) storCdr.CdrSource = "OSIPS_" + osipsEv.GetName() storCdr.ReqType = osipsEv.GetReqType(utils.META_DEFAULT) storCdr.Direction = osipsEv.GetDirection(utils.META_DEFAULT) diff --git a/sessionmanager/osipsevent_test.go b/sessionmanager/osipsevent_test.go index ec2edbf22..e76faa457 100644 --- a/sessionmanager/osipsevent_test.go +++ b/sessionmanager/osipsevent_test.go @@ -91,7 +91,7 @@ func TestOsipsEventGetValues(t *testing.T) { !answerTime.Equal(eAnswerTime) || !endTime.Equal(eAnswerTime.Add(dur)) || dur != time.Duration(20*time.Second) || - osipsEv.GetOriginatorIP() != "172.16.254.77" { + osipsEv.GetOriginatorIP(utils.META_DEFAULT) != "172.16.254.77" { t.Error("GetValues not matching: ", osipsEv.GetName() != "E_ACC_CDR", osipsEv.GetCgrId() != utils.Sha1("ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ"+";"+"eb082607"+";"+"4ea9687f", setupTime.UTC().String()), osipsEv.GetUUID() != "ODVkMDI2Mzc2MDY5N2EzODhjNTAzNTdlODhiZjRlYWQ;eb082607;4ea9687f", @@ -107,7 +107,7 @@ func TestOsipsEventGetValues(t *testing.T) { !answerTime.Equal(time.Date(2014, 7, 26, 12, 28, 19, 0, time.Local)), !endTime.Equal(time.Date(2014, 7, 26, 12, 28, 39, 0, time.Local)), dur != time.Duration(20*time.Second), - osipsEv.GetOriginatorIP() != "172.16.254.77", + osipsEv.GetOriginatorIP(utils.META_DEFAULT) != "172.16.254.77", ) } }