diff --git a/apier/v1/apier.go b/apier/v1/apier.go index deaf9703f..04303cbe6 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -156,11 +156,11 @@ func (self *ApierV1) SetRatingPlan(attrs AttrSetRatingPlan, reply *string) error return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } dbReader := engine.NewDbReader(self.StorDb, self.DataDb, attrs.TPid) - - if err := dbReader.LoadRatingPlanByTag(attrs.RatingPlanId); err != nil { + if loaded, err := dbReader.LoadRatingPlanByTag(attrs.RatingPlanId); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } else if !loaded { + return errors.New("NOT_FOUND") } - *reply = OK return nil } diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index 9857657d0..db9956de3 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -729,3 +729,11 @@ func TestApierLoadTariffPlanFromFolder(t *testing.T) { t.Error("Calling ApierV1.LoadTariffPlanFromFolder got reply: ", reply) } } + +// Simply kill the engine after we are done with tests within this file +func TestStopEngine(t *testing.T) { + if !*testLocal { + return + } + exec.Command("pkill", "cgr-engine").Run() +} diff --git a/console/add_balance.go b/console/add_balance.go index a5fa59c2a..1a5a18d30 100644 --- a/console/add_balance.go +++ b/console/add_balance.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/console/add_triggeredaction.go b/console/add_triggeredaction.go index 27a49d273..3f9e38d0a 100644 --- a/console/add_triggeredaction.go +++ b/console/add_triggeredaction.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/console/command.go b/console/command.go index 770666157..18371f309 100644 --- a/console/command.go +++ b/console/command.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/console/destination.go b/console/destination.go index c02bb111a..30fb9b0e1 100644 --- a/console/destination.go +++ b/console/destination.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/console/execute_action.go b/console/execute_action.go index d61cdab85..821e6be83 100644 --- a/console/execute_action.go +++ b/console/execute_action.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/console/get_balance.go b/console/get_balance.go index f1c86e38c..9774c1cc8 100644 --- a/console/get_balance.go +++ b/console/get_balance.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/console/set_accountactions.go b/console/set_accountactions.go index aa3241dc3..ae0046efc 100644 --- a/console/set_accountactions.go +++ b/console/set_accountactions.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -52,7 +52,7 @@ func (self *CmdSetAccountActions) FromArgs(args []string) error { } // Args look OK, set defaults before going further self.defaults() - self.rpcParams = &utils.TPAccountActions{TPid: args[2], LoadId: args[3], Tenant: args[4], Account: args[5], Direction:"*out"} + self.rpcParams = &utils.TPAccountActions{TPid: args[2], LoadId: args[3], Tenant: args[4], Account: args[5], Direction: "*out"} return nil } diff --git a/console/set_ratingprofile.go b/console/set_ratingprofile.go index 1c30520e4..52277344c 100644 --- a/console/set_ratingprofile.go +++ b/console/set_ratingprofile.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -52,7 +52,7 @@ func (self *CmdSetrRatingProfile) FromArgs(args []string) error { } // Args look OK, set defaults before going further self.defaults() - self.rpcParams = &utils.TPRatingProfile{TPid: args[2], LoadId: args[3], Tenant: args[4], TOR: args[5], Direction: "*out"} + self.rpcParams = &utils.TPRatingProfile{TPid: args[2], LoadId: args[3], Tenant: args[4], TOR: args[5], Direction: "*out", Subject: args[6]} return nil } diff --git a/console/status.go b/console/status.go index cac4a8c72..1a2a25b78 100644 --- a/console/status.go +++ b/console/status.go @@ -1,6 +1,6 @@ /* Rating system designed to be used in VoIP Carriers World -Copyright (C) 2013 ITsysCOM +Copyright (C) 2013 ITsysCOM This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/engine/loader_db.go b/engine/loader_db.go index 6e0833a89..6aa919dba 100644 --- a/engine/loader_db.go +++ b/engine/loader_db.go @@ -241,10 +241,13 @@ func (dbr *DbReader) LoadRatingProfiles() error { return nil } -func (dbr *DbReader) LoadRatingPlanByTag(tag string) error { +// Returns true, nil in case of load success, false, nil in case of RatingPlan not found in storDb +func (dbr *DbReader) LoadRatingPlanByTag(tag string) (bool, error) { mpRpls, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, tag) - if err != nil || len(mpRpls) == 0 { - return fmt.Errorf("No RatingPlan profile with id %s: %v", tag, err) + if err != nil { + return false, err + } else if len(mpRpls) == 0 { + return false, nil } for tag, rplBnds := range mpRpls { ratingPlan := &RatingPlan{Id: tag} @@ -253,18 +256,18 @@ func (dbr *DbReader) LoadRatingPlanByTag(tag string) error { tm, err := dbr.storDb.GetTpTimings(dbr.tpid, rp.TimingId) Logger.Debug(fmt.Sprintf("Timing: %v", tm)) if err != nil || len(tm) == 0 { - return fmt.Errorf("No Timings profile with id %s: %v", rp.TimingId, err) + return false, fmt.Errorf("No Timings profile with id %s: %v", rp.TimingId, err) } rp.SetTiming(tm[rp.TimingId]) drm, err := dbr.storDb.GetTpDestinationRates(dbr.tpid, rp.DestinationRatesId) if err != nil || len(drm) == 0 { - return fmt.Errorf("No DestinationRates profile with id %s: %v", rp.DestinationRatesId, err) + return false, fmt.Errorf("No DestinationRates profile with id %s: %v", rp.DestinationRatesId, err) } for _, drate := range drm[rp.DestinationRatesId].DestinationRates { Logger.Debug(fmt.Sprintf("Destination rate: %v", drate)) rt, err := dbr.storDb.GetTpRates(dbr.tpid, drate.RateId) if err != nil || len(rt) == 0 { - return fmt.Errorf("No Rates profile with id %s: %v", drate.RateId, err) + return false, fmt.Errorf("No Rates profile with id %s: %v", drate.RateId, err) } Logger.Debug(fmt.Sprintf("Rate: %v", rt)) drate.Rate = rt[drate.RateId] @@ -272,12 +275,12 @@ func (dbr *DbReader) LoadRatingPlanByTag(tag string) error { dms, err := dbr.storDb.GetTpDestinations(dbr.tpid, drate.DestinationId) if err != nil { - return err + return false, err } else if len(dms) == 0 { if dbExists, err := dbr.dataDb.ExistsData(DESTINATION_PREFIX, drate.DestinationId); err != nil { - return err + return false, err } else if !dbExists { - return fmt.Errorf("Could not get destination for tag %v", drate.DestinationId) + return false, fmt.Errorf("Could not get destination for tag %v", drate.DestinationId) } continue } @@ -289,10 +292,10 @@ func (dbr *DbReader) LoadRatingPlanByTag(tag string) error { } } if err := dbr.dataDb.SetRatingPlan(ratingPlan); err != nil { - return err + return false, err } } - return nil + return true, nil } func (dbr *DbReader) LoadRatingProfileFiltered(qriedRpf *utils.TPRatingProfile) error { @@ -310,11 +313,15 @@ func (dbr *DbReader) LoadRatingProfileFiltered(qriedRpf *utils.TPRatingProfile) return errors.New(fmt.Sprintf("Cannot parse activation time from %v", tpRa.ActivationTime)) } _, exists := dbr.ratingPlans[tpRa.RatingPlanId] - if !exists { - if dbExists, err := dbr.dataDb.ExistsData(RATING_PLAN_PREFIX, tpRa.RatingPlanId); err != nil { + if !exists { // Try loading locally, on errrors, give up + if loaded, err := dbr.LoadRatingPlanByTag(tpRa.RatingPlanId); err != nil { return err - } else if !dbExists { - return errors.New(fmt.Sprintf("Could not load rating plans for tag: %v", tpRa.RatingPlanId)) + } else if !loaded { // Not found + if dbExists, err := dbr.dataDb.ExistsData(RATING_PLAN_PREFIX, tpRa.RatingPlanId); err != nil { + return err + } else if !dbExists { + return errors.New(fmt.Sprintf("Could not load rating plans for tag: %v", tpRa.RatingPlanId)) + } } } resultRatingProfile.RatingPlanActivations = append(resultRatingProfile.RatingPlanActivations, diff --git a/engine/loader_local_test.go b/engine/loader_local_test.go index e5bf87eee..4e582ea9f 100644 --- a/engine/loader_local_test.go +++ b/engine/loader_local_test.go @@ -219,8 +219,10 @@ func TestLoadIndividualProfiles(t *testing.T) { t.Fatal("Could not retrieve rating plans") } else { for tag := range ratingPlans { - if err := loader.LoadRatingPlanByTag(tag); err != nil { + if loaded, err := loader.LoadRatingPlanByTag(tag); err != nil { t.Fatalf("Could not load ratingPlan for tag: %s, error: %s", tag, err.Error()) + } else if !loaded { + t.Fatal("Cound not find ratingPLan with id:", tag) } } } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 3bfb77fcd..ceed1941f 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -215,6 +215,9 @@ func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { result, err := rs.ms.Marshal(dest) + if err != nil { + return err + } var b bytes.Buffer w := zlib.NewWriter(&b) w.Write(result)