Added GuardianSv1 to DispatcherSv1

This commit is contained in:
Trial97
2019-03-22 14:50:58 +02:00
committed by Dan Christian Bogos
parent be7814d7f6
commit c3567208ed
9 changed files with 149 additions and 25 deletions

View File

@@ -524,3 +524,27 @@ func (dS *DispatcherCacheSv1) LoadCache(args dispatchers.AttrReloadCacheWithApiK
func (dS *DispatcherCacheSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error {
return dS.dS.CacheSv1Ping(args, reply)
}
func NewDispatcherGuardianSv1(dps *dispatchers.DispatcherService) *DispatcherGuardianSv1 {
return &DispatcherGuardianSv1{dS: dps}
}
// Exports RPC from CacheSv1
type DispatcherGuardianSv1 struct {
dS *dispatchers.DispatcherService
}
// RemoteLock will lock a key from remote
func (dS *DispatcherGuardianSv1) RemoteLock(attr *dispatchers.AttrRemoteLockWithApiKey, reply *string) (err error) {
return dS.dS.GuardianSv1RemoteLock(attr, reply)
}
// RemoteUnlock will unlock a key from remote based on reference ID
func (dS *DispatcherGuardianSv1) RemoteUnlock(attr *dispatchers.AttrRemoteUnlockWithApiKey, reply *[]string) (err error) {
return dS.dS.GuardianSv1RemoteUnlock(attr, reply)
}
// Ping used to detreminate if component is active
func (dS *DispatcherGuardianSv1) Ping(args *dispatchers.CGREvWithApiKey, reply *string) error {
return dS.dS.GuardianSv1Ping(args, reply)
}

View File

@@ -19,21 +19,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package v1
import (
"time"
"github.com/cgrates/cgrates/guardian"
"github.com/cgrates/cgrates/utils"
)
func NewGuardianSv1() *GuardianSv1 {
return &GuardianSv1{}
}
type GuardianSv1 struct{}
type AttrRemoteLock struct {
ReferenceID string // reference ID for this lock if available
LockIDs []string // List of IDs to obtain lock for
Timeout time.Duration // Automatically unlock on timeout
}
// RemoteLock will lock a key from remote
func (self *GuardianSv1) RemoteLock(attr AttrRemoteLock, reply *string) (err error) {
func (self *GuardianSv1) RemoteLock(attr utils.AttrRemoteLock, reply *string) (err error) {
*reply = guardian.Guardian.GuardIDs(attr.ReferenceID, attr.Timeout, attr.LockIDs...)
return
}
@@ -43,3 +40,9 @@ func (self *GuardianSv1) RemoteUnlock(refID string, reply *[]string) (err error)
*reply = guardian.Guardian.UnguardIDs(refID)
return
}
// Ping return pong if the service is active
func (self *GuardianSv1) Ping(ign *utils.CGREvent, reply *string) error {
*reply = utils.Pong
return nil
}

View File

@@ -1004,6 +1004,9 @@ func startDispatcherService(internalDispatcherSChan chan *dispatchers.Dispatcher
server.RpcRegisterName(utils.CacheSv1,
v1.NewDispatcherCacheSv1(dspS))
server.RpcRegisterName(utils.GuardianSv1,
v1.NewDispatcherGuardianSv1(dspS))
internalDispatcherSChan <- dspS
}

View File

@@ -153,7 +153,7 @@ func startRater(internalRaterChan chan rpcclient.RpcClientConnection, cacheS *en
apierRpcV2 := &v2.ApierV2{
ApierV1: *apierRpcV1}
guardianSv1 := &v1.GuardianSv1{}
guardianSv1 := v1.NewGuardianSv1()
server.RpcRegister(responder)
server.RpcRegister(apierRpcV1)

View File

@@ -35,7 +35,7 @@ func (dS *DispatcherService) CacheSv1Ping(args *CGREvWithApiKey,
return
}
}
return dS.Dispatch(&args.CGREvent, utils.MetaCache, args.RouteID,
return dS.Dispatch(&args.CGREvent, utils.MetaCaches, args.RouteID,
utils.CacheSv1Ping, args.CGREvent, reply)
}
@@ -49,7 +49,7 @@ func (dS *DispatcherService) CacheSv1GetItemIDs(args *ArgsGetCacheItemIDsWithApi
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1GetItemIDs, &args.ArgsGetCacheItemIDs, reply)
}
@@ -63,7 +63,7 @@ func (dS *DispatcherService) CacheSv1HasItem(args *ArgsGetCacheItemWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1HasItem, &args.ArgsGetCacheItem, reply)
}
@@ -77,7 +77,7 @@ func (dS *DispatcherService) CacheSv1GetItemExpiryTime(args *ArgsGetCacheItemWit
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1GetItemExpiryTime, &args.ArgsGetCacheItem, reply)
}
@@ -91,7 +91,7 @@ func (dS *DispatcherService) CacheSv1RemoveItem(args *ArgsGetCacheItemWithApiKey
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1RemoveItem, &args.ArgsGetCacheItem, reply)
}
@@ -105,7 +105,7 @@ func (dS *DispatcherService) CacheSv1Clear(args *AttrCacheIDsWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1Clear, args.CacheIDs, reply)
}
@@ -118,7 +118,7 @@ func (dS *DispatcherService) CacheSv1FlushCache(args AttrReloadCacheWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1FlushCache, args.AttrReloadCache, reply)
}
@@ -132,7 +132,7 @@ func (dS *DispatcherService) CacheSv1GetCacheStats(args *AttrCacheIDsWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1GetCacheStats, args.CacheIDs, reply)
}
@@ -145,7 +145,7 @@ func (dS *DispatcherService) CacheSv1PrecacheStatus(args *AttrCacheIDsWithApiKey
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1PrecacheStatus, args.CacheIDs, reply)
}
@@ -159,7 +159,7 @@ func (dS *DispatcherService) CacheSv1HasGroup(args *ArgsGetGroupWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1HasGroup, args.ArgsGetGroup, reply)
}
@@ -173,7 +173,7 @@ func (dS *DispatcherService) CacheSv1GetGroupItemIDs(args *ArgsGetGroupWithApiKe
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1GetGroupItemIDs, args.ArgsGetGroup, reply)
}
@@ -187,7 +187,7 @@ func (dS *DispatcherService) CacheSv1RemoveGroup(args *ArgsGetGroupWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1RemoveGroup, args.ArgsGetGroup, reply)
}
@@ -200,7 +200,7 @@ func (dS *DispatcherService) CacheSv1ReloadCache(args AttrReloadCacheWithApiKey,
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1ReloadCache, args.AttrReloadCache, reply)
}
@@ -213,6 +213,6 @@ func (dS *DispatcherService) CacheSv1LoadCache(args AttrReloadCacheWithApiKey, r
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCache, args.RouteID,
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaCaches, args.RouteID,
utils.CacheSv1LoadCache, args.AttrReloadCache, reply)
}

67
dispatchers/guardian.go Normal file
View File

@@ -0,0 +1,67 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
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 dispatchers
import (
"time"
"github.com/cgrates/cgrates/utils"
)
// GuardianSv1Ping interogates GuardianSv1 server responsible to process the event
func (dS *DispatcherService) GuardianSv1Ping(args *CGREvWithApiKey,
reply *string) (err error) {
if dS.attrS != nil {
if err = dS.authorize(utils.GuardianSv1Ping,
args.CGREvent.Tenant,
args.APIKey, args.CGREvent.Time); err != nil {
return
}
}
return dS.Dispatch(&args.CGREvent, utils.MetaGuardian, args.RouteID,
utils.GuardianSv1Ping, args.CGREvent, reply)
}
// RemoteLock will lock a key from remote
func (dS *DispatcherService) GuardianSv1RemoteLock(args *AttrRemoteLockWithApiKey,
reply *string) (err error) {
if dS.attrS != nil {
if err = dS.authorize(utils.GuardianSv1RemoteLock,
args.TenantArg.Tenant,
args.APIKey, utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaGuardian, args.RouteID,
utils.GuardianSv1RemoteLock, args.AttrRemoteLock, reply)
}
// RemoteUnlock will unlock a key from remote based on reference ID
func (dS *DispatcherService) GuardianSv1RemoteUnlock(args *AttrRemoteUnlockWithApiKey,
reply *[]string) (err error) {
if dS.attrS != nil {
if err = dS.authorize(utils.GuardianSv1RemoteUnlock,
args.TenantArg.Tenant,
args.APIKey, utils.TimePointer(time.Now())); err != nil {
return
}
}
return dS.Dispatch(&utils.CGREvent{Tenant: args.TenantArg.Tenant}, utils.MetaGuardian, args.RouteID,
utils.GuardianSv1RemoteUnlock, args.RefID, reply)
}

View File

@@ -155,6 +155,18 @@ type ArgsGetGroupWithApiKey struct {
engine.ArgsGetGroup
}
type AttrRemoteLockWithApiKey struct {
DispatcherResource
utils.TenantArg
utils.AttrRemoteLock
}
type AttrRemoteUnlockWithApiKey struct {
DispatcherResource
utils.TenantArg
RefID string
}
func ParseStringMap(s string) utils.StringMap {
if s == utils.ZERO {
return make(utils.StringMap)

View File

@@ -1213,6 +1213,12 @@ type TimeInterval struct {
End *time.Time
}
type AttrRemoteLock struct {
ReferenceID string // reference ID for this lock if available
LockIDs []string // List of IDs to obtain lock for
Timeout time.Duration // Automatically unlock on timeout
}
type SMCostFilter struct { //id cu litere mare
CGRIDs []string
NotCGRIDs []string

View File

@@ -389,7 +389,8 @@ const (
MetaResources = "*resources"
MetaFilters = "*filters"
MetaCDRs = "*cdrs"
MetaCache = "*cache"
MetaCaches = "*caches"
MetaGuardian = "*guardians"
Migrator = "migrator"
UnsupportedMigrationTask = "unsupported migration task"
NoStorDBConnection = "not connected to StorDB"
@@ -834,6 +835,14 @@ const (
CacheSv1Ping = "CacheSv1.Ping"
)
// GuardianS APIs
const (
GuardianSv1 = "GuardianSv1"
GuardianSv1RemoteLock = "GuardianSv1.RemoteLock"
GuardianSv1RemoteUnlock = "GuardianSv1.RemoteUnlock"
GuardianSv1Ping = "GuardianSv1.Ping"
)
// Cdrs APIs
const (
CDRsV1CountCDRs = "CDRsV1.CountCDRs"