mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Added *sipcid field type
This commit is contained in:
committed by
Dan Christian Bogos
parent
2a00b1cd5e
commit
cd7119c1ef
@@ -22,6 +22,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -429,6 +430,16 @@ func (ar *AgentRequest) ParseField(
|
||||
return nil, err
|
||||
}
|
||||
out = strconv.Itoa(int(t.Unix()))
|
||||
case utils.MetaSIPCID:
|
||||
isString = true
|
||||
values := make([]string, len(cfgFld.Value))
|
||||
for i, val := range cfgFld.Value {
|
||||
if values[i], err = val.ParseDataProvider(ar, utils.NestingSep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sort.Strings(values[1:])
|
||||
out = strings.Join(values, utils.INFIELD_SEP)
|
||||
}
|
||||
|
||||
if err != nil &&
|
||||
|
||||
@@ -1734,3 +1734,45 @@ func BenchmarkAgReqSetField(b *testing.B) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgReqSetFieldsSIPCID(t *testing.T) {
|
||||
cfg, _ := config.NewDefaultCGRConfig()
|
||||
data := engine.NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items)
|
||||
dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
|
||||
filterS := engine.NewFilterS(cfg, nil, dm)
|
||||
agReq := NewAgentRequest(nil, nil, nil, nil, nil, "cgrates.org", "", filterS, nil, nil)
|
||||
agReq.CGRRequest.Set(&utils.FullPath{Path: "cid", PathItems: utils.PathItems{{Field: "cid"}}}, utils.NewNMData("12345"))
|
||||
agReq.CGRRequest.Set(&utils.FullPath{Path: "to", PathItems: utils.PathItems{{Field: "to"}}}, utils.NewNMData("1001"))
|
||||
agReq.CGRRequest.Set(&utils.FullPath{Path: "from", PathItems: utils.PathItems{{Field: "from"}}}, utils.NewNMData("1002"))
|
||||
|
||||
tplFlds := []*config.FCTemplate{
|
||||
{Tag: "OriginID",
|
||||
Path: utils.MetaVars + utils.NestingSep + "OriginID", Type: utils.MetaSIPCID,
|
||||
Value: config.NewRSRParsersMustCompile("~*cgreq.cid;~*cgreq.to;~*cgreq.from", true, utils.INFIELD_SEP)},
|
||||
}
|
||||
for _, v := range tplFlds {
|
||||
v.ComputePath()
|
||||
}
|
||||
eMp := utils.NavigableMap2{}
|
||||
eMp.Set(utils.PathItems{{Field: utils.NodeID}}, utils.NewNMData(config.CgrConfig().GeneralCfg().NodeID))
|
||||
eMp.Set(utils.PathItems{{Field: "OriginID"}}, &utils.NMSlice{
|
||||
&config.NMItem{Data: "12345;1001;1002", Path: []string{"OriginID"},
|
||||
Config: tplFlds[0]}})
|
||||
|
||||
if err := agReq.SetFields(tplFlds); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(agReq.Vars, eMp) {
|
||||
t.Errorf("expecting: %+v,\n received: %+v", eMp, agReq.Vars)
|
||||
}
|
||||
|
||||
agReq = NewAgentRequest(nil, nil, nil, nil, nil, "cgrates.org", "", filterS, nil, nil)
|
||||
agReq.CGRRequest.Set(&utils.FullPath{Path: "cid", PathItems: utils.PathItems{{Field: "cid"}}}, utils.NewNMData("12345"))
|
||||
agReq.CGRRequest.Set(&utils.FullPath{Path: "to", PathItems: utils.PathItems{{Field: "to"}}}, utils.NewNMData("1002"))
|
||||
agReq.CGRRequest.Set(&utils.FullPath{Path: "from", PathItems: utils.PathItems{{Field: "from"}}}, utils.NewNMData("1001"))
|
||||
|
||||
if err := agReq.SetFields(tplFlds); err != nil {
|
||||
t.Error(err)
|
||||
} else if !reflect.DeepEqual(agReq.Vars, eMp) {
|
||||
t.Errorf("expecting: %+v,\n received: %+v", eMp, agReq.Vars)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ package engine
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -246,6 +247,15 @@ func (alS *AttributeService) processEvent(args *AttrArgsProcessEvent) (
|
||||
}
|
||||
substitute = strconv.FormatFloat(utils.Round(val*math.Pow10(exp),
|
||||
config.CgrConfig().GeneralCfg().RoundingDecimals, utils.ROUNDING_MIDDLE), 'f', -1, 64)
|
||||
case utils.MetaSIPCID:
|
||||
values := make([]string, len(attribute.Value))
|
||||
for i, val := range attribute.Value {
|
||||
if values[i], err = val.ParseDataProvider(evNm, utils.NestingSep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sort.Strings(values[1:])
|
||||
substitute = strings.Join(values, utils.INFIELD_SEP)
|
||||
default: // backwards compatible in case that Type is empty
|
||||
substitute, err = attribute.Value.ParseDataProvider(evNm, utils.NestingSep)
|
||||
}
|
||||
|
||||
@@ -1852,3 +1852,98 @@ func TestGetAttributeProfileFromInline(t *testing.T) {
|
||||
t.Errorf("Expecting %+v, received: %+v", utils.ToJSON(expAttrPrf1), utils.ToJSON(attr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttributesProcessEventSIPCID(t *testing.T) {
|
||||
//refresh the DM
|
||||
if err := dmAtr.DataDB().Flush(""); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
Cache.Clear(nil)
|
||||
if test, err := dmAtr.DataDB().IsDBEmpty(); err != nil {
|
||||
t.Error(err)
|
||||
} else if test != true {
|
||||
t.Errorf("\nExpecting: true got :%+v", test)
|
||||
}
|
||||
attrPrf1 := &AttributeProfile{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: "ATTR_1",
|
||||
Contexts: []string{utils.MetaSessionS},
|
||||
Attributes: []*Attribute{
|
||||
{
|
||||
Path: utils.MetaReq + utils.NestingSep + "OriginID",
|
||||
Type: utils.MetaSIPCID,
|
||||
Value: config.NewRSRParsersMustCompile("~*req.cid;~*req.to;~*req.from", true, utils.INFIELD_SEP),
|
||||
},
|
||||
},
|
||||
Blocker: true,
|
||||
Weight: 10,
|
||||
}
|
||||
// Add attribute in DM
|
||||
if err := dmAtr.SetAttributeProfile(attrPrf1, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
attrArgs := &AttrArgsProcessEvent{
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
ProcessRuns: utils.IntPointer(1),
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"cid": "12345",
|
||||
"to": "1001",
|
||||
"from": "1002",
|
||||
},
|
||||
},
|
||||
}
|
||||
eRply := &AttrSProcessEventReply{
|
||||
MatchedProfiles: []string{"ATTR_1"},
|
||||
AlteredFields: []string{utils.MetaReq + utils.NestingSep + "OriginID"},
|
||||
CGREvent: &utils.CGREvent{
|
||||
Tenant: config.CgrConfig().GeneralCfg().DefaultTenant,
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]interface{}{
|
||||
"cid": "12345",
|
||||
"to": "1001",
|
||||
"from": "1002",
|
||||
"OriginID": "12345;1001;1002",
|
||||
},
|
||||
},
|
||||
}
|
||||
var reply AttrSProcessEventReply
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
|
||||
attrArgs.CGREvent.Event = map[string]interface{}{
|
||||
"cid": "12345",
|
||||
"to": "1002",
|
||||
"from": "1001",
|
||||
}
|
||||
eRply.CGREvent.Event = map[string]interface{}{
|
||||
"cid": "12345",
|
||||
"to": "1002",
|
||||
"from": "1001",
|
||||
"OriginID": "12345;1001;1002",
|
||||
}
|
||||
if err := attrService.V1ProcessEvent(attrArgs, &reply); err != nil {
|
||||
t.Errorf("Error: %+v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.MatchedProfiles, reply.MatchedProfiles) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.MatchedProfiles, reply.MatchedProfiles)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.AlteredFields, reply.AlteredFields) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.AlteredFields, reply.AlteredFields)
|
||||
}
|
||||
if !reflect.DeepEqual(eRply.CGREvent.Event, reply.CGREvent.Event) {
|
||||
t.Errorf("Expecting %+v, received: %+v", eRply.CGREvent.Event, reply.CGREvent.Event)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,6 +309,7 @@ const (
|
||||
HIERARCHY_SEP = ">"
|
||||
META_COMPOSED = "*composed"
|
||||
META_USAGE_DIFFERENCE = "*usage_difference"
|
||||
MetaSIPCID = "*sipcid"
|
||||
MetaDifference = "*difference"
|
||||
MetaVariable = "*variable"
|
||||
MetaCCUsage = "*cc_usage"
|
||||
|
||||
Reference in New Issue
Block a user