Exemplo n.º 1
0
func (self *TPCSVImporter) importDerivedChargers(fn string) error {
	if self.Verbose {
		log.Printf("Processing file: <%s> ", fn)
	}
	fParser, err := NewTPCSVFileParser(self.DirPath, fn)
	if err != nil {
		return err
	}
	loadId := utils.CSV_LOAD //Autogenerate account actions profile id
	if self.ImportId != "" {
		loadId += "_" + self.ImportId
	}
	dcs := make(map[string][]*utils.TPDerivedCharger)
	lineNr := 0
	for {
		lineNr++
		record, err := fParser.ParseNextLine()
		if err == io.EOF { // Reached end of file
			break
		} else if err != nil {
			if self.Verbose {
				log.Printf("Ignoring line %d, warning: <%s> ", lineNr, err.Error())
			}
			continue
		}
		newDcs := utils.TPDerivedChargers{TPid: self.TPid,
			Loadid:    loadId,
			Direction: record[0],
			Tenant:    record[1],
			Category:  record[2],
			Account:   record[3],
			Subject:   record[4]}
		dcsId := newDcs.GetDerivedChargesId()

		if _, hasIt := dcs[dcsId]; !hasIt {
			dcs[dcsId] = make([]*utils.TPDerivedCharger, 0)
		}
		dcs[dcsId] = append(dcs[dcsId], &utils.TPDerivedCharger{
			RunId:            ValueOrDefault(record[5], "*default"),
			RunFilters:       record[6],
			ReqTypeField:     ValueOrDefault(record[7], "*default"),
			DirectionField:   ValueOrDefault(record[8], "*default"),
			TenantField:      ValueOrDefault(record[9], "*default"),
			CategoryField:    ValueOrDefault(record[10], "*default"),
			AccountField:     ValueOrDefault(record[11], "*default"),
			SubjectField:     ValueOrDefault(record[12], "*default"),
			DestinationField: ValueOrDefault(record[13], "*default"),
			SetupTimeField:   ValueOrDefault(record[14], "*default"),
			AnswerTimeField:  ValueOrDefault(record[15], "*default"),
			UsageField:       ValueOrDefault(record[16], "*default"),
		})
	}
	if err := self.StorDb.SetTPDerivedChargers(self.TPid, dcs); err != nil {
		if self.Verbose {
			log.Printf("Ignoring line %d, storDb operational error: <%s> ", lineNr, err.Error())
		}
	}
	return nil
}
Exemplo n.º 2
0
// Load derived chargers from storDb into dataDb.
func (self *ApierV1) LoadDerivedChargers(attrs utils.TPDerivedChargers, 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)
	dc := engine.APItoModelDerivedCharger(&attrs)
	if err := dbReader.LoadDerivedChargersFiltered(&dc[0], true); err != nil {
		return utils.NewErrServerError(err)
	}
	//Automatic cache of the newly inserted rating plan
	var derivedChargingKeys []string
	if len(attrs.Direction) != 0 && len(attrs.Tenant) != 0 && len(attrs.Category) != 0 && len(attrs.Account) != 0 && len(attrs.Subject) != 0 {
		derivedChargingKeys = []string{utils.DERIVEDCHARGERS_PREFIX + attrs.GetDerivedChargersKey()}
	}
	if err := self.RatingDb.CacheRatingPrefixValues(map[string][]string{utils.DERIVEDCHARGERS_PREFIX: derivedChargingKeys}); err != nil {
		return err
	}
	*reply = OK
	return nil
}
Exemplo n.º 3
0
// Creates a new DerivedCharges profile within a tariff plan
func (self *ApierV1) SetTPDerivedChargers(attrs utils.TPDerivedChargers, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"TPid", "Direction", "Tenant", "Category", "Account", "Subject"}); len(missing) != 0 {
		return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
	}
	/*for _, action := range attrs.DerivedCharges {
		requiredFields := []string{"Identifier", "Weight"}
		if action.BalanceType != "" { // Add some inter-dependent parameters - if balanceType then we are not talking about simply calling actions
			requiredFields = append(requiredFields, "Direction", "Units")
		}
		if missing := utils.MissingStructFields(action, requiredFields); len(missing) != 0 {
			return fmt.Errorf("%s:DerivedCharge:%s:%v", utils.ERR_MANDATORY_IE_MISSING, action.Identifier, missing)
		}
	}*/
	if err := self.StorDb.SetTPDerivedChargers(attrs.TPid, map[string][]*utils.TPDerivedCharger{
		attrs.GetDerivedChargesId(): attrs.DerivedChargers,
	}); err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	*reply = "OK"
	return nil
}
Exemplo n.º 4
0
// Load derived chargers from storDb into dataDb.
func (self *ApierV1) LoadDerivedChargers(attrs utils.TPDerivedChargers, reply *string) error {
	if missing := utils.MissingStructFields(&attrs, []string{"TPid", "LoadId", "Tenant", "Category", "Direction", "Account", "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.Account == utils.EMPTY {
		attrs.Account = ""
	}
	if attrs.Subject == utils.EMPTY {
		attrs.Subject = ""
	}
	dbReader := engine.NewDbReader(self.StorDb, self.RatingDb, self.AccountDb, attrs.TPid)
	if err := dbReader.LoadDerivedChargersFiltered(&attrs); err != nil {
		return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
	}
	//Automatic cache of the newly inserted rating plan
	didNotChange := []string{}
	if err := self.AccountDb.CacheAccounting(didNotChange, didNotChange, didNotChange, nil); err != nil {
		return err
	}
	*reply = OK
	return nil
}