From 06a292cab406d46fe1dd23b0fb0027f77416da89 Mon Sep 17 00:00:00 2001 From: adragusin Date: Mon, 2 Dec 2019 18:00:34 +0200 Subject: [PATCH] Added a new API: SessionSv1.DeactivateSession --- apier/v1/sessions.go | 5 +++ apier/v1/sessionsbirpc.go | 5 +++ apier/v1/sessionsv1_it_test.go | 60 ++++++++++++++++++++++++++++++++++ sessions/sessions.go | 26 +++++++++++++++ utils/consts.go | 1 + 5 files changed, 97 insertions(+) diff --git a/apier/v1/sessions.go b/apier/v1/sessions.go index 40dcbc705..db2ffb171 100644 --- a/apier/v1/sessions.go +++ b/apier/v1/sessions.go @@ -125,6 +125,11 @@ func (ssv1 *SessionSv1) ActivateSessions(args []string, reply *string) error { return ssv1.Ss.BiRPCv1ActivateSessions(nil, args, reply) } +// DeactivateSessions is called to deactivate a list/all active sessios +func (ssv1 *SessionSv1) DeactivateSessions(args []string, reply *string) error { + return ssv1.Ss.BiRPCv1DeactivateSessions(nil, args, reply) +} + // Call implements rpcclient.RpcClientConnection interface for internal RPC func (ssv1 *SessionSv1) Call(serviceMethod string, args interface{}, reply interface{}) error { diff --git a/apier/v1/sessionsbirpc.go b/apier/v1/sessionsbirpc.go index 03baa1464..f058d5049 100644 --- a/apier/v1/sessionsbirpc.go +++ b/apier/v1/sessionsbirpc.go @@ -150,3 +150,8 @@ func (ssv1 *SessionSv1) BiRPCv1ActivateSessions(clnt *rpc2.Client, args []string, reply *string) error { return ssv1.Ss.BiRPCv1ActivateSessions(clnt, args, reply) } + +func (ssv1 *SessionSv1) BiRPCv1DeactivateSessions(clnt *rpc2.Client, + args []string, reply *string) error { + return ssv1.Ss.BiRPCv1DeactivateSessions(clnt, args, reply) +} diff --git a/apier/v1/sessionsv1_it_test.go b/apier/v1/sessionsv1_it_test.go index 092d961fd..480b1cc3d 100644 --- a/apier/v1/sessionsv1_it_test.go +++ b/apier/v1/sessionsv1_it_test.go @@ -60,6 +60,7 @@ var ( testSSv1ItCDRsGetCdrs, testSSv1ItForceUpdateSession, testSSv1ItDynamicDebit, + testSSv1ItDeactivateSessions, testSSv1ItStopCgrEngine, } ) @@ -953,6 +954,65 @@ func testSSv1ItDynamicDebit(t *testing.T) { } } +func testSSv1ItDeactivateSessions(t *testing.T) { + initUsage := 5 * time.Minute + args := &sessions.V1InitSessionArgs{ + InitSession: true, + AllocateResources: true, + GetAttributes: true, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "TestSSv1ItInitiateSession", + Event: map[string]interface{}{ + utils.Tenant: "cgrates.org", + utils.ToR: utils.VOICE, + utils.OriginID: "TestSSv1It1", + utils.RequestType: sSV1RequestType, + utils.Account: "1001", + utils.Subject: "ANY2CNT", + utils.Destination: "1002", + utils.SetupTime: time.Date(2018, time.January, 7, 16, 60, 0, 0, time.UTC), + utils.AnswerTime: time.Date(2018, time.January, 7, 16, 60, 10, 0, time.UTC), + utils.Usage: initUsage, + }, + }, + } + var rply sessions.V1InitSessionReply + if err := sSv1BiRpc.Call(utils.SessionSv1InitiateSession, + args, &rply); err != nil { + t.Fatal(err) + } + aSessions := make([]*sessions.ExternalSession, 0) + pSessions := make([]*sessions.ExternalSession, 0) + if err := sSv1BiRpc.Call(utils.SessionSv1GetActiveSessions, &utils.SessionFilter{}, &aSessions); err != nil { + t.Error(err) + } else if len(aSessions) != 2 { + t.Errorf("wrong active sessions: %s \n , and len(aSessions) %+v", utils.ToJSON(aSessions), len(aSessions)) + } + if err := sSv1BiRpc.Call(utils.SessionSv1GetPassiveSessions, &utils.SessionFilter{}, &pSessions); err != nil { + t.Error(err) + } else if len(pSessions) != 0 { + t.Errorf("Expecting: 0, received: %+v", len(pSessions)) + } + var reply string + err := sSv1BiRpc.Call(utils.SessionSv1DeactivateSessions, nil, &reply) + if err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Expecting: OK, received : %+v", reply) + } + if err := sSv1BiRpc.Call(utils.SessionSv1GetActiveSessions, &utils.SessionFilter{}, &aSessions); err != nil { + t.Error(err) + } else if len(aSessions) != 0 { + t.Errorf("Expecting: 0, received: %+v", len(aSessions)) + } + if err := sSv1BiRpc.Call(utils.SessionSv1GetPassiveSessions, &utils.SessionFilter{}, &pSessions); err != nil { + t.Error(err) + } else if len(pSessions) != 2 { + t.Errorf("Expecting: 2, received: %+v", len(pSessions)) + } +} + func testSSv1ItStopCgrEngine(t *testing.T) { if err := sSv1BiRpc.Close(); err != nil { // Close the connection so we don't get EOF warnings from client t.Error(err) diff --git a/sessions/sessions.go b/sessions/sessions.go index 04f7b4e16..0183473e8 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -3310,6 +3310,32 @@ func (sS *SessionS) BiRPCv1ActivateSessions(clnt rpcclient.RpcClientConnection, return } +// BiRPCv1DeactivateSessions is called to deactivate a list/all active sessios +// returns utils.ErrPartiallyExecuted in case of errors +func (sS *SessionS) BiRPCv1DeactivateSessions(clnt rpcclient.RpcClientConnection, + sIDs []string, reply *string) (err error) { + if len(sIDs) == 0 { + sS.aSsMux.RLock() + i := 0 + sIDs = make([]string, len(sS.aSessions)) + for sID := range sS.aSessions { + sIDs[i] = sID + i++ + } + sS.aSsMux.RUnlock() + } + for _, sID := range sIDs { + if s := sS.transitSState(sID, true); s == nil { + utils.Logger.Warning(fmt.Sprintf("<%s> no active session with id: <%s>", utils.SessionS, sID)) + err = utils.ErrPartiallyExecuted + } + } + if err == nil { + *reply = utils.OK + } + return +} + // processThreshold will receive the event and send it to ThresholdS to be processed func (sS *SessionS) processThreshold(cgrEv *utils.CGREvent, argDisp *utils.ArgDispatcher, thIDs []string) (tIDs []string, err error) { if sS.thdS == nil { diff --git a/utils/consts.go b/utils/consts.go index 3d34bef37..7dc8298f2 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1243,6 +1243,7 @@ const ( SessionSv1RegisterInternalBiJSONConn = "SessionSv1.RegisterInternalBiJSONConn" SessionSv1ReplicateSessions = "SessionSv1.ReplicateSessions" SessionSv1ActivateSessions = "SessionSv1.ActivateSessions" + SessionSv1DeactivateSessions = "SessionSv1.DeactivateSessions" SMGenericV1InitiateSession = "SMGenericV1.InitiateSession" )