Пример #1
0
// Removes an ActionTimings or parts of it depending on filters being set
func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"ActionPlanId"}); len(missing) != 0 { // Only mandatory ActionPlanId
		return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
	}
	if len(attrs.Account) != 0 { // Presence of Account requires complete account details to be provided
		if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account", "Direction"}); len(missing) != 0 {
			return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
		}
	}
	_, err := engine.AccLock.Guard(engine.ACTION_TIMING_PREFIX, func() (float64, error) {
		ats, err := self.AccountDb.GetActionTimings(attrs.ActionPlanId)
		if err != nil {
			return 0, err
		} else if len(ats) == 0 {
			return 0, errors.New(utils.ERR_NOT_FOUND)
		}
		ats = engine.RemActionTiming(ats, attrs.ActionTimingId, utils.AccountKey(attrs.Tenant, attrs.Account, attrs.Direction))
		if err := self.AccountDb.SetActionTimings(attrs.ActionPlanId, ats); err != nil {
			return 0, err
		}
		return 0, nil
	})
	if err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	if attrs.ReloadScheduler && self.Sched != nil {
		self.Sched.LoadActionTimings(self.AccountDb)
		self.Sched.Restart()
	}
	*reply = OK
	return nil
}
Пример #2
0
func (self *ApierV1) GetAccountActionPlan(attrs AttrAcntAction, reply *[]*AccountActionTiming) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(strings.Join(missing, ","), "")
	}
	accountATs := make([]*AccountActionTiming, 0) // needs to be initialized if remains empty
	allAPs, err := self.RatingDb.GetAllActionPlans()
	if err != nil {
		return utils.NewErrServerError(err)
	}
	accID := utils.AccountKey(attrs.Tenant, attrs.Account)
	for _, ap := range allAPs {
		if ap == nil {
			continue
		}
		if _, exists := ap.AccountIDs[accID]; exists {
			for _, at := range ap.ActionTimings {
				accountATs = append(accountATs, &AccountActionTiming{
					ActionPlanId: ap.Id,
					Uuid:         at.Uuid,
					ActionsId:    at.ActionsID,
					NextExecTime: at.GetNextStartTime(time.Now()),
				})
			}
		}

	}
	*reply = accountATs
	return nil
}
Пример #3
0
// Returns a list of ActionTriggers on an account
func (self *ApierV1) RemAccountActionTriggers(attrs AttrRemAcntActionTriggers, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	balanceId := utils.AccountKey(attrs.Tenant, attrs.Account)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		ub, err := self.AccountDb.GetAccount(balanceId)
		if err != nil {
			return 0, err
		}
		nactrs := make(engine.ActionTriggers, 0)
		for _, actr := range ub.ActionTriggers {
			match, _ := regexp.MatchString(attrs.ActionTriggersId, actr.Id)
			if len(attrs.ActionTriggersId) != 0 && !match {
				nactrs = append(nactrs, actr)
			}
		}
		ub.ActionTriggers = nactrs
		if err := self.AccountDb.SetAccount(ub); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, balanceId)
	if err != nil {
		return utils.NewErrServerError(err)
	}
	*reply = OK
	return nil
}
Пример #4
0
func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	dirtyActionPlans := make(map[string]*engine.ActionPlan)
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		// remove it from all action plans
		_, err := engine.Guardian.Guard(func() (interface{}, error) {
			actionPlansMap, err := self.RatingDb.GetAllActionPlans()
			if err == utils.ErrNotFound {
				// no action plans
				return 0, nil
			}
			if err != nil {
				return 0, err
			}

			for actionPlanID, ap := range actionPlansMap {
				if _, exists := ap.AccountIDs[accID]; exists {
					delete(ap.AccountIDs, accID)
					dirtyActionPlans[actionPlanID] = ap
				}
			}

			var actionPlansCacheIds []string
			for actionPlanID, ap := range dirtyActionPlans {
				if err := self.RatingDb.SetActionPlan(actionPlanID, ap, true); err != nil {
					return 0, err
				}
				actionPlansCacheIds = append(actionPlansCacheIds, utils.ACTION_PLAN_PREFIX+actionPlanID)
			}
			if len(actionPlansCacheIds) > 0 {
				// update cache
				self.RatingDb.CacheRatingPrefixValues("RemoveAccountAPI", map[string][]string{
					utils.ACTION_PLAN_PREFIX: actionPlansCacheIds})
			}
			return 0, nil
		}, 0, utils.ACTION_PLAN_PREFIX)
		if err != nil {
			return 0, err
		}

		if err := self.AccountDb.RemoveAccount(accID); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accID)
	if err != nil {
		return utils.NewErrServerError(err)
	}
	if attr.ReloadScheduler && len(dirtyActionPlans) > 0 {
		// reload scheduler
		if self.Sched != nil {
			self.Sched.Reload(true)
		}
	}
	*reply = OK
	return nil
}
Пример #5
0
// Removes an ActionTimings or parts of it depending on filters being set
func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"ActionPlanId"}); len(missing) != 0 { // Only mandatory ActionPlanId
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	if len(attrs.Account) != 0 { // Presence of Account requires complete account details to be provided
		if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
			return utils.NewErrMandatoryIeMissing(missing...)
		}
	}
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		ats, err := self.RatingDb.GetActionPlans(attrs.ActionPlanId, false)
		if err != nil {
			return 0, err
		} else if len(ats) == 0 {
			return 0, utils.ErrNotFound
		}
		ats = engine.RemActionPlan(ats, attrs.ActionTimingId, utils.AccountKey(attrs.Tenant, attrs.Account))
		if err := self.RatingDb.SetActionPlans(attrs.ActionPlanId, ats); err != nil {
			return 0, err
		}
		if len(ats) > 0 { // update cache
			self.RatingDb.CacheRatingPrefixValues(map[string][]string{utils.ACTION_PLAN_PREFIX: []string{utils.ACTION_PLAN_PREFIX + attrs.ActionPlanId}})
		}
		return 0, nil
	}, 0, utils.ACTION_PLAN_PREFIX)
	if err != nil {
		return utils.NewErrServerError(err)
	}
	if attrs.ReloadScheduler && self.Sched != nil {
		self.Sched.LoadActionPlans(self.RatingDb)
		self.Sched.Restart()
	}
	*reply = OK
	return nil
}
Пример #6
0
func (self *ApierV1) RemoveAccountActionTriggers(attr AttrRemoveAccountActionTriggers, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		var account *engine.Account
		if acc, err := self.AccountDb.GetAccount(accID); err == nil {
			account = acc
		} else {
			return 0, err
		}
		var newActionTriggers engine.ActionTriggers
		for _, at := range account.ActionTriggers {
			if (attr.UniqueID == "" || at.UniqueID == attr.UniqueID) &&
				(attr.GroupID == "" || at.ID == attr.GroupID) {
				// remove action trigger
				continue
			}
			newActionTriggers = append(newActionTriggers, at)
		}
		account.ActionTriggers = newActionTriggers
		account.InitCounters()
		if err := self.AccountDb.SetAccount(account); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accID)
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = utils.OK
	return nil
}
Пример #7
0
// Returns a list of ActionTriggers on an account
func (self *ApierV1) RemAccountActionTriggers(attrs AttrRemAcntActionTriggers, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account", "Direction"}); len(missing) != 0 {
		return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
	}
	balanceId := utils.AccountKey(attrs.Tenant, attrs.Account, attrs.Direction)
	_, err := engine.AccLock.Guard(balanceId, func() (float64, error) {
		ub, err := self.AccountDb.GetAccount(balanceId)
		if err != nil {
			return 0, err
		}
		for idx, actr := range ub.ActionTriggers {
			if len(attrs.ActionTriggerId) != 0 && actr.Id != attrs.ActionTriggerId { // Empty actionTriggerId will match always
				continue
			}
			if len(ub.ActionTriggers) != 1 { // Remove by index
				ub.ActionTriggers[idx], ub.ActionTriggers = ub.ActionTriggers[len(ub.ActionTriggers)-1], ub.ActionTriggers[:len(ub.ActionTriggers)-1]
			} else { // For last item, simply reinit the slice
				ub.ActionTriggers = make(engine.ActionTriggerPriotityList, 0)
			}
		}
		if err := self.AccountDb.SetAccount(ub); err != nil {
			return 0, err
		}
		return 0, nil
	})
	if err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	*reply = OK
	return nil
}
Пример #8
0
func (self *ApierV1) RemoveBalances(attr *utils.AttrSetBalance, reply *string) error {
	if missing := utils.MissingStructFields(attr, []string{"Tenant", "Account", "BalanceType"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	var expTime *time.Time
	if attr.ExpiryTime != nil {
		expTimeVal, err := utils.ParseTimeDetectLayout(*attr.ExpiryTime, self.Config.DefaultTimezone)
		if err != nil {
			*reply = err.Error()
			return err
		}
		expTime = &expTimeVal
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	if _, err := self.AccountDb.GetAccount(accID); err != nil {
		return utils.ErrNotFound
	}

	at := &engine.ActionTiming{}
	at.SetAccountIDs(utils.StringMap{accID: true})
	a := &engine.Action{
		ActionType: engine.REMOVE_BALANCE,
		Balance: &engine.BalanceFilter{
			Uuid:           attr.BalanceUUID,
			ID:             attr.BalanceID,
			Type:           utils.StringPointer(attr.BalanceType),
			ExpirationDate: expTime,
			RatingSubject:  attr.RatingSubject,
			Weight:         attr.Weight,
			Blocker:        attr.Blocker,
			Disabled:       attr.Disabled,
		},
	}
	if attr.Value != nil {
		a.Balance.Value = &utils.ValueFormula{Static: *attr.Value}
	}
	if attr.Directions != nil {
		a.Balance.Directions = utils.StringMapPointer(utils.ParseStringMap(*attr.Directions))
	}
	if attr.DestinationIds != nil {
		a.Balance.DestinationIDs = utils.StringMapPointer(utils.ParseStringMap(*attr.DestinationIds))
	}
	if attr.Categories != nil {
		a.Balance.Categories = utils.StringMapPointer(utils.ParseStringMap(*attr.Categories))
	}
	if attr.SharedGroups != nil {
		a.Balance.SharedGroups = utils.StringMapPointer(utils.ParseStringMap(*attr.SharedGroups))
	}
	if attr.TimingIds != nil {
		a.Balance.TimingIDs = utils.StringMapPointer(utils.ParseStringMap(*attr.TimingIds))
	}
	at.SetActions(engine.Actions{a})
	if err := at.Execute(); err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #9
0
func removeAccountAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) error {

	var accID string
	if ub != nil {
		accID = ub.ID
	} else {
		accountInfo := struct {
			Tenant  string
			Account string
		}{}
		if a.ExtraParameters != "" {
			if err := json.Unmarshal([]byte(a.ExtraParameters), &accountInfo); err != nil {
				return err
			}
		}
		accID = utils.AccountKey(accountInfo.Tenant, accountInfo.Account)
	}
	if accID == "" {
		return utils.ErrInvalidKey
	}

	if err := accountingStorage.RemoveAccount(accID); err != nil {
		utils.Logger.Err(fmt.Sprintf("Could not remove account Id: %s: %v", accID, err))
		return err
	}

	_, err := Guardian.Guard(func() (interface{}, error) {
		// clean the account id from all action plans
		allAPs, err := ratingStorage.GetAllActionPlans()
		if err != nil && err != utils.ErrNotFound {
			utils.Logger.Err(fmt.Sprintf("Could not get action plans: %s: %v", accID, err))
			return 0, err
		}
		//var dirtyAps []string
		aps := make([]string, len(allAPs))
		i := 0
		for key, ap := range allAPs {

			if _, exists := ap.AccountIDs[accID]; exists {

				// save action plan
				delete(ap.AccountIDs, accID)
				ratingStorage.SetActionPlan(key, ap, true, utils.NonTransactional)
				//dirtyAps = append(dirtyAps, utils.ACTION_PLAN_PREFIX+key)
			}
			aps[i] = key
			i++
		}
		if err = ratingStorage.CacheDataFromDB(utils.ACTION_PLAN_PREFIX, aps, true); err != nil {
			return 0, err
		}
		return 0, nil

	}, 0, utils.ACTION_PLAN_PREFIX)
	if err != nil {
		return err
	}
	return nil
}
Пример #10
0
// Removes an ActionTimings or parts of it depending on filters being set
func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"ActionPlanId"}); len(missing) != 0 { // Only mandatory ActionPlanId
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	if len(attrs.Account) != 0 { // Presence of Account requires complete account details to be provided
		if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
			return utils.NewErrMandatoryIeMissing(missing...)
		}
	}
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		ap, err := self.RatingDb.GetActionPlan(attrs.ActionPlanId, false)
		if err != nil {
			return 0, err
		} else if ap == nil {
			return 0, utils.ErrNotFound
		}

		if attrs.Tenant != "" && attrs.Account != "" {
			accID := utils.AccountKey(attrs.Tenant, attrs.Account)
			delete(ap.AccountIDs, accID)
			err = self.RatingDb.SetActionPlan(ap.Id, ap, true)
			goto UPDATE
		}

		if attrs.ActionTimingId != "" { // delete only a action timing from action plan
			for i, at := range ap.ActionTimings {
				if at.Uuid == attrs.ActionTimingId {
					ap.ActionTimings[i] = ap.ActionTimings[len(ap.ActionTimings)-1]
					ap.ActionTimings = ap.ActionTimings[:len(ap.ActionTimings)-1]
					break
				}
			}
			err = self.RatingDb.SetActionPlan(ap.Id, ap, true)
			goto UPDATE
		}

		if attrs.ActionPlanId != "" { // delete the entire action plan
			ap.ActionTimings = nil // will delete the action plan
			err = self.RatingDb.SetActionPlan(ap.Id, ap, true)
			goto UPDATE
		}
	UPDATE:
		if err != nil {
			return 0, err
		}
		// update cache
		self.RatingDb.CacheRatingPrefixValues("AttrRemActionTimingAPI", map[string][]string{utils.ACTION_PLAN_PREFIX: []string{utils.ACTION_PLAN_PREFIX + attrs.ActionPlanId}})
		return 0, nil
	}, 0, utils.ACTION_PLAN_PREFIX)
	if err != nil {
		*reply = err.Error()
		return utils.NewErrServerError(err)
	}
	if attrs.ReloadScheduler && self.Sched != nil {
		self.Sched.Reload(true)
	}
	*reply = OK
	return nil
}
Пример #11
0
// Ads a new account into dataDb. If already defined, returns success.
func (self *ApierV1) SetAccount(attr utils.AttrSetAccount, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Direction", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	balanceId := utils.AccountKey(attr.Tenant, attr.Account, attr.Direction)
	var ub *engine.Account
	var ats engine.ActionPlans
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		if bal, _ := self.AccountDb.GetAccount(balanceId); bal != nil {
			ub = bal
		} else { // Not found in db, create it here
			ub = &engine.Account{
				Id: balanceId,
			}
		}

		if len(attr.ActionPlanId) != 0 {
			var err error
			ats, err = self.RatingDb.GetActionPlans(attr.ActionPlanId)
			if err != nil {
				return 0, err
			}
			for _, at := range ats {
				at.AccountIds = append(at.AccountIds, balanceId)
			}
		}
		if attr.AllowNegative != nil {
			ub.AllowNegative = *attr.AllowNegative
		}
		if attr.Disabled != nil {
			ub.Disabled = *attr.Disabled
		}
		// All prepared, save account
		if err := self.AccountDb.SetAccount(ub); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, balanceId)
	if err != nil {
		return utils.NewErrServerError(err)
	}
	if len(ats) != 0 {
		_, err := engine.Guardian.Guard(func() (interface{}, error) { // ToDo: Try locking it above on read somehow
			if err := self.RatingDb.SetActionPlans(attr.ActionPlanId, ats); err != nil {
				return 0, err
			}
			return 0, nil
		}, 0, utils.ACTION_TIMING_PREFIX)
		if err != nil {
			return utils.NewErrServerError(err)
		}
		if self.Sched != nil {
			self.Sched.LoadActionPlans(self.RatingDb)
			self.Sched.Restart()
		}
	}
	*reply = OK // This will mark saving of the account, error still can show up in actionTimingsId
	return nil
}
Пример #12
0
// Deprecated in rc8, replaced by AddAccountActionTriggers
func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	at := &engine.ActionTrigger{
		ID:             attr.ActionTriggersId,
		ThresholdType:  attr.ThresholdType,
		ThresholdValue: attr.ThresholdValue,
		Balance:        new(engine.BalanceFilter),
		Weight:         attr.Weight,
		ActionsID:      attr.ActionsId,
	}
	if attr.BalanceId != "" {
		at.Balance.ID = utils.StringPointer(attr.BalanceId)
	}
	if attr.BalanceType != "" {
		at.Balance.Type = utils.StringPointer(attr.BalanceType)
	}
	if attr.BalanceDirection != "" {
		at.Balance.Directions = &utils.StringMap{attr.BalanceDirection: true}
	}
	if attr.BalanceDestinationIds != "" {
		dstIDsMp := utils.StringMapFromSlice(strings.Split(attr.BalanceDestinationIds, utils.INFIELD_SEP))
		at.Balance.DestinationIDs = &dstIDsMp
	}
	if attr.BalanceRatingSubject != "" {
		at.Balance.RatingSubject = utils.StringPointer(attr.BalanceRatingSubject)
	}
	if attr.BalanceWeight != 0.0 {
		at.Balance.Weight = utils.Float64Pointer(attr.BalanceWeight)
	}
	if balExpiryTime, err := utils.ParseTimeDetectLayout(attr.BalanceExpiryTime, self.Config.DefaultTimezone); err != nil {
		return utils.NewErrServerError(err)
	} else {
		at.Balance.ExpirationDate = &balExpiryTime
	}
	if attr.BalanceSharedGroup != "" {
		at.Balance.SharedGroups = &utils.StringMap{attr.BalanceSharedGroup: true}
	}
	acntID := utils.AccountKey(attr.Tenant, attr.Account)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		acnt, err := self.AccountDb.GetAccount(acntID)
		if err != nil {
			return 0, err
		}
		acnt.ActionTriggers = append(acnt.ActionTriggers, at)

		if err = self.AccountDb.SetAccount(acnt); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, acntID)
	if err != nil {
		return err
	}
	*reply = OK
	return nil
}
Пример #13
0
func (self *ApierV1) AddAccountActionTriggers(attr AttrAddAccountActionTriggers, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	actTime, err := utils.ParseTimeDetectLayout(attr.ActivationDate, self.Config.DefaultTimezone)
	if err != nil {
		*reply = err.Error()
		return err
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	var account *engine.Account
	_, err = engine.Guardian.Guard(func() (interface{}, error) {
		if acc, err := self.AccountDb.GetAccount(accID); err == nil {
			account = acc
		} else {
			return 0, err
		}
		if attr.ActionTriggerIDs != nil {
			if attr.ActionTriggerOverwrite {
				account.ActionTriggers = make(engine.ActionTriggers, 0)
			}
			for _, actionTriggerID := range *attr.ActionTriggerIDs {
				atrs, err := self.RatingDb.GetActionTriggers(actionTriggerID, false, utils.NonTransactional)
				if err != nil {

					return 0, err
				}
				for _, at := range atrs {
					var found bool
					for _, existingAt := range account.ActionTriggers {
						if existingAt.Equals(at) {
							found = true
							break
						}
					}
					at.ActivationDate = actTime
					at.Executed = attr.Executed
					if !found {
						account.ActionTriggers = append(account.ActionTriggers, at)
					}
				}
			}
		}
		account.InitCounters()
		if err := self.AccountDb.SetAccount(account); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accID)
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = utils.OK
	return nil
}
Пример #14
0
func (self *ApierV1) ResetTriggeredActions(attr AttrResetTriggeredAction, reply *string) error {
	var a *engine.Action
	if attr.Id != "" {
		// we can identify the trigge by the id
		a = &engine.Action{Id: attr.Id}
	} else {
		if attr.Direction == "" {
			attr.Direction = engine.OUTBOUND
		}
		extraParameters, err := json.Marshal(struct {
			ThresholdType        string
			ThresholdValue       float64
			DestinationId        string
			BalanceWeight        float64
			BalanceRatingSubject string
			BalanceSharedGroup   string
		}{
			attr.ThresholdType,
			attr.ThresholdValue,
			attr.DestinationId,
			attr.BalanceWeight,
			attr.BalanceRatingSubject,
			attr.BalanceSharedGroup,
		})
		if err != nil {
			*reply = err.Error()
			return err
		}
		a = &engine.Action{
			BalanceType:     attr.BalanceType,
			Direction:       attr.Direction,
			ExtraParameters: string(extraParameters),
		}
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account, attr.Direction)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		acc, err := self.AccountDb.GetAccount(accID)
		if err != nil {
			return 0, err
		}

		acc.ResetActionTriggers(a)

		if err = self.AccountDb.SetAccount(acc); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accID)
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #15
0
// Returns a list of ActionTriggers on an account
func (self *ApierV1) GetAccountActionTriggers(attrs AttrAcntAction, reply *engine.ActionTriggers) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	if balance, err := self.AccountDb.GetAccount(utils.AccountKey(attrs.Tenant, attrs.Account)); err != nil {
		return utils.NewErrServerError(err)
	} else {
		*reply = balance.ActionTriggers
	}
	return nil
}
Пример #16
0
func (self *ApierV1) modifyBalance(aType string, attr *AttrAddBalance, reply *string) error {
	if missing := utils.MissingStructFields(attr, []string{"Tenant", "Account", "BalanceType"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	expTime, err := utils.ParseTimeDetectLayout(attr.ExpiryTime, self.Config.DefaultTimezone)
	if err != nil {
		*reply = err.Error()
		return err
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	if _, err := self.AccountDb.GetAccount(accID); err != nil {
		// create account if not exists
		account := &engine.Account{
			Id: accID,
		}
		if err := self.AccountDb.SetAccount(account); err != nil {
			*reply = err.Error()
			return err
		}
	}
	at := &engine.ActionTiming{}
	at.SetAccountIDs(utils.StringMap{accID: true})

	if attr.Overwrite {
		aType += "_reset" // => *topup_reset/*debit_reset
	}
	at.SetActions(engine.Actions{
		&engine.Action{
			ActionType:  aType,
			BalanceType: attr.BalanceType,
			Balance: &engine.Balance{
				Uuid:           attr.BalanceUuid,
				Id:             attr.BalanceId,
				Value:          attr.Value,
				ExpirationDate: expTime,
				RatingSubject:  attr.RatingSubject,
				Directions:     utils.ParseStringMap(attr.Directions),
				DestinationIds: utils.ParseStringMap(attr.DestinationIds),
				Categories:     utils.ParseStringMap(attr.Categories),
				Weight:         attr.Weight,
				SharedGroups:   utils.ParseStringMap(attr.SharedGroups),
				TimingIDs:      utils.ParseStringMap(attr.TimingIds),
				Blocker:        attr.Blocker,
				Disabled:       attr.Disabled,
			},
		},
	})
	if err := at.Execute(); err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #17
0
// Returns a list of ActionTriggers on an account
func (self *ApierV1) GetAccountActionTriggers(attrs AttrAcntAction, reply *engine.ActionTriggerPriotityList) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account", "Direction"}); len(missing) != 0 {
		return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
	}
	if balance, err := self.AccountDb.GetAccount(utils.AccountKey(attrs.Tenant, attrs.Account, attrs.Direction)); err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	} else {
		*reply = balance.ActionTriggers
	}
	return nil
}
Пример #18
0
// Ads a new account into dataDb. If already defined, returns success.
func (self *ApierV1) SetAccount(attr AttrSetAccount, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Direction", "Account"}); len(missing) != 0 {
		return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
	}
	balanceId := utils.AccountKey(attr.Tenant, attr.Account, attr.Direction)
	var ub *engine.Account
	var ats engine.ActionPlan
	_, err := engine.AccLock.Guard(balanceId, func() (float64, error) {
		if bal, _ := self.AccountDb.GetAccount(balanceId); bal != nil {
			ub = bal
		} else { // Not found in db, create it here
			ub = &engine.Account{
				Id:            balanceId,
				AllowNegative: attr.AllowNegative,
			}
		}

		if len(attr.ActionPlanId) != 0 {
			var err error
			ats, err = self.AccountDb.GetActionTimings(attr.ActionPlanId)
			if err != nil {
				return 0, err
			}
			for _, at := range ats {
				at.AccountIds = append(at.AccountIds, balanceId)
			}
		}
		// All prepared, save account
		if err := self.AccountDb.SetAccount(ub); err != nil {
			return 0, err
		}
		return 0, nil
	})
	if err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	if len(ats) != 0 {
		_, err := engine.AccLock.Guard(engine.ACTION_TIMING_PREFIX, func() (float64, error) { // ToDo: Try locking it above on read somehow
			if err := self.AccountDb.SetActionTimings(attr.ActionPlanId, ats); err != nil {
				return 0, err
			}
			return 0, nil
		})
		if err != nil {
			return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
		}
		if self.Sched != nil {
			self.Sched.LoadActionTimings(self.AccountDb)
			self.Sched.Restart()
		}
	}
	*reply = OK // This will mark saving of the account, error still can show up in actionTimingsId
	return nil
}
Пример #19
0
func removeAccountAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) error {
	var accID string
	if ub != nil {
		accID = ub.Id
	} else {
		accountInfo := struct {
			Tenant  string
			Account string
		}{}
		if a.ExtraParameters != "" {
			if err := json.Unmarshal([]byte(a.ExtraParameters), &accountInfo); err != nil {
				return err
			}
		}
		accID = utils.AccountKey(accountInfo.Tenant, accountInfo.Account)
	}
	if accID == "" {
		return utils.ErrInvalidKey
	}
	if err := accountingStorage.RemoveAccount(accID); err != nil {
		utils.Logger.Err(fmt.Sprintf("Could not remove account Id: %s: %v", accID, err))
		return err
	}
	_, err := Guardian.Guard(func() (interface{}, error) {
		// clean the account id from all action plans
		allAPs, err := ratingStorage.GetAllActionPlans()
		if err != nil && err != utils.ErrNotFound {
			utils.Logger.Err(fmt.Sprintf("Could not get action plans: %s: %v", accID, err))
			return 0, err
		}
		var dirtyAps []string
		for key, ap := range allAPs {
			if _, exists := ap.AccountIDs[accID]; !exists {
				// save action plan
				delete(ap.AccountIDs, key)
				ratingStorage.SetActionPlan(key, ap, true)
				dirtyAps = append(dirtyAps, utils.ACTION_PLAN_PREFIX+key)
			}
		}
		if len(dirtyAps) > 0 {
			// cache
			ratingStorage.CacheRatingPrefixValues(map[string][]string{
				utils.ACTION_PLAN_PREFIX: dirtyAps})
		}
		return 0, nil

	}, 0, utils.ACTION_PLAN_PREFIX)
	if err != nil {
		return err
	}
	return nil
}
Пример #20
0
func (self *ApierV1) ExecuteAction(attr *utils.AttrExecuteAction, reply *string) error {
	accId := utils.AccountKey(attr.Tenant, attr.Account)
	at := &engine.ActionPlan{
		AccountIds: []string{accId},
		ActionsId:  attr.ActionsId,
	}
	if err := at.Execute(); err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #21
0
func (self *ApierV1) ExecuteAction(attr *utils.AttrExecuteAction, reply *string) error {
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	at := &engine.ActionTiming{
		ActionsID: attr.ActionsId,
	}
	at.SetAccountIDs(utils.StringMap{accID: true})
	if err := at.Execute(); err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #22
0
func (self *ApierV1) ResetTriggeredActions(attr AttrResetTriggeredAction, reply *string) error {
	if attr.Direction == "" {
		attr.Direction = engine.OUTBOUND
	}
	extraParameters, err := json.Marshal(struct {
		ThresholdType        string
		ThresholdValue       float64
		DestinationId        string
		BalanceWeight        float64
		BalanceRatingSubject string
		BalanceSharedGroup   string
	}{
		attr.ThresholdType,
		attr.ThresholdValue,
		attr.DestinationId,
		attr.BalanceWeight,
		attr.BalanceRatingSubject,
		attr.BalanceSharedGroup,
	})
	if err != nil {
		*reply = err.Error()
		return err
	}
	a := &engine.Action{
		BalanceType:     attr.BalanceType,
		Direction:       attr.Direction,
		ExtraParameters: string(extraParameters),
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account, attr.Direction)
	_, err = engine.AccLock.Guard(accID, func() (float64, error) {
		acc, err := self.AccountDb.GetAccount(accID)
		if err != nil {
			return 0, err
		}

		acc.ResetActionTriggers(a)

		if err = self.AccountDb.SetAccount(acc); err != nil {
			return 0, err
		}
		return 0, nil
	})
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #23
0
func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	dirtyActionPlans := make(map[string]*engine.ActionPlan)
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		// remove it from all action plans
		_, err := engine.Guardian.Guard(func() (interface{}, error) {
			actionPlansMap, err := self.RatingDb.GetAllActionPlans()
			if err == utils.ErrNotFound {
				// no action plans
				return 0, nil
			}
			if err != nil {
				return 0, err
			}

			for actionPlanID, ap := range actionPlansMap {
				if _, exists := ap.AccountIDs[accID]; exists {
					delete(ap.AccountIDs, accID)
					dirtyActionPlans[actionPlanID] = ap
				}
			}

			for actionPlanID, ap := range dirtyActionPlans {
				if err := self.RatingDb.SetActionPlan(actionPlanID, ap, true, utils.NonTransactional); err != nil {
					return 0, err
				}
			}
			return 0, nil
		}, 0, utils.ACTION_PLAN_PREFIX)
		if err != nil {
			return 0, err
		}

		if err := self.AccountDb.RemoveAccount(accID); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accID)
	if err != nil {
		return utils.NewErrServerError(err)
	}

	*reply = OK
	return nil
}
Пример #24
0
func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Direction", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	accountId := utils.AccountKey(attr.Tenant, attr.Account, attr.Direction)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		if err := self.AccountDb.RemoveAccount(accountId); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accountId)
	if err != nil {
		return utils.NewErrServerError(err)
	}

	*reply = OK
	return nil
}
Пример #25
0
func (self *ApierV1) RemoveAccount(attr utils.AttrRemoveAccount, reply *string) error {
	if missing := utils.MissingStructFields(&attr, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	accountId := utils.AccountKey(attr.Tenant, attr.Account)
	_, err := engine.Guardian.Guard(func() (interface{}, error) {
		// remove it from all action plans
		allATs, err := self.RatingDb.GetAllActionPlans()
		if err != nil && err != utils.ErrNotFound {
			return 0, err
		}
		for key, ats := range allATs {
			changed := false
			for _, at := range ats {
				for i := 0; i < len(at.AccountIds); i++ {
					if at.AccountIds[i] == accountId {
						// delete without preserving order
						at.AccountIds[i] = at.AccountIds[len(at.AccountIds)-1]
						at.AccountIds = at.AccountIds[:len(at.AccountIds)-1]
						i -= 1
						changed = true
					}
				}
			}
			if changed {
				// save action plan
				self.RatingDb.SetActionPlans(key, ats)
				// cache
				self.RatingDb.CacheRatingPrefixValues(map[string][]string{utils.ACTION_PLAN_PREFIX: []string{utils.ACTION_PLAN_PREFIX + key}})
			}
		}
		if err := self.AccountDb.RemoveAccount(accountId); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, accountId)
	// FIXME: remove from all actionplans?
	if err != nil {
		return utils.NewErrServerError(err)
	}

	*reply = OK
	return nil
}
Пример #26
0
func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string) error {
	if attr.BalanceDirection == "" {
		attr.BalanceDirection = engine.OUTBOUND
	}
	balExpiryTime, err := utils.ParseTimeDetectLayout(attr.BalanceExpiryTime, self.Config.DefaultTimezone)
	if err != nil {
		return utils.NewErrServerError(err)
	}
	at := &engine.ActionTrigger{
		Id:                    attr.ActionTriggersId,
		ThresholdType:         attr.ThresholdType,
		ThresholdValue:        attr.ThresholdValue,
		BalanceId:             attr.BalanceId,
		BalanceType:           attr.BalanceType,
		BalanceDirection:      attr.BalanceDirection,
		BalanceDestinationIds: attr.BalanceDestinationIds,
		BalanceWeight:         attr.BalanceWeight,
		BalanceExpirationDate: balExpiryTime,
		Weight:                attr.Weight,
		ActionsId:             attr.ActionsId,
		Executed:              false,
	}

	tag := utils.AccountKey(attr.Tenant, attr.Account, attr.BalanceDirection)
	_, err = engine.Guardian.Guard(func() (interface{}, error) {
		userBalance, err := self.AccountDb.GetAccount(tag)
		if err != nil {
			return 0, err
		}

		userBalance.ActionTriggers = append(userBalance.ActionTriggers, at)

		if err = self.AccountDb.SetAccount(userBalance); err != nil {
			return 0, err
		}
		return 0, nil
	}, 0, tag)
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #27
0
func (self *ApierV1) RemoveBalances(attr *AttrAddBalance, reply *string) error {
	if missing := utils.MissingStructFields(attr, []string{"Tenant", "Account", "BalanceType"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(missing...)
	}
	expTime, err := utils.ParseTimeDetectLayout(attr.ExpiryTime, self.Config.DefaultTimezone)
	if err != nil {
		*reply = err.Error()
		return err
	}
	accID := utils.AccountKey(attr.Tenant, attr.Account)
	if _, err := self.AccountDb.GetAccount(accID); err != nil {
		return utils.ErrNotFound
	}

	at := &engine.ActionTiming{}
	at.SetAccountIDs(utils.StringMap{accID: true})
	at.SetActions(engine.Actions{
		&engine.Action{
			ActionType:  engine.REMOVE_BALANCE,
			BalanceType: attr.BalanceType,
			Balance: &engine.Balance{
				Uuid:           attr.BalanceUuid,
				Id:             attr.BalanceId,
				Value:          attr.Value,
				ExpirationDate: expTime,
				RatingSubject:  attr.RatingSubject,
				Directions:     utils.ParseStringMap(attr.Directions),
				DestinationIds: utils.ParseStringMap(attr.DestinationIds),
				Categories:     utils.ParseStringMap(attr.Categories),
				Weight:         attr.Weight,
				SharedGroups:   utils.ParseStringMap(attr.SharedGroups),
				TimingIDs:      utils.ParseStringMap(attr.TimingIds),
				Blocker:        attr.Blocker,
				Disabled:       attr.Disabled,
			},
		},
	})
	if err := at.Execute(); err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #28
0
func (self *ApierV1) AddTriggeredAction(attr AttrAddActionTrigger, reply *string) error {
	if attr.Direction == "" {
		attr.Direction = engine.OUTBOUND
	}
	balExpiryTime, err := utils.ParseTimeDetectLayout(attr.BalanceExpiryTime)
	if err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	at := &engine.ActionTrigger{
		Id:                    utils.GenUUID(),
		BalanceType:           attr.BalanceType,
		Direction:             attr.Direction,
		ThresholdType:         attr.ThresholdType,
		ThresholdValue:        attr.ThresholdValue,
		DestinationId:         attr.DestinationId,
		BalanceWeight:         attr.BalanceWeight,
		BalanceExpirationDate: balExpiryTime,
		Weight:                attr.Weight,
		ActionsId:             attr.ActionsId,
		Executed:              false,
	}

	tag := utils.AccountKey(attr.Tenant, attr.Account, attr.Direction)
	_, err = engine.AccLock.Guard(tag, func() (float64, error) {
		userBalance, err := self.AccountDb.GetAccount(tag)
		if err != nil {
			return 0, err
		}

		userBalance.ActionTriggers = append(userBalance.ActionTriggers, at)

		if err = self.AccountDb.SetAccount(userBalance); err != nil {
			return 0, err
		}
		return 0, nil
	})
	if err != nil {
		*reply = err.Error()
		return err
	}
	*reply = OK
	return nil
}
Пример #29
0
func (self *ApierV1) GetAccountActionPlan(attrs AttrAcntAction, reply *[]*AccountActionTiming) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account", "Direction"}); len(missing) != 0 {
		return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
	}
	accountATs := make([]*AccountActionTiming, 0)
	allATs, err := self.AccountDb.GetAllActionTimings()
	if err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	for _, ats := range allATs {
		for _, at := range ats {
			if utils.IsSliceMember(at.AccountIds, utils.AccountKey(attrs.Tenant, attrs.Account, attrs.Direction)) {
				accountATs = append(accountATs, &AccountActionTiming{Uuid: at.Uuid, ActionPlanId: at.Id, ActionsId: at.ActionsId, NextExecTime: at.GetNextStartTime(time.Now())})
			}
		}
	}
	*reply = accountATs
	return nil
}
Пример #30
0
func (self *ApierV1) GetAccountActionPlan(attrs AttrAcntAction, reply *[]*AccountActionTiming) error {
	if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Account"}); len(missing) != 0 {
		return utils.NewErrMandatoryIeMissing(strings.Join(missing, ","), "")
	}
	accountATs := make([]*AccountActionTiming, 0)
	allATs, err := self.RatingDb.GetAllActionPlans()
	if err != nil {
		return utils.NewErrServerError(err)
	}
	for _, ats := range allATs {
		for _, at := range ats {
			if utils.IsSliceMember(at.AccountIds, utils.AccountKey(attrs.Tenant, attrs.Account)) {
				accountATs = append(accountATs, &AccountActionTiming{Uuid: at.Uuid, ActionPlanId: at.Id, ActionsId: at.ActionsId, NextExecTime: at.GetNextStartTime(time.Now())})
			}
		}
	}
	*reply = accountATs
	return nil
}