diff --git a/engine/model_converters.go b/engine/model_converters.go deleted file mode 100644 index 4b1736a64..000000000 --- a/engine/model_converters.go +++ /dev/null @@ -1,666 +0,0 @@ -/* -Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments -Copyright (C) ITsysCOM GmbH - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see -*/ -package engine - -import ( - "strconv" - - "github.com/cgrates/cgrates/utils" -) - -func APItoModelTiming(t *utils.ApierTPTiming) (result TpTiming) { - return TpTiming{ - Tpid: t.TPid, - Tag: t.ID, - Years: t.Years, - Months: t.Months, - MonthDays: t.MonthDays, - WeekDays: t.WeekDays, - Time: t.Time, - } -} - -func APItoModelTimings(ts []*utils.ApierTPTiming) (result TpTimings) { - for _, t := range ts { - if t != nil { - at := APItoModelTiming(t) - result = append(result, at) - } - } - return result -} - -func APItoModelDestination(d *utils.TPDestination) (result TpDestinations) { - if d != nil { - for _, p := range d.Prefixes { - result = append(result, TpDestination{ - Tpid: d.TPid, - Tag: d.ID, - Prefix: p, - }) - } - if len(d.Prefixes) == 0 { - result = append(result, TpDestination{ - Tpid: d.TPid, - Tag: d.ID, - }) - } - } - return -} - -func APItoModelRate(r *utils.TPRate) (result TpRates) { - if r != nil { - for _, rs := range r.RateSlots { - result = append(result, TpRate{ - Tpid: r.TPid, - Tag: r.ID, - ConnectFee: rs.ConnectFee, - Rate: rs.Rate, - RateUnit: rs.RateUnit, - RateIncrement: rs.RateIncrement, - GroupIntervalStart: rs.GroupIntervalStart, - }) - } - if len(r.RateSlots) == 0 { - result = append(result, TpRate{ - Tpid: r.TPid, - Tag: r.ID, - }) - } - } - return -} - -func APItoModelRates(rs []*utils.TPRate) (result TpRates) { - for _, r := range rs { - for _, sr := range APItoModelRate(r) { - result = append(result, sr) - } - } - return result -} - -func APItoModelDestinationRate(d *utils.TPDestinationRate) (result TpDestinationRates) { - if d != nil { - for _, dr := range d.DestinationRates { - result = append(result, TpDestinationRate{ - Tpid: d.TPid, - Tag: d.ID, - DestinationsTag: dr.DestinationId, - RatesTag: dr.RateId, - RoundingMethod: dr.RoundingMethod, - RoundingDecimals: dr.RoundingDecimals, - MaxCost: dr.MaxCost, - MaxCostStrategy: dr.MaxCostStrategy, - }) - } - if len(d.DestinationRates) == 0 { - result = append(result, TpDestinationRate{ - Tpid: d.TPid, - Tag: d.ID, - }) - } - } - return -} - -func APItoModelDestinationRates(drs []*utils.TPDestinationRate) (result TpDestinationRates) { - if drs != nil { - for _, dr := range drs { - for _, sdr := range APItoModelDestinationRate(dr) { - result = append(result, sdr) - } - } - } - return result -} - -func APItoModelRatingPlan(rp *utils.TPRatingPlan) (result TpRatingPlans) { - if rp != nil { - for _, rpb := range rp.RatingPlanBindings { - result = append(result, TpRatingPlan{ - Tpid: rp.TPid, - Tag: rp.ID, - DestratesTag: rpb.DestinationRatesId, - TimingTag: rpb.TimingId, - Weight: rpb.Weight, - }) - } - if len(rp.RatingPlanBindings) == 0 { - result = append(result, TpRatingPlan{ - Tpid: rp.TPid, - Tag: rp.ID, - }) - } - } - return -} - -func APItoModelRatingPlans(rps []*utils.TPRatingPlan) (result TpRatingPlans) { - for _, rp := range rps { - for _, srp := range APItoModelRatingPlan(rp) { - result = append(result, srp) - } - } - return result -} - -func APItoModelRatingProfile(rp *utils.TPRatingProfile) (result TpRatingProfiles) { - if rp != nil { - for _, rpa := range rp.RatingPlanActivations { - result = append(result, TpRatingProfile{ - Tpid: rp.TPid, - Loadid: rp.LoadId, - Direction: rp.Direction, - Tenant: rp.Tenant, - Category: rp.Category, - Subject: rp.Subject, - ActivationTime: rpa.ActivationTime, - RatingPlanTag: rpa.RatingPlanId, - FallbackSubjects: rpa.FallbackSubjects, - CdrStatQueueIds: rpa.CdrStatQueueIds, - }) - } - if len(rp.RatingPlanActivations) == 0 { - result = append(result, TpRatingProfile{ - Tpid: rp.TPid, - Loadid: rp.LoadId, - Direction: rp.Direction, - Tenant: rp.Tenant, - Category: rp.Category, - Subject: rp.Subject, - }) - } - } - return -} - -func APItoModelRatingProfiles(rps []*utils.TPRatingProfile) (result TpRatingProfiles) { - for _, rp := range rps { - for _, srp := range APItoModelRatingProfile(rp) { - result = append(result, srp) - } - } - return result -} - -func APItoModelLcrRule(lrs *utils.TPLcrRules) (result TpLcrRules) { - if lrs != nil { - for _, r := range lrs.Rules { - result = append(result, TpLcrRule{ - Tpid: lrs.TPid, - Direction: lrs.Direction, - Tenant: lrs.Tenant, - Category: lrs.Category, - Account: lrs.Account, - Subject: lrs.Subject, - DestinationTag: r.DestinationId, - RpCategory: r.RpCategory, - Strategy: r.Strategy, - StrategyParams: r.StrategyParams, - ActivationTime: r.ActivationTime, - Weight: r.Weight, - }) - } - if len(lrs.Rules) == 0 { - result = append(result, TpLcrRule{ - Tpid: lrs.TPid, - Direction: lrs.Direction, - Tenant: lrs.Tenant, - Category: lrs.Category, - Account: lrs.Account, - Subject: lrs.Subject, - }) - } - } - return -} - -func APItoModelLcrRules(ts []*utils.TPLcrRules) (result TpLcrRules) { - for _, t := range ts { - for _, st := range APItoModelLcrRule(t) { - result = append(result, st) - } - } - return result -} - -func APItoModelAction(as *utils.TPActions) (result TpActions) { - if as != nil { - for _, a := range as.Actions { - result = append(result, TpAction{ - Tpid: as.TPid, - Tag: as.ID, - Action: a.Identifier, - BalanceTag: a.BalanceId, - BalanceType: a.BalanceType, - Directions: a.Directions, - Units: a.Units, - ExpiryTime: a.ExpiryTime, - Filter: a.Filter, - TimingTags: a.TimingTags, - DestinationTags: a.DestinationIds, - RatingSubject: a.RatingSubject, - Categories: a.Categories, - SharedGroups: a.SharedGroups, - BalanceWeight: a.BalanceWeight, - BalanceBlocker: a.BalanceBlocker, - BalanceDisabled: a.BalanceDisabled, - ExtraParameters: a.ExtraParameters, - Weight: a.Weight, - }) - } - if len(as.Actions) == 0 { - result = append(result, TpAction{ - Tpid: as.TPid, - Tag: as.ID, - }) - } - } - return -} - -func APItoModelActions(as []*utils.TPActions) (result TpActions) { - for _, a := range as { - for _, sa := range APItoModelAction(a) { - result = append(result, sa) - } - } - return result -} - -func APItoModelActionPlan(a *utils.TPActionPlan) (result TpActionPlans) { - if a != nil { - for _, ap := range a.ActionPlan { - result = append(result, TpActionPlan{ - Tpid: a.TPid, - Tag: a.ID, - ActionsTag: ap.ActionsId, - TimingTag: ap.TimingId, - Weight: ap.Weight, - }) - } - if len(a.ActionPlan) == 0 { - result = append(result, TpActionPlan{ - Tpid: a.TPid, - Tag: a.ID, - }) - } - } - return -} - -func APItoModelActionPlans(aps []*utils.TPActionPlan) (result TpActionPlans) { - for _, ap := range aps { - for _, sap := range APItoModelActionPlan(ap) { - result = append(result, sap) - } - } - return result -} - -func APItoModelActionTrigger(ats *utils.TPActionTriggers) (result TpActionTriggers) { - if ats != nil { - for _, at := range ats.ActionTriggers { - result = append(result, TpActionTrigger{ - Tpid: ats.TPid, - Tag: ats.ID, - UniqueId: at.UniqueID, - ThresholdType: at.ThresholdType, - ThresholdValue: at.ThresholdValue, - Recurrent: at.Recurrent, - MinSleep: at.MinSleep, - ExpiryTime: at.ExpirationDate, - ActivationTime: at.ActivationDate, - BalanceTag: at.BalanceId, - BalanceType: at.BalanceType, - BalanceDirections: at.BalanceDirections, - BalanceDestinationTags: at.BalanceDestinationIds, - BalanceWeight: at.BalanceWeight, - BalanceExpiryTime: at.BalanceExpirationDate, - BalanceTimingTags: at.BalanceTimingTags, - BalanceRatingSubject: at.BalanceRatingSubject, - BalanceCategories: at.BalanceCategories, - BalanceSharedGroups: at.BalanceSharedGroups, - BalanceBlocker: at.BalanceBlocker, - BalanceDisabled: at.BalanceDisabled, - MinQueuedItems: at.MinQueuedItems, - ActionsTag: at.ActionsId, - Weight: at.Weight, - }) - } - if len(ats.ActionTriggers) == 0 { - result = append(result, TpActionTrigger{ - Tpid: ats.TPid, - Tag: ats.ID, - }) - } - } - return -} - -func APItoModelActionTriggers(ts []*utils.TPActionTriggers) (result TpActionTriggers) { - for _, t := range ts { - for _, st := range APItoModelActionTrigger(t) { - result = append(result, st) - } - } - return result -} - -func APItoModelAccountAction(aa *utils.TPAccountActions) *TpAccountAction { - return &TpAccountAction{ - Tpid: aa.TPid, - Loadid: aa.LoadId, - Tenant: aa.Tenant, - Account: aa.Account, - ActionPlanTag: aa.ActionPlanId, - ActionTriggersTag: aa.ActionTriggersId, - AllowNegative: aa.AllowNegative, - Disabled: aa.Disabled, - } -} - -func APItoModelAccountActions(aas []*utils.TPAccountActions) (result TpAccountActions) { - for _, aa := range aas { - if aa != nil { - result = append(result, *APItoModelAccountAction(aa)) - } - } - return result -} - -func APItoModelSharedGroup(sgs *utils.TPSharedGroups) (result TpSharedGroups) { - if sgs != nil { - for _, sg := range sgs.SharedGroups { - result = append(result, TpSharedGroup{ - Tpid: sgs.TPid, - Tag: sgs.ID, - Account: sg.Account, - Strategy: sg.Strategy, - RatingSubject: sg.RatingSubject, - }) - } - if len(sgs.SharedGroups) == 0 { - result = append(result, TpSharedGroup{ - Tpid: sgs.TPid, - Tag: sgs.ID, - }) - } - } - return -} - -func APItoModelSharedGroups(sgs []*utils.TPSharedGroups) (result TpSharedGroups) { - for _, sg := range sgs { - for _, ssg := range APItoModelSharedGroup(sg) { - result = append(result, ssg) - } - } - return result -} - -func APItoModelDerivedCharger(dcs *utils.TPDerivedChargers) (result TpDerivedChargers) { - if dcs != nil { - for _, dc := range dcs.DerivedChargers { - result = append(result, TpDerivedCharger{ - Tpid: dcs.TPid, - Loadid: dcs.LoadId, - Direction: dcs.Direction, - Tenant: dcs.Tenant, - Category: dcs.Category, - Account: dcs.Account, - Subject: dcs.Subject, - Runid: dc.RunId, - RunFilters: dc.RunFilters, - ReqTypeField: dc.ReqTypeField, - DirectionField: dc.DirectionField, - TenantField: dc.TenantField, - CategoryField: dc.CategoryField, - AccountField: dc.AccountField, - SubjectField: dc.SubjectField, - PddField: dc.PddField, - DestinationField: dc.DestinationField, - SetupTimeField: dc.SetupTimeField, - AnswerTimeField: dc.AnswerTimeField, - UsageField: dc.UsageField, - SupplierField: dc.SupplierField, - DisconnectCauseField: dc.DisconnectCauseField, - CostField: dc.CostField, - RatedField: dc.RatedField, - }) - } - if len(dcs.DerivedChargers) == 0 { - result = append(result, TpDerivedCharger{ - Tpid: dcs.TPid, - Loadid: dcs.LoadId, - Direction: dcs.Direction, - Tenant: dcs.Tenant, - Category: dcs.Category, - Account: dcs.Account, - Subject: dcs.Subject, - }) - } - } - return -} - -func APItoModelDerivedChargers(dcs []*utils.TPDerivedChargers) (result TpDerivedChargers) { - for _, dc := range dcs { - for _, sdc := range APItoModelDerivedCharger(dc) { - result = append(result, sdc) - } - } - return result -} - -func APItoModelCdrStat(css *utils.TPCdrStats) (result TpCdrStats) { - if css != nil { - for _, cs := range css.CdrStats { - ql, _ := strconv.Atoi(cs.QueueLength) - result = append(result, TpCdrstat{ - Tpid: css.TPid, - Tag: css.ID, - QueueLength: ql, - TimeWindow: cs.TimeWindow, - SaveInterval: cs.SaveInterval, - Metrics: cs.Metrics, - SetupInterval: cs.SetupInterval, - Tors: cs.TORs, - CdrHosts: cs.CdrHosts, - CdrSources: cs.CdrSources, - ReqTypes: cs.ReqTypes, - Directions: cs.Directions, - Tenants: cs.Tenants, - Categories: cs.Categories, - Accounts: cs.Accounts, - Subjects: cs.Subjects, - DestinationIds: cs.DestinationIds, - PddInterval: cs.PddInterval, - UsageInterval: cs.UsageInterval, - Suppliers: cs.Suppliers, - DisconnectCauses: cs.DisconnectCauses, - MediationRunids: cs.MediationRunIds, - RatedAccounts: cs.RatedAccounts, - RatedSubjects: cs.RatedSubjects, - CostInterval: cs.CostInterval, - ActionTriggers: cs.ActionTriggers, - }) - } - if len(css.CdrStats) == 0 { - result = append(result, TpCdrstat{ - Tpid: css.TPid, - Tag: css.ID, - }) - } - } - return -} - -func APItoModelCdrStats(css []*utils.TPCdrStats) (result TpCdrStats) { - for _, cs := range css { - for _, scs := range APItoModelCdrStat(cs) { - result = append(result, scs) - } - } - return result -} - -func APItoModelAliases(as *utils.TPAliases) (result TpAliases) { - if as != nil { - for _, v := range as.Values { - result = append(result, TpAlias{ - Tpid: as.TPid, - Direction: as.Direction, - Tenant: as.Tenant, - Category: as.Category, - Account: as.Account, - Subject: as.Subject, - Context: as.Context, - DestinationId: v.DestinationId, - Target: v.Target, - Original: v.Original, - Alias: v.Alias, - Weight: v.Weight, - }) - } - if len(as.Values) == 0 { - result = append(result, TpAlias{ - Tpid: as.TPid, - Direction: as.Direction, - Tenant: as.Tenant, - Category: as.Category, - Account: as.Account, - Subject: as.Subject, - Context: as.Context, - }) - } - } - return -} - -func APItoModelAliasesA(as []*utils.TPAliases) (result TpAliases) { - for _, a := range as { - for _, sa := range APItoModelAliases(a) { - result = append(result, sa) - } - } - return result -} - -func APItoModelUsers(us *utils.TPUsers) (result TpUsers) { - if us != nil { - for _, p := range us.Profile { - result = append(result, TpUser{ - Tpid: us.TPid, - Tenant: us.Tenant, - UserName: us.UserName, - Masked: us.Masked, - Weight: us.Weight, - AttributeName: p.AttrName, - AttributeValue: p.AttrValue, - }) - } - if len(us.Profile) == 0 { - result = append(result, TpUser{ - Tpid: us.TPid, - Tenant: us.Tenant, - UserName: us.UserName, - Masked: us.Masked, - Weight: us.Weight, - }) - } - } - return -} - -func APItoModelUsersA(ts []*utils.TPUsers) (result TpUsers) { - for _, t := range ts { - for _, st := range APItoModelUsers(t) { - result = append(result, st) - } - } - return result -} - -func APItoModelResourceLimit(rl *utils.TPResourceLimit) TpResourceLimits { - result := TpResourceLimits{} - for _, f := range rl.Filters { - tp := &TpResourceLimit{ - Tpid: rl.TPid, - Tag: rl.ID, - ActivationTime: rl.ActivationTime, - Weight: rl.Weight, - Limit: rl.Limit, - } - for i, atid := range rl.ActionTriggerIDs { - if i != 0 { - tp.ActionTriggerIds = tp.ActionTriggerIds + utils.INFIELD_SEP + atid - } else { - tp.ActionTriggerIds = atid - } - } - tp.FilterType = f.Type - tp.FilterFieldName = f.FieldName - for i, val := range f.Values { - if i != 0 { - tp.FilterFieldValues = tp.FilterFieldValues + utils.INFIELD_SEP + val - } else { - tp.FilterFieldValues = val - } - } - result = append(result, tp) - } - if len(rl.Filters) == 0 { - tp := &TpResourceLimit{ - Tpid: rl.TPid, - Tag: rl.ID, - ActivationTime: rl.ActivationTime, - Weight: rl.Weight, - Limit: rl.Limit, - } - for i, atid := range rl.ActionTriggerIDs { - if i != 0 { - tp.ActionTriggerIds = tp.ActionTriggerIds + utils.INFIELD_SEP + atid - } else { - tp.ActionTriggerIds = atid - } - } - result = append(result, tp) - } - return result -} - -func APItoResourceLimit(tpRL *utils.TPResourceLimit, timezone string) (rl *ResourceLimit, err error) { - rl = &ResourceLimit{ID: tpRL.ID, Weight: tpRL.Weight, Filters: make([]*RequestFilter, len(tpRL.Filters)), Usage: make(map[string]*ResourceUsage)} - for i, f := range tpRL.Filters { - rf := &RequestFilter{Type: f.Type, FieldName: f.FieldName, Values: f.Values} - if err := rf.CompileValues(); err != nil { - return nil, err - } - rl.Filters[i] = rf - } - if rl.ActivationTime, err = utils.ParseTimeDetectLayout(tpRL.ActivationTime, timezone); err != nil { - return nil, err - } - if rl.Limit, err = strconv.ParseFloat(tpRL.Limit, 64); err != nil { - return nil, err - } - return rl, nil -} diff --git a/engine/model_converters_test.go b/engine/model_converters_test.go deleted file mode 100644 index 0ada2be4a..000000000 --- a/engine/model_converters_test.go +++ /dev/null @@ -1,62 +0,0 @@ -// +build integration - -/* -Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments -Copyright (C) ITsysCOM GmbH - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see -*/ -package engine - -import ( - "reflect" - "testing" - - "github.com/cgrates/cgrates/utils" -) - -func TestAPItoResourceLimit(t *testing.T) { - - tpRL := &utils.TPResourceLimit{ - TPid: testTPID, - ID: "ResGroup1", - Filters: []*utils.TPRequestFilter{ - &utils.TPRequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"1001", "1002"}}, - &utils.TPRequestFilter{Type: MetaStringPrefix, FieldName: "Destination", Values: []string{"10", "20"}}, - &utils.TPRequestFilter{Type: MetaCDRStats, Values: []string{"CDRST1:*min_ASR:34", "CDRST_1001:*min_ASR:20"}}, - &utils.TPRequestFilter{Type: MetaRSRFields, Values: []string{"Subject(~^1.*1$)", "Destination(1002)"}}, - }, - ActivationTime: "2014-07-29T15:00:00Z", - Weight: 10, - Limit: "2", - } - eRL := &ResourceLimit{ID: tpRL.ID, Weight: tpRL.Weight, Filters: make([]*RequestFilter, len(tpRL.Filters)), Usage: make(map[string]*ResourceUsage)} - eRL.Filters[0] = &RequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"1001", "1002"}} - eRL.Filters[1] = &RequestFilter{Type: MetaStringPrefix, FieldName: "Destination", Values: []string{"10", "20"}} - eRL.Filters[2] = &RequestFilter{Type: MetaCDRStats, Values: []string{"CDRST1:*min_ASR:34", "CDRST_1001:*min_ASR:20"}, - cdrStatSThresholds: []*RFStatSThreshold{ - &RFStatSThreshold{QueueID: "CDRST1", ThresholdType: "*MIN_ASR", ThresholdValue: 34}, - &RFStatSThreshold{QueueID: "CDRST_1001", ThresholdType: "*MIN_ASR", ThresholdValue: 20}, - }} - eRL.Filters[3] = &RequestFilter{Type: MetaRSRFields, Values: []string{"Subject(~^1.*1$)", "Destination(1002)"}, - rsrFields: utils.ParseRSRFieldsMustCompile("Subject(~^1.*1$);Destination(1002)", utils.INFIELD_SEP), - } - eRL.ActivationTime, _ = utils.ParseTimeDetectLayout("2014-07-29T15:00:00Z", "UTC") - eRL.Limit = 2 - if rl, err := APItoResourceLimit(tpRL, "UTC"); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(eRL, rl) { - t.Errorf("Expecting: %+v, received: %+v", eRL, rl) - } -} diff --git a/engine/model_helpers.go b/engine/model_helpers.go index 172589bcf..d655b43e1 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -216,6 +216,25 @@ func (tps TpDestinations) AsTPDestinations() (result []*utils.TPDestination) { return } +func APItoModelDestination(d *utils.TPDestination) (result TpDestinations) { + if d != nil { + for _, p := range d.Prefixes { + result = append(result, TpDestination{ + Tpid: d.TPid, + Tag: d.ID, + Prefix: p, + }) + } + if len(d.Prefixes) == 0 { + result = append(result, TpDestination{ + Tpid: d.TPid, + Tag: d.ID, + }) + } + } + return +} + type TpTimings []TpTiming func (tps TpTimings) GetTimings() (map[string]*utils.TPTiming, error) { @@ -265,6 +284,28 @@ func (tps TpTimings) AsTPTimings() (result []*utils.ApierTPTiming) { return result } +func APItoModelTiming(t *utils.ApierTPTiming) (result TpTiming) { + return TpTiming{ + Tpid: t.TPid, + Tag: t.ID, + Years: t.Years, + Months: t.Months, + MonthDays: t.MonthDays, + WeekDays: t.WeekDays, + Time: t.Time, + } +} + +func APItoModelTimings(ts []*utils.ApierTPTiming) (result TpTimings) { + for _, t := range ts { + if t != nil { + at := APItoModelTiming(t) + result = append(result, at) + } + } + return result +} + type TpRates []TpRate func (tps TpRates) GetRates() (map[string]*utils.TPRate, error) { @@ -325,6 +366,38 @@ func (tps TpRates) AsTPRates() (result []*utils.TPRate, err error) { } } +func APItoModelRate(r *utils.TPRate) (result TpRates) { + if r != nil { + for _, rs := range r.RateSlots { + result = append(result, TpRate{ + Tpid: r.TPid, + Tag: r.ID, + ConnectFee: rs.ConnectFee, + Rate: rs.Rate, + RateUnit: rs.RateUnit, + RateIncrement: rs.RateIncrement, + GroupIntervalStart: rs.GroupIntervalStart, + }) + } + if len(r.RateSlots) == 0 { + result = append(result, TpRate{ + Tpid: r.TPid, + Tag: r.ID, + }) + } + } + return +} + +func APItoModelRates(rs []*utils.TPRate) (result TpRates) { + for _, r := range rs { + for _, sr := range APItoModelRate(r) { + result = append(result, sr) + } + } + return result +} + type TpDestinationRates []TpDestinationRate func (tps TpDestinationRates) GetDestinationRates() (map[string]*utils.TPDestinationRate, error) { @@ -366,6 +439,41 @@ func (tps TpDestinationRates) AsTPDestinationRates() (result []*utils.TPDestinat } } +func APItoModelDestinationRate(d *utils.TPDestinationRate) (result TpDestinationRates) { + if d != nil { + for _, dr := range d.DestinationRates { + result = append(result, TpDestinationRate{ + Tpid: d.TPid, + Tag: d.ID, + DestinationsTag: dr.DestinationId, + RatesTag: dr.RateId, + RoundingMethod: dr.RoundingMethod, + RoundingDecimals: dr.RoundingDecimals, + MaxCost: dr.MaxCost, + MaxCostStrategy: dr.MaxCostStrategy, + }) + } + if len(d.DestinationRates) == 0 { + result = append(result, TpDestinationRate{ + Tpid: d.TPid, + Tag: d.ID, + }) + } + } + return +} + +func APItoModelDestinationRates(drs []*utils.TPDestinationRate) (result TpDestinationRates) { + if drs != nil { + for _, dr := range drs { + for _, sdr := range APItoModelDestinationRate(dr) { + result = append(result, sdr) + } + } + } + return result +} + type TpRatingPlans []TpRatingPlan func (tps TpRatingPlans) GetRatingPlanBindings() (map[string][]*utils.TPRatingPlanBinding, error) { @@ -449,6 +557,36 @@ func GetRateInterval(rpl *utils.TPRatingPlanBinding, dr *utils.DestinationRate) return } +func APItoModelRatingPlan(rp *utils.TPRatingPlan) (result TpRatingPlans) { + if rp != nil { + for _, rpb := range rp.RatingPlanBindings { + result = append(result, TpRatingPlan{ + Tpid: rp.TPid, + Tag: rp.ID, + DestratesTag: rpb.DestinationRatesId, + TimingTag: rpb.TimingId, + Weight: rpb.Weight, + }) + } + if len(rp.RatingPlanBindings) == 0 { + result = append(result, TpRatingPlan{ + Tpid: rp.TPid, + Tag: rp.ID, + }) + } + } + return +} + +func APItoModelRatingPlans(rps []*utils.TPRatingPlan) (result TpRatingPlans) { + for _, rp := range rps { + for _, srp := range APItoModelRatingPlan(rp) { + result = append(result, srp) + } + } + return result +} + type TpRatingProfiles []TpRatingProfile func (tps TpRatingProfiles) GetRatingProfiles() (map[string]*utils.TPRatingProfile, error) { @@ -489,6 +627,45 @@ func (tps TpRatingProfiles) AsTPRatingProfiles() (result []*utils.TPRatingProfil } } +func APItoModelRatingProfile(rp *utils.TPRatingProfile) (result TpRatingProfiles) { + if rp != nil { + for _, rpa := range rp.RatingPlanActivations { + result = append(result, TpRatingProfile{ + Tpid: rp.TPid, + Loadid: rp.LoadId, + Direction: rp.Direction, + Tenant: rp.Tenant, + Category: rp.Category, + Subject: rp.Subject, + ActivationTime: rpa.ActivationTime, + RatingPlanTag: rpa.RatingPlanId, + FallbackSubjects: rpa.FallbackSubjects, + CdrStatQueueIds: rpa.CdrStatQueueIds, + }) + } + if len(rp.RatingPlanActivations) == 0 { + result = append(result, TpRatingProfile{ + Tpid: rp.TPid, + Loadid: rp.LoadId, + Direction: rp.Direction, + Tenant: rp.Tenant, + Category: rp.Category, + Subject: rp.Subject, + }) + } + } + return +} + +func APItoModelRatingProfiles(rps []*utils.TPRatingProfile) (result TpRatingProfiles) { + for _, rp := range rps { + for _, srp := range APItoModelRatingProfile(rp) { + result = append(result, srp) + } + } + return result +} + type TpSharedGroups []TpSharedGroup func (tps TpSharedGroups) GetSharedGroups() (map[string][]*utils.TPSharedGroup, error) { @@ -536,6 +713,36 @@ func (tps TpSharedGroups) AsTPSharedGroups() (result []*utils.TPSharedGroups, er } } +func APItoModelSharedGroup(sgs *utils.TPSharedGroups) (result TpSharedGroups) { + if sgs != nil { + for _, sg := range sgs.SharedGroups { + result = append(result, TpSharedGroup{ + Tpid: sgs.TPid, + Tag: sgs.ID, + Account: sg.Account, + Strategy: sg.Strategy, + RatingSubject: sg.RatingSubject, + }) + } + if len(sgs.SharedGroups) == 0 { + result = append(result, TpSharedGroup{ + Tpid: sgs.TPid, + Tag: sgs.ID, + }) + } + } + return +} + +func APItoModelSharedGroups(sgs []*utils.TPSharedGroups) (result TpSharedGroups) { + for _, sg := range sgs { + for _, ssg := range APItoModelSharedGroup(sg) { + result = append(result, ssg) + } + } + return result +} + type TpActions []TpAction func (tps TpActions) GetActions() (map[string][]*utils.TPAction, error) { @@ -612,6 +819,50 @@ func (tps TpActions) AsTPActions() (result []*utils.TPActions, err error) { } } +func APItoModelAction(as *utils.TPActions) (result TpActions) { + if as != nil { + for _, a := range as.Actions { + result = append(result, TpAction{ + Tpid: as.TPid, + Tag: as.ID, + Action: a.Identifier, + BalanceTag: a.BalanceId, + BalanceType: a.BalanceType, + Directions: a.Directions, + Units: a.Units, + ExpiryTime: a.ExpiryTime, + Filter: a.Filter, + TimingTags: a.TimingTags, + DestinationTags: a.DestinationIds, + RatingSubject: a.RatingSubject, + Categories: a.Categories, + SharedGroups: a.SharedGroups, + BalanceWeight: a.BalanceWeight, + BalanceBlocker: a.BalanceBlocker, + BalanceDisabled: a.BalanceDisabled, + ExtraParameters: a.ExtraParameters, + Weight: a.Weight, + }) + } + if len(as.Actions) == 0 { + result = append(result, TpAction{ + Tpid: as.TPid, + Tag: as.ID, + }) + } + } + return +} + +func APItoModelActions(as []*utils.TPActions) (result TpActions) { + for _, a := range as { + for _, sa := range APItoModelAction(a) { + result = append(result, sa) + } + } + return result +} + type TpActionPlans []TpActionPlan func (tps TpActionPlans) GetActionPlans() (map[string][]*utils.TPActionTiming, error) { @@ -655,6 +906,36 @@ func (tps TpActionPlans) AsTPActionPlans() (result []*utils.TPActionPlan, err er } } +func APItoModelActionPlan(a *utils.TPActionPlan) (result TpActionPlans) { + if a != nil { + for _, ap := range a.ActionPlan { + result = append(result, TpActionPlan{ + Tpid: a.TPid, + Tag: a.ID, + ActionsTag: ap.ActionsId, + TimingTag: ap.TimingId, + Weight: ap.Weight, + }) + } + if len(a.ActionPlan) == 0 { + result = append(result, TpActionPlan{ + Tpid: a.TPid, + Tag: a.ID, + }) + } + } + return +} + +func APItoModelActionPlans(aps []*utils.TPActionPlan) (result TpActionPlans) { + for _, ap := range aps { + for _, sap := range APItoModelActionPlan(ap) { + result = append(result, sap) + } + } + return result +} + type TpActionTriggers []TpActionTrigger func (tps TpActionTriggers) GetActionTriggers() (map[string][]*utils.TPActionTrigger, error) { @@ -743,6 +1024,55 @@ func (tps TpActionTriggers) AsTPActionTriggers() (result []*utils.TPActionTrigge } } +func APItoModelActionTrigger(ats *utils.TPActionTriggers) (result TpActionTriggers) { + if ats != nil { + for _, at := range ats.ActionTriggers { + result = append(result, TpActionTrigger{ + Tpid: ats.TPid, + Tag: ats.ID, + UniqueId: at.UniqueID, + ThresholdType: at.ThresholdType, + ThresholdValue: at.ThresholdValue, + Recurrent: at.Recurrent, + MinSleep: at.MinSleep, + ExpiryTime: at.ExpirationDate, + ActivationTime: at.ActivationDate, + BalanceTag: at.BalanceId, + BalanceType: at.BalanceType, + BalanceDirections: at.BalanceDirections, + BalanceDestinationTags: at.BalanceDestinationIds, + BalanceWeight: at.BalanceWeight, + BalanceExpiryTime: at.BalanceExpirationDate, + BalanceTimingTags: at.BalanceTimingTags, + BalanceRatingSubject: at.BalanceRatingSubject, + BalanceCategories: at.BalanceCategories, + BalanceSharedGroups: at.BalanceSharedGroups, + BalanceBlocker: at.BalanceBlocker, + BalanceDisabled: at.BalanceDisabled, + MinQueuedItems: at.MinQueuedItems, + ActionsTag: at.ActionsId, + Weight: at.Weight, + }) + } + if len(ats.ActionTriggers) == 0 { + result = append(result, TpActionTrigger{ + Tpid: ats.TPid, + Tag: ats.ID, + }) + } + } + return +} + +func APItoModelActionTriggers(ts []*utils.TPActionTriggers) (result TpActionTriggers) { + for _, t := range ts { + for _, st := range APItoModelActionTrigger(t) { + result = append(result, st) + } + } + return result +} + type TpAccountActions []TpAccountAction func (tps TpAccountActions) GetAccountActions() (map[string]*utils.TPAccountActions, error) { @@ -774,6 +1104,28 @@ func (tps TpAccountActions) AsTPAccountActions() (result []*utils.TPAccountActio } } +func APItoModelAccountAction(aa *utils.TPAccountActions) *TpAccountAction { + return &TpAccountAction{ + Tpid: aa.TPid, + Loadid: aa.LoadId, + Tenant: aa.Tenant, + Account: aa.Account, + ActionPlanTag: aa.ActionPlanId, + ActionTriggersTag: aa.ActionTriggersId, + AllowNegative: aa.AllowNegative, + Disabled: aa.Disabled, + } +} + +func APItoModelAccountActions(aas []*utils.TPAccountActions) (result TpAccountActions) { + for _, aa := range aas { + if aa != nil { + result = append(result, *APItoModelAccountAction(aa)) + } + } + return result +} + type TpDerivedChargers []TpDerivedCharger func (tps TpDerivedChargers) GetDerivedChargers() (map[string]*utils.TPDerivedChargers, error) { @@ -828,6 +1180,60 @@ func (tps TpDerivedChargers) AsTPDerivedChargers() (result []*utils.TPDerivedCha } } +func APItoModelDerivedCharger(dcs *utils.TPDerivedChargers) (result TpDerivedChargers) { + if dcs != nil { + for _, dc := range dcs.DerivedChargers { + result = append(result, TpDerivedCharger{ + Tpid: dcs.TPid, + Loadid: dcs.LoadId, + Direction: dcs.Direction, + Tenant: dcs.Tenant, + Category: dcs.Category, + Account: dcs.Account, + Subject: dcs.Subject, + Runid: dc.RunId, + RunFilters: dc.RunFilters, + ReqTypeField: dc.ReqTypeField, + DirectionField: dc.DirectionField, + TenantField: dc.TenantField, + CategoryField: dc.CategoryField, + AccountField: dc.AccountField, + SubjectField: dc.SubjectField, + PddField: dc.PddField, + DestinationField: dc.DestinationField, + SetupTimeField: dc.SetupTimeField, + AnswerTimeField: dc.AnswerTimeField, + UsageField: dc.UsageField, + SupplierField: dc.SupplierField, + DisconnectCauseField: dc.DisconnectCauseField, + CostField: dc.CostField, + RatedField: dc.RatedField, + }) + } + if len(dcs.DerivedChargers) == 0 { + result = append(result, TpDerivedCharger{ + Tpid: dcs.TPid, + Loadid: dcs.LoadId, + Direction: dcs.Direction, + Tenant: dcs.Tenant, + Category: dcs.Category, + Account: dcs.Account, + Subject: dcs.Subject, + }) + } + } + return +} + +func APItoModelDerivedChargers(dcs []*utils.TPDerivedChargers) (result TpDerivedChargers) { + for _, dc := range dcs { + for _, sdc := range APItoModelDerivedCharger(dc) { + result = append(result, sdc) + } + } + return result +} + type TpCdrStats []TpCdrstat func (tps TpCdrStats) GetCdrStats() (map[string][]*utils.TPCdrStat, error) { @@ -917,6 +1323,58 @@ func (tps TpCdrStats) AsTPCdrStats() (result []*utils.TPCdrStats, err error) { } } +func APItoModelCdrStat(css *utils.TPCdrStats) (result TpCdrStats) { + if css != nil { + for _, cs := range css.CdrStats { + ql, _ := strconv.Atoi(cs.QueueLength) + result = append(result, TpCdrstat{ + Tpid: css.TPid, + Tag: css.ID, + QueueLength: ql, + TimeWindow: cs.TimeWindow, + SaveInterval: cs.SaveInterval, + Metrics: cs.Metrics, + SetupInterval: cs.SetupInterval, + Tors: cs.TORs, + CdrHosts: cs.CdrHosts, + CdrSources: cs.CdrSources, + ReqTypes: cs.ReqTypes, + Directions: cs.Directions, + Tenants: cs.Tenants, + Categories: cs.Categories, + Accounts: cs.Accounts, + Subjects: cs.Subjects, + DestinationIds: cs.DestinationIds, + PddInterval: cs.PddInterval, + UsageInterval: cs.UsageInterval, + Suppliers: cs.Suppliers, + DisconnectCauses: cs.DisconnectCauses, + MediationRunids: cs.MediationRunIds, + RatedAccounts: cs.RatedAccounts, + RatedSubjects: cs.RatedSubjects, + CostInterval: cs.CostInterval, + ActionTriggers: cs.ActionTriggers, + }) + } + if len(css.CdrStats) == 0 { + result = append(result, TpCdrstat{ + Tpid: css.TPid, + Tag: css.ID, + }) + } + } + return +} + +func APItoModelCdrStats(css []*utils.TPCdrStats) (result TpCdrStats) { + for _, cs := range css { + for _, scs := range APItoModelCdrStat(cs) { + result = append(result, scs) + } + } + return result +} + func UpdateCdrStats(cs *CdrStats, triggers ActionTriggers, tpCs *utils.TPCdrStat, timezone string) { if tpCs.QueueLength != "" && tpCs.QueueLength != "0" { if qi, err := strconv.Atoi(tpCs.QueueLength); err == nil { @@ -1162,6 +1620,41 @@ func (tps TpUsers) AsTPUsers() (result []*utils.TPUsers, err error) { } } +func APItoModelUsers(us *utils.TPUsers) (result TpUsers) { + if us != nil { + for _, p := range us.Profile { + result = append(result, TpUser{ + Tpid: us.TPid, + Tenant: us.Tenant, + UserName: us.UserName, + Masked: us.Masked, + Weight: us.Weight, + AttributeName: p.AttrName, + AttributeValue: p.AttrValue, + }) + } + if len(us.Profile) == 0 { + result = append(result, TpUser{ + Tpid: us.TPid, + Tenant: us.Tenant, + UserName: us.UserName, + Masked: us.Masked, + Weight: us.Weight, + }) + } + } + return +} + +func APItoModelUsersA(ts []*utils.TPUsers) (result TpUsers) { + for _, t := range ts { + for _, st := range APItoModelUsers(t) { + result = append(result, st) + } + } + return result +} + type TpAliases []TpAlias func (tps TpAliases) GetAliases() (map[string]*utils.TPAliases, error) { @@ -1202,6 +1695,48 @@ func (tps TpAliases) AsTPAliases() (result []*utils.TPAliases, err error) { } } +func APItoModelAliases(as *utils.TPAliases) (result TpAliases) { + if as != nil { + for _, v := range as.Values { + result = append(result, TpAlias{ + Tpid: as.TPid, + Direction: as.Direction, + Tenant: as.Tenant, + Category: as.Category, + Account: as.Account, + Subject: as.Subject, + Context: as.Context, + DestinationId: v.DestinationId, + Target: v.Target, + Original: v.Original, + Alias: v.Alias, + Weight: v.Weight, + }) + } + if len(as.Values) == 0 { + result = append(result, TpAlias{ + Tpid: as.TPid, + Direction: as.Direction, + Tenant: as.Tenant, + Category: as.Category, + Account: as.Account, + Subject: as.Subject, + Context: as.Context, + }) + } + } + return +} + +func APItoModelAliasesA(as []*utils.TPAliases) (result TpAliases) { + for _, a := range as { + for _, sa := range APItoModelAliases(a) { + result = append(result, sa) + } + } + return result +} + type TpLcrRules []TpLcrRule func (tps TpLcrRules) GetLcrRules() (map[string]*utils.TPLcrRules, error) { @@ -1270,6 +1805,47 @@ func (tps TpLcrRules) AsTPLcrRules() (result []*utils.TPLcrRules, err error) { } } +func APItoModelLcrRule(lrs *utils.TPLcrRules) (result TpLcrRules) { + if lrs != nil { + for _, r := range lrs.Rules { + result = append(result, TpLcrRule{ + Tpid: lrs.TPid, + Direction: lrs.Direction, + Tenant: lrs.Tenant, + Category: lrs.Category, + Account: lrs.Account, + Subject: lrs.Subject, + DestinationTag: r.DestinationId, + RpCategory: r.RpCategory, + Strategy: r.Strategy, + StrategyParams: r.StrategyParams, + ActivationTime: r.ActivationTime, + Weight: r.Weight, + }) + } + if len(lrs.Rules) == 0 { + result = append(result, TpLcrRule{ + Tpid: lrs.TPid, + Direction: lrs.Direction, + Tenant: lrs.Tenant, + Category: lrs.Category, + Account: lrs.Account, + Subject: lrs.Subject, + }) + } + } + return +} + +func APItoModelLcrRules(ts []*utils.TPLcrRules) (result TpLcrRules) { + for _, t := range ts { + for _, st := range APItoModelLcrRule(t) { + result = append(result, st) + } + } + return result +} + type TpResourceLimits []*TpResourceLimit func (tps TpResourceLimits) AsTPResourceLimits() (result []*utils.TPResourceLimit) { @@ -1304,3 +1880,69 @@ func (tps TpResourceLimits) AsTPResourceLimits() (result []*utils.TPResourceLimi } return } + +func APItoModelResourceLimit(rl *utils.TPResourceLimit) TpResourceLimits { + result := TpResourceLimits{} + for _, f := range rl.Filters { + tp := &TpResourceLimit{ + Tpid: rl.TPid, + Tag: rl.ID, + ActivationTime: rl.ActivationTime, + Weight: rl.Weight, + Limit: rl.Limit, + } + for i, atid := range rl.ActionTriggerIDs { + if i != 0 { + tp.ActionTriggerIds = tp.ActionTriggerIds + utils.INFIELD_SEP + atid + } else { + tp.ActionTriggerIds = atid + } + } + tp.FilterType = f.Type + tp.FilterFieldName = f.FieldName + for i, val := range f.Values { + if i != 0 { + tp.FilterFieldValues = tp.FilterFieldValues + utils.INFIELD_SEP + val + } else { + tp.FilterFieldValues = val + } + } + result = append(result, tp) + } + if len(rl.Filters) == 0 { + tp := &TpResourceLimit{ + Tpid: rl.TPid, + Tag: rl.ID, + ActivationTime: rl.ActivationTime, + Weight: rl.Weight, + Limit: rl.Limit, + } + for i, atid := range rl.ActionTriggerIDs { + if i != 0 { + tp.ActionTriggerIds = tp.ActionTriggerIds + utils.INFIELD_SEP + atid + } else { + tp.ActionTriggerIds = atid + } + } + result = append(result, tp) + } + return result +} + +func APItoResourceLimit(tpRL *utils.TPResourceLimit, timezone string) (rl *ResourceLimit, err error) { + rl = &ResourceLimit{ID: tpRL.ID, Weight: tpRL.Weight, Filters: make([]*RequestFilter, len(tpRL.Filters)), Usage: make(map[string]*ResourceUsage)} + for i, f := range tpRL.Filters { + rf := &RequestFilter{Type: f.Type, FieldName: f.FieldName, Values: f.Values} + if err := rf.CompileValues(); err != nil { + return nil, err + } + rl.Filters[i] = rf + } + if rl.ActivationTime, err = utils.ParseTimeDetectLayout(tpRL.ActivationTime, timezone); err != nil { + return nil, err + } + if rl.Limit, err = strconv.ParseFloat(tpRL.Limit, 64); err != nil { + return nil, err + } + return rl, nil +} diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 8d2ba91f8..3c925cd6b 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -737,3 +737,37 @@ func TestTpResourceLimitsAsTPResourceLimits(t *testing.T) { t.Errorf("\nExpecting:\n%+v\nReceived:\n%+v", utils.ToIJSON(eTPs), utils.ToIJSON(rcvTPs)) } } + +func TestAPItoResourceLimit(t *testing.T) { + tpRL := &utils.TPResourceLimit{ + TPid: testTPID, + ID: "ResGroup1", + Filters: []*utils.TPRequestFilter{ + &utils.TPRequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"1001", "1002"}}, + &utils.TPRequestFilter{Type: MetaStringPrefix, FieldName: "Destination", Values: []string{"10", "20"}}, + &utils.TPRequestFilter{Type: MetaCDRStats, Values: []string{"CDRST1:*min_ASR:34", "CDRST_1001:*min_ASR:20"}}, + &utils.TPRequestFilter{Type: MetaRSRFields, Values: []string{"Subject(~^1.*1$)", "Destination(1002)"}}, + }, + ActivationTime: "2014-07-29T15:00:00Z", + Weight: 10, + Limit: "2", + } + eRL := &ResourceLimit{ID: tpRL.ID, Weight: tpRL.Weight, Filters: make([]*RequestFilter, len(tpRL.Filters)), Usage: make(map[string]*ResourceUsage)} + eRL.Filters[0] = &RequestFilter{Type: MetaString, FieldName: "Account", Values: []string{"1001", "1002"}} + eRL.Filters[1] = &RequestFilter{Type: MetaStringPrefix, FieldName: "Destination", Values: []string{"10", "20"}} + eRL.Filters[2] = &RequestFilter{Type: MetaCDRStats, Values: []string{"CDRST1:*min_ASR:34", "CDRST_1001:*min_ASR:20"}, + cdrStatSThresholds: []*RFStatSThreshold{ + &RFStatSThreshold{QueueID: "CDRST1", ThresholdType: "*MIN_ASR", ThresholdValue: 34}, + &RFStatSThreshold{QueueID: "CDRST_1001", ThresholdType: "*MIN_ASR", ThresholdValue: 20}, + }} + eRL.Filters[3] = &RequestFilter{Type: MetaRSRFields, Values: []string{"Subject(~^1.*1$)", "Destination(1002)"}, + rsrFields: utils.ParseRSRFieldsMustCompile("Subject(~^1.*1$);Destination(1002)", utils.INFIELD_SEP), + } + eRL.ActivationTime, _ = utils.ParseTimeDetectLayout("2014-07-29T15:00:00Z", "UTC") + eRL.Limit = 2 + if rl, err := APItoResourceLimit(tpRL, "UTC"); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eRL, rl) { + t.Errorf("Expecting: %+v, received: %+v", eRL, rl) + } +}