// Queries specific RatingProfile on tariff plan func (self *ApierV1) GetTPRatingProfilesByLoadId(attrs utils.TPRatingProfile, reply *[]*utils.TPRatingProfile) error { mndtryFlds := []string{"TPid", "LoadId"} if len(attrs.Subject) != 0 { // If Subject provided as filter, make all related fields mandatory mndtryFlds = append(mndtryFlds, "Tenant", "TOR", "Direction", "Subject") } if missing := utils.MissingStructFields(&attrs, mndtryFlds); len(missing) != 0 { //Params missing return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } if dr, err := self.StorDb.GetTpRatingProfiles(&attrs); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } else if dr == nil { return errors.New(utils.ERR_NOT_FOUND) } else { var rpfs []*utils.TPRatingProfile if len(attrs.Subject) != 0 { rpfs = []*utils.TPRatingProfile{dr[attrs.KeyId()]} } else { for _, rpfLst := range dr { rpfs = append(rpfs, rpfLst) } } *reply = rpfs } return nil }
// Queries specific RatingProfile on tariff plan func (self *ApierV1) GetTPRatingProfilesByLoadId(attrs utils.TPRatingProfile, reply *[]*utils.TPRatingProfile) error { mndtryFlds := []string{"TPid", "LoadId"} if len(attrs.Subject) != 0 { // If Subject provided as filter, make all related fields mandatory mndtryFlds = append(mndtryFlds, "Tenant", "TOR", "Direction", "Subject") } if missing := utils.MissingStructFields(&attrs, mndtryFlds); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) } rpf := engine.APItoModelRatingProfile(&attrs) if dr, err := self.StorDb.GetTpRatingProfiles(&rpf[0]); err != nil { return utils.NewErrServerError(err) } else if dr == nil { return utils.ErrNotFound } else { rpfMap, err := engine.TpRatingProfiles(dr).GetRatingProfiles() if err != nil { return err } var rpfs []*utils.TPRatingProfile if len(attrs.Subject) != 0 { rpfs = []*utils.TPRatingProfile{rpfMap[attrs.KeyId()]} } else { for _, rpfLst := range rpfMap { rpfs = append(rpfs, rpfLst) } } *reply = rpfs } return nil }
// Sets a specific rating profile working with data directly in the RatingDb without involving storDb func (self *ApierV1) SetRatingProfile(attrs AttrSetRatingProfile, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "TOR", "Direction", "Subject", "RatingPlanActivations"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } for _, rpa := range attrs.RatingPlanActivations { if missing := utils.MissingStructFields(rpa, []string{"ActivationTime", "RatingPlanId"}); len(missing) != 0 { return fmt.Errorf("%s:RatingPlanActivation:%v", utils.ErrMandatoryIeMissing.Error(), missing) } } tpRpf := utils.TPRatingProfile{Tenant: attrs.Tenant, Category: attrs.Category, Direction: attrs.Direction, Subject: attrs.Subject} keyId := tpRpf.KeyId() var rpfl *engine.RatingProfile if !attrs.Overwrite { if exists, err := self.RatingDb.HasData(utils.RATING_PROFILE_PREFIX, keyId); err != nil { return utils.NewErrServerError(err) } else if exists { var err error if rpfl, err = self.RatingDb.GetRatingProfile(keyId, false); err != nil { return utils.NewErrServerError(err) } } } if rpfl == nil { rpfl = &engine.RatingProfile{Id: keyId, RatingPlanActivations: make(engine.RatingPlanActivations, 0)} } for _, ra := range attrs.RatingPlanActivations { at, err := utils.ParseTimeDetectLayout(ra.ActivationTime, self.Config.DefaultTimezone) if err != nil { return fmt.Errorf(fmt.Sprintf("%s:Cannot parse activation time from %v", utils.ErrServerError.Error(), ra.ActivationTime)) } if exists, err := self.RatingDb.HasData(utils.RATING_PLAN_PREFIX, ra.RatingPlanId); err != nil { return utils.NewErrServerError(err) } else if !exists { return fmt.Errorf(fmt.Sprintf("%s:RatingPlanId:%s", utils.ErrNotFound.Error(), ra.RatingPlanId)) } rpfl.RatingPlanActivations = append(rpfl.RatingPlanActivations, &engine.RatingPlanActivation{ActivationTime: at, RatingPlanId: ra.RatingPlanId, FallbackKeys: utils.FallbackSubjKeys(tpRpf.Direction, tpRpf.Tenant, tpRpf.Category, ra.FallbackSubjects)}) } if err := self.RatingDb.SetRatingProfile(rpfl); err != nil { return utils.NewErrServerError(err) } //Automatic cache of the newly inserted rating profile if err := self.RatingDb.CacheRatingPrefixValues(map[string][]string{ utils.RATING_PROFILE_PREFIX: []string{utils.RATING_PROFILE_PREFIX + keyId}, }); err != nil { return err } *reply = OK return nil }
// Sets a specific rating profile working with data directly in the RatingDb without involving storDb func (self *ApierV1) SetRatingProfile(attrs AttrSetRatingProfile, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"Tenant", "TOR", "Direction", "Subject", "RatingPlanActivations"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } for _, rpa := range attrs.RatingPlanActivations { if missing := utils.MissingStructFields(rpa, []string{"ActivationTime", "RatingPlanId"}); len(missing) != 0 { return fmt.Errorf("%s:RatingPlanActivation:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } } tpRpf := utils.TPRatingProfile{Tenant: attrs.Tenant, Category: attrs.Category, Direction: attrs.Direction, Subject: attrs.Subject} keyId := tpRpf.KeyId() if !attrs.Overwrite { if exists, err := self.RatingDb.HasData(engine.RATING_PROFILE_PREFIX, keyId); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } else if exists { return errors.New(utils.ERR_EXISTS) } } rpfl := &engine.RatingProfile{Id: keyId, RatingPlanActivations: make(engine.RatingPlanActivations, len(attrs.RatingPlanActivations))} for idx, ra := range attrs.RatingPlanActivations { at, err := utils.ParseDate(ra.ActivationTime) if err != nil { return fmt.Errorf(fmt.Sprintf("%s:Cannot parse activation time from %v", utils.ERR_SERVER_ERROR, ra.ActivationTime)) } if exists, err := self.RatingDb.HasData(engine.RATING_PLAN_PREFIX, ra.RatingPlanId); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } else if !exists { return fmt.Errorf(fmt.Sprintf("%s:RatingPlanId:%s", utils.ERR_NOT_FOUND, ra.RatingPlanId)) } rpfl.RatingPlanActivations[idx] = &engine.RatingPlanActivation{ActivationTime: at, RatingPlanId: ra.RatingPlanId, FallbackKeys: utils.FallbackSubjKeys(tpRpf.Direction, tpRpf.Tenant, tpRpf.Category, ra.FallbackSubjects)} } if err := self.RatingDb.SetRatingProfile(rpfl); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } //Automatic cache of the newly inserted rating profile didNotChange := []string{} if err := self.RatingDb.CacheRating(didNotChange, didNotChange, []string{engine.RATING_PROFILE_PREFIX + keyId}, didNotChange, didNotChange); err != nil { return err } *reply = OK return nil }
// Process dependencies and load a specific rating profile from storDb into dataDb. func (self *ApierV1) LoadRatingProfile(attrs utils.TPRatingProfile, reply *string) error { if len(attrs.TPid) == 0 { return utils.NewErrMandatoryIeMissing("TPid") } dbReader := engine.NewTpReader(self.RatingDb, self.AccountDb, self.StorDb, attrs.TPid, self.Config.DefaultTimezone, self.Config.LoadHistorySize) rp := engine.APItoModelRatingProfile(&attrs) if err := dbReader.LoadRatingProfilesFiltered(&rp[0]); err != nil { return utils.NewErrServerError(err) } //Automatic cache of the newly inserted rating profile var ratingProfile []string if attrs.KeyId() != ":::" { // if has some filters ratingProfile = []string{utils.RATING_PROFILE_PREFIX + attrs.KeyId()} } if err := self.RatingDb.CacheRatingPrefixValues(map[string][]string{utils.RATING_PROFILE_PREFIX: ratingProfile}); err != nil { return err } *reply = OK return nil }
// Process dependencies and load a specific rating profile from storDb into dataDb. func (self *ApierV1) LoadRatingProfile(attrs utils.TPRatingProfile, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LoadId", "Tenant", "Category", "Direction", "Subject"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } if attrs.LoadId == utils.EMPTY { attrs.LoadId = "" } if attrs.Tenant == utils.EMPTY { attrs.Tenant = "" } if attrs.Category == utils.EMPTY { attrs.Category = "" } if attrs.Direction == utils.EMPTY { attrs.Direction = "" } if attrs.Subject == utils.EMPTY { attrs.Subject = "" } dbReader := engine.NewDbReader(self.StorDb, self.RatingDb, self.AccountDb, attrs.TPid) if err := dbReader.LoadRatingProfileFiltered(&attrs); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } //Automatic cache of the newly inserted rating profile didNotChange := []string{} var ratingProfile []string if attrs.KeyId() != ":::" { // if has some filters ratingProfile = []string{engine.RATING_PROFILE_PREFIX + attrs.KeyId()} } if err := self.RatingDb.CacheRating(didNotChange, didNotChange, ratingProfile, didNotChange, didNotChange); err != nil { return err } *reply = OK return nil }
// Creates a new RatingProfile within a tariff plan func (self *ApierV1) SetTPRatingProfile(attrs utils.TPRatingProfile, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LoadId", "Tenant", "Category", "Direction", "Subject", "RatingPlanActivations"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } if err := self.StorDb.SetTPRatingProfiles(attrs.TPid, map[string]*utils.TPRatingProfile{attrs.KeyId(): &attrs}); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } *reply = "OK" return nil }