diff --git a/rater/actions_test.go b/rater/actions_test.go index 7193faa1d..2ae7f1dad 100644 --- a/rater/actions_test.go +++ b/rater/actions_test.go @@ -689,12 +689,6 @@ func TestActionResetCounterCREDIT(t *testing.T) { } } -func TestUUID(t *testing.T) { - uuid := GenUUID() - if len(uuid) == 0 { - t.Fatalf("GenUUID error %s", uuid) - } -} func TestActionTriggerLogging(t *testing.T) { at := &ActionTrigger{ diff --git a/rater/loader_csv.go b/rater/loader_csv.go index 785ef7927..2e10ff746 100644 --- a/rater/loader_csv.go +++ b/rater/loader_csv.go @@ -27,6 +27,7 @@ import ( "strconv" "strings" "time" + "github.com/cgrates/cgrates/utils" ) type CSVReader struct { @@ -375,7 +376,7 @@ func (csvr *CSVReader) LoadActions() (err error) { return errors.New(fmt.Sprintf("Could not parse action weight: %v", err)) } a = &Action{ - Id: GenUUID(), + Id: utils.GenUUID(), ActionType: record[1], BalanceId: record[2], Direction: record[3], @@ -424,7 +425,7 @@ func (csvr *CSVReader) LoadActionTimings() (err error) { } for _, t := range ts { at := &ActionTiming{ - Id: GenUUID(), + Id: utils.GenUUID(), Tag: record[2], Weight: weight, Timing: &Interval{ @@ -466,7 +467,7 @@ func (csvr *CSVReader) LoadActionTriggers() (err error) { return errors.New(fmt.Sprintf("Could not parse action trigger weight: %v", err)) } at := &ActionTrigger{ - Id: GenUUID(), + Id: utils.GenUUID(), BalanceId: record[1], Direction: record[2], ThresholdValue: value, diff --git a/rater/loader_db.go b/rater/loader_db.go index 99b584e8a..54e51a495 100644 --- a/rater/loader_db.go +++ b/rater/loader_db.go @@ -23,6 +23,7 @@ import ( "fmt" "log" "time" + "github.com/cgrates/cgrates/utils" ) type DbReader struct { @@ -216,7 +217,7 @@ func (dbr *DbReader) LoadActionTimings() (err error) { } for _, t := range ts { actTmg := &ActionTiming{ - Id: GenUUID(), + Id: utils.GenUUID(), Tag: at.Tag, Weight: at.Weight, Timing: &Interval{ diff --git a/rater/storage_mysql.go b/rater/storage_mysql.go index e16f567e0..93e378b30 100644 --- a/rater/storage_mysql.go +++ b/rater/storage_mysql.go @@ -92,7 +92,7 @@ func (mys *MySQLStorage) GetAllActionTimings(tpid string) (ats map[string][]*Act } at := &ActionTiming{ - Id: GenUUID(), + Id: utils.GenUUID(), Tag: timings_tag, Weight: weight, ActionsId: actions_tag, @@ -348,7 +348,7 @@ func (mys *MySQLStorage) GetAllActions(tpid string) (map[string][]*Action, error price = rate } a = &Action{ - Id: GenUUID(), + Id: utils.GenUUID(), ActionType: action, BalanceId: balances_tag, Direction: direction, @@ -381,7 +381,7 @@ func (mys *MySQLStorage) GetAllActionTriggers(tpid string) (map[string][]*Action } at := &ActionTrigger{ - Id: GenUUID(), + Id: utils.GenUUID(), BalanceId: balances_tag, Direction: direction, ThresholdValue: threshold, diff --git a/rater/storage_postgres.go b/rater/storage_postgres.go index 49c43b995..554ed2c2f 100644 --- a/rater/storage_postgres.go +++ b/rater/storage_postgres.go @@ -330,7 +330,7 @@ func (psl *PostgresStorage) GetAllActions(tpid string) (map[string][]*Action, er price = rate } a = &Action{ - Id: GenUUID(), + Id: utils.GenUUID(), ActionType: action, BalanceId: balances_tag, Direction: direction, @@ -363,7 +363,7 @@ func (psl *PostgresStorage) GetAllActionTriggers(tpid string) (map[string][]*Act } at := &ActionTrigger{ - Id: GenUUID(), + Id: utils.GenUUID(), BalanceId: balances_tag, Direction: direction, ThresholdValue: threshold, diff --git a/utils/coreutils.go b/utils/coreutils.go index a6f36b5a8..3e7d5f8d3 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -23,6 +23,8 @@ import ( "fmt" "encoding/hex" "crypto/rand" + "strconv" + "time" ) // Returns first non empty string out of vals. Useful to extract defaults @@ -43,18 +45,21 @@ func FSCgrId(uuid string) string { func NewTPid() string { hasher := sha1.New() - uuid,_ := GenUUID() + uuid := GenUUID() hasher.Write([]byte(uuid)) return fmt.Sprintf("%x", hasher.Sum(nil)) } -func GenUUID() (string, error) { +// helper function for uuid generation +func GenUUID() string { uuid := make([]byte, 16) n, err := rand.Read(uuid) if n != len(uuid) || err != nil { - return "", err + return strconv.FormatInt(time.Now().UnixNano(), 10) } - uuid[8] = 0x80 - uuid[4] = 0x40 - return hex.EncodeToString(uuid), nil + // TODO: verify the two lines implement RFC 4122 correctly + uuid[8] = 0x80 // variant bits see page 5 + uuid[4] = 0x40 // version 4 Pseudo Random, see page 7 + + return hex.EncodeToString(uuid) } diff --git a/utils/utils_test.go b/utils/utils_test.go index b7fc82cda..e1518d52c 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -32,3 +32,10 @@ func TestFirstNonEmpty(t *testing.T) { t.Error("Wrong elemnt returned: ", winnerElmnt) } } + +func TestUUID(t *testing.T) { + uuid := GenUUID() + if len(uuid) == 0 { + t.Fatalf("GenUUID error %s", uuid) + } +}