From c3567208ed88aea9f5126c4a7ea2f6f4428ba741 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 22 Mar 2019 14:50:58 +0200 Subject: [PATCH] Added GuardianSv1 to DispatcherSv1 --- apier/v1/dispatcher.go | 24 +++++++++++++ apier/v1/guardian.go | 21 ++++++----- cmd/cgr-engine/cgr-engine.go | 3 ++ cmd/cgr-engine/rater.go | 2 +- dispatchers/caches.go | 28 +++++++-------- dispatchers/guardian.go | 67 ++++++++++++++++++++++++++++++++++++ dispatchers/utils.go | 12 +++++++ utils/apitpdata.go | 6 ++++ utils/consts.go | 11 +++++- 9 files changed, 149 insertions(+), 25 deletions(-) create mode 100644 dispatchers/guardian.go diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 4241699bf..9f281a507 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -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) +} diff --git a/apier/v1/guardian.go b/apier/v1/guardian.go index 577ff87ff..4c25d613a 100644 --- a/apier/v1/guardian.go +++ b/apier/v1/guardian.go @@ -19,21 +19,18 @@ along with this program. If not, see 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 +} diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 040fbc4dc..8eb5a12f3 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -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 } diff --git a/cmd/cgr-engine/rater.go b/cmd/cgr-engine/rater.go index 32d437dad..e15cf157a 100755 --- a/cmd/cgr-engine/rater.go +++ b/cmd/cgr-engine/rater.go @@ -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) diff --git a/dispatchers/caches.go b/dispatchers/caches.go index d61f4d81a..14850e01e 100644 --- a/dispatchers/caches.go +++ b/dispatchers/caches.go @@ -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) } diff --git a/dispatchers/guardian.go b/dispatchers/guardian.go new file mode 100644 index 000000000..f8393ec16 --- /dev/null +++ b/dispatchers/guardian.go @@ -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 +*/ + +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) +} diff --git a/dispatchers/utils.go b/dispatchers/utils.go index d0baef4f9..0eb633461 100755 --- a/dispatchers/utils.go +++ b/dispatchers/utils.go @@ -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) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index d80223f06..36b22dd42 100755 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -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 diff --git a/utils/consts.go b/utils/consts.go index 93473f330..adeb10282 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -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"