Add Wrapper for CallDescriptor

This commit is contained in:
TeoV
2019-04-08 12:46:58 -04:00
committed by Dan Christian Bogos
parent 22250fe9cc
commit edb2f6ab97
12 changed files with 71 additions and 63 deletions

View File

@@ -27,7 +27,7 @@ import (
)
// Returns MaxUsage (for calls in seconds), -1 for no limit
func (self *ApierV1) GetMaxUsage(usageRecord engine.UsageRecord, maxUsage *int64) error {
func (self *ApierV1) GetMaxUsage(usageRecord engine.UsageRecordWithArgDispatcher, maxUsage *int64) error {
if usageRecord.ToR == "" {
usageRecord.ToR = utils.VOICE
}
@@ -55,7 +55,8 @@ func (self *ApierV1) GetMaxUsage(usageRecord engine.UsageRecord, maxUsage *int64
return utils.NewErrServerError(err)
}
var maxDur time.Duration
if err := self.Responder.GetMaxSessionTime(cd, &maxDur); err != nil {
if err := self.Responder.GetMaxSessionTime(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: usageRecord.ArgDispatcher}, &maxDur); err != nil {
return err
}
if maxDur == time.Duration(-1) {

View File

@@ -32,6 +32,7 @@ type AttrGetCost struct {
AnswerTime string
Destination string
Usage string
*utils.ArgDispatcher
}
func (apier *ApierV1) GetCost(attrs AttrGetCost, ec *engine.EventCost) error {
@@ -55,7 +56,8 @@ func (apier *ApierV1) GetCost(attrs AttrGetCost, ec *engine.EventCost) error {
DurationIndex: usage,
}
var cc engine.CallCost
if err := apier.Responder.GetCost(cd, &cc); err != nil {
if err := apier.Responder.GetCost(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: attrs.ArgDispatcher}, &cc); err != nil {
return utils.NewErrServerError(err)
}
*ec = *engine.NewEventCostFromCallCost(&cc, "", "")
@@ -69,6 +71,7 @@ type AttrGetDataCost struct {
Subject string
AnswerTime string
Usage time.Duration // the call duration so far (till TimeEnd)
*utils.ArgDispatcher
}
func (apier *ApierV1) GetDataCost(attrs AttrGetDataCost, reply *engine.DataCost) error {
@@ -87,7 +90,8 @@ func (apier *ApierV1) GetDataCost(attrs AttrGetDataCost, reply *engine.DataCost)
TOR: utils.DATA,
}
var cc engine.CallCost
if err := apier.Responder.GetCost(cd, &cc); err != nil {
if err := apier.Responder.GetCost(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: attrs.ArgDispatcher}, &cc); err != nil {
return utils.NewErrServerError(err)
}
if dc, err := cc.ToDataCost(); err != nil {

View File

@@ -25,7 +25,7 @@ import (
// DebitUsage will debit the balance for the usage cost, allowing the
// account to go negative if the cost calculated is greater than the balance
func (apier *ApierV1) DebitUsage(usageRecord engine.UsageRecord, reply *string) error {
func (apier *ApierV1) DebitUsage(usageRecord engine.UsageRecordWithArgDispatcher, reply *string) error {
return apier.DebitUsageWithOptions(AttrDebitUsageWithOptions{
UsageRecord: &usageRecord,
AllowNegativeAccount: true,
@@ -34,14 +34,14 @@ func (apier *ApierV1) DebitUsage(usageRecord engine.UsageRecord, reply *string)
// AttrDebitUsageWithOptions represents the DebitUsage request
type AttrDebitUsageWithOptions struct {
UsageRecord *engine.UsageRecord
UsageRecord *engine.UsageRecordWithArgDispatcher
AllowNegativeAccount bool // allow account to go negative during debit
}
// DebitUsageWithOptions will debit the account based on the usage cost with
// additional options to control if the balance can go negative
func (apier *ApierV1) DebitUsageWithOptions(args AttrDebitUsageWithOptions, reply *string) error {
usageRecord := args.UsageRecord
usageRecord := args.UsageRecord.UsageRecord
if missing := utils.MissingStructFields(usageRecord, []string{"Account", "Destination", "Usage"}); len(missing) != 0 {
return utils.NewErrMandatoryIeMissing(missing...)
}
@@ -75,7 +75,8 @@ func (apier *ApierV1) DebitUsageWithOptions(args AttrDebitUsageWithOptions, repl
// Calculate the cost for usage and debit the account
var cc engine.CallCost
if err := apier.Responder.Debit(cd, &cc); err != nil {
if err := apier.Responder.Debit(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: args.UsageRecord.ArgDispatcher}, &cc); err != nil {
return utils.NewErrServerError(err)
}

View File

@@ -130,7 +130,8 @@ func TestDebitUsageWithOptions(t *testing.T) {
}
var reply string
if err := apierDebit.DebitUsageWithOptions(AttrDebitUsageWithOptions{UsageRecord: usageRecord,
if err := apierDebit.DebitUsageWithOptions(AttrDebitUsageWithOptions{
UsageRecord: &engine.UsageRecordWithArgDispatcher{UsageRecord: usageRecord},
AllowNegativeAccount: false}, &reply); err != nil {
t.Error(err)
}

View File

@@ -490,27 +490,27 @@ func (dS *DispatcherResponder) Status(args *utils.TenantWithArgDispatcher, reply
return dS.dS.ResponderStatus(args, reply)
}
func (dS *DispatcherResponder) GetCost(args *engine.CallDescriptor, reply *engine.CallCost) error {
func (dS *DispatcherResponder) GetCost(args *engine.CallDescriptorWithArgDispatcher, reply *engine.CallCost) error {
return dS.dS.ResponderGetCost(args, reply)
}
func (dS *DispatcherResponder) Debit(args *engine.CallDescriptor, reply *engine.CallCost) error {
func (dS *DispatcherResponder) Debit(args *engine.CallDescriptorWithArgDispatcher, reply *engine.CallCost) error {
return dS.dS.ResponderDebit(args, reply)
}
func (dS *DispatcherResponder) MaxDebit(args *engine.CallDescriptor, reply *engine.CallCost) error {
func (dS *DispatcherResponder) MaxDebit(args *engine.CallDescriptorWithArgDispatcher, reply *engine.CallCost) error {
return dS.dS.ResponderMaxDebit(args, reply)
}
func (dS *DispatcherResponder) RefundIncrements(args *engine.CallDescriptor, reply *engine.Account) error {
func (dS *DispatcherResponder) RefundIncrements(args *engine.CallDescriptorWithArgDispatcher, reply *engine.Account) error {
return dS.dS.ResponderRefundIncrements(args, reply)
}
func (dS *DispatcherResponder) RefundRounding(args *engine.CallDescriptor, reply *float64) error {
func (dS *DispatcherResponder) RefundRounding(args *engine.CallDescriptorWithArgDispatcher, reply *float64) error {
return dS.dS.ResponderRefundRounding(args, reply)
}
func (dS *DispatcherResponder) GetMaxSessionTime(args *engine.CallDescriptor, reply *time.Duration) error {
func (dS *DispatcherResponder) GetMaxSessionTime(args *engine.CallDescriptorWithArgDispatcher, reply *time.Duration) error {
return dS.dS.ResponderGetMaxSessionTime(args, reply)
}

View File

@@ -59,7 +59,7 @@ func (dS *DispatcherService) ResponderStatus(args *utils.TenantWithArgDispatcher
"", reply)
}
func (dS *DispatcherService) ResponderGetCost(args *engine.CallDescriptor,
func (dS *DispatcherService) ResponderGetCost(args *engine.CallDescriptorWithArgDispatcher,
reply *engine.CallCost) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")
@@ -74,7 +74,7 @@ func (dS *DispatcherService) ResponderGetCost(args *engine.CallDescriptor,
args.RouteID, utils.ResponderGetCost, args, reply)
}
func (dS *DispatcherService) ResponderDebit(args *engine.CallDescriptor,
func (dS *DispatcherService) ResponderDebit(args *engine.CallDescriptorWithArgDispatcher,
reply *engine.CallCost) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")
@@ -89,7 +89,7 @@ func (dS *DispatcherService) ResponderDebit(args *engine.CallDescriptor,
args.RouteID, utils.ResponderDebit, args, reply)
}
func (dS *DispatcherService) ResponderMaxDebit(args *engine.CallDescriptor,
func (dS *DispatcherService) ResponderMaxDebit(args *engine.CallDescriptorWithArgDispatcher,
reply *engine.CallCost) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")
@@ -104,7 +104,7 @@ func (dS *DispatcherService) ResponderMaxDebit(args *engine.CallDescriptor,
args.RouteID, utils.ResponderMaxDebit, args, reply)
}
func (dS *DispatcherService) ResponderRefundIncrements(args *engine.CallDescriptor,
func (dS *DispatcherService) ResponderRefundIncrements(args *engine.CallDescriptorWithArgDispatcher,
reply *engine.Account) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")
@@ -119,7 +119,7 @@ func (dS *DispatcherService) ResponderRefundIncrements(args *engine.CallDescript
args.RouteID, utils.ResponderRefundIncrements, args, reply)
}
func (dS *DispatcherService) ResponderRefundRounding(args *engine.CallDescriptor,
func (dS *DispatcherService) ResponderRefundRounding(args *engine.CallDescriptorWithArgDispatcher,
reply *float64) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")
@@ -134,7 +134,7 @@ func (dS *DispatcherService) ResponderRefundRounding(args *engine.CallDescriptor
args.RouteID, utils.ResponderRefundRounding, args, reply)
}
func (dS *DispatcherService) ResponderGetMaxSessionTime(args *engine.CallDescriptor,
func (dS *DispatcherService) ResponderGetMaxSessionTime(args *engine.CallDescriptorWithArgDispatcher,
reply *time.Duration) (err error) {
if args.ArgDispatcher == nil {
return utils.NewErrMandatoryIeMissing("ArgDispatcher")

View File

@@ -171,7 +171,6 @@ type CallDescriptor struct {
DenyNegativeAccount bool // prevent account going on negative during debit
account *Account
testCallcost *CallCost // testing purpose only!
*utils.ArgDispatcher
}
// AsCGREvent converts the CallDescriptor into CGREvent
@@ -1067,3 +1066,8 @@ func (cd *CallDescriptor) AsNavigableMap(tpl []*config.FCTemplate) (nM *config.N
func (cd *CallDescriptor) RemoteHost() net.Addr {
return utils.LocalAddr()
}
type CallDescriptorWithArgDispatcher struct {
*CallDescriptor
*utils.ArgDispatcher
}

View File

@@ -889,3 +889,8 @@ type ExternalCDRWithArgDispatcher struct {
*ExternalCDR
*utils.ArgDispatcher
}
type UsageRecordWithArgDispatcher struct {
*UsageRecord
*utils.ArgDispatcher
}

View File

@@ -220,14 +220,13 @@ func (cdrS *CDRServer) getCostFromRater(cdr *CDRWithArgDispatcher) (*CallCost, e
DurationIndex: cdr.Usage,
PerformRounding: true,
}
if cdr.ArgDispatcher != nil {
cd.ArgDispatcher = cdr.ArgDispatcher
}
if utils.IsSliceMember([]string{utils.META_PSEUDOPREPAID, utils.META_POSTPAID, utils.META_PREPAID,
utils.PSEUDOPREPAID, utils.POSTPAID, utils.PREPAID}, cdr.RequestType) { // Prepaid - Cost can be recalculated in case of missing records from SM
err = cdrS.rals.Call("Responder.Debit", cd, cc)
err = cdrS.rals.Call(utils.ResponderDebit, &CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: cdr.ArgDispatcher}, cc)
} else {
err = cdrS.rals.Call("Responder.GetCost", cd, cc)
err = cdrS.rals.Call(utils.ResponderGetCost, &CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: cdr.ArgDispatcher}, cc)
}
if err != nil {
return cc, err

View File

@@ -60,7 +60,7 @@ func (rs *Responder) usageAllowed(tor string, reqUsage time.Duration) (allowed b
/*
RPC method thet provides the external RPC interface for getting the rating information.
*/
func (rs *Responder) GetCost(arg *CallDescriptor, reply *CallCost) (err error) {
func (rs *Responder) GetCost(arg *CallDescriptorWithArgDispatcher, reply *CallCost) (err error) {
if arg.Tenant == "" {
arg.Tenant = config.CgrConfig().GeneralCfg().DefaultTenant
}
@@ -85,7 +85,7 @@ func (rs *Responder) GetCost(arg *CallDescriptor, reply *CallCost) (err error) {
return
}
func (rs *Responder) Debit(arg *CallDescriptor, reply *CallCost) (err error) {
func (rs *Responder) Debit(arg *CallDescriptorWithArgDispatcher, reply *CallCost) (err error) {
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResponderDebit, arg.CgrID)
@@ -123,7 +123,7 @@ func (rs *Responder) Debit(arg *CallDescriptor, reply *CallCost) (err error) {
return
}
func (rs *Responder) MaxDebit(arg *CallDescriptor, reply *CallCost) (err error) {
func (rs *Responder) MaxDebit(arg *CallDescriptorWithArgDispatcher, reply *CallCost) (err error) {
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResponderMaxDebit, arg.CgrID)
@@ -161,7 +161,7 @@ func (rs *Responder) MaxDebit(arg *CallDescriptor, reply *CallCost) (err error)
return
}
func (rs *Responder) RefundIncrements(arg *CallDescriptor, reply *Account) (err error) {
func (rs *Responder) RefundIncrements(arg *CallDescriptorWithArgDispatcher, reply *Account) (err error) {
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResponderRefundIncrements, arg.CgrID)
@@ -199,7 +199,7 @@ func (rs *Responder) RefundIncrements(arg *CallDescriptor, reply *Account) (err
return
}
func (rs *Responder) RefundRounding(arg *CallDescriptor, reply *float64) (err error) {
func (rs *Responder) RefundRounding(arg *CallDescriptorWithArgDispatcher, reply *float64) (err error) {
// RPC caching
if config.CgrConfig().CacheCfg()[utils.CacheRPCResponses].Limit != 0 {
cacheKey := utils.ConcatenatedKey(utils.ResponderRefundRounding, arg.CgrID)
@@ -230,7 +230,7 @@ func (rs *Responder) RefundRounding(arg *CallDescriptor, reply *float64) (err er
return
}
func (rs *Responder) GetMaxSessionTime(arg *CallDescriptor, reply *time.Duration) (err error) {
func (rs *Responder) GetMaxSessionTime(arg *CallDescriptorWithArgDispatcher, reply *time.Duration) (err error) {
if arg.Subject == "" {
arg.Subject = arg.Account
}

View File

@@ -102,7 +102,7 @@ func TestAuthPostpaidNoAcnt(t *testing.T) {
t.Error(err)
}
var maxSessionTime time.Duration
if err := rsponder.GetMaxSessionTime(cd, &maxSessionTime); err != utils.ErrAccountNotFound {
if err := rsponder.GetMaxSessionTime(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd}, &maxSessionTime); err != utils.ErrAccountNotFound {
t.Error(err)
}
}
@@ -117,7 +117,7 @@ func TestAuthPostpaidFallbackDest(t *testing.T) {
t.Error(err)
}
var maxSessionTime time.Duration
if err = rsponder.GetMaxSessionTime(cd, &maxSessionTime); err != nil {
if err = rsponder.GetMaxSessionTime(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd}, &maxSessionTime); err != nil {
t.Error(err)
} else if maxSessionTime != time.Duration(0) {
t.Error("Unexpected maxSessionTime received: ", maxSessionTime)
@@ -134,7 +134,7 @@ func TestAuthPostpaidWithDestination(t *testing.T) {
t.Error(err)
}
var maxSessionTime time.Duration
if err := rsponder.GetMaxSessionTime(cd, &maxSessionTime); err != nil {
if err := rsponder.GetMaxSessionTime(&engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd}, &maxSessionTime); err != nil {
t.Error(err)
} else if maxSessionTime != time.Duration(0) {
t.Error("Unexpected maxSessionTime received: ", maxSessionTime)

View File

@@ -468,12 +468,11 @@ func (sS *SessionS) debitSession(s *Session, sRunIdx int, dur time.Duration,
sr.CD.TimeEnd = sr.CD.TimeStart.Add(rDur)
sr.CD.DurationIndex += rDur
cd := sr.CD.Clone()
if s.ArgDispatcher != nil {
cd.ArgDispatcher = s.ArgDispatcher
}
argDsp := s.ArgDispatcher
s.Unlock()
cc := new(engine.CallCost)
if err := sS.ralS.Call(utils.ResponderMaxDebit, cd, cc); err != nil {
if err := sS.ralS.Call(utils.ResponderMaxDebit, &engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: argDsp}, cc); err != nil {
s.Lock()
sr.ExtraDuration += dbtRsrv
s.Unlock()
@@ -596,19 +595,19 @@ func (sS *SessionS) refundSession(s *Session, sRunIdx int, rUsage time.Duration)
}
}
cd := &engine.CallDescriptor{
CgrID: s.CGRID,
RunID: sr.Event.GetStringIgnoreErrors(utils.RunID),
Category: sr.CD.Category,
Tenant: sr.CD.Tenant,
Subject: sr.CD.Subject,
Account: sr.CD.Account,
Destination: sr.CD.Destination,
TOR: sr.CD.TOR,
Increments: incrmts,
ArgDispatcher: s.ArgDispatcher,
CgrID: s.CGRID,
RunID: sr.Event.GetStringIgnoreErrors(utils.RunID),
Category: sr.CD.Category,
Tenant: sr.CD.Tenant,
Subject: sr.CD.Subject,
Account: sr.CD.Account,
Destination: sr.CD.Destination,
TOR: sr.CD.TOR,
Increments: incrmts,
}
var acnt engine.Account
if err = sS.ralS.Call(utils.ResponderRefundIncrements, cd, &acnt); err != nil {
if err = sS.ralS.Call(utils.ResponderRefundIncrements, &engine.CallDescriptorWithArgDispatcher{CallDescriptor: cd,
ArgDispatcher: s.ArgDispatcher}, &acnt); err != nil {
return
}
if acnt.ID != "" { // Account info updated, update also cached AccountSummary
@@ -1206,14 +1205,10 @@ func (sS *SessionS) authSession(tnt string, evStart *engine.SafEvent) (maxUsage
if !utils.IsSliceMember(prepaidReqs,
sr.Event.GetStringIgnoreErrors(utils.RequestType)) {
rplyMaxUsage = time.Duration(-1)
} else {
if s.ArgDispatcher != nil {
sr.CD.ArgDispatcher = s.ArgDispatcher
}
if err = sS.ralS.Call(utils.ResponderGetMaxSessionTime,
sr.CD, &rplyMaxUsage); err != nil {
return
}
} else if err = sS.ralS.Call(utils.ResponderGetMaxSessionTime,
&engine.CallDescriptorWithArgDispatcher{CallDescriptor: sr.CD,
ArgDispatcher: s.ArgDispatcher}, &rplyMaxUsage); err != nil {
return
}
if !maxUsageSet ||
maxUsage == time.Duration(-1) ||
@@ -1337,11 +1332,9 @@ func (sS *SessionS) endSession(s *Session, tUsage, lastUsage *time.Duration) (er
}
sr.CD.TimeEnd = sr.CD.TimeStart.Add(notCharged)
sr.CD.DurationIndex += notCharged
if s.ArgDispatcher != nil {
sr.CD.ArgDispatcher = s.ArgDispatcher
}
cc := new(engine.CallCost)
if err = sS.ralS.Call(utils.ResponderDebit, sr.CD, cc); err == nil {
if err = sS.ralS.Call(utils.ResponderDebit, &engine.CallDescriptorWithArgDispatcher{CallDescriptor: sr.CD,
ArgDispatcher: s.ArgDispatcher}, cc); err == nil {
sr.EventCost.Merge(
engine.NewEventCostFromCallCost(cc, s.CGRID,
sr.Event.GetStringIgnoreErrors(utils.RunID)))