From ec040b4ae66b107baad2889058be6f9535083f3d Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 7 Sep 2015 17:21:15 +0300 Subject: [PATCH] added reverse alias APIs and console commands fixes #170, fixes #168 --- cache2go/cache.go | 2 +- cache2go/cache_test.go | 30 ++++----- cache2go/store.go | 2 +- console/aliases.go | 66 +++++++++++++++++++ console/aliases_matching.go | 66 +++++++++++++++++++ console/aliases_remove.go | 66 +++++++++++++++++++ console/aliases_reverse.go | 63 ++++++++++++++++++ console/aliases_reverse_remove.go | 63 ++++++++++++++++++ console/aliases_set.go | 66 +++++++++++++++++++ console/aliases_update.go | 66 +++++++++++++++++++ data/conf/samples/cgradmin/cgradmin.json | 9 ++- engine/account.go | 2 +- engine/aliases.go | 81 ++++++++++++++++++++++-- engine/balances.go | 2 +- engine/destinations.go | 2 +- engine/destinations_test.go | 10 +-- engine/lcr.go | 2 +- engine/ratingprofile.go | 2 +- engine/storage_map.go | 18 +++--- engine/storage_redis.go | 22 +++---- engine/storage_test.go | 12 ++-- engine/units_counter.go | 2 +- 22 files changed, 591 insertions(+), 63 deletions(-) create mode 100644 console/aliases.go create mode 100644 console/aliases_matching.go create mode 100644 console/aliases_remove.go create mode 100644 console/aliases_reverse.go create mode 100644 console/aliases_reverse_remove.go create mode 100644 console/aliases_set.go create mode 100644 console/aliases_update.go diff --git a/cache2go/cache.go b/cache2go/cache.go index 43b0067bf..cd44a2c7c 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -111,7 +111,7 @@ func CachePush(key string, value interface{}) { } // The function to extract a value for a key that never expire -func GetCached(key string) (v interface{}, err error) { +func Get(key string) (v interface{}, err error) { mux.RLock() defer mux.RUnlock() return cache.Get(key) diff --git a/cache2go/cache_test.go b/cache2go/cache_test.go index 1bae5050b..4b062ecf1 100644 --- a/cache2go/cache_test.go +++ b/cache2go/cache_test.go @@ -4,11 +4,11 @@ import "testing" func TestRemKey(t *testing.T) { Cache("t11_mm", "test") - if t1, err := GetCached("t11_mm"); err != nil || t1 != "test" { + if t1, err := Get("t11_mm"); err != nil || t1 != "test" { t.Error("Error setting cache: ", err, t1) } RemKey("t11_mm") - if t1, err := GetCached("t11_mm"); err == nil || t1 == "test" { + if t1, err := Get("t11_mm"); err == nil || t1 == "test" { t.Error("Error removing cached key") } } @@ -16,16 +16,16 @@ func TestRemKey(t *testing.T) { func TestTransaction(t *testing.T) { BeginTransaction() Cache("t11_mm", "test") - if t1, err := GetCached("t11_mm"); err == nil || t1 == "test" { + if t1, err := Get("t11_mm"); err == nil || t1 == "test" { t.Error("Error in transaction cache") } Cache("t12_mm", "test") RemKey("t11_mm") CommitTransaction() - if t1, err := GetCached("t12_mm"); err != nil || t1 != "test" { + if t1, err := Get("t12_mm"); err != nil || t1 != "test" { t.Error("Error commiting transaction") } - if t1, err := GetCached("t11_mm"); err == nil || t1 == "test" { + if t1, err := Get("t11_mm"); err == nil || t1 == "test" { t.Error("Error in transaction cache") } } @@ -36,10 +36,10 @@ func TestTransactionRem(t *testing.T) { Cache("t21_nn", "test") RemPrefixKey("t21_") CommitTransaction() - if t1, err := GetCached("t21_mm"); err == nil || t1 == "test" { + if t1, err := Get("t21_mm"); err == nil || t1 == "test" { t.Error("Error commiting transaction") } - if t1, err := GetCached("t21_nn"); err == nil || t1 == "test" { + if t1, err := Get("t21_nn"); err == nil || t1 == "test" { t.Error("Error in transaction cache") } } @@ -47,15 +47,15 @@ func TestTransactionRem(t *testing.T) { func TestTransactionRollback(t *testing.T) { BeginTransaction() Cache("t31_mm", "test") - if t1, err := GetCached("t31_mm"); err == nil || t1 == "test" { + if t1, err := Get("t31_mm"); err == nil || t1 == "test" { t.Error("Error in transaction cache") } Cache("t32_mm", "test") RollbackTransaction() - if t1, err := GetCached("t32_mm"); err == nil || t1 == "test" { + if t1, err := Get("t32_mm"); err == nil || t1 == "test" { t.Error("Error commiting transaction") } - if t1, err := GetCached("t31_mm"); err == nil || t1 == "test" { + if t1, err := Get("t31_mm"); err == nil || t1 == "test" { t.Error("Error in transaction cache") } } @@ -66,10 +66,10 @@ func TestTransactionRemBefore(t *testing.T) { Cache("t41_mm", "test") Cache("t41_nn", "test") CommitTransaction() - if t1, err := GetCached("t41_mm"); err != nil || t1 != "test" { + if t1, err := Get("t41_mm"); err != nil || t1 != "test" { t.Error("Error commiting transaction") } - if t1, err := GetCached("t41_nn"); err != nil || t1 != "test" { + if t1, err := Get("t41_nn"); err != nil || t1 != "test" { t.Error("Error in transaction cache") } } @@ -78,8 +78,8 @@ func TestRemPrefixKey(t *testing.T) { Cache("xxx_t1", "test") Cache("yyy_t1", "test") RemPrefixKey("xxx_") - _, errX := GetCached("xxx_t1") - _, errY := GetCached("yyy_t1") + _, errX := Get("xxx_t1") + _, errY := Get("yyy_t1") if errX == nil || errY != nil { t.Error("Error removing prefix: ", errX, errY) } @@ -88,7 +88,7 @@ func TestRemPrefixKey(t *testing.T) { func TestCachePush(t *testing.T) { CachePush("ccc_t1", "1") CachePush("ccc_t1", "2") - v, err := GetCached("ccc_t1") + v, err := Get("ccc_t1") if err != nil || len(v.(map[interface{}]struct{})) != 2 { t.Error("Error in cache push: ", v) } diff --git a/cache2go/store.go b/cache2go/store.go index fe0e74d78..de6750f76 100644 --- a/cache2go/store.go +++ b/cache2go/store.go @@ -95,7 +95,7 @@ func (cs cacheDoubleStore) GetKeysForPrefix(prefix string) (keys []string) { prefix, key := prefix[:PREFIX_LEN], prefix[PREFIX_LEN:] if keyMap, ok := cs[prefix]; ok { for iterKey := range keyMap { - if len(key) > 0 && strings.HasPrefix(iterKey, key) { + if len(key) == 0 || strings.HasPrefix(iterKey, key) { keys = append(keys, prefix+iterKey) } } diff --git a/console/aliases.go b/console/aliases.go new file mode 100644 index 000000000..57fb30fe5 --- /dev/null +++ b/console/aliases.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + c := &CmdGetAliases{ + name: "aliases", + rpcMethod: "AliasesV1.GetAlias", + rpcParams: &engine.Alias{Direction: utils.OUT}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdGetAliases struct { + name string + rpcMethod string + rpcParams *engine.Alias + *CommandExecuter +} + +func (self *CmdGetAliases) Name() string { + return self.name +} + +func (self *CmdGetAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.Alias{Direction: utils.OUT} + } + return self.rpcParams +} + +func (self *CmdGetAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdGetAliases) RpcResult() interface{} { + a := engine.Alias{} + return &a +} diff --git a/console/aliases_matching.go b/console/aliases_matching.go new file mode 100644 index 000000000..b87a23f72 --- /dev/null +++ b/console/aliases_matching.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + c := &CmdGetMatchingAliases{ + name: "aliases_matching", + rpcMethod: "AliasesV1.GetMatchingAlias", + rpcParams: &engine.AttrMatchingAlias{Direction: utils.OUT}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdGetMatchingAliases struct { + name string + rpcMethod string + rpcParams *engine.AttrMatchingAlias + *CommandExecuter +} + +func (self *CmdGetMatchingAliases) Name() string { + return self.name +} + +func (self *CmdGetMatchingAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetMatchingAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.AttrMatchingAlias{Direction: utils.OUT} + } + return self.rpcParams +} + +func (self *CmdGetMatchingAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdGetMatchingAliases) RpcResult() interface{} { + var s string + return &s +} diff --git a/console/aliases_remove.go b/console/aliases_remove.go new file mode 100644 index 000000000..d338b26eb --- /dev/null +++ b/console/aliases_remove.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + c := &CmdRemoveAliases{ + name: "aliases_remove", + rpcMethod: "AliasesV1.RemoveAlias", + rpcParams: &engine.Alias{Direction: utils.OUT}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdRemoveAliases struct { + name string + rpcMethod string + rpcParams *engine.Alias + *CommandExecuter +} + +func (self *CmdRemoveAliases) Name() string { + return self.name +} + +func (self *CmdRemoveAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdRemoveAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.Alias{Direction: utils.OUT} + } + return self.rpcParams +} + +func (self *CmdRemoveAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdRemoveAliases) RpcResult() interface{} { + var s string + return &s +} diff --git a/console/aliases_reverse.go b/console/aliases_reverse.go new file mode 100644 index 000000000..df03147ef --- /dev/null +++ b/console/aliases_reverse.go @@ -0,0 +1,63 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import "github.com/cgrates/cgrates/engine" + +func init() { + c := &CmdGetReverseAliases{ + name: "aliases_reverse", + rpcMethod: "AliasesV1.GetReverseAlias", + rpcParams: &engine.AttrReverseAlias{}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdGetReverseAliases struct { + name string + rpcMethod string + rpcParams *engine.AttrReverseAlias + *CommandExecuter +} + +func (self *CmdGetReverseAliases) Name() string { + return self.name +} + +func (self *CmdGetReverseAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdGetReverseAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.AttrReverseAlias{} + } + return self.rpcParams +} + +func (self *CmdGetReverseAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdGetReverseAliases) RpcResult() interface{} { + a := make(map[string]*engine.Alias) + return &a +} diff --git a/console/aliases_reverse_remove.go b/console/aliases_reverse_remove.go new file mode 100644 index 000000000..9b177859f --- /dev/null +++ b/console/aliases_reverse_remove.go @@ -0,0 +1,63 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import "github.com/cgrates/cgrates/engine" + +func init() { + c := &CmdRemoveReverseAliases{ + name: "aliases_reverse_remove", + rpcMethod: "AliasesV1.RemoveReverseAlias", + rpcParams: &engine.AttrReverseAlias{}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdRemoveReverseAliases struct { + name string + rpcMethod string + rpcParams *engine.AttrReverseAlias + *CommandExecuter +} + +func (self *CmdRemoveReverseAliases) Name() string { + return self.name +} + +func (self *CmdRemoveReverseAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdRemoveReverseAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.AttrReverseAlias{} + } + return self.rpcParams +} + +func (self *CmdRemoveReverseAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdRemoveReverseAliases) RpcResult() interface{} { + var s string + return &s +} diff --git a/console/aliases_set.go b/console/aliases_set.go new file mode 100644 index 000000000..991836167 --- /dev/null +++ b/console/aliases_set.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + c := &CmdSetAliases{ + name: "aliases_set", + rpcMethod: "AliasesV1.SetAlias", + rpcParams: &engine.Alias{Direction: utils.OUT}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdSetAliases struct { + name string + rpcMethod string + rpcParams *engine.Alias + *CommandExecuter +} + +func (self *CmdSetAliases) Name() string { + return self.name +} + +func (self *CmdSetAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdSetAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.Alias{Direction: utils.OUT} + } + return self.rpcParams +} + +func (self *CmdSetAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdSetAliases) RpcResult() interface{} { + var s string + return &s +} diff --git a/console/aliases_update.go b/console/aliases_update.go new file mode 100644 index 000000000..7063141be --- /dev/null +++ b/console/aliases_update.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 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 console + +import ( + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func init() { + c := &CmdUpdateAliases{ + name: "aliases_update", + rpcMethod: "AliasesV1.UpdateAlias", + rpcParams: &engine.Alias{Direction: utils.OUT}, + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdUpdateAliases struct { + name string + rpcMethod string + rpcParams *engine.Alias + *CommandExecuter +} + +func (self *CmdUpdateAliases) Name() string { + return self.name +} + +func (self *CmdUpdateAliases) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdUpdateAliases) RpcParams(reset bool) interface{} { + if reset || self.rpcParams == nil { + self.rpcParams = &engine.Alias{Direction: utils.OUT} + } + return self.rpcParams +} + +func (self *CmdUpdateAliases) PostprocessRpcParams() error { + return nil +} + +func (self *CmdUpdateAliases) RpcResult() interface{} { + var s string + return &s +} diff --git a/data/conf/samples/cgradmin/cgradmin.json b/data/conf/samples/cgradmin/cgradmin.json index 93016038d..4ea1f0eef 100644 --- a/data/conf/samples/cgradmin/cgradmin.json +++ b/data/conf/samples/cgradmin/cgradmin.json @@ -12,8 +12,9 @@ "rater": { "enabled": true, // enable Rater service: - "pubsubs": "internal", - "users": "internal", + "pubsubs": "internal", + "users": "internal", + "aliases": "internal", }, "scheduler": { @@ -28,4 +29,8 @@ "enabled": true, // starts users service: . }, +"aliases": { + "enabled": true, +} + } diff --git a/engine/account.go b/engine/account.go index a07b3e4e2..d4ced0431 100644 --- a/engine/account.go +++ b/engine/account.go @@ -162,7 +162,7 @@ func (ub *Account) getBalancesForPrefix(prefix, category string, balances Balanc b.account = ub if b.DestinationIds != "" && b.DestinationIds != utils.ANY { for _, p := range utils.SplitPrefix(prefix, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) for dId, _ := range destIds { balDestIds := strings.Split(b.DestinationIds, utils.INFIELD_SEP) diff --git a/engine/aliases.go b/engine/aliases.go index 9fdb41a3e..587dd85ce 100644 --- a/engine/aliases.go +++ b/engine/aliases.go @@ -112,12 +112,19 @@ type AttrMatchingAlias struct { Group string } +type AttrReverseAlias struct { + Alias string + Group string +} + type AliasService interface { SetAlias(Alias, *string) error UpdateAlias(Alias, *string) error RemoveAlias(Alias, *string) error GetAlias(Alias, *Alias) error GetMatchingAlias(AttrMatchingAlias, *string) error + GetReverseAlias(AttrReverseAlias, *map[string]*Alias) error + RemoveReverseAlias(AttrReverseAlias, *string) error } type AliasHandler struct { @@ -194,6 +201,28 @@ func (am *AliasHandler) RemoveAlias(al Alias, reply *string) error { return nil } +func (am *AliasHandler) RemoveReverseAlias(attr AttrReverseAlias, reply *string) error { + am.mu.Lock() + defer am.mu.Unlock() + rKey := utils.REVERSE_ALIASES_PREFIX + attr.Alias + attr.Group + if x, err := cache2go.Get(rKey); err == nil { + existingKeys := x.(map[string]bool) + for key := range existingKeys { + // get destination id + elems := strings.Split(key, utils.CONCATENATED_KEY_SEP) + if len(elems) > 0 { + key = strings.Join(elems[:len(elems)-1], utils.CONCATENATED_KEY_SEP) + } + if err := am.accountingDb.RemoveAlias(key); err != nil { + *reply = err.Error() + return err + } + } + } + *reply = utils.OK + return nil +} + func (am *AliasHandler) GetAlias(al Alias, result *Alias) error { am.mu.RLock() defer am.mu.RUnlock() @@ -207,8 +236,38 @@ func (am *AliasHandler) GetAlias(al Alias, result *Alias) error { return utils.ErrNotFound } +func (am *AliasHandler) GetReverseAlias(attr AttrReverseAlias, result *map[string]*Alias) error { + am.mu.Lock() + defer am.mu.Unlock() + aliases := make(map[string]*Alias) + rKey := utils.REVERSE_ALIASES_PREFIX + attr.Alias + attr.Group + if x, err := cache2go.Get(rKey); err == nil { + existingKeys := x.(map[string]bool) + for key := range existingKeys { + + // get destination id + elems := strings.Split(key, utils.CONCATENATED_KEY_SEP) + var destID string + if len(elems) > 0 { + destID = elems[len(elems)-1] + key = strings.Join(elems[:len(elems)-1], utils.CONCATENATED_KEY_SEP) + } + if r, err := am.accountingDb.GetAlias(key, false); err != nil { + return err + } else { + aliases[destID] = r + } + } + } + *result = aliases + return nil +} + func (am *AliasHandler) GetMatchingAlias(attr AttrMatchingAlias, result *string) error { response := Alias{} + if attr.Destination == "" { + attr.Destination = utils.ANY + } if err := aliasService.GetAlias(Alias{ Direction: attr.Direction, Tenant: attr.Tenant, @@ -224,7 +283,7 @@ func (am *AliasHandler) GetMatchingAlias(attr AttrMatchingAlias, result *string) // check destination ids for _, p := range utils.SplitPrefix(attr.Destination, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { for _, aliasHandler := range values { for alias, aliasDestIds := range aliasHandler { destIds := x.(map[interface{}]struct{}) @@ -257,25 +316,33 @@ func NewProxyAliasService(addr string, attempts, reconnects int) (*ProxyAliasSer } func (ps *ProxyAliasService) SetAlias(al Alias, reply *string) error { - return ps.Client.Call("AliasV1.SetAlias", al, reply) + return ps.Client.Call("AliasesV1.SetAlias", al, reply) } func (ps *ProxyAliasService) UpdateAlias(al Alias, reply *string) error { - return ps.Client.Call("AliasV1.UpdateAlias", al, reply) + return ps.Client.Call("AliasesV1.UpdateAlias", al, reply) } func (ps *ProxyAliasService) RemoveAlias(al Alias, reply *string) error { - return ps.Client.Call("AliasV1.RemoveAlias", al, reply) + return ps.Client.Call("AliasesV1.RemoveAlias", al, reply) } func (ps *ProxyAliasService) GetAlias(al Alias, alias *Alias) error { - return ps.Client.Call("AliasV1.GetAlias", al, alias) + return ps.Client.Call("AliasesV1.GetAlias", al, alias) } func (ps *ProxyAliasService) GetMatchingAlias(attr AttrMatchingAlias, alias *string) error { - return ps.Client.Call("AliasV1.GetMatchingAlias", attr, alias) + return ps.Client.Call("AliasesV1.GetMatchingAlias", attr, alias) +} + +func (ps *ProxyAliasService) GetReverseAlias(attr AttrReverseAlias, alias *map[string]*Alias) error { + return ps.Client.Call("AliasesV1.GetReverseAlias", attr, alias) +} + +func (ps *ProxyAliasService) RemoveReverseAlias(attr AttrReverseAlias, reply *string) error { + return ps.Client.Call("AliasesV1.RemoveReverseAlias", attr, reply) } func (ps *ProxyAliasService) ReloadAliases(in string, reply *string) error { - return ps.Client.Call("AliasV1.ReloadAliases", in, reply) + return ps.Client.Call("AliasesV1.ReloadAliases", in, reply) } diff --git a/engine/balances.go b/engine/balances.go index 0408bdcc1..a75b72f5b 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -189,7 +189,7 @@ func (b *Balance) Clone() *Balance { func (b *Balance) getMatchingPrefixAndDestId(dest string) (prefix, destId string) { if b.DestinationIds != "" && b.DestinationIds != utils.ANY { for _, p := range utils.SplitPrefix(dest, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) for dId, _ := range destIds { balDestIds := strings.Split(b.DestinationIds, utils.INFIELD_SEP) diff --git a/engine/destinations.go b/engine/destinations.go index 14380dc3c..234075368 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -74,7 +74,7 @@ func (d *Destination) GetHistoryRecord() history.Record { // Reverse search in cache to see if prefix belongs to destination id func CachedDestHasPrefix(destId, prefix string) bool { - if cached, err := cache2go.GetCached(utils.DESTINATION_PREFIX + prefix); err == nil { + if cached, err := cache2go.Get(utils.DESTINATION_PREFIX + prefix); err == nil { _, found := cached.(map[interface{}]struct{})[destId] return found } diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 5d7a4d1b9..ec0ba399b 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -83,7 +83,7 @@ func TestDestinationGetExists(t *testing.T) { func TestDestinationGetExistsCache(t *testing.T) { ratingStorage.GetDestination("NAT") - if _, err := cache2go.GetCached(utils.DESTINATION_PREFIX + "0256"); err != nil { + if _, err := cache2go.Get(utils.DESTINATION_PREFIX + "0256"); err != nil { t.Error("Destination not cached:", err) } } @@ -97,7 +97,7 @@ func TestDestinationGetNotExists(t *testing.T) { func TestDestinationGetNotExistsCache(t *testing.T) { ratingStorage.GetDestination("not existing") - if d, err := cache2go.GetCached("not existing"); err == nil { + if d, err := cache2go.Get("not existing"); err == nil { t.Error("Bad destination cached: ", d) } } @@ -132,13 +132,13 @@ func TestCleanStalePrefixes(t *testing.T) { cache2go.Cache(utils.DESTINATION_PREFIX+"2", map[interface{}]struct{}{"D1": x}) cache2go.Cache(utils.DESTINATION_PREFIX+"3", map[interface{}]struct{}{"D2": x}) CleanStalePrefixes([]string{"D1"}) - if r, err := cache2go.GetCached(utils.DESTINATION_PREFIX + "1"); err != nil || len(r.(map[interface{}]struct{})) != 1 { + if r, err := cache2go.Get(utils.DESTINATION_PREFIX + "1"); err != nil || len(r.(map[interface{}]struct{})) != 1 { t.Error("Error cleaning stale destination ids", r) } - if r, err := cache2go.GetCached(utils.DESTINATION_PREFIX + "2"); err == nil { + if r, err := cache2go.Get(utils.DESTINATION_PREFIX + "2"); err == nil { t.Error("Error removing stale prefix: ", r) } - if r, err := cache2go.GetCached(utils.DESTINATION_PREFIX + "3"); err != nil || len(r.(map[interface{}]struct{})) != 1 { + if r, err := cache2go.Get(utils.DESTINATION_PREFIX + "3"); err != nil || len(r.(map[interface{}]struct{})) != 1 { t.Error("Error performing stale cleaning: ", r) } } diff --git a/engine/lcr.go b/engine/lcr.go index 54bef4477..9c88f0c9b 100644 --- a/engine/lcr.go +++ b/engine/lcr.go @@ -274,7 +274,7 @@ func (es LCREntriesSorter) Sort() { func (lcra *LCRActivation) GetLCREntryForPrefix(destination string) *LCREntry { var potentials LCREntriesSorter for _, p := range utils.SplitPrefix(destination, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) for idId := range destIds { diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index 264551b46..159b7baa7 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -133,7 +133,7 @@ func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) } } else { for _, p := range utils.SplitPrefix(cd.Destination, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) for idId := range destIds { diff --git a/engine/storage_map.go b/engine/storage_map.go index f9c7e117c..bafa1fe3a 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -247,7 +247,7 @@ func (ms *MapStorage) HasData(categ, subject string) (bool, error) { func (ms *MapStorage) GetRatingPlan(key string, skipCache bool) (rp *RatingPlan, err error) { key = utils.RATING_PLAN_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*RatingPlan), nil } else { return nil, err @@ -290,7 +290,7 @@ func (ms *MapStorage) SetRatingPlan(rp *RatingPlan) (err error) { func (ms *MapStorage) GetRatingProfile(key string, skipCache bool) (rpf *RatingProfile, err error) { key = utils.RATING_PROFILE_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*RatingProfile), nil } else { return nil, err @@ -335,7 +335,7 @@ func (ms *MapStorage) RemoveRatingProfile(key string) (err error) { func (ms *MapStorage) GetLCR(key string, skipCache bool) (lcr *LCR, err error) { key = utils.LCR_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*LCR), nil } else { return nil, err @@ -398,7 +398,7 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { func (ms *MapStorage) GetActions(key string, skipCache bool) (as Actions, err error) { key = utils.ACTION_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(Actions), nil } else { return nil, err @@ -422,7 +422,7 @@ func (ms *MapStorage) SetActions(key string, as Actions) (err error) { func (ms *MapStorage) GetSharedGroup(key string, skipCache bool) (sg *SharedGroup, err error) { key = utils.SHARED_GROUP_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*SharedGroup), nil } else { return nil, err @@ -565,7 +565,7 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { al = &Alias{Values: x.(AliasValues)} al.SetId(key[len(utils.ALIASES_PREFIX):]) return al, nil @@ -582,7 +582,7 @@ func (ms *MapStorage) GetAlias(key string, skipCache bool) (al *Alias, err error for _, v := range al.Values { var existingKeys map[string]bool rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.GetCached(rKey); err == nil { + if x, err := cache2go.Get(rKey); err == nil { existingKeys = x.(map[string]bool) } else { existingKeys = make(map[string]bool) @@ -610,7 +610,7 @@ func (ms *MapStorage) RemoveAlias(key string) error { for _, v := range aliasValues { var existingKeys map[string]bool rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.GetCached(rKey); err == nil { + if x, err := cache2go.Get(rKey); err == nil { existingKeys = x.(map[string]bool) } for eKey := range existingKeys { @@ -673,7 +673,7 @@ func (ms *MapStorage) GetAllActionPlans() (ats map[string]ActionPlans, err error func (ms *MapStorage) GetDerivedChargers(key string, skipCache bool) (dcs utils.DerivedChargers, err error) { key = utils.DERIVEDCHARGERS_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(utils.DerivedChargers), nil } else { return nil, err diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 7cc5ba45a..85bdbbfe9 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -343,7 +343,7 @@ func (rs *RedisStorage) HasData(category, subject string) (bool, error) { func (rs *RedisStorage) GetRatingPlan(key string, skipCache bool) (rp *RatingPlan, err error) { key = utils.RATING_PLAN_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*RatingPlan), nil } else { return nil, err @@ -386,7 +386,7 @@ func (rs *RedisStorage) GetRatingProfile(key string, skipCache bool) (rpf *Ratin key = utils.RATING_PROFILE_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*RatingProfile), nil } else { return nil, err @@ -434,7 +434,7 @@ func (rs *RedisStorage) RemoveRatingProfile(key string) error { func (rs *RedisStorage) GetLCR(key string, skipCache bool) (lcr *LCR, err error) { key = utils.LCR_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*LCR), nil } else { return nil, err @@ -501,7 +501,7 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { func (rs *RedisStorage) GetActions(key string, skipCache bool) (as Actions, err error) { key = utils.ACTION_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(Actions), nil } else { return nil, err @@ -524,7 +524,7 @@ func (rs *RedisStorage) SetActions(key string, as Actions) (err error) { func (rs *RedisStorage) GetSharedGroup(key string, skipCache bool) (sg *SharedGroup, err error) { key = utils.SHARED_GROUP_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(*SharedGroup), nil } else { return nil, err @@ -669,7 +669,7 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err err origKey := key key = utils.ALIASES_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { al = &Alias{Values: x.(AliasValues)} al.SetId(key[len(utils.ALIASES_PREFIX):]) return al, nil @@ -688,7 +688,7 @@ func (rs *RedisStorage) GetAlias(key string, skipCache bool) (al *Alias, err err for _, v := range al.Values { var existingKeys map[string]bool rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.GetCached(rKey); err == nil { + if x, err := cache2go.Get(rKey); err == nil { existingKeys = x.(map[string]bool) } else { existingKeys = make(map[string]bool) @@ -715,11 +715,11 @@ func (rs *RedisStorage) RemoveAlias(key string) (err error) { for _, v := range aliasValues { var existingKeys map[string]bool rKey := utils.REVERSE_ALIASES_PREFIX + v.Alias + al.Group - if x, err := cache2go.GetCached(rKey); err == nil { + if x, err := cache2go.Get(rKey); err == nil { existingKeys = x.(map[string]bool) } for eKey := range existingKeys { - if strings.HasPrefix(eKey, origKey) { + if strings.HasPrefix(origKey, eKey) { delete(existingKeys, eKey) } } @@ -740,7 +740,7 @@ func (rs *RedisStorage) GetLoadHistory(limit int, skipCache bool) ([]*LoadInstan return nil, nil } if !skipCache { - if x, err := cache2go.GetCached(utils.LOADINST_KEY); err != nil { + if x, err := cache2go.Get(utils.LOADINST_KEY); err != nil { return nil, err } else { items := x.([]*LoadInstance) @@ -840,7 +840,7 @@ func (rs *RedisStorage) GetAllActionPlans() (ats map[string]ActionPlans, err err func (rs *RedisStorage) GetDerivedChargers(key string, skipCache bool) (dcs utils.DerivedChargers, err error) { key = utils.DERIVEDCHARGERS_PREFIX + key if !skipCache { - if x, err := cache2go.GetCached(key); err == nil { + if x, err := cache2go.Get(key); err == nil { return x.(utils.DerivedChargers), nil } else { return nil, err diff --git a/engine/storage_test.go b/engine/storage_test.go index 8f4f80c8a..749ea3316 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -179,7 +179,7 @@ func TestStorageCacheGetReverseAliases(t *testing.T) { Subject: "b1", Group: utils.ALIAS_GROUP_ACC, } - if x, err := cache2go.GetCached(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_RP); err == nil { + if x, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_RP); err == nil { aliasKeys := x.(map[string]bool) _, found := aliasKeys[utils.ConcatenatedKey(ala.GetId(), utils.ANY)] if !found { @@ -188,7 +188,7 @@ func TestStorageCacheGetReverseAliases(t *testing.T) { } else { t.Error("Error getting reverse alias: ", err) } - if x, err := cache2go.GetCached(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_ACC); err == nil { + if x, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_ACC); err == nil { aliasKeys := x.(map[string]bool) _, found := aliasKeys[utils.ConcatenatedKey(alb.GetId(), utils.ANY)] if !found { @@ -219,17 +219,17 @@ func TestStorageCacheRemoveCachedAliases(t *testing.T) { accountingStorage.RemoveAlias(ala.GetId()) accountingStorage.RemoveAlias(alb.GetId()) - if _, err := cache2go.GetCached(utils.ALIASES_PREFIX + ala.GetId()); err == nil { + if _, err := cache2go.Get(utils.ALIASES_PREFIX + ala.GetId()); err == nil { t.Error("Error removing cached alias: ", err) } - if _, err := cache2go.GetCached(utils.ALIASES_PREFIX + alb.GetId()); err == nil { + if _, err := cache2go.Get(utils.ALIASES_PREFIX + alb.GetId()); err == nil { t.Error("Error removing cached alias: ", err) } - if _, err := cache2go.GetCached(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_RP); err == nil { + if _, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_RP); err == nil { t.Error("Error removing cached reverse alias: ", err) } - if _, err := cache2go.GetCached(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_ACC); err == nil { + if _, err := cache2go.Get(utils.REVERSE_ALIASES_PREFIX + "aaa" + utils.ALIAS_GROUP_ACC); err == nil { t.Error("Error removing cached reverse alias: ", err) } } diff --git a/engine/units_counter.go b/engine/units_counter.go index 20eeb9573..d74364f8c 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -77,7 +77,7 @@ func (uc *UnitsCounter) addUnits(amount float64, prefix string) { continue } for _, p := range utils.SplitPrefix(prefix, MIN_PREFIX_MATCH) { - if x, err := cache2go.GetCached(utils.DESTINATION_PREFIX + p); err == nil { + if x, err := cache2go.Get(utils.DESTINATION_PREFIX + p); err == nil { destIds := x.(map[interface{}]struct{}) if _, found := destIds[mb.DestinationIds]; found { mb.AddValue(amount)