From cec4a4338e933d6784d12c24b7441371f168729b Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 8 Dec 2017 11:37:05 +0100 Subject: [PATCH] Alias profile context and Append, AliaSv1.GetAliasForEvent with test --- apier/v1/alias.go | 10 ++--- apier/v1/alias_it_test.go | 52 +++++++++++++++++++++++++ data/conf/samples/tutmysql/cgrates.json | 6 +++ data/tariffplans/tutorial/Alias.csv | 6 +-- engine/alias.go | 24 ++++++++++++ engine/libalias.go | 2 + utils/cgrevent.go | 9 +++-- utils/consts.go | 5 +++ 8 files changed, 101 insertions(+), 13 deletions(-) diff --git a/apier/v1/alias.go b/apier/v1/alias.go index 9b1a638e1..8cb86ecaa 100644 --- a/apier/v1/alias.go +++ b/apier/v1/alias.go @@ -82,13 +82,11 @@ func (alSv1 *AliasSv1) Call(serviceMethod string, return utils.APIerRPCCall(alSv1, serviceMethod, args, reply) } -/* -// GetAliasProfileForEvent returns matching AliasProfile for Event -func (alSv1 *AliasSv1) GetAliasProfileForEvent(ev *utils.CGREvent, - reply *engine.ApierAliasProfile) error { - return alSv1.alS.V1GetSuppliers(args, reply) +// GetAliasForEvent returns matching AliasProfile for Event +func (alSv1 *AliasSv1) GetAliasForEvent(ev *utils.CGREvent, + reply *engine.ExternalAliasProfile) error { + return alSv1.alS.V1GetAliasForEvent(ev, reply) } -*/ // ProcessEvent will replace event fields with the ones in maching AliasProfile func (alSv1 *AliasSv1) ProcessEvent(ev *utils.CGREvent, diff --git a/apier/v1/alias_it_test.go b/apier/v1/alias_it_test.go index cd14ab106..c2de188c0 100644 --- a/apier/v1/alias_it_test.go +++ b/apier/v1/alias_it_test.go @@ -49,6 +49,8 @@ var sTestsAlsPrf = []func(t *testing.T){ testAlsPrfResetStorDb, testAlsPrfStartEngine, testAlsPrfRPCConn, + testAlsPrfFromFolder, + //testAlsPrfGetAliasForEvent, testAlsPrfGetAlsPrfBeforeSet, testAlsPrfSetAlsPrf, testAlsPrfUpdateAlsPrf, @@ -124,6 +126,56 @@ func testAlsPrfGetAlsPrfBeforeSet(t *testing.T) { } } +func testAlsPrfFromFolder(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := alsPrfRPC.Call("ApierV1.LoadTariffPlanFromFolder", attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(500 * time.Millisecond) +} + +func testAlsPrfGetAliasForEvent(t *testing.T) { + ev := &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "testAlsPrfGetAliasForEvent", + Event: map[string]interface{}{ + "Account": "1007", + "Destination": "+491511231234", + }, + } + eAlsPrfl := engine.ExternalAliasProfile{ + Tenant: ev.Tenant, + ID: "ALS1", + FilterIDs: []string{"FLTR_ACNT_1007"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 35, 0, 0, time.UTC).Local()}, + Aliases: []*engine.AliasEntry{ + &engine.AliasEntry{ + FieldName: utils.ACCOUNT, + Initial: utils.ANY, + Alias: "1001", + Append: false, + }, + &engine.AliasEntry{ + FieldName: utils.SUBJECT, + Initial: utils.ANY, + Alias: "1001", + Append: false, + }, + }, + Weight: 10.0, + } + var alsReply engine.ExternalAliasProfile + if err := alsPrfRPC.Call(utils.AliasSv1GetAliasForEvent, + ev, &alsReply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eAlsPrfl, alsReply) { + t.Errorf("Expecting: %s, received: %s", + utils.ToJSON(eAlsPrfl), utils.ToJSON(alsReply)) + } +} + func testAlsPrfSetAlsPrf(t *testing.T) { alsPrf = &engine.ExternalAliasProfile{ Tenant: "cgrates.org", diff --git a/data/conf/samples/tutmysql/cgrates.json b/data/conf/samples/tutmysql/cgrates.json index 24d18013b..d9b270523 100644 --- a/data/conf/samples/tutmysql/cgrates.json +++ b/data/conf/samples/tutmysql/cgrates.json @@ -116,6 +116,12 @@ }, +"alias": { // Alias service (*new) + "enabled": true, // starts Alias service: . + "indexed_fields": [], // query indexes based on these fields for faster processing +}, + + "resources": { "enabled": true, "store_interval": "1s", diff --git a/data/tariffplans/tutorial/Alias.csv b/data/tariffplans/tutorial/Alias.csv index 9859d5b83..a7a31f09b 100644 --- a/data/tariffplans/tutorial/Alias.csv +++ b/data/tariffplans/tutorial/Alias.csv @@ -1,3 +1,3 @@ -#,Tenant,ID,FilterIDs,ActivationInterval,FieldName,Initial,Alias,Weight -cgrates.org,ALS1,FLTR_1,2014-01-14T00:00:00Z,Field1,Initial1,Alias1,20 -cgrates.org,ALS1,,,Field2,Initial2,Alias2, +#Tenant,ID,FilterIDs,ActivationInterval,Context,FieldName,Initial,Alias,Append,Weight +cgrates.org,ALS1,FLTR_ACNT_1007,2014-01-14T00:00:00Z,Account,*any,1001,10 +cgrates.org,ALS1,,,Subject,*any,1001, diff --git a/engine/alias.go b/engine/alias.go index 09765a3b8..ddee306b4 100644 --- a/engine/alias.go +++ b/engine/alias.go @@ -98,6 +98,30 @@ func (alS *AliasService) matchingAliasProfilesForEvent(ev *utils.CGREvent) (aPrf return } +func (alS *AliasService) aliasProfileForEvent(ev *utils.CGREvent) (alsPrfl *AliasProfile, err error) { + var alsPrfls AliasProfiles + if alsPrfls, err = alS.matchingAliasProfilesForEvent(ev); err != nil { + return + } else if len(alsPrfls) == 0 { + return nil, utils.ErrNotFound + } + return alsPrfls[0], nil +} + +func (alS *AliasService) V1GetAliasForEvent(ev *utils.CGREvent, + extAlsPrf *ExternalAliasProfile) (err error) { + alsPrf, err := alS.aliasProfileForEvent(ev) + if err != nil { + if err != utils.ErrNotFound { + err = utils.NewErrServerError(err) + } + return err + } + eAlsPrfl := NewExternalAliasProfileFromAliasProfile(alsPrf) + *extAlsPrf = *eAlsPrfl + return +} + func (alS *AliasService) V1ProcessEvent(ev *utils.CGREvent, reply *string) (err error) { return diff --git a/engine/libalias.go b/engine/libalias.go index ba8853b4b..04ecfc111 100644 --- a/engine/libalias.go +++ b/engine/libalias.go @@ -28,6 +28,7 @@ type AliasEntry struct { FieldName string Initial string Alias string + Append bool } type AliasProfile struct { @@ -35,6 +36,7 @@ type AliasProfile struct { ID string FilterIDs []string ActivationInterval *utils.ActivationInterval // Activation interval + Context string // bind this AliasProfile to specific context Aliases map[string]map[string]string // map[FieldName][InitialValue]AliasValue Weight float64 } diff --git a/utils/cgrevent.go b/utils/cgrevent.go index a82ae6f1d..fb4bab0d5 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -27,10 +27,11 @@ import ( // CGREvent is a generic event processed by CGR services type CGREvent struct { - Tenant string - ID string - Time *time.Time // event time - Event map[string]interface{} + Tenant string + ID string + Context string // attach the event to a context + Time *time.Time // event time + Event map[string]interface{} } func (ev *CGREvent) CheckMandatoryFields(fldNames []string) error { diff --git a/utils/consts.go b/utils/consts.go index be8f2290b..85bc5cc0e 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -622,6 +622,11 @@ const ( SupplierSv1GetSuppliers = "SupplierSv1.GetSuppliers" ) +// AliasS APIs +const ( + AliasSv1GetAliasForEvent = "AliasSv1.GetAliasForEvent" +) + func buildCacheInstRevPrefixes() { CachePrefixToInstance = make(map[string]string) for k, v := range CacheInstanceToPrefix {