Adding GetTPDestinations API and storage method

This commit is contained in:
DanB
2013-06-12 12:27:30 +02:00
parent dd14b7e210
commit a254242b46
12 changed files with 118 additions and 4 deletions

View File

@@ -18,6 +18,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package apier
import (
"github.com/cgrates/cgrates/rater"
)
type Apier struct {
StorDb rater.DataStorage
}

49
apier/tpdestinations.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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
}

View File

@@ -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("<Rater> 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("<Rater> 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))

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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"
)