From fd5b8774ab96553b5d5263b378c5015c8846d722 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 19 May 2021 10:49:44 +0300 Subject: [PATCH] Added all Responder Methods to DispatacherS.Fixes #2954 --- apier/v1/api_interfaces.go | 2 ++ apier/v1/dispatcher.go | 7 ++++++ dispatchers/responder.go | 37 ++++++++++++++++++++++++++++++++ engine/libsuppliers.go | 6 +++--- engine/spls_highestcost.go | 4 ++-- engine/spls_leastcost.go | 4 ++-- engine/spls_load_distribution.go | 4 ++-- engine/spls_qos.go | 4 ++-- engine/spls_reas.go | 4 ++-- engine/spls_reds.go | 4 ++-- engine/spls_weight.go | 4 ++-- engine/suppliers.go | 22 ++++++++++--------- utils/apitpdata.go | 2 ++ 13 files changed, 77 insertions(+), 27 deletions(-) diff --git a/apier/v1/api_interfaces.go b/apier/v1/api_interfaces.go index b0e78ed42..8288dc50f 100644 --- a/apier/v1/api_interfaces.go +++ b/apier/v1/api_interfaces.go @@ -101,6 +101,8 @@ type ResponderInterface interface { RefundRounding(arg *engine.CallDescriptorWithArgDispatcher, reply *float64) (err error) GetMaxSessionTime(arg *engine.CallDescriptorWithArgDispatcher, reply *time.Duration) (err error) Shutdown(arg *utils.TenantWithArgDispatcher, reply *string) (err error) + GetCostOnRatingPlans(arg *utils.GetCostOnRatingPlansArgs, reply *map[string]interface{}) (err error) + GetMaxSessionTimeOnAccounts(arg *utils.GetMaxSessionTimeOnAccountsArgs, reply *map[string]interface{}) (err error) Ping(ign *utils.CGREventWithArgDispatcher, reply *string) error } diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index c6b042dbc..f573aac89 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -535,6 +535,13 @@ func (dS *DispatcherResponder) Shutdown(args *utils.TenantWithArgDispatcher, rep return dS.dS.ResponderShutdown(args, reply) } +func (dS *DispatcherResponder) GetCostOnRatingPlans(arg *utils.GetCostOnRatingPlansArgs, reply *map[string]interface{}) (err error) { + return dS.dS.ResponderGetCostOnRatingPlans(arg, reply) +} +func (dS *DispatcherResponder) GetMaxSessionTimeOnAccounts(arg *utils.GetMaxSessionTimeOnAccountsArgs, reply *map[string]interface{}) (err error) { + return dS.dS.ResponderGetMaxSessionTimeOnAccounts(arg, reply) +} + // Ping used to determinate if component is active func (dS *DispatcherResponder) Ping(args *utils.CGREventWithArgDispatcher, reply *string) error { return dS.dS.ResponderPing(args, reply) diff --git a/dispatchers/responder.go b/dispatchers/responder.go index d20ddf976..519ecbfbf 100644 --- a/dispatchers/responder.go +++ b/dispatchers/responder.go @@ -186,3 +186,40 @@ func (dS *DispatcherService) ResponderShutdown(args *utils.TenantWithArgDispatch return dS.Dispatch(&utils.CGREvent{Tenant: tnt}, utils.MetaResponder, routeID, utils.ResponderShutdown, args, reply) } + +func (dS *DispatcherService) ResponderGetCostOnRatingPlans(arg *utils.GetCostOnRatingPlansArgs, reply *map[string]interface{}) (err error) { + tnt := utils.FirstNonEmpty(arg.Tenant, dS.cfg.GeneralCfg().DefaultTenant) + if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { + if arg.ArgDispatcher == nil { + return utils.NewErrMandatoryIeMissing(utils.ArgDispatcherField) + } + if err = dS.authorize(utils.ResponderShutdown, tnt, + arg.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + var routeID *string + if arg.ArgDispatcher != nil { + routeID = arg.ArgDispatcher.RouteID + } + return dS.Dispatch(&utils.CGREvent{Tenant: tnt}, utils.MetaResponder, + routeID, utils.ResponderShutdown, arg, reply) +} +func (dS *DispatcherService) ResponderGetMaxSessionTimeOnAccounts(args *utils.GetMaxSessionTimeOnAccountsArgs, reply *map[string]interface{}) (err error) { + tnt := utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant) + if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { + if args.ArgDispatcher == nil { + return utils.NewErrMandatoryIeMissing(utils.ArgDispatcherField) + } + if err = dS.authorize(utils.ResponderShutdown, tnt, + args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + var routeID *string + if args.ArgDispatcher != nil { + routeID = args.ArgDispatcher.RouteID + } + return dS.Dispatch(&utils.CGREvent{Tenant: tnt}, utils.MetaResponder, + routeID, utils.ResponderShutdown, args, reply) +} diff --git a/engine/libsuppliers.go b/engine/libsuppliers.go index 99c8ae73d..9c1f0f32f 100644 --- a/engine/libsuppliers.go +++ b/engine/libsuppliers.go @@ -194,7 +194,7 @@ type SupplierWithParams struct { // SuppliersSorter is the interface which needs to be implemented by supplier sorters type SuppliersSorter interface { - SortSuppliers(string, []*Supplier, *utils.CGREvent, *optsGetSuppliers) (*SortedSuppliers, error) + SortSuppliers(string, []*Supplier, *utils.CGREvent, *optsGetSuppliers, *utils.ArgDispatcher) (*SortedSuppliers, error) } // NewSupplierSortDispatcher constructs SupplierSortDispatcher @@ -215,10 +215,10 @@ func NewSupplierSortDispatcher(lcrS *SupplierService) (ssd SupplierSortDispatche type SupplierSortDispatcher map[string]SuppliersSorter func (ssd SupplierSortDispatcher) SortSuppliers(prflID, strategy string, - suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sd, has := ssd[strategy] if !has { return nil, fmt.Errorf("unsupported sorting strategy: %s", strategy) } - return sd.SortSuppliers(prflID, suppls, suplEv, extraOpts) + return sd.SortSuppliers(prflID, suppls, suplEv, extraOpts, argDsp) } diff --git a/engine/spls_highestcost.go b/engine/spls_highestcost.go index deeb68b12..ce16a03f3 100755 --- a/engine/spls_highestcost.go +++ b/engine/spls_highestcost.go @@ -36,7 +36,7 @@ type HightCostSorter struct { } func (hcs *HightCostSorter) SortSuppliers(prflID string, suppls []*Supplier, - ev *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + ev *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: hcs.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} @@ -47,7 +47,7 @@ func (hcs *HightCostSorter) SortSuppliers(prflID string, suppls []*Supplier, utils.SupplierS, s.ID)) return nil, utils.NewErrMandatoryIeMissing("RatingPlanIDs") } - if srtSpl, pass, err := hcs.spS.populateSortingData(ev, s, extraOpts); err != nil { + if srtSpl, pass, err := hcs.spS.populateSortingData(ev, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, srtSpl) diff --git a/engine/spls_leastcost.go b/engine/spls_leastcost.go index 9a50a7138..9d1ce1f25 100644 --- a/engine/spls_leastcost.go +++ b/engine/spls_leastcost.go @@ -36,7 +36,7 @@ type LeastCostSorter struct { } func (lcs *LeastCostSorter) SortSuppliers(prflID string, suppls []*Supplier, - ev *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + ev *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: lcs.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} @@ -47,7 +47,7 @@ func (lcs *LeastCostSorter) SortSuppliers(prflID string, suppls []*Supplier, utils.SupplierS, s.ID)) return nil, utils.NewErrMandatoryIeMissing("RatingPlanIDs") } - if srtSpl, pass, err := lcs.spS.populateSortingData(ev, s, extraOpts); err != nil { + if srtSpl, pass, err := lcs.spS.populateSortingData(ev, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, srtSpl) diff --git a/engine/spls_load_distribution.go b/engine/spls_load_distribution.go index 21531b124..a00a15a19 100644 --- a/engine/spls_load_distribution.go +++ b/engine/spls_load_distribution.go @@ -36,7 +36,7 @@ type LoadDistributionSorter struct { } func (ws *LoadDistributionSorter) SortSuppliers(prflID string, - suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: ws.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} @@ -48,7 +48,7 @@ func (ws *LoadDistributionSorter) SortSuppliers(prflID string, utils.SupplierS, s.ID)) return nil, utils.NewErrMandatoryIeMissing("StatIDs") } - if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts); err != nil { + if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { // Add the ratio in SortingData so we can used it later in SortLoadDistribution diff --git a/engine/spls_qos.go b/engine/spls_qos.go index 3c425cb03..31970f9cf 100755 --- a/engine/spls_qos.go +++ b/engine/spls_qos.go @@ -34,12 +34,12 @@ type QOSSupplierSorter struct { } func (qos *QOSSupplierSorter) SortSuppliers(prflID string, suppls []*Supplier, - ev *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + ev *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: qos.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} for _, s := range suppls { - if srtSpl, pass, err := qos.spS.populateSortingData(ev, s, extraOpts); err != nil { + if srtSpl, pass, err := qos.spS.populateSortingData(ev, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, srtSpl) diff --git a/engine/spls_reas.go b/engine/spls_reas.go index a51acfe8d..0034b20b4 100644 --- a/engine/spls_reas.go +++ b/engine/spls_reas.go @@ -36,7 +36,7 @@ type ResourceAscendentSorter struct { } func (ws *ResourceAscendentSorter) SortSuppliers(prflID string, - suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: ws.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} @@ -47,7 +47,7 @@ func (ws *ResourceAscendentSorter) SortSuppliers(prflID string, utils.SupplierS, s.ID)) return nil, utils.NewErrMandatoryIeMissing("ResourceIDs") } - if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts); err != nil { + if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, srtSpl) diff --git a/engine/spls_reds.go b/engine/spls_reds.go index 726df7839..491387bf1 100644 --- a/engine/spls_reds.go +++ b/engine/spls_reds.go @@ -36,7 +36,7 @@ type ResourceDescendentSorter struct { } func (ws *ResourceDescendentSorter) SortSuppliers(prflID string, - suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: ws.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} @@ -47,7 +47,7 @@ func (ws *ResourceDescendentSorter) SortSuppliers(prflID string, utils.SupplierS, s.ID)) return nil, utils.NewErrMandatoryIeMissing("ResourceIDs") } - if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts); err != nil { + if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, srtSpl) diff --git a/engine/spls_weight.go b/engine/spls_weight.go index 5fea0188c..2ea7075df 100755 --- a/engine/spls_weight.go +++ b/engine/spls_weight.go @@ -34,12 +34,12 @@ type WeightSorter struct { } func (ws *WeightSorter) SortSuppliers(prflID string, - suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent, extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: ws.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} for _, s := range suppls { - if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts); err != nil { + if srtSpl, pass, err := ws.spS.populateSortingData(suplEv, s, extraOpts, argDsp); err != nil { return nil, err } else if pass && srtSpl != nil { sortedSuppls.SortedSuppliers = append(sortedSuppls.SortedSuppliers, srtSpl) diff --git a/engine/suppliers.go b/engine/suppliers.go index 3e6c2344f..ef1e4b50e 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -203,7 +203,7 @@ func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent, // costForEvent will compute cost out of accounts and rating plans for event // returns map[string]interface{} with cost and relevant matching information inside func (spS *SupplierService) costForEvent(ev *utils.CGREvent, - acntIDs, rpIDs []string) (costData map[string]interface{}, err error) { + acntIDs, rpIDs []string, argDsp *utils.ArgDispatcher) (costData map[string]interface{}, err error) { costData = make(map[string]interface{}) if err = ev.CheckMandatoryFields([]string{utils.Account, utils.Destination, utils.SetupTime}); err != nil { @@ -241,12 +241,13 @@ func (spS *SupplierService) costForEvent(ev *utils.CGREvent, if len(acntIDs) != 0 { if err := spS.connMgr.Call(spS.cgrcfg.SupplierSCfg().RALsConns, nil, utils.ResponderGetMaxSessionTimeOnAccounts, &utils.GetMaxSessionTimeOnAccountsArgs{ - Tenant: ev.Tenant, - Subject: subj, - Destination: dst, - SetupTime: sTime, - Usage: usage, - AccountIDs: acntIDs, + Tenant: ev.Tenant, + Subject: subj, + Destination: dst, + SetupTime: sTime, + Usage: usage, + AccountIDs: acntIDs, + ArgDispatcher: argDsp, }, &acntCost); err != nil { return nil, err } @@ -281,6 +282,7 @@ func (spS *SupplierService) costForEvent(ev *utils.CGREvent, SetupTime: sTime, Usage: usage, RatingPlanIDs: rpIDs, + ArgDispatcher: argDsp, }, &rpCost); err != nil { return nil, err } @@ -388,7 +390,7 @@ func (spS *SupplierService) resourceUsage(resIDs []string, tenant string) (tUsag } func (spS *SupplierService) populateSortingData(ev *utils.CGREvent, spl *Supplier, - extraOpts *optsGetSuppliers) (srtSpl *SortedSupplier, pass bool, err error) { + extraOpts *optsGetSuppliers, argDsp *utils.ArgDispatcher) (srtSpl *SortedSupplier, pass bool, err error) { sortedSpl := &SortedSupplier{ SupplierID: spl.ID, SortingData: map[string]interface{}{ @@ -398,7 +400,7 @@ func (spS *SupplierService) populateSortingData(ev *utils.CGREvent, spl *Supplie } //calculate costData if we have fields if len(spl.AccountIDs) != 0 || len(spl.RatingPlanIDs) != 0 { - costData, err := spS.costForEvent(ev, spl.AccountIDs, spl.RatingPlanIDs) + costData, err := spS.costForEvent(ev, spl.AccountIDs, spl.RatingPlanIDs, argDsp) if err != nil { if extraOpts.ignoreErrors { utils.Logger.Warning( @@ -520,7 +522,7 @@ func (spS *SupplierService) sortedSuppliersForEvent(args *ArgsGetSuppliers) (sor extraOpts.sortingParameters = splPrfl.SortingParameters // populate sortingParameters in extraOpts extraOpts.sortingStragety = splPrfl.Sorting // populate sortinStrategy in extraOpts sortedSuppliers, err := spS.sorter.SortSuppliers(splPrfl.ID, splPrfl.Sorting, - splPrfl.Suppliers, args.CGREvent, extraOpts) + splPrfl.Suppliers, args.CGREvent, extraOpts, args.ArgDispatcher) if err != nil { return nil, err } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index f001e53e6..b27c515b7 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -1423,6 +1423,7 @@ type GetCostOnRatingPlansArgs struct { SetupTime time.Time Usage time.Duration RatingPlanIDs []string + *ArgDispatcher } type GetMaxSessionTimeOnAccountsArgs struct { @@ -1432,4 +1433,5 @@ type GetMaxSessionTimeOnAccountsArgs struct { SetupTime time.Time Usage time.Duration AccountIDs []string + *ArgDispatcher }