diff --git a/apier/v1/aliases.go b/apier/v1/aliases.go index a0e9d6c79..609f8f9c5 100644 --- a/apier/v1/aliases.go +++ b/apier/v1/aliases.go @@ -50,7 +50,7 @@ func (self *ApierV1) AddRatingSubjectAliases(attrs AttrAddRatingSubjectAliases, var ignr string for _, alias := range attrs.Aliases { if err := aliases.SetAlias( - engine.Alias{Direction: utils.META_OUT, Tenant: attrs.Tenant, Category: attrs.Category, Account: alias, Subject: alias, Group: utils.ALIAS_GROUP_RP, + engine.Alias{Direction: utils.META_OUT, Tenant: attrs.Tenant, Category: attrs.Category, Account: alias, Subject: alias, Context: utils.ALIAS_GROUP_RP, Values: engine.AliasValues{&engine.AliasValue{DestinationId: utils.META_ANY, Alias: attrs.Subject, Weight: 10.0}}}, &ignr); err != nil { return utils.NewErrServerError(err) } @@ -69,7 +69,7 @@ func (self *ApierV1) RemRatingSubjectAliases(tenantRatingSubject engine.TenantRa return errors.New("ALIASES_NOT_ENABLED") } var reverseAliases map[string][]*engine.Alias - if err := aliases.GetReverseAlias(engine.AttrReverseAlias{Alias: tenantRatingSubject.Subject, Group: utils.ALIAS_GROUP_RP}, &reverseAliases); err != nil { + if err := aliases.GetReverseAlias(engine.AttrReverseAlias{Alias: tenantRatingSubject.Subject, Context: utils.ALIAS_GROUP_RP}, &reverseAliases); err != nil { return utils.NewErrServerError(err) } var ignr string @@ -101,7 +101,7 @@ func (self *ApierV1) AddAccountAliases(attrs AttrAddAccountAliases, reply *strin var ignr string for _, alias := range attrs.Aliases { if err := aliases.SetAlias( - engine.Alias{Direction: utils.META_OUT, Tenant: attrs.Tenant, Category: attrs.Category, Account: alias, Subject: utils.META_ANY, Group: utils.ALIAS_GROUP_ACC, + engine.Alias{Direction: utils.META_OUT, Tenant: attrs.Tenant, Category: attrs.Category, Account: alias, Subject: utils.META_ANY, Context: utils.ALIAS_GROUP_ACC, Values: engine.AliasValues{&engine.AliasValue{DestinationId: utils.META_ANY, Alias: attrs.Account, Weight: 10.0}}}, &ignr); err != nil { return utils.NewErrServerError(err) } @@ -120,7 +120,7 @@ func (self *ApierV1) RemAccountAliases(tenantAccount engine.TenantAccount, reply return errors.New("ALIASES_NOT_ENABLED") } var reverseAliases map[string][]*engine.Alias - if err := aliases.GetReverseAlias(engine.AttrReverseAlias{Alias: tenantAccount.Account, Group: utils.ALIAS_GROUP_ACC}, &reverseAliases); err != nil { + if err := aliases.GetReverseAlias(engine.AttrReverseAlias{Alias: tenantAccount.Account, Context: utils.ALIAS_GROUP_ACC}, &reverseAliases); err != nil { return utils.NewErrServerError(err) } var ignr string diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index c804fa768..af4e5853f 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -1548,12 +1548,12 @@ func TestApierLocalGetAliases(t *testing.T) { } var alias engine.Alias //al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Group - if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Group: utils.ALIAS_GROUP_RP, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil { + if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Context: utils.ALIAS_GROUP_RP, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil { t.Error("Unexpected nil error received") } else if err.Error() != utils.ErrNotFound.Error() { t.Error("Unexpected error", err.Error()) } - if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Group: utils.ALIAS_GROUP_ACC, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil { + if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Context: utils.ALIAS_GROUP_ACC, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil { t.Error("Unexpected nil error received") } else if err.Error() != utils.ErrNotFound.Error() { t.Error("Unexpected error", err.Error()) @@ -1573,7 +1573,7 @@ func TestApierLocalAddRatingSubjectAliases(t *testing.T) { } var alias engine.Alias for _, als := range addRtSubjAliases.Aliases { - if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Group: utils.ALIAS_GROUP_RP, Direction: "*out", Tenant: addRtSubjAliases.Tenant, Category: addRtSubjAliases.Category, + if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Context: utils.ALIAS_GROUP_RP, Direction: "*out", Tenant: addRtSubjAliases.Tenant, Category: addRtSubjAliases.Category, Account: als, Subject: als}, &alias); err != nil { t.Error("Unexpected error", err.Error()) } @@ -1593,7 +1593,7 @@ func TestApierLocalRemRatingSubjectAliases(t *testing.T) { } var alias engine.Alias //al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Group - if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Group: utils.ALIAS_GROUP_RP, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil || err.Error() != utils.ErrNotFound.Error() { + if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Context: utils.ALIAS_GROUP_RP, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Errorf("Unexpected error %v, alias: %+v", err, alias) } } @@ -1611,7 +1611,7 @@ func TestApierLocalAddAccountAliases(t *testing.T) { } var alias engine.Alias for _, als := range addAcntAliases.Aliases { - if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Group: utils.ALIAS_GROUP_ACC, Direction: "*out", Tenant: addAcntAliases.Tenant, Category: addAcntAliases.Category, + if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Context: utils.ALIAS_GROUP_ACC, Direction: "*out", Tenant: addAcntAliases.Tenant, Category: addAcntAliases.Category, Account: als, Subject: als}, &alias); err != nil { t.Error("Unexpected error", err.Error()) } @@ -1631,7 +1631,7 @@ func TestApierLocalRemAccountAliases(t *testing.T) { } var alias engine.Alias //al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Group - if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Group: utils.ALIAS_GROUP_ACC, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil || err.Error() != utils.ErrNotFound.Error() { + if err := rater.Call("AliasesV1.GetAlias", engine.Alias{Context: utils.ALIAS_GROUP_ACC, Direction: "*out", Tenant: "cgrates.org", Category: "call", Account: "2001", Subject: "2001"}, &alias); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Errorf("Unexpected error %v, alias: %+v", err, alias) } } diff --git a/data/tariffplans/prepaid1centpsec/Aliases.csv b/data/tariffplans/prepaid1centpsec/Aliases.csv index b67a15ddf..303527763 100644 --- a/data/tariffplans/prepaid1centpsec/Aliases.csv +++ b/data/tariffplans/prepaid1centpsec/Aliases.csv @@ -1,3 +1,3 @@ -#Direction,Tenant,Category,Account,Subject,DestinationId,Group,Alias,Weight -*out,cgrates.org,call,1006,1006,*any,*rating_profile,1001,10 -*out,cgrates.org,call,1006,1006,*any,*account,1002,10 +#Direction,Tenant,Category,Account,Subject,DestinationId,Context,Target,Original,Alias,Weight +*out,cgrates.org,call,1006,1006,*any,*rating_profile,Subject,1006,1001,10 +*out,cgrates.org,call,1006,1006,*any,*account,Account,1006,1002,10 diff --git a/data/tariffplans/tutorial/Aliases.csv b/data/tariffplans/tutorial/Aliases.csv index b67a15ddf..303527763 100644 --- a/data/tariffplans/tutorial/Aliases.csv +++ b/data/tariffplans/tutorial/Aliases.csv @@ -1,3 +1,3 @@ -#Direction,Tenant,Category,Account,Subject,DestinationId,Group,Alias,Weight -*out,cgrates.org,call,1006,1006,*any,*rating_profile,1001,10 -*out,cgrates.org,call,1006,1006,*any,*account,1002,10 +#Direction,Tenant,Category,Account,Subject,DestinationId,Context,Target,Original,Alias,Weight +*out,cgrates.org,call,1006,1006,*any,*rating_profile,Subject,1006,1001,10 +*out,cgrates.org,call,1006,1006,*any,*account,Account,1006,1002,10 diff --git a/engine/aliases.go b/engine/aliases.go index 0e2841164..127357c93 100644 --- a/engine/aliases.go +++ b/engine/aliases.go @@ -21,22 +21,43 @@ type Alias struct { Category string Account string Subject string - Group string + Context string Values AliasValues } type AliasValue struct { DestinationId string - Alias string + Pairs AliasPairs Weight float64 } func (av *AliasValue) Equals(other *AliasValue) bool { return av.DestinationId == other.DestinationId && - av.Alias == other.Alias && + av.Pairs.Equals(other.Pairs) && av.Weight == other.Weight } +type AliasPairs map[string]map[string]string + +func (ap AliasPairs) Equals(other AliasPairs) bool { + if len(ap) != len(other) { + return false + } + + for attribute, origAlias := range ap { + otherOrigAlias, ok := other[attribute] + if !ok || len(origAlias) != len(otherOrigAlias) { + return false + } + for orig := range origAlias { + if origAlias[orig] != otherOrigAlias[orig] { + return false + } + } + } + return true +} + type AliasValues []*AliasValue func (avs AliasValues) Len() int { @@ -68,23 +89,27 @@ func (avs AliasValues) GetWeightSlice() (result []map[string][]string) { } else { m = result[len(result)-1] } - m[value.Alias] = append(m[value.Alias], value.DestinationId) + for _, pairs := range value.Pairs { + for _, alias := range pairs { + m[alias] = append(m[alias], value.DestinationId) + } + } } return } func (al *Alias) GetId() string { - return utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Group) + return utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Context) } func (al *Alias) GenerateIds() []string { var result []string - result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Group)) - result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, al.Account, utils.ANY, al.Group)) - result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, utils.ANY, utils.ANY, al.Group)) - result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, utils.ANY, utils.ANY, utils.ANY, al.Group)) - result = append(result, utils.ConcatenatedKey(al.Direction, utils.ANY, utils.ANY, utils.ANY, utils.ANY, al.Group)) - result = append(result, utils.ConcatenatedKey(utils.ANY, utils.ANY, utils.ANY, utils.ANY, al.Group)) + result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, al.Account, al.Subject, al.Context)) + result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, al.Account, utils.ANY, al.Context)) + result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, al.Category, utils.ANY, utils.ANY, al.Context)) + result = append(result, utils.ConcatenatedKey(al.Direction, al.Tenant, utils.ANY, utils.ANY, utils.ANY, al.Context)) + result = append(result, utils.ConcatenatedKey(al.Direction, utils.ANY, utils.ANY, utils.ANY, utils.ANY, al.Context)) + result = append(result, utils.ConcatenatedKey(utils.ANY, utils.ANY, utils.ANY, utils.ANY, al.Context)) return result } @@ -98,7 +123,7 @@ func (al *Alias) SetId(id string) error { al.Category = vals[2] al.Account = vals[3] al.Subject = vals[4] - al.Group = vals[5] + al.Context = vals[5] return nil } @@ -109,12 +134,13 @@ type AttrMatchingAlias struct { Category string Account string Subject string - Group string + Context string } type AttrReverseAlias struct { - Alias string - Group string + Alias string + Target string + Context string } type AliasService interface { @@ -204,7 +230,7 @@ func (am *AliasHandler) RemoveAlias(al Alias, reply *string) error { func (am *AliasHandler) RemoveReverseAlias(attr AttrReverseAlias, reply *string) error { am.mu.Lock() defer am.mu.Unlock() - rKey := utils.REVERSE_ALIASES_PREFIX + attr.Alias + attr.Group + rKey := utils.REVERSE_ALIASES_PREFIX + attr.Alias + attr.Target + attr.Context if x, err := cache2go.Get(rKey); err == nil { existingKeys := x.(map[string]bool) for key := range existingKeys { @@ -240,7 +266,7 @@ func (am *AliasHandler) GetReverseAlias(attr AttrReverseAlias, result *map[strin am.mu.Lock() defer am.mu.Unlock() aliases := make(map[string][]*Alias) - rKey := utils.REVERSE_ALIASES_PREFIX + attr.Alias + attr.Group + rKey := utils.REVERSE_ALIASES_PREFIX + attr.Alias + attr.Target + attr.Context if x, err := cache2go.Get(rKey); err == nil { existingKeys := x.(map[string]bool) for key := range existingKeys { @@ -271,7 +297,7 @@ func (am *AliasHandler) GetMatchingAlias(attr AttrMatchingAlias, result *string) Category: attr.Category, Account: attr.Account, Subject: attr.Subject, - Group: attr.Group, + Context: attr.Context, }, &response); err != nil { return err } diff --git a/engine/calldesc.go b/engine/calldesc.go index 2636ce645..6e6851ca2 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -313,7 +313,7 @@ func (cd *CallDescriptor) GetKey(subject string) string { Category: cd.Category, Account: cd.Account, Subject: cd.Subject, - Group: utils.ALIAS_GROUP_RP}, &alias); err == nil && alias != "" { + Context: utils.ALIAS_GROUP_RP}, &alias); err == nil && alias != "" { subject = alias cd.Subject = alias } @@ -336,7 +336,7 @@ func (cd *CallDescriptor) GetAccountKey() string { Category: cd.Category, Account: cd.Account, Subject: cd.Subject, - Group: utils.ALIAS_GROUP_ACC}, &alias) + Context: utils.ALIAS_GROUP_ACC}, &alias) if err == nil && alias != "" { cd.Account = alias } diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index a09c2834e..2e0c83292 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -222,12 +222,12 @@ cgrates.org,dan,another,value ` aliases = ` #Direction[0],Tenant[1],Category[2],Account[3],Subject[4],DestinationId[5],Group[6],Alias[7],Weight[8] -*out,cgrates.org,call,dan,dan,EU_LANDLINE,*rating_profile,dan1,10 -*out,cgrates.org,call,dan,dan,GLOBAL1,*rating_profile,dan2,20 -*any,*any,*any,*any,*any,*any,*rating_profile,rif1,20 -*any,*any,*any,*any,*any,*any,*account,dan1,10 -*out,vdf,0,a1,a1,*any,*rating_profile,minu,10 -*out,vdf,0,a1,a1,*any,*account,minu,10 +*out,cgrates.org,call,dan,dan,EU_LANDLINE,*rating_profile,Subject,dan,dan1,10 +*out,cgrates.org,call,dan,dan,GLOBAL1,*rating_profile,Subject,dan,dan2,20 +*any,*any,*any,*any,*any,*any,*rating_profile,Subject,*any,rif1,20 +*any,*any,*any,*any,*any,*any,*account,Account,*any,dan1,10 +*out,vdf,0,a1,a1,*any,*rating_profile,Subject,a1,minu,10 +*out,vdf,0,a1,a1,*any,*account,Account,a1,minu,10 ` ) @@ -1139,22 +1139,22 @@ func TestLoadAliases(t *testing.T) { Category: "call", Account: "dan", Subject: "dan", - Group: "*rating_profile", + Context: "*rating_profile", Values: AliasValues{ &AliasValue{ DestinationId: "EU_LANDLINE", - Alias: "dan1", + Pairs: AliasPairs{"Subject": map[string]string{"dan": "dan1"}}, Weight: 10, }, &AliasValue{ DestinationId: "GLOBAL1", - Alias: "dan2", + Pairs: AliasPairs{"Subject": map[string]string{"dan": "dan2"}}, Weight: 20, }, }, } if !reflect.DeepEqual(csvr.aliases[alias1.GetId()], alias1) { - t.Errorf("Unexpected alias %+v", csvr.aliases[alias1.GetId()]) + t.Errorf("Unexpected alias %+v", csvr.aliases[alias1.GetId()].Values[1]) } } diff --git a/engine/model_converters.go b/engine/model_converters.go index 0dac85177..c720e1fc9 100644 --- a/engine/model_converters.go +++ b/engine/model_converters.go @@ -367,8 +367,10 @@ func APItoModelAliases(attr *utils.TPAliases) (result []TpAlias) { Category: attr.Category, Account: attr.Account, Subject: attr.Subject, - Group: attr.Group, + Context: attr.Context, DestinationId: v.DestinationId, + Target: v.Target, + Original: v.Original, Alias: v.Alias, Weight: v.Weight, }) diff --git a/engine/model_helpers.go b/engine/model_helpers.go index d59c4e8f2..a9bb8a24d 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -752,12 +752,14 @@ func (tps TpAliases) GetAliases() (map[string]*utils.TPAliases, error) { Category: tp.Category, Account: tp.Account, Subject: tp.Subject, - Group: tp.Group, + Context: tp.Context, } als[tp.GetId()] = al } al.Values = append(al.Values, &utils.TPAliasValue{ DestinationId: tp.DestinationId, + Target: tp.Target, + Original: tp.Original, Alias: tp.Alias, Weight: tp.Weight, }) diff --git a/engine/models.go b/engine/models.go index e15bde880..ba897438f 100644 --- a/engine/models.go +++ b/engine/models.go @@ -167,7 +167,7 @@ type TpAction struct { TimingTags string `index:"11" re:"[0-9A-Za-z_;]*|\*any"` Units float64 `index:"12" re:"\d+\s*"` BalanceWeight float64 `index:"13" re:"\d+\.?\d*\s*"` - BalanceDisabled bool `index:"14"` + BalanceDisabled bool `index:"14" re:""` Weight float64 `index:"15" re:"\d+\.?\d*\s*"` CreatedAt time.Time } @@ -357,9 +357,11 @@ type TpAlias struct { Account string `index:"3" re:""` Subject string `index:"4" re:""` DestinationId string `index:"5" re:""` - Group string `index:"6" re:""` - Alias string `index:"7" re:""` - Weight float64 `index:"8" re:""` + Context string `index:"6" re:""` + Target string `index:"7" re:""` + Original string `index:"8" re:""` + Alias string `index:"9" re:""` + Weight float64 `index:"10" re:""` } func (ta *TpAlias) TableName() string { @@ -376,12 +378,12 @@ func (ta *TpAlias) SetId(id string) error { ta.Category = vals[2] ta.Account = vals[3] ta.Subject = vals[4] - ta.Group = vals[5] + ta.Context = vals[5] return nil } func (ta *TpAlias) GetId() string { - return utils.ConcatenatedKey(ta.Direction, ta.Tenant, ta.Category, ta.Account, ta.Subject, ta.Group) + return utils.ConcatenatedKey(ta.Direction, ta.Tenant, ta.Category, ta.Account, ta.Subject, ta.Context) } type TblCdrsPrimary struct { diff --git a/engine/storage_map.go b/engine/storage_map.go index bafa1fe3a..cc6010365 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -579,16 +579,21 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error err = ms.ms.Unmarshal(values, &al.Values) if err == nil { cache2go.Cache(key, al.Values) - for _, v := range al.Values { - var existingKeys map[string]bool - rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.Get(rKey); err == nil { - existingKeys = x.(map[string]bool) - } else { - existingKeys = make(map[string]bool) + for _, value := range al.Values { + + for target, pairs := range value.Pairs { + for _, alias := range pairs { + var existingKeys map[string]bool + rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context + if x, err := cache2go.Get(rKey); err == nil { + existingKeys = x.(map[string]bool) + } else { + existingKeys = make(map[string]bool) + } + existingKeys[utils.ConcatenatedKey(origKey, value.DestinationId)] = true + cache2go.Cache(rKey, existingKeys) + } } - existingKeys[utils.ConcatenatedKey(origKey, v.DestinationId)] = true - cache2go.Cache(rKey, existingKeys) } } } else { @@ -607,24 +612,28 @@ func (ms *MapStorage) RemoveAlias(key string) error { ms.ms.Unmarshal(values, &aliasValues) } delete(ms.dict, key) - for _, v := range aliasValues { - var existingKeys map[string]bool - rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.Get(rKey); err == nil { - existingKeys = x.(map[string]bool) - } - for eKey := range existingKeys { - if strings.HasPrefix(eKey, origKey) { - delete(existingKeys, eKey) + for _, value := range aliasValues { + for target, pairs := range value.Pairs { + for _, alias := range pairs { + var existingKeys map[string]bool + rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context + if x, err := cache2go.Get(rKey); err == nil { + existingKeys = x.(map[string]bool) + } + for eKey := range existingKeys { + if strings.HasPrefix(eKey, origKey) { + delete(existingKeys, eKey) + } + } + if len(existingKeys) == 0 { + cache2go.RemKey(rKey) + } else { + cache2go.Cache(rKey, existingKeys) + } } - } - if len(existingKeys) == 0 { - cache2go.RemKey(rKey) - } else { - cache2go.Cache(rKey, existingKeys) + cache2go.RemKey(key) } } - cache2go.RemKey(key) return nil } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 401d8641c..db120b01b 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -685,16 +685,20 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err err if err == nil { cache2go.Cache(key, al.Values) // cache reverse alias - for _, v := range al.Values { - var existingKeys map[string]bool - rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.Get(rKey); err == nil { - existingKeys = x.(map[string]bool) - } else { - existingKeys = make(map[string]bool) + for _, value := range al.Values { + for target, pairs := range value.Pairs { + for _, alias := range pairs { + var existingKeys map[string]bool + rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context + if x, err := cache2go.Get(rKey); err == nil { + existingKeys = x.(map[string]bool) + } else { + existingKeys = make(map[string]bool) + } + existingKeys[utils.ConcatenatedKey(origKey, value.DestinationId)] = true + cache2go.Cache(rKey, existingKeys) + } } - existingKeys[utils.ConcatenatedKey(origKey, v.DestinationId)] = true - cache2go.Cache(rKey, existingKeys) } } } @@ -712,24 +716,28 @@ func (rs *RedisStorage) RemoveAlias(key string) (err error) { } _, err = rs.db.Del(key) if err == nil { - for _, v := range aliasValues { - var existingKeys map[string]bool - rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.Get(rKey); err == nil { - existingKeys = x.(map[string]bool) - } - for eKey := range existingKeys { - if strings.HasPrefix(origKey, eKey) { - delete(existingKeys, eKey) + for _, value := range aliasValues { + for target, pairs := range value.Pairs { + for _, alias := range pairs { + var existingKeys map[string]bool + rKey := utils.REVERSE_ALIASES_PREFIX + alias + target + al.Context + if x, err := cache2go.Get(rKey); err == nil { + existingKeys = x.(map[string]bool) + } + for eKey := range existingKeys { + if strings.HasPrefix(origKey, eKey) { + delete(existingKeys, eKey) + } + } + if len(existingKeys) == 0 { + cache2go.RemKey(rKey) + } else { + cache2go.Cache(rKey, existingKeys) + } } - } - if len(existingKeys) == 0 { - cache2go.RemKey(rKey) - } else { - cache2go.Cache(rKey, existingKeys) + cache2go.RemKey(key) } } - cache2go.RemKey(key) } return } diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 1571ebc83..9d7a673a8 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -1385,7 +1385,7 @@ func (self *SQLStorage) SetTpAliases(aliases []TpAlias) error { Category: alias.Category, Account: alias.Account, Subject: alias.Subject, - Group: alias.Group, + Context: alias.Context, }).Delete(TpAlias{}).Error; err != nil { tx.Rollback() return err @@ -1419,8 +1419,8 @@ func (self *SQLStorage) GetTpAliases(filter *TpAlias) ([]TpAlias, error) { if len(filter.Subject) != 0 { q = q.Where("`subject` = ?", filter.Subject) } - if len(filter.Group) != 0 { - q = q.Where("`group` = ?", filter.Group) + if len(filter.Context) != 0 { + q = q.Where("`context` = ?", filter.Context) } if err := q.Find(&tpAliases).Error; err != nil { diff --git a/engine/storage_test.go b/engine/storage_test.go index 749ea3316..21e86f2b5 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -118,10 +118,10 @@ func TestStorageGetAliases(t *testing.T) { Category: "0", Account: "b1", Subject: "b1", - Group: utils.ALIAS_GROUP_RP, + Context: utils.ALIAS_GROUP_RP, Values: AliasValues{ &AliasValue{ - Alias: "aaa", + Pairs: AliasPairs{"Subject": map[string]string{"b1": "aaa"}}, Weight: 10, DestinationId: utils.ANY, }, @@ -133,10 +133,10 @@ func TestStorageGetAliases(t *testing.T) { Category: "0", Account: "b1", Subject: "b1", - Group: utils.ALIAS_GROUP_ACC, + Context: utils.ALIAS_GROUP_ACC, Values: AliasValues{ &AliasValue{ - Alias: "aaa", + Pairs: AliasPairs{"Account": map[string]string{"b1": "aaa"}}, Weight: 10, DestinationId: utils.ANY, }, @@ -169,7 +169,7 @@ func TestStorageCacheGetReverseAliases(t *testing.T) { Category: "0", Account: "b1", Subject: "b1", - Group: utils.ALIAS_GROUP_RP, + Context: utils.ALIAS_GROUP_RP, } alb := &Alias{ Direction: "*out", @@ -177,9 +177,9 @@ func TestStorageCacheGetReverseAliases(t *testing.T) { Category: "0", Account: "b1", Subject: "b1", - Group: utils.ALIAS_GROUP_ACC, + Context: utils.ALIAS_GROUP_ACC, } - if x, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_RP); err == nil { + if x, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + "Subject" + utils.ALIAS_GROUP_RP); err == nil { aliasKeys := x.(map[string]bool) _, found := aliasKeys[utils.ConcatenatedKey(ala.GetId(), utils.ANY)] if !found { @@ -188,7 +188,7 @@ func TestStorageCacheGetReverseAliases(t *testing.T) { } else { t.Error("Error getting reverse alias: ", err) } - if x, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_ACC); err == nil { + if x, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + "Account" + utils.ALIAS_GROUP_ACC); err == nil { aliasKeys := x.(map[string]bool) _, found := aliasKeys[utils.ConcatenatedKey(alb.GetId(), utils.ANY)] if !found { @@ -206,7 +206,7 @@ func TestStorageCacheRemoveCachedAliases(t *testing.T) { Category: "0", Account: "b1", Subject: "b1", - Group: utils.ALIAS_GROUP_RP, + Context: utils.ALIAS_GROUP_RP, } alb := &Alias{ Direction: "*out", @@ -214,7 +214,7 @@ func TestStorageCacheRemoveCachedAliases(t *testing.T) { Category: "0", Account: "b1", Subject: "b1", - Group: utils.ALIAS_GROUP_ACC, + Context: utils.ALIAS_GROUP_ACC, } accountingStorage.RemoveAlias(ala.GetId()) accountingStorage.RemoveAlias(alb.GetId()) diff --git a/engine/tp_reader.go b/engine/tp_reader.go index 5a5036591..c95df35d7 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -1092,15 +1092,18 @@ func (tpr *TpReader) LoadAliasesFiltered(filter *TpAlias) (bool, error) { Category: filter.Category, Account: filter.Account, Subject: filter.Subject, - Group: filter.Group, + Context: filter.Context, Values: make(AliasValues, 0), } for _, tpAlias := range tpAliases { - alias.Values = append(alias.Values, &AliasValue{ + av := &AliasValue{ DestinationId: tpAlias.DestinationId, - Alias: tpAlias.Alias, + Pairs: make(AliasPairs), Weight: tpAlias.Weight, - }) + } + av.Pairs[tpAlias.Target] = make(map[string]string) + av.Pairs[tpAlias.Target][tpAlias.Original] = tpAlias.Alias + alias.Values = append(alias.Values) } tpr.accountingStorage.SetAlias(alias) return len(tpAliases) > 0, err @@ -1124,18 +1127,22 @@ func (tpr *TpReader) LoadAliases() error { Category: tal.Category, Account: tal.Account, Subject: tal.Subject, - Group: tal.Group, + Context: tal.Context, Values: make(AliasValues, 0), } tpr.aliases[key] = al } for _, v := range tal.Values { - al.Values = append(al.Values, &AliasValue{ + av := &AliasValue{ DestinationId: v.DestinationId, - Alias: v.Alias, + Pairs: make(AliasPairs), Weight: v.Weight, - }) + } + av.Pairs[v.Target] = make(map[string]string) + av.Pairs[v.Target][v.Original] = v.Alias + al.Values = append(al.Values, av) } + } return err } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index b33e9a6e9..1c5d2af73 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -327,12 +327,14 @@ type TPAliases struct { Category string Account string Subject string - Group string + Context string Values []*TPAliasValue } type TPAliasValue struct { DestinationId string + Target string + Original string Alias string Weight float64 }