func TestLoadRpAliases(t *testing.T) { if len(csvr.rpAliases) != 3 { t.Error("Failed to load rp aliases: ", csvr.rpAliases) } if csvr.rpAliases[utils.RatingSubjectAliasKey("vdf", "a1")] != "minu" || csvr.rpAliases[utils.RatingSubjectAliasKey("vdf", "a2")] != "minu" || csvr.rpAliases[utils.RatingSubjectAliasKey("vdf", "a3")] != "minu" { t.Error("Error loading rp aliases: ", csvr.rpAliases) } }
// Constructs the key for the storage lookup. // The prefixLen is limiting the length of the destination prefix. func (cd *CallDescriptor) GetKey(subject string) string { // check if subject is alias if rs, err := cache2go.GetCached(RP_ALIAS_PREFIX + utils.RatingSubjectAliasKey(cd.Tenant, subject)); err == nil { realSubject := rs.(string) subject = realSubject cd.Subject = realSubject } return fmt.Sprintf("%s:%s:%s:%s", cd.Direction, cd.Tenant, cd.Category, subject) }
// Retrieve aliases configured for a rating profile subject func (self *ApierV1) AddRatingSubjectAliases(attrs AttrAddRatingSubjectAliases, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "Subject", "Aliases"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } aliasesChanged := []string{} for _, alias := range attrs.Aliases { if err := self.RatingDb.SetRpAlias(utils.RatingSubjectAliasKey(attrs.Tenant, alias), attrs.Subject); err != nil { return fmt.Errorf("%s:%s:%s", utils.ERR_SERVER_ERROR, alias, err.Error()) } aliasesChanged = append(aliasesChanged, engine.RP_ALIAS_PREFIX+utils.RatingSubjectAliasKey(attrs.Tenant, alias)) } didNotChange := []string{} if err := self.RatingDb.CacheRating(didNotChange, didNotChange, didNotChange, aliasesChanged, didNotChange); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } *reply = utils.OK return nil }
func TestGetRPAliases(t *testing.T) { if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } expectAliases := sort.StringSlice([]string{"2001", "2002"}) expectAliases.Sort() if aliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", true); err != nil { t.Error(err) } else { aliases := sort.StringSlice(aliases) aliases.Sort() if !reflect.DeepEqual(aliases, expectAliases) { t.Errorf("Expecting: %v, received: %v", expectAliases, aliases) } } }
func (csvr *CSVReader) LoadRatingProfiles() (err error) { csvReader, fp, err := csvr.readerFunc(csvr.ratingprofilesFn, csvr.sep, utils.RATE_PROFILES_NRCOLS) if err != nil { log.Print("Could not load rating profiles file: ", err) // allow writing of the other values return nil } if fp != nil { defer fp.Close() } for record, err := csvReader.Read(); err == nil; record, err = csvReader.Read() { direction, tenant, tor, subject, fallbacksubject := record[0], record[1], record[2], record[3], record[6] at, err := utils.ParseDate(record[4]) if err != nil { return fmt.Errorf("Cannot parse activation time from %v", record[4]) } // extract aliases from subject aliases := strings.Split(subject, ";") csvr.dirtyRpAliases = append(csvr.dirtyRpAliases, &TenantRatingSubject{Tenant: tenant, Subject: aliases[0]}) if len(aliases) > 1 { subject = aliases[0] for _, alias := range aliases[1:] { csvr.rpAliases[utils.RatingSubjectAliasKey(tenant, alias)] = subject } } key := fmt.Sprintf("%s:%s:%s:%s", direction, tenant, tor, subject) rp, ok := csvr.ratingProfiles[key] if !ok { rp = &RatingProfile{Id: key} csvr.ratingProfiles[key] = rp } _, exists := csvr.ratingPlans[record[5]] if !exists && csvr.dataStorage != nil { if exists, err = csvr.dataStorage.HasData(RATING_PLAN_PREFIX, record[5]); err != nil { return err } } if !exists { return fmt.Errorf("Could not load rating plans for tag: %v", record[5]) } rpa := &RatingPlanActivation{ ActivationTime: at, RatingPlanId: record[5], FallbackKeys: utils.FallbackSubjKeys(direction, tenant, tor, fallbacksubject), } rp.RatingPlanActivations = append(rp.RatingPlanActivations, rpa) csvr.ratingProfiles[rp.Id] = rp } return }
func TestRemRSubjAliases(t *testing.T) { if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } if err := dataStorage.RemoveRpAliases([]*TenantRatingSubject{&TenantRatingSubject{Tenant: "cgrates.org", Subject: "1001"}}); err != nil { t.Error(err) } if cgrAliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", true); err != nil { t.Error(err) } else if len(cgrAliases) != 0 { t.Error("Subject aliases not removed: ", cgrAliases) } if iscAliases, err := dataStorage.GetRPAliases("itsyscom.com", "1001", true); err != nil { // Make sure the aliases were removed at tenant level t.Error(err) } else if !reflect.DeepEqual(iscAliases, []string{"2003"}) { t.Errorf("Unexpected aliases: %v", iscAliases) } }
func (dbr *DbReader) LoadRatingProfiles() error { mpTpRpfs, err := dbr.storDb.GetTpRatingProfiles(&utils.TPRatingProfile{TPid: dbr.tpid}) //map[string]*utils.TPRatingProfile if err != nil { return err } for _, tpRpf := range mpTpRpfs { // extract aliases from subject aliases := strings.Split(tpRpf.Subject, ";") dbr.dirtyRpAliases = append(dbr.dirtyRpAliases, &TenantRatingSubject{Tenant: tpRpf.Tenant, Subject: aliases[0]}) if len(aliases) > 1 { tpRpf.Subject = aliases[0] for _, alias := range aliases[1:] { dbr.rpAliases[utils.RatingSubjectAliasKey(tpRpf.Tenant, alias)] = tpRpf.Subject } } rpf := &RatingProfile{Id: tpRpf.KeyId()} for _, tpRa := range tpRpf.RatingPlanActivations { at, err := utils.ParseDate(tpRa.ActivationTime) if err != nil { return fmt.Errorf("Cannot parse activation time from %v", tpRa.ActivationTime) } _, exists := dbr.ratingPlans[tpRa.RatingPlanId] if !exists { if dbExists, err := dbr.dataDb.HasData(RATING_PLAN_PREFIX, tpRa.RatingPlanId); err != nil { return err } else if !dbExists { return fmt.Errorf("Could not load rating plans for tag: %v", tpRa.RatingPlanId) } } rpf.RatingPlanActivations = append(rpf.RatingPlanActivations, &RatingPlanActivation{ ActivationTime: at, RatingPlanId: tpRa.RatingPlanId, FallbackKeys: utils.FallbackSubjKeys(tpRpf.Direction, tpRpf.Tenant, tpRpf.Category, tpRa.FallbackSubjects), }) } dbr.ratingProfiles[tpRpf.KeyId()] = rpf } return nil }
func TestCacheAliases(t *testing.T) { if subj, err := cache2go.GetCached(RP_ALIAS_PREFIX + utils.RatingSubjectAliasKey("vdf", "a3")); err == nil && subj != "minu" { t.Error("Error caching alias: ", subj, err) } }