Skip to content

Commit

Permalink
fix: resolve parseCDR error and add concatenated CDRs update
Browse files Browse the repository at this point in the history
  • Loading branch information
TungChiaMing authored and Alonza0314 committed Jan 2, 2025
1 parent 735a6b1 commit 263e760
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cdr/asn/ber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func TestUnmarshal(t *testing.T) {

func TestParseInt64(t *testing.T) {
testCases := [][]byte{}
origInts := []int64{0, 1, 127, 128, 32767, -128, -129, -32768}
origInts := []int64{0, 1, 127, 128, 32767}

for _, origInt := range origInts {
buf := new(bytes.Buffer)
Expand Down
23 changes: 0 additions & 23 deletions cdr/asn/ber_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,11 @@ func parseInt64(bytes []byte) (r int64, e error) {
return r, e
}

minus := false
if uint(bytes[0]) > 127 {
minus = true
}

for i := 0; i < 8-len(bytes); i++ {
extend_byte := byte(0)
if minus {
extend_byte = byte(255)
}
bytes = append([]byte{extend_byte}, bytes...)
}

if minus {
for i := range bytes {
bytes[i] = ^bytes[i]
}
}

for _, b := range bytes {
r <<= 8
r |= int64(b)
}

if minus {
r = -r - 1
}

return r, e
}

Expand Down
7 changes: 4 additions & 3 deletions cdr/cdrFile/cdrFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ func (cdfFile *CDRFile) Decoding(fileName string) {

// fmt.Println("[Decode]cdrfileheader:\n", cdfFile.Hdr)

tail := n + 2
var tail uint32
tail = uint32(n) + 2

Check failure on line 491 in cdr/cdrFile/cdrFile.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)

Check failure on line 491 in cdr/cdrFile/cdrFile.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)

for i := 1; i <= int(numberOfCdrsInFile); i++ {
cdrLength := binary.BigEndian.Uint16(data[tail : tail+2])
Expand All @@ -506,10 +507,10 @@ func (cdfFile *CDRFile) Decoding(fileName string) {

cdr := CDR{
Hdr: cdrHeader,
CdrByte: data[tail+5 : tail+5+cdrLength],
CdrByte: data[tail+5 : tail+5+uint32(cdrLength)],
}
cdfFile.CdrList = append(cdfFile.CdrList, cdr)
tail += 5 + cdrLength
tail += 5 + uint32(cdrLength)
}
// fmt.Println("[Decode]cdrfile:\n", cdfFile)
// fmt.Printf("%#v\n", cdfFile)
Expand Down
3 changes: 2 additions & 1 deletion internal/context/ue_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ChfUe struct {
RatingChan chan *diam.Message
RatingType map[int32]charging_datatype.RequestSubType
RateSessionId uint32
Records []*cdrType.CHFRecord

// lock
Cdr map[string]*cdrType.CHFRecord
Expand All @@ -58,7 +59,7 @@ func (ue *ChfUe) FindRatingGroup(ratingGroup int32) bool {

func (ue *ChfUe) init() {
config := factory.ChfConfig

ue.Records = []*cdrType.CHFRecord{}
ue.Cdr = make(map[string]*cdrType.CHFRecord)
ue.VolumeLimit = config.Configuration.VolumeLimit
ue.VolumeLimitPDU = config.Configuration.VolumeLimitPDU
Expand Down
3 changes: 2 additions & 1 deletion internal/sbi/processor/cdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func dumpCdrFile(ueid string, records []*cdrType.CHFRecord) error {
cdrfile.Hdr.HeaderLength = uint32(54 + cdrfile.Hdr.LengthOfCdrRouteingFilter + cdrfile.Hdr.LengthOfPrivateExtension)
cdrfile.Hdr.NumberOfCdrsInFile = uint32(len(records))
cdrfile.Hdr.FileLength = cdrfile.Hdr.HeaderLength
logger.ChargingdataPostLog.Traceln("cdrfile.Hdr.NumberOfCdrsInFile:", uint32(len(records)))

for _, record := range records {
cdrBytes, err := asn.BerMarshalWithParams(&record, "explicit,choice")
Expand All @@ -283,7 +284,7 @@ func dumpCdrFile(ueid string, records []*cdrType.CHFRecord) error {
}
cdrfile.CdrList = append(cdrfile.CdrList, tmpCdr)

cdrfile.Hdr.FileLength += uint32(cdrHdr.CdrLength) + 5
cdrfile.Hdr.FileLength += uint32(len(cdrBytes)) + 5
}

cdrfile.Encoding("/tmp/" + ueid + ".cdr")
Expand Down
38 changes: 33 additions & 5 deletions internal/sbi/processor/converged_charging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package processor

import (
"context"
"encoding/json"
"math"
"net/http"
"strconv"
Expand All @@ -13,6 +14,8 @@ import (
"golang.org/x/exp/constraints"

charging_datatype "github.com/free5gc/chf/ccs_diameter/datatype"
"github.com/free5gc/chf/cdr/asn"
"github.com/free5gc/chf/cdr/cdrConvert"
"github.com/free5gc/chf/cdr/cdrType"
"github.com/free5gc/chf/internal/abmf"
"github.com/free5gc/chf/internal/cgf"
Expand Down Expand Up @@ -178,6 +181,7 @@ func (p *Processor) ChargingDataCreate(
}

ue.Cdr[chargingSessionId] = cdr
ue.Records = append(ue.Records, ue.Cdr[chargingSessionId])

if chargingData.OneTimeEvent {
err = p.CloseCDR(cdr, false)
Expand Down Expand Up @@ -211,7 +215,6 @@ func (p *Processor) ChargingDataCreate(
func (p *Processor) ChargingDataUpdate(
chargingData models.ChfConvergedChargingChargingDataRequest, chargingSessionId string,
) (*models.ChfConvergedChargingChargingDataResponse, *models.ProblemDetails) {

Check failure on line 217 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

unnecessary leading newline (whitespace)

Check failure on line 217 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

unnecessary leading newline (whitespace)
var records []*cdrType.CHFRecord

Check failure on line 218 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 218 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gofumpt`-ed (gofumpt)
self := chf_context.GetSelf()
ueId := chargingData.SubscriberIdentifier
Expand All @@ -231,6 +234,34 @@ func (p *Processor) ChargingDataUpdate(
responseBody, partialRecord := p.BuildConvergedChargingDataUpdateResopone(chargingData)

cdr := ue.Cdr[chargingSessionId]

if len(ue.Records) > 1 {
cdr = ue.Records[len(ue.Records)-1]
}

cdrBytes, errBer := asn.BerMarshalWithParams(&cdr, "explicit,choice")
if errBer != nil {
problemDetails := &models.ProblemDetails{
Status: http.StatusBadRequest,
}
return nil, problemDetails
}

Check failure on line 249 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)

Check failure on line 249 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/free5gc) --custom-order (gci)
var chgDataBytes []byte
if chargingData.MultipleUnitUsage != nil && len(chargingData.MultipleUnitUsage) != 0 {
cdrMultiUnitUsage := cdrConvert.MultiUnitUsageToCdr(chargingData.MultipleUnitUsage)
chgDataBytes, _ = asn.BerMarshalWithParams(&cdrMultiUnitUsage, "explicit,choice")

Check failure on line 253 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `asn.BerMarshalWithParams` is not checked (errcheck)

Check failure on line 253 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `asn.BerMarshalWithParams` is not checked (errcheck)
}

if len(cdrBytes)+len(chgDataBytes) > math.MaxUint16 {
var newRecord *cdrType.CHFRecord
cdrJson, _ := json.Marshal(cdr)

Check failure on line 258 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `json.Marshal` is not checked (errcheck)

Check failure on line 258 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `json.Marshal` is not checked (errcheck)
json.Unmarshal(cdrJson, &newRecord)

Check failure on line 259 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `json.Unmarshal` is not checked (errcheck)

Check failure on line 259 in internal/sbi/processor/converged_charging.go

View workflow job for this annotation

GitHub Actions / lint (1.21)

Error return value of `json.Unmarshal` is not checked (errcheck)
newRecord.ChargingFunctionRecord.ListOfMultipleUnitUsage = []cdrType.MultipleUnitUsage{}
cdr = newRecord
ue.Records = append(ue.Records, cdr)
}

err := p.UpdateCDR(cdr, chargingData)
if err != nil {
problemDetails := &models.ProblemDetails{
Expand Down Expand Up @@ -262,10 +293,7 @@ func (p *Processor) ChargingDataUpdate(
"CDR Record Sequence Number after Reopen %+v", *cdr.ChargingFunctionRecord.RecordSequenceNumber)
}

for _, cdr := range ue.Cdr {
records = append(records, cdr)
}
err = dumpCdrFile(ueId, records)
err = dumpCdrFile(ueId, ue.Records)
if err != nil {
problemDetails := &models.ProblemDetails{
Status: http.StatusBadRequest,
Expand Down

0 comments on commit 263e760

Please sign in to comment.