diff --git a/apier/apier.go b/apier/apier.go index d9a9792de..0e2e0c5f8 100644 --- a/apier/apier.go +++ b/apier/apier.go @@ -18,6 +18,11 @@ along with this program. If not, see package apier +import ( + "github.com/cgrates/cgrates/rater" +) + type Apier struct { + StorDb rater.DataStorage } diff --git a/apier/tpdestinations.go b/apier/tpdestinations.go new file mode 100644 index 000000000..1efbfd6c7 --- /dev/null +++ b/apier/tpdestinations.go @@ -0,0 +1,49 @@ +/* +Rating system designed to be used in VoIP Carriers World +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package apier + + +import ( + "fmt" + "errors" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/cgrates/rater" +) + +type AttrGetTPDestinations struct { + TPid string + DestinationsTag string +} + + +// Return destinations profile for a destination tag received as parameter +func (self *Apier) GetTPDestinations( attrs AttrGetTPDestinations, reply *rater.Destination) error { + if missing := utils.MissingStructFields(&attrs, []string{"TPid","DestinationsTag"}); len(missing) != 0 { //Params missing + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if dst, err := self.StorDb.GetTPDestination( attrs.TPid, attrs.DestinationsTag ); err!= nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } else if dst == nil { + return errors.New(utils.ERR_DST_NOT_FOUND) + } else { + reply = dst + } + return nil +} + diff --git a/cmd/cgr-rater/cgr-rater.go b/cmd/cgr-rater/cgr-rater.go index e892902aa..7009fe170 100644 --- a/cmd/cgr-rater/cgr-rater.go +++ b/cmd/cgr-rater/cgr-rater.go @@ -64,7 +64,7 @@ var ( err error ) -func listenToRPCRequests(rpcResponder interface{}, rpcAddress string, rpc_encoding string) { +func listenToRPCRequests(rpcResponder interface{}, rpcAddress string, rpc_encoding string, loggerDb rater.DataStorage) { l, err := net.Listen("tcp", rpcAddress) if err != nil { rater.Logger.Crit(fmt.Sprintf(" Could not listen to %v: %v", rpcAddress, err)) @@ -75,7 +75,7 @@ func listenToRPCRequests(rpcResponder interface{}, rpcAddress string, rpc_encodi rater.Logger.Info(fmt.Sprintf(" Listening for incomming RPC requests on %v", l.Addr())) rpc.Register(rpcResponder) - rpc.Register(&apier.Apier{}) + rpc.Register(&apier.Apier{StorDb:loggerDb}) var serveFunc func(io.ReadWriteCloser) if rpc_encoding == JSON { serveFunc = jsonrpc.ServeConn @@ -298,13 +298,13 @@ func main() { responder := &rater.Responder{ExitChan: exitChan} if cfg.RaterEnabled && !cfg.BalancerEnabled && cfg.RaterListen != INTERNAL { rater.Logger.Info(fmt.Sprintf("Starting CGRateS Rater on %s.", cfg.RaterListen)) - go listenToRPCRequests(responder, cfg.RaterListen, cfg.RPCEncoding) + go listenToRPCRequests(responder, cfg.RaterListen, cfg.RPCEncoding, loggerDb) } if cfg.BalancerEnabled { rater.Logger.Info(fmt.Sprintf("Starting CGRateS Balancer on %s.", cfg.BalancerListen)) go stopBalancerSingnalHandler() responder.Bal = bal - go listenToRPCRequests(responder, cfg.BalancerListen, cfg.RPCEncoding) + go listenToRPCRequests(responder, cfg.BalancerListen, cfg.RPCEncoding, loggerDb) if cfg.RaterEnabled { rater.Logger.Info("Starting internal rater.") bal.AddClient("local", new(rater.ResponderWorker)) diff --git a/rater/storage_gosexy.go b/rater/storage_gosexy.go index 17a4f61ad..1bf3fc0b4 100644 --- a/rater/storage_gosexy.go +++ b/rater/storage_gosexy.go @@ -101,6 +101,11 @@ func (rs *GosexyStorage) SetDestination(dest *Destination) (err error) { return } +// Extracts destinations from StorDB on specific tariffplan id +func (rs *GosexyStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + return nil, nil +} + func (rs *GosexyStorage) GetActions(key string) (as Actions, err error) { var values string if values, err = rs.db.Get(ACTION_PREFIX + key); err == nil { diff --git a/rater/storage_interface.go b/rater/storage_interface.go index 89cf54b64..9c2efe94f 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -57,6 +57,7 @@ type DataStorage interface { SetRatingProfile(*RatingProfile) error GetDestination(string) (*Destination, error) SetDestination(*Destination) error + GetTPDestination(string,string) (*Destination, error) GetActions(string) (Actions, error) SetActions(string, Actions) error GetUserBalance(string) (*UserBalance, error) diff --git a/rater/storage_map.go b/rater/storage_map.go index 68be5b5de..852195fb1 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -73,6 +73,12 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { return } +// Extracts destinations from StorDB on specific tariffplan id +func (ms *MapStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + return nil, nil +} + + func (ms *MapStorage) GetActions(key string) (as Actions, err error) { if values, ok := ms.dict[ACTION_PREFIX+key]; ok { err = ms.ms.Unmarshal(values, &as) diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index 0748512ee..5c63e2496 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -146,6 +146,11 @@ func (ms *MongoStorage) SetDestination(dest *Destination) error { return ms.db.C("destinations").Insert(dest) } +// Extracts destinations from StorDB on specific tariffplan id +func (ms *MongoStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + return nil, nil +} + func (ms *MongoStorage) GetActions(key string) (as Actions, err error) { result := AcKeyValue{} err = ms.db.C("actions").Find(bson.M{"key": key}).One(&result) diff --git a/rater/storage_mysql.go b/rater/storage_mysql.go index 93e378b30..096b68733 100644 --- a/rater/storage_mysql.go +++ b/rater/storage_mysql.go @@ -63,6 +63,31 @@ func (mys *MySQLStorage) SetDestination(d *Destination) (err error) { return } +// Extracts destinations from StorDB on specific tariffplan id +func (mys *MySQLStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + rows,err := mys.Db.Query(fmt.Sprintf("SELECT prefix FROM tp_destinatins WHERE id='%s' AND tag='%s'", tpid, destTag)) + if err != nil { + return nil, err + } + defer rows.Close() + d := &Destination{Id:destTag} + i := 0 + for rows.Next() { + i++ //Keep here a reference so we know we got at least one prefix + var pref string + err = rows.Scan( &pref ) + if err != nil { + return nil, err + } + d.Prefixes = append( d.Prefixes, pref ) + } + if i == 0 { + return nil, nil + } + return d, nil +} + + func (mys *MySQLStorage) GetActions(string) (as Actions, err error) { return } diff --git a/rater/storage_postgres.go b/rater/storage_postgres.go index 554ed2c2f..e305d5417 100644 --- a/rater/storage_postgres.go +++ b/rater/storage_postgres.go @@ -63,6 +63,11 @@ func (psl *PostgresStorage) SetDestination(d *Destination) (err error) { return } +// Extracts destinations from StorDB on specific tariffplan id +func (psl *PostgresStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + return nil, nil +} + func (psl *PostgresStorage) GetActions(string) (as Actions, err error) { return } diff --git a/rater/storage_redigo.go b/rater/storage_redigo.go index 06bc54d3e..d4e1a59fb 100644 --- a/rater/storage_redigo.go +++ b/rater/storage_redigo.go @@ -93,6 +93,11 @@ func (rs *RedigoStorage) SetDestination(dest *Destination) (err error) { return } +// Extracts destinations from StorDB on specific tariffplan id +func (rs *RedigoStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + return nil, nil +} + func (rs *RedigoStorage) GetActions(key string) (as Actions, err error) { var values []byte if values, err = redis.Bytes(rs.db.Do("get", ACTION_PREFIX+key)); err == nil { diff --git a/rater/storage_redis.go b/rater/storage_redis.go index 001dbc892..3267648e9 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -107,6 +107,11 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { return } +// Extracts destinations from StorDB on specific tariffplan id +func (rs *RedisStorage) GetTPDestination( tpid, destTag string ) (*Destination, error) { + return nil, nil +} + func (rs *RedisStorage) GetActions(key string) (as Actions, err error) { var values []byte if values, err = rs.db.Cmd("get", ACTION_PREFIX+key).Bytes(); err == nil { diff --git a/utils/consts.go b/utils/consts.go index 307c1fee4..b1e0c697f 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -9,5 +9,8 @@ const ( POSTPAID = "postpaid" PSEUDOPREPAID = "pseudoprepaid" RATED = "rated" + ERR_SERVER_ERROR = "SERVER_ERROR" + ERR_DST_NOT_FOUND = "DESTINATION_NOT_FOUND" + ERR_MANDATORY_IE_MISSING = "MANDATORY_IE_MISSING" )