From 774dc123c8d8d9b89d2fdfd91305524200dd9c74 Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 3 Nov 2017 16:48:28 +0100 Subject: [PATCH] ExternalCDR with time string as Usage for *voice ToR, diameter test fixes --- agents/dmtagent.go | 3 +- agents/dmtagent_it_test.go | 105 +++++++++++++++--------- agents/libdmt.go | 20 ++--- agents/libdmt_test.go | 14 ++-- agents/radagent_it_test.go | 2 +- data/conf/samples/dmtagent/cgrates.json | 4 + engine/cdr.go | 9 +- 7 files changed, 95 insertions(+), 62 deletions(-) diff --git a/agents/dmtagent.go b/agents/dmtagent.go index cdf75b279..f5be11dcb 100644 --- a/agents/dmtagent.go +++ b/agents/dmtagent.go @@ -75,7 +75,8 @@ func (self *DiameterAgent) handlers() diam.Handler { return dSM } -func (self DiameterAgent) processCCR(ccr *CCR, reqProcessor *config.DARequestProcessor, processorVars map[string]string, cca *CCA) (bool, error) { +func (self DiameterAgent) processCCR(ccr *CCR, reqProcessor *config.DARequestProcessor, + processorVars map[string]string, cca *CCA) (bool, error) { passesAllFilters := true for _, fldFilter := range reqProcessor.RequestFilter { if passes, _ := passesFieldFilter(ccr.diamMessage, fldFilter, nil); !passes { diff --git a/agents/dmtagent_it_test.go b/agents/dmtagent_it_test.go index a38257282..121899d84 100644 --- a/agents/dmtagent_it_test.go +++ b/agents/dmtagent_it_test.go @@ -64,27 +64,6 @@ func TestDmtAgentInitCfg(t *testing.T) { rplyTimeout, _ = utils.ParseDurationWithSecs(*replyTimeout) } -// Remove data in both rating and accounting db -func TestDmtAgentResetDataDb(t *testing.T) { - if err := engine.InitDataDb(daCfg); err != nil { - t.Fatal(err) - } -} - -// Wipe out the cdr database -func TestDmtAgentResetStorDb(t *testing.T) { - if err := engine.InitStorDb(daCfg); err != nil { - t.Fatal(err) - } -} - -// Start CGR Engine -func TestDmtAgentStartEngine(t *testing.T) { - if _, err := engine.StopStartEngine(daCfgPath, 4000); err != nil { - t.Fatal(err) - } -} - func TestDmtAgentCCRAsSMGenericEvent(t *testing.T) { cfgDefaults, _ := config.NewDefaultCGRConfig() loadDictionaries(cfgDefaults.DiameterAgentCfg().DictionariesDir, "UNIT_TEST") @@ -133,10 +112,15 @@ func TestDmtAgentCCRAsSMGenericEvent(t *testing.T) { if ccr.diamMessage, err = ccr.AsDiameterMessage(); err != nil { t.Error(err) } - eSMGE := sessionmanager.SMGenericEvent{"EventName": DIAMETER_CCR, "OriginID": "routinga;1442095190;1476802709", - "Account": "*users", "AnswerTime": "2015-11-23 12:22:24 +0000 UTC", "Category": "call", - "Destination": "4986517174964", "Direction": "*out", "RequestType": "*users", "SetupTime": "2015-11-23 12:22:24 +0000 UTC", - "Subject": "*users", "SubscriberId": "4986517174963", "ToR": "*voice", "Tenant": "*users", "Usage": "300"} + eSMGE := sessionmanager.SMGenericEvent{"EventName": DIAMETER_CCR, + "OriginID": "routinga;1442095190;1476802709", + "Account": "*users", "Category": "call", + "AnswerTime": "2015-11-23 12:22:24 +0000 UTC", + "Destination": "4986517174964", "Direction": "*out", + "RequestType": "*users", + "SetupTime": "2015-11-23 12:22:24 +0000 UTC", + "Subject": "*users", "SubscriberId": "4986517174963", + "ToR": "*voice", "Tenant": "*users", "Usage": "5m0s"} ccrFields := []*config.CfgCdrField{ &config.CfgCdrField{Tag: "TOR", FieldId: "ToR", Type: "*composed", Value: utils.ParseRSRFieldsMustCompile("^*voice", utils.INFIELD_SEP), Mandatory: true}, @@ -172,25 +156,35 @@ func TestDmtAgentCCRAsSMGenericEvent(t *testing.T) { func TestDmtAgentPopulateCCTotalOctets(t *testing.T) { daRP := &config.DARequestProcessor{CCAFields: []*config.CfgCdrField{ - &config.CfgCdrField{Tag: "GrantedUnit", FieldFilter: utils.ParseRSRFieldsMustCompile("CGRError(^$)", utils.INFIELD_SEP), - FieldId: "Multiple-Services-Credit-Control>Granted-Service-Unit>CC-Time", Type: utils.META_COMPOSED, Value: utils.ParseRSRFieldsMustCompile("CGRMaxUsage", utils.INFIELD_SEP), Mandatory: true}, - &config.CfgCdrField{Tag: "GrantedOctet", FieldFilter: utils.ParseRSRFieldsMustCompile("CGRError(^$)", utils.INFIELD_SEP), - FieldId: "Multiple-Services-Credit-Control>Granted-Service-Unit>CC-Total-Octets", Type: utils.META_COMPOSED, Value: utils.ParseRSRFieldsMustCompile("CGRMaxUsage", utils.INFIELD_SEP), Mandatory: true}, + &config.CfgCdrField{Tag: "GrantedUnit", + FieldFilter: utils.ParseRSRFieldsMustCompile("CGRError(^$)", utils.INFIELD_SEP), + FieldId: "Multiple-Services-Credit-Control>Granted-Service-Unit>CC-Time", + Type: utils.META_COMPOSED, + Value: utils.ParseRSRFieldsMustCompile("CGRMaxUsage", utils.INFIELD_SEP), Mandatory: true}, + &config.CfgCdrField{Tag: "GrantedOctet", + FieldFilter: utils.ParseRSRFieldsMustCompile("CGRError(^$)", utils.INFIELD_SEP), + FieldId: "Multiple-Services-Credit-Control>Granted-Service-Unit>CC-Total-Octets", + Type: utils.META_COMPOSED, Value: utils.ParseRSRFieldsMustCompile("CGRMaxUsage", utils.INFIELD_SEP), + Mandatory: true}, }} ccr := new(CCR) ccr.diamMessage = ccr.AsBareDiameterMessage() cca := NewBareCCAFromCCR(ccr, "cgr-da", "cgrates.org") - if err := cca.SetProcessorAVPs(daRP, map[string]string{CGRError: "", CGRMaxUsage: "153600"}); err != nil { + if err := cca.SetProcessorAVPs(daRP, + map[string]string{CGRError: "", CGRMaxUsage: "153600"}); err != nil { t.Error(err) } - if avps, err := cca.diamMessage.FindAVPsWithPath([]interface{}{"Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil { + if avps, err := cca.diamMessage.FindAVPsWithPath([]interface{}{ + "Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil { t.Error(err) } else if len(avps) == 0 { t.Error("Not found") } else if strResult := avpValAsString(avps[0]); strResult != "153600" { // Result-Code set in the template t.Errorf("Expecting 153600, received: %s", strResult) } - if avps, err := cca.diamMessage.FindAVPsWithPath([]interface{}{"Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Total-Octets"}, dict.UndefinedVendorID); err != nil { + if avps, err := cca.diamMessage.FindAVPsWithPath([]interface{}{ + "Multiple-Services-Credit-Control", "Granted-Service-Unit", "CC-Total-Octets"}, + dict.UndefinedVendorID); err != nil { t.Error(err) } else if len(avps) == 0 { t.Error("Not found") @@ -199,6 +193,27 @@ func TestDmtAgentPopulateCCTotalOctets(t *testing.T) { } } +// Remove data in both rating and accounting db +func TestDmtAgentResetDataDb(t *testing.T) { + if err := engine.InitDataDb(daCfg); err != nil { + t.Fatal(err) + } +} + +// Wipe out the cdr database +func TestDmtAgentResetStorDb(t *testing.T) { + if err := engine.InitStorDb(daCfg); err != nil { + t.Fatal(err) + } +} + +// Start CGR Engine +func TestDmtAgentStartEngine(t *testing.T) { + if _, err := engine.StopStartEngine(daCfgPath, 4000); err != nil { + t.Fatal(err) + } +} + // Connect rpc client to rater func TestDmtAgentApierRpcConn(t *testing.T) { var err error @@ -228,14 +243,21 @@ func TestDmtAgentConnectDiameterClient(t *testing.T) { // cgr-console 'cost Category="call" Tenant="cgrates.org" Subject="1001" Destination="1004" TimeStart="2015-11-07T08:42:26Z" TimeEnd="2015-11-07T08:47:26Z"' func TestDmtAgentSendCCRInit(t *testing.T) { - cdr := &engine.CDR{CGRID: utils.Sha1("testccr1", time.Date(2015, 11, 7, 8, 42, 20, 0, time.UTC).String()), OrderID: 123, ToR: utils.VOICE, - OriginID: "testccr1", OriginHost: "192.168.1.1", Source: utils.UNIT_TEST, RequestType: utils.META_RATED, - Tenant: "cgrates.org", Category: "call", Account: "1001", Subject: "1001", Destination: "1004", - SetupTime: time.Date(2015, 11, 7, 8, 42, 20, 0, time.UTC), AnswerTime: time.Date(2015, 11, 7, 8, 42, 26, 0, time.UTC), RunID: utils.DEFAULT_RUNID, - Usage: time.Duration(0), ExtraFields: map[string]string{"Service-Context-Id": "voice@huawei.com"}, + cdr := &engine.CDR{CGRID: utils.Sha1("testccr1", + time.Date(2015, 11, 7, 8, 42, 20, 0, time.UTC).String()), + OrderID: 123, ToR: utils.VOICE, OriginID: "testccr1", OriginHost: "192.168.1.1", + Source: utils.UNIT_TEST, RequestType: utils.META_RATED, + Tenant: "cgrates.org", Category: "call", Account: "1001", + Subject: "1001", Destination: "1004", + SetupTime: time.Date(2015, 11, 7, 8, 42, 20, 0, time.UTC), + AnswerTime: time.Date(2015, 11, 7, 8, 42, 26, 0, time.UTC), + Usage: time.Duration(0), RunID: utils.DEFAULT_RUNID, + ExtraFields: map[string]string{"Service-Context-Id": "voice@huawei.com"}, } - ccr := storedCdrToCCR(cdr, "UNIT_TEST", daCfg.DiameterAgentCfg().OriginRealm, daCfg.DiameterAgentCfg().VendorId, - daCfg.DiameterAgentCfg().ProductName, utils.DIAMETER_FIRMWARE_REVISION, daCfg.DiameterAgentCfg().DebitInterval, false) + ccr := storedCdrToCCR(cdr, "UNIT_TEST", daCfg.DiameterAgentCfg().OriginRealm, + daCfg.DiameterAgentCfg().VendorId, + daCfg.DiameterAgentCfg().ProductName, utils.DIAMETER_FIRMWARE_REVISION, + daCfg.DiameterAgentCfg().DebitInterval, false) m, err := ccr.AsDiameterMessage() if err != nil { t.Error(err) @@ -245,7 +267,8 @@ func TestDmtAgentSendCCRInit(t *testing.T) { } time.Sleep(time.Duration(*waitRater) * time.Millisecond) msg := dmtClient.ReceivedMessage(rplyTimeout) - if avps, err := msg.FindAVPsWithPath([]interface{}{"Granted-Service-Unit", "CC-Time"}, dict.UndefinedVendorID); err != nil { + if avps, err := msg.FindAVPsWithPath([]interface{}{"Granted-Service-Unit", "CC-Time"}, + dict.UndefinedVendorID); err != nil { t.Error(err) } else if len(avps) == 0 { t.Error("Granted-Service-Unit not found") @@ -643,7 +666,7 @@ func TestDmtAgentCdrs(t *testing.T) { } else if len(cdrs) != 1 { t.Error("Unexpected number of CDRs returned: ", len(cdrs)) } else { - if cdrs[0].Usage != "610" { + if cdrs[0].Usage != "10m10s" { t.Errorf("Unexpected CDR Usage received, cdr: %+v ", cdrs[0]) } if cdrs[0].Cost != 0.7565 { diff --git a/agents/libdmt.go b/agents/libdmt.go index c3bc67aea..a35a32df7 100644 --- a/agents/libdmt.go +++ b/agents/libdmt.go @@ -126,16 +126,15 @@ func disectUsageForCCR(usage time.Duration, debitInterval time.Duration, callEnd return } -func usageFromCCR(reqType, reqNr, reqCCTime, usedCCTime int, debitIterval time.Duration) time.Duration { - dISecs := debitIterval.Seconds() - var ccTime int +func usageFromCCR(reqType int, reqNr, usedCCTime int64, debitIterval time.Duration) (usage time.Duration) { + //dISecs := debitIterval.Nano() + //var ccTime int + usage = debitIterval if reqType == 3 { reqNr -= 1 // decrease request number to reach the real number - ccTime = usedCCTime + (int(dISecs) * reqNr) - } else { - ccTime = int(dISecs) + usage = (time.Duration(usedCCTime) * time.Second) + time.Duration(debitIterval.Nanoseconds()*reqNr) } - return time.Duration(ccTime) * time.Second + return } // Utility function to convert from StoredCdr to CCR struct @@ -185,7 +184,7 @@ func metaHandler(m *diam.Message, tag, arg string, dur time.Duration) (string, e case META_CCR_USAGE: var ok bool var reqType datatype.Enumerated - var reqNr, reqUnit, usedUnit datatype.Unsigned32 + var reqNr, usedUnit datatype.Unsigned32 if ccReqTypeAvp, err := m.FindAVP("CC-Request-Type", 0); err != nil { return "", err } else if ccReqTypeAvp == nil { @@ -206,7 +205,7 @@ func metaHandler(m *diam.Message, tag, arg string, dur time.Duration) (string, e return "", err } else if len(reqUnitAVPs) == 0 { return "", errors.New("Requested-Service-Unit>CC-Time not found") - } else if reqUnit, ok = reqUnitAVPs[0].Data.(datatype.Unsigned32); !ok { + } else if usedUnit, ok = reqUnitAVPs[0].Data.(datatype.Unsigned32); !ok { return "", fmt.Errorf("Requested-Service-Unit>CC-Time must be Unsigned32 and not %v", reqUnitAVPs[0].Data.Type()) } case datatype.Enumerated(3), datatype.Enumerated(4): @@ -218,8 +217,7 @@ func metaHandler(m *diam.Message, tag, arg string, dur time.Duration) (string, e } } } - usage := usageFromCCR(int(reqType), int(reqNr), int(reqUnit), int(usedUnit), dur) - return strconv.FormatFloat(usage.Seconds(), 'f', -1, 64), nil + return usageFromCCR(int(reqType), int64(reqNr), int64(usedUnit), dur).String(), nil } return "", nil } diff --git a/agents/libdmt_test.go b/agents/libdmt_test.go index 2a747babb..0e776eae1 100644 --- a/agents/libdmt_test.go +++ b/agents/libdmt_test.go @@ -59,25 +59,25 @@ func TestDisectUsageForCCR(t *testing.T) { } func TestUsageFromCCR(t *testing.T) { - if usage := usageFromCCR(1, 0, 300, 0, time.Duration(300)*time.Second); usage != time.Duration(300)*time.Second { + if usage := usageFromCCR(1, 0, 0, time.Duration(300)*time.Second); usage != time.Duration(300)*time.Second { t.Error(usage) } - if usage := usageFromCCR(2, 0, 300, 300, time.Duration(300)*time.Second); usage != time.Duration(300)*time.Second { + if usage := usageFromCCR(2, 0, 300, time.Duration(300)*time.Second); usage != time.Duration(300)*time.Second { t.Error(usage) } - if usage := usageFromCCR(2, 3, 300, 300, time.Duration(300)*time.Second); usage != time.Duration(300)*time.Second { + if usage := usageFromCCR(2, 3, 300, time.Duration(300)*time.Second); usage != time.Duration(300)*time.Second { t.Error(usage.Seconds()) } - if usage := usageFromCCR(3, 3, 0, 10, time.Duration(300)*time.Second); usage != time.Duration(610)*time.Second { + if usage := usageFromCCR(3, 3, 10, time.Duration(300)*time.Second); usage != time.Duration(610)*time.Second { t.Error(usage) } - if usage := usageFromCCR(3, 4, 0, 35, time.Duration(300)*time.Second); usage != time.Duration(935)*time.Second { + if usage := usageFromCCR(3, 4, 35, time.Duration(300)*time.Second); usage != time.Duration(935)*time.Second { t.Error(usage) } - if usage := usageFromCCR(3, 1, 0, 35, time.Duration(300)*time.Second); usage != time.Duration(35)*time.Second { + if usage := usageFromCCR(3, 1, 35, time.Duration(300)*time.Second); usage != time.Duration(35)*time.Second { t.Error(usage) } - if usage := usageFromCCR(1, 0, 360, 0, time.Duration(360)*time.Second); usage != time.Duration(360)*time.Second { + if usage := usageFromCCR(1, 0, 0, time.Duration(360)*time.Second); usage != time.Duration(360)*time.Second { t.Error(usage) } } diff --git a/agents/radagent_it_test.go b/agents/radagent_it_test.go index 53d30fbc9..eae9797f0 100644 --- a/agents/radagent_it_test.go +++ b/agents/radagent_it_test.go @@ -272,7 +272,7 @@ func TestRAitAcctStop(t *testing.T) { } else if len(cdrs) != 1 { t.Error("Unexpected number of CDRs returned: ", len(cdrs)) } else { - if cdrs[0].Usage != "4" { + if cdrs[0].Usage != "4s" { t.Errorf("Unexpected CDR Usage received, cdr: %v %+v ", cdrs[0].Usage, cdrs[0]) } if cdrs[0].CostSource != utils.SESSION_MANAGER_SOURCE { diff --git a/data/conf/samples/dmtagent/cgrates.json b/data/conf/samples/dmtagent/cgrates.json index a01d42a04..97da4c4db 100644 --- a/data/conf/samples/dmtagent/cgrates.json +++ b/data/conf/samples/dmtagent/cgrates.json @@ -4,6 +4,10 @@ // Used for cgradmin // Starts rater, scheduler +"general": { + "log_level": 7, +}, + "listen": { "rpc_json": ":2012", // RPC JSON listening address "rpc_gob": ":2013", // RPC GOB listening address diff --git a/engine/cdr.go b/engine/cdr.go index 3d072fe80..792f97f24 100644 --- a/engine/cdr.go +++ b/engine/cdr.go @@ -403,6 +403,13 @@ func (cdr *CDR) ForkCdr(runId string, RequestTypeFld, tenantFld, categFld, accou } func (cdr *CDR) AsExternalCDR() *ExternalCDR { + var usageStr string + switch cdr.ToR { + case utils.VOICE: // usage as time + usageStr = cdr.Usage.String() + default: // usage as units + usageStr = strconv.FormatInt(cdr.Usage.Nanoseconds(), 10) + } return &ExternalCDR{CGRID: cdr.CGRID, RunID: cdr.RunID, OrderID: cdr.OrderID, @@ -418,7 +425,7 @@ func (cdr *CDR) AsExternalCDR() *ExternalCDR { Destination: cdr.Destination, SetupTime: cdr.SetupTime.Format(time.RFC3339), AnswerTime: cdr.AnswerTime.Format(time.RFC3339), - Usage: cdr.FormatUsage(utils.SECONDS), + Usage: usageStr, ExtraFields: cdr.ExtraFields, CostSource: cdr.CostSource, Cost: cdr.Cost,