Пример #1
0
func (cd *CallDescriptor) GetLCRFromStorage() (*LCR, error) {
	keyVariants := []string{
		utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject),
		utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, cd.Account, utils.ANY),
		utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, utils.ANY, utils.ANY),
		utils.LCRKey(cd.Direction, cd.Tenant, utils.ANY, utils.ANY, utils.ANY),
		utils.LCRKey(utils.ANY, utils.ANY, cd.Category, utils.ANY, utils.ANY),
		utils.LCRKey(utils.ANY, utils.ANY, utils.ANY, utils.ANY, utils.ANY),
	}
	if lcrSubjectPrefixMatching {
		var partialSubjects []string
		lenSubject := len(cd.Subject)
		for i := 1; i < lenSubject; i++ {
			partialSubjects = append(partialSubjects, utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject[:lenSubject-i]))
		}
		// insert partialsubjects into keyVariants
		keyVariants = append(keyVariants[:1], append(partialSubjects, keyVariants[1:]...)...)
	}
	for _, key := range keyVariants {
		if lcr, err := ratingStorage.GetLCR(key, false, utils.NonTransactional); err != nil && err != utils.ErrNotFound {
			return nil, err
		} else if err == nil && lcr != nil {
			return lcr, nil
		}
	}
	return nil, utils.ErrNotFound
}
Пример #2
0
func (cd *CallDescriptor) GetLCRFromStorage() (*LCR, error) {
	keyVariants := []string{
		utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, cd.Account, cd.Subject),
		utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, cd.Account, utils.ANY),
		utils.LCRKey(cd.Direction, cd.Tenant, cd.Category, utils.ANY, utils.ANY),
		utils.LCRKey(cd.Direction, cd.Tenant, utils.ANY, utils.ANY, utils.ANY),
		utils.LCRKey(cd.Direction, utils.ANY, utils.ANY, utils.ANY, utils.ANY),
		utils.LCRKey(utils.ANY, utils.ANY, utils.ANY, utils.ANY, utils.ANY),
	}
	for _, key := range keyVariants {
		if lcr, err := ratingStorage.GetLCR(key, false); err != nil && err != utils.ErrNotFound {
			return nil, err
		} else if err == nil {
			return lcr, nil
		}
	}
	return nil, utils.ErrNotFound
}
Пример #3
0
func (lcr *TpLcrRule) GetLcrRuleId() string {
	return utils.LCRKey(lcr.Direction, lcr.Tenant, lcr.Category, lcr.Account, lcr.Subject)
}
Пример #4
0
func (tpr *TpReader) LoadLCRs() (err error) {
	tps, err := tpr.lr.GetTpLCRs(&TpLcrRule{Tpid: tpr.tpid})
	if err != nil {
		return err
	}

	for _, tpLcr := range tps {
		// check the rating profiles
		ratingProfileSearchKey := utils.ConcatenatedKey(tpLcr.Direction, tpLcr.Tenant, tpLcr.RpCategory)
		found := false
		for rpfKey := range tpr.ratingProfiles {
			if strings.HasPrefix(rpfKey, ratingProfileSearchKey) {
				found = true
				break
			}
		}
		if !found && tpr.ratingStorage != nil {
			if keys, err := tpr.ratingStorage.GetKeysForPrefix(utils.RATING_PROFILE_PREFIX + ratingProfileSearchKey); err != nil {
				return fmt.Errorf("[LCR] error querying ratingDb %s", err.Error())
			} else if len(keys) != 0 {
				found = true
			}
		}
		if !found {
			return fmt.Errorf("[LCR] could not find ratingProfiles with prefix %s", ratingProfileSearchKey)
		}

		// check destination tags
		if tpLcr.DestinationTag != "" && tpLcr.DestinationTag != utils.ANY {
			_, found := tpr.destinations[tpLcr.DestinationTag]
			if !found && tpr.ratingStorage != nil {
				if found, err = tpr.ratingStorage.HasData(utils.DESTINATION_PREFIX, tpLcr.DestinationTag); err != nil {
					return fmt.Errorf("[LCR] error querying ratingDb %s", err.Error())
				}
			}
			if !found {
				return fmt.Errorf("[LCR] could not find destination with tag %s", tpLcr.DestinationTag)
			}
		}
		tag := utils.LCRKey(tpLcr.Direction, tpLcr.Tenant, tpLcr.Category, tpLcr.Account, tpLcr.Subject)
		activationTime, _ := utils.ParseTimeDetectLayout(tpLcr.ActivationTime, tpr.timezone)

		lcr, found := tpr.lcrs[tag]
		if !found {
			lcr = &LCR{
				Direction: tpLcr.Direction,
				Tenant:    tpLcr.Tenant,
				Category:  tpLcr.Category,
				Account:   tpLcr.Account,
				Subject:   tpLcr.Subject,
			}
		}
		var act *LCRActivation
		for _, existingAct := range lcr.Activations {
			if existingAct.ActivationTime.Equal(activationTime) {
				act = existingAct
				break
			}
		}
		if act == nil {
			act = &LCRActivation{
				ActivationTime: activationTime,
			}
			lcr.Activations = append(lcr.Activations, act)
		}
		act.Entries = append(act.Entries, &LCREntry{
			DestinationId:  tpLcr.DestinationTag,
			RPCategory:     tpLcr.RpCategory,
			Strategy:       tpLcr.Strategy,
			StrategyParams: tpLcr.StrategyParams,
			Weight:         tpLcr.Weight,
		})
		tpr.lcrs[tag] = lcr
	}
	return nil
}