From 3d8d979540288c48806023dc3101eb3a368935d1 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 19 Aug 2015 19:23:39 +0300 Subject: [PATCH] adde GetMatchingAlias to alias API --- engine/aliases.go | 91 ++++++++++++++++++++++++++-------------------- engine/calldesc.go | 33 ++++++++++++++--- 2 files changed, 79 insertions(+), 45 deletions(-) diff --git a/engine/aliases.go b/engine/aliases.go index 5cd33b331..6735bf0f5 100644 --- a/engine/aliases.go +++ b/engine/aliases.go @@ -97,11 +97,22 @@ func (al *Alias) SetId(id string) error { return nil } +type AttrMatchingAlias struct { + Destination string + Direction string + Tenant string + Category string + Account string + Subject string + Group string +} + type AliasService interface { SetAlias(Alias, *string) error UpdateAlias(Alias, *string) error RemoveAlias(Alias, *string) error GetAlias(Alias, *Alias) error + GetMatchingAlias(AttrMatchingAlias, *string) error } type AliasHandler struct { @@ -191,6 +202,43 @@ func (am *AliasHandler) GetAlias(al Alias, result *Alias) error { return utils.ErrNotFound } +func (am *AliasHandler) GetMatchingAlias(attr AttrMatchingAlias, result *string) error { + response := Alias{} + if err := aliasService.GetAlias(Alias{ + Direction: attr.Direction, + Tenant: attr.Tenant, + Category: attr.Category, + Account: attr.Account, + Subject: attr.Subject, + Group: attr.Group, + }, &response); err != nil { + return err + } + // sort according to weight + values := response.Values.GetWeightSlice() + // check destination ids + + for _, p := range utils.SplitPrefix(attr.Destination, MIN_PREFIX_MATCH) { + if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + for _, aliasHandler := range values { + for alias, aliasDestIds := range aliasHandler { + destIds := x.(map[interface{}]struct{}) + for idId := range destIds { + dId := idId.(string) + for _, aliasDestId := range aliasDestIds { + if aliasDestId == utils.ANY || aliasDestId == dId { + *result = alias + return nil + } + } + } + } + } + } + } + return utils.ErrNotFound +} + type ProxyAliasService struct { Client *rpcclient.RpcClient } @@ -219,45 +267,10 @@ func (ps *ProxyAliasService) GetAlias(al Alias, alias *Alias) error { return ps.Client.Call("AliasV1.GetAlias", al, alias) } +func (ps *ProxyAliasService) GetMatchingAlias(attr AttrMatchingAlias, alias *string) error { + return ps.Client.Call("AliasV1.GetMatchingAlias", attr, alias) +} + func (ps *ProxyAliasService) ReloadAliases(in string, reply *string) error { return ps.Client.Call("AliasV1.ReloadAliases", in, reply) } - -func GetBestAlias(destination, direction, tenant, category, account, subject, group string) (string, error) { - if aliasService == nil { - return "", nil - } - response := Alias{} - if err := aliasService.GetAlias(Alias{ - Direction: direction, - Tenant: tenant, - Category: category, - Account: account, - Subject: subject, - Group: group, - }, &response); err != nil { - return "", err - } - // sort according to weight - values := response.Values.GetWeightSlice() - // check destination ids - - for _, p := range utils.SplitPrefix(destination, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { - for _, aliasHandler := range values { - for alias, aliasDestIds := range aliasHandler { - destIds := x.(map[interface{}]struct{}) - for idId := range destIds { - dId := idId.(string) - for _, aliasDestId := range aliasDestIds { - if aliasDestId == utils.ANY || aliasDestId == dId { - return alias, nil - } - } - } - } - } - } - } - return "", utils.ErrNotFound -} diff --git a/engine/calldesc.go b/engine/calldesc.go index a509e9b67..715b1dfda 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -303,9 +303,19 @@ func (cd *CallDescriptor) addRatingInfos(ris RatingInfos) bool { // The prefixLen is limiting the length of the destination prefix. func (cd *CallDescriptor) GetKey(subject string) string { // check if subject is alias - if alias, err := GetBestAlias(cd.Destination, cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject, utils.ALIAS_GROUP_RP); err == nil && alias != "" { - subject = alias - cd.Subject = alias + if aliasService != nil { + var alias string + if err := aliasService.GetMatchingAlias(AttrMatchingAlias{ + Destination: cd.Destination, + Direction: cd.Direction, + Tenant: cd.Tenant, + Category: cd.Category, + Account: cd.Account, + Subject: cd.Subject, + Group: utils.ALIAS_GROUP_RP}, &alias); err == nil && alias != "" { + subject = alias + cd.Subject = alias + } } return utils.ConcatenatedKey(cd.Direction, cd.Tenant, cd.Category, subject) } @@ -315,9 +325,20 @@ func (cd *CallDescriptor) GetAccountKey() string { subj := cd.Subject if cd.Account != "" { // check if subject is alias - alias, err := GetBestAlias(cd.Destination, cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject, utils.ALIAS_GROUP_ACC) - if err == nil && alias != "" { - cd.Account = alias + if aliasService != nil { + var alias string + err := aliasService.GetMatchingAlias( + AttrMatchingAlias{ + Destination: cd.Destination, + Direction: cd.Direction, + Tenant: cd.Tenant, + Category: cd.Category, + Account: cd.Account, + Subject: cd.Subject, + Group: utils.ALIAS_GROUP_ACC}, &alias) + if err == nil && alias != "" { + cd.Account = alias + } } subj = cd.Account }