mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Improve *destinations filter logic
This commit is contained in:
committed by
Dan Christian Bogos
parent
b8cc20bb7c
commit
815e4476bf
@@ -556,29 +556,29 @@ func (fltr *FilterRule) passDestinations(dDP utils.DataProvider) (bool, error) {
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
for _, p := range utils.SplitPrefix(dst, MIN_PREFIX_MATCH) {
|
||||
var destIDs []string
|
||||
if err = connMgr.Call(context.TODO(), config.CgrConfig().FilterSCfg().ApierSConns,
|
||||
utils.APIerSv1GetReverseDestination,
|
||||
&p, &destIDs); err != nil {
|
||||
if err.Error() != utils.ErrNotFound.Error() {
|
||||
return false, err
|
||||
}
|
||||
// iterate the DestinationIDs gotten from filter values, and try to get their prefixes from DataDB
|
||||
for _, valDstIDVal := range fltr.rsrValues {
|
||||
valDstID, err := valDstIDVal.ParseDataProvider(dDP)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, dID := range destIDs {
|
||||
for _, valDstIDVal := range fltr.rsrValues {
|
||||
valDstID, err := valDstIDVal.ParseDataProvider(dDP)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if valDstID == dID {
|
||||
return true, nil
|
||||
}
|
||||
var rplDest Destination
|
||||
if err = connMgr.Call(context.TODO(), config.CgrConfig().FilterSCfg().ApierSConns,
|
||||
utils.APIerSv1GetDestination,
|
||||
&valDstID, &rplDest); err != nil {
|
||||
if err.Error() == utils.ErrNotFound.Error() {
|
||||
continue // if destination not found, continue on next filter value
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
// return true if any prefixes found from GetDestination match the destination gotten from filter element
|
||||
for _, rplPrfx := range rplDest.Prefixes {
|
||||
if strings.HasPrefix(dst, rplPrfx) {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
return false, err
|
||||
}
|
||||
|
||||
func (fltr *FilterRule) passRSR(dDP utils.DataProvider) (bool, error) {
|
||||
|
||||
@@ -2056,7 +2056,7 @@ func TestFiltersPassTimingsCallErr(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFiltersPassDestinationsErrParseNotFound(t *testing.T) {
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Account", []string{"1001"})
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Destination", []string{"DST_1001"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -2073,7 +2073,7 @@ func TestFiltersPassDestinationsErrParseNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFiltersPassDestinationsErrParseWrongPath(t *testing.T) {
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Account", []string{"1001"})
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Destination", []string{"DST_1001"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -2094,13 +2094,13 @@ func TestFiltersPassDestinationsErrParseWrongPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFiltersPassDestinationsCallErr(t *testing.T) {
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Account", []string{"1001"})
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Destination", []string{"DST_1001"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dtP := utils.MapStorage{
|
||||
utils.MetaReq: map[string]any{
|
||||
utils.AccountField: "1001",
|
||||
utils.Destination: "DST_1001",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2130,9 +2130,11 @@ func TestFiltersPassDestinationsCallSuccessSameDest(t *testing.T) {
|
||||
client := make(chan birpc.ClientConnector, 1)
|
||||
ccM := &ccMock{
|
||||
calls: map[string]func(ctx *context.Context, args any, reply any) error{
|
||||
utils.APIerSv1GetReverseDestination: func(ctx *context.Context, args, reply any) error {
|
||||
rply := []string{"1002"}
|
||||
*reply.(*[]string) = rply
|
||||
utils.APIerSv1GetDestination: func(ctx *context.Context, args, reply any) error {
|
||||
*reply.(*Destination) = Destination{
|
||||
Id: "DST_1002",
|
||||
Prefixes: []string{"1002"},
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
@@ -2143,13 +2145,13 @@ func TestFiltersPassDestinationsCallSuccessSameDest(t *testing.T) {
|
||||
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaApier): client,
|
||||
})
|
||||
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Account", []string{"1002"})
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Destination", []string{"DST_1002"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dtP := utils.MapStorage{
|
||||
utils.MetaReq: map[string]any{
|
||||
utils.AccountField: "1002",
|
||||
utils.Destination: "1002",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2179,9 +2181,11 @@ func TestFiltersPassDestinationsCallSuccessParseErr(t *testing.T) {
|
||||
client := make(chan birpc.ClientConnector, 1)
|
||||
ccM := &ccMock{
|
||||
calls: map[string]func(ctx *context.Context, args any, reply any) error{
|
||||
utils.APIerSv1GetReverseDestination: func(ctx *context.Context, args, reply any) error {
|
||||
rply := []string{"1002"}
|
||||
*reply.(*[]string) = rply
|
||||
utils.APIerSv1GetDestination: func(ctx *context.Context, args, reply any) error {
|
||||
*reply.(*Destination) = Destination{
|
||||
Id: "DST_1002",
|
||||
Prefixes: []string{"1002"},
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
@@ -2192,13 +2196,13 @@ func TestFiltersPassDestinationsCallSuccessParseErr(t *testing.T) {
|
||||
utils.ConcatenatedKey(utils.MetaInternal, utils.MetaApier): client,
|
||||
})
|
||||
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Account", []string{"~1002"})
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Destination", []string{"~DST_1002"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dtP := utils.MapStorage{
|
||||
utils.MetaReq: map[string]any{
|
||||
utils.AccountField: "1002",
|
||||
utils.Destination: "DST_1002",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2214,7 +2218,7 @@ func TestFiltersPassDestinationsCallSuccessParseErr(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFiltersPassRSRErrParseWrongPath(t *testing.T) {
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Account", []string{"1001"})
|
||||
fltr, err := NewFilterRule(utils.MetaDestinations, "~*req.Destination", []string{"DST_1001"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -30,8 +30,12 @@ import (
|
||||
)
|
||||
|
||||
func TestFilterPassDestinations(t *testing.T) {
|
||||
if err := engine.Cache.Set(utils.CacheReverseDestinations, "+49",
|
||||
[]string{"DE", "EU_LANDLINE"}, nil, true, ""); err != nil {
|
||||
if err := engine.Cache.Set(utils.CacheDestinations, "DE",
|
||||
&engine.Destination{Id: "DE", Prefixes: []string{"+49"}}, nil, true, ""); err != nil {
|
||||
t.Errorf("Expecting: nil, received: %s", err)
|
||||
}
|
||||
if err := engine.Cache.Set(utils.CacheDestinations, "EU_LANDLINE",
|
||||
&engine.Destination{Id: "EU_LANDLINE", Prefixes: []string{"+49"}}, nil, true, ""); err != nil {
|
||||
t.Errorf("Expecting: nil, received: %s", err)
|
||||
}
|
||||
config.CgrConfig().FilterSCfg().ApierSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaApier)}
|
||||
|
||||
Reference in New Issue
Block a user