mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-24 16:48:45 +05:00
SessionSv1.InitiateSession integration tests
This commit is contained in:
@@ -176,22 +176,33 @@ func (sma *SMAsterisk) handleChannelStateChange(ev *SMAsteriskEvent) {
|
||||
err := ev.UpdateSMGEvent(smgEv) // Updates the event directly in the cache
|
||||
sma.evCacheMux.Unlock()
|
||||
if err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<SMAsterisk> Error: %s when attempting to initiate session for channelID: %s", err.Error(), ev.ChannelID()))
|
||||
utils.Logger.Err(
|
||||
fmt.Sprintf("<SMAsterisk> Error: %s when attempting to initiate session for channelID: %s",
|
||||
err.Error(), ev.ChannelID()))
|
||||
if err := sma.hangupChannel(ev.ChannelID()); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<SMAsterisk> Error: %s when attempting to disconnect channelID: %s", err.Error(), ev.ChannelID()))
|
||||
utils.Logger.Err(
|
||||
fmt.Sprintf("<SMAsterisk> Error: %s when attempting to disconnect channelID: %s",
|
||||
err.Error(), ev.ChannelID()))
|
||||
}
|
||||
return
|
||||
}
|
||||
var maxUsage time.Duration
|
||||
if err := sma.smg.Call(utils.SMGenericV2InitiateSession, *smgEv, &maxUsage); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<SMAsterisk> Error: %s when attempting to initiate session for channelID: %s", err.Error(), ev.ChannelID()))
|
||||
if err := sma.smg.Call(utils.SMGenericV2InitiateSession,
|
||||
*smgEv, &maxUsage); err != nil {
|
||||
utils.Logger.Err(
|
||||
fmt.Sprintf("<SMAsterisk> Error: %s when attempting to initiate session for channelID: %s",
|
||||
err.Error(), ev.ChannelID()))
|
||||
if err := sma.hangupChannel(ev.ChannelID()); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<SMAsterisk> Error: %s when attempting to disconnect channelID: %s", err.Error(), ev.ChannelID()))
|
||||
utils.Logger.Err(
|
||||
fmt.Sprintf("<SMAsterisk> Error: %s when attempting to disconnect channelID: %s",
|
||||
err.Error(), ev.ChannelID()))
|
||||
}
|
||||
return
|
||||
} else if maxUsage == 0 {
|
||||
if err := sma.hangupChannel(ev.ChannelID()); err != nil {
|
||||
utils.Logger.Err(fmt.Sprintf("<SMAsterisk> Error: %s when attempting to disconnect channelID: %s", err.Error(), ev.ChannelID()))
|
||||
utils.Logger.Err(
|
||||
fmt.Sprintf("<SMAsterisk> Error: %s when attempting to disconnect channelID: %s",
|
||||
err.Error(), ev.ChannelID()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -157,11 +157,11 @@ func (sm *FSSessionManager) onChannelPark(fsev FSEvent, connId string) {
|
||||
}
|
||||
}
|
||||
if authArgs.AuthorizeResources {
|
||||
if _, err := sm.conns[connId].SendApiCmd(fmt.Sprintf("uuid_setvar %s %s %b\n\n",
|
||||
fsev.GetUUID(), CGRResourcesAllowed, authReply.ResourcesAuthorized)); err != nil {
|
||||
if _, err := sm.conns[connId].SendApiCmd(fmt.Sprintf("uuid_setvar %s %s %s\n\n",
|
||||
fsev.GetUUID(), CGRResourceAllocation, authReply.ResourceAllocation)); err != nil {
|
||||
utils.Logger.Info(
|
||||
fmt.Sprintf("<%s> error %s setting channel variabile: %s",
|
||||
utils.SMFreeSWITCH, err.Error(), CGRResourcesAllowed))
|
||||
utils.FreeSWITCHAgent, err.Error(), CGRResourceAllocation))
|
||||
sm.unparkCall(fsev.GetUUID(), connId,
|
||||
fsev.GetCallDestNr(utils.META_DEFAULT), utils.ErrServerError.Error())
|
||||
return
|
||||
@@ -171,7 +171,7 @@ func (sm *FSSessionManager) onChannelPark(fsev FSEvent, connId string) {
|
||||
fsArray := SliceAsFsArray(authReply.Suppliers.SupplierIDs())
|
||||
if _, err := sm.conns[connId].SendApiCmd(fmt.Sprintf("uuid_setvar %s %s %s\n\n",
|
||||
fsev.GetUUID(), utils.CGR_SUPPLIERS, fsArray)); err != nil {
|
||||
utils.Logger.Info(fmt.Sprintf("<SM-FreeSWITCH> LCR_ERROR: %s", err.Error()))
|
||||
utils.Logger.Info(fmt.Sprintf("<%s> error setting suppliers: %s", utils.FreeSWITCHAgent, err.Error()))
|
||||
sm.unparkCall(fsev.GetUUID(), connId, fsev.GetCallDestNr(utils.META_DEFAULT), utils.ErrServerError.Error())
|
||||
return
|
||||
}
|
||||
@@ -184,7 +184,7 @@ func (sm *FSSessionManager) onChannelPark(fsev FSEvent, connId string) {
|
||||
authReply.Attributes.CGREvent.Event[fldName])); err != nil {
|
||||
utils.Logger.Info(
|
||||
fmt.Sprintf("<%s> error %s setting channel variabile: %s",
|
||||
utils.SMFreeSWITCH, err.Error(), fldName))
|
||||
utils.FreeSWITCHAgent, err.Error(), fldName))
|
||||
sm.unparkCall(fsev.GetUUID(), connId,
|
||||
fsev.GetCallDestNr(utils.META_DEFAULT), utils.ErrServerError.Error())
|
||||
return
|
||||
@@ -215,7 +215,7 @@ func (sm *FSSessionManager) onChannelAnswer(fsev FSEvent, connId string) {
|
||||
return
|
||||
}
|
||||
if initSessionArgs.AllocateResources {
|
||||
if initReply.ResAllocMessage == "" {
|
||||
if initReply.ResourceAllocation == nil {
|
||||
sm.DisconnectSession(fsev, connId,
|
||||
utils.ErrUnallocatedResource.Error())
|
||||
}
|
||||
|
||||
@@ -38,43 +38,43 @@ type FSEvent map[string]string
|
||||
|
||||
const (
|
||||
// Freswitch event proprities names
|
||||
DIRECTION = "Call-Direction"
|
||||
SUBJECT = "variable_" + utils.CGR_SUBJECT
|
||||
ACCOUNT = "variable_" + utils.CGR_ACCOUNT
|
||||
DESTINATION = "variable_" + utils.CGR_DESTINATION
|
||||
REQTYPE = "variable_" + utils.CGR_REQTYPE //prepaid or postpaid
|
||||
CATEGORY = "variable_" + utils.CGR_CATEGORY
|
||||
VAR_CGR_SUPPLIER = "variable_" + utils.CGR_SUPPLIER
|
||||
UUID = "Unique-ID" // -Unique ID for this call leg
|
||||
CSTMID = "variable_" + utils.CGR_TENANT
|
||||
CALL_DEST_NR = "Caller-Destination-Number"
|
||||
SIP_REQ_USER = "variable_sip_req_user"
|
||||
PARK_TIME = "Caller-Profile-Created-Time"
|
||||
SETUP_TIME = "Caller-Channel-Created-Time"
|
||||
ANSWER_TIME = "Caller-Channel-Answered-Time"
|
||||
END_TIME = "Caller-Channel-Hangup-Time"
|
||||
DURATION = "variable_billsec"
|
||||
NAME = "Event-Name"
|
||||
HEARTBEAT = "HEARTBEAT"
|
||||
ANSWER = "CHANNEL_ANSWER"
|
||||
HANGUP = "CHANNEL_HANGUP_COMPLETE"
|
||||
PARK = "CHANNEL_PARK"
|
||||
AUTH_OK = "AUTH_OK"
|
||||
DISCONNECT = "SWITCH DISCONNECT"
|
||||
MANAGER_REQUEST = "MANAGER_REQUEST"
|
||||
USERNAME = "Caller-Username"
|
||||
FS_IPv4 = "FreeSWITCH-IPv4"
|
||||
HANGUP_CAUSE = "Hangup-Cause"
|
||||
PDD_MEDIA_MS = "variable_progress_mediamsec"
|
||||
PDD_NOMEDIA_MS = "variable_progressmsec"
|
||||
IGNOREPARK = "variable_cgr_ignorepark"
|
||||
FS_VARPREFIX = "variable_"
|
||||
VarCGRSubsystems = "variable_cgr_subsystems"
|
||||
SubSAccountS = "accounts"
|
||||
SubSSupplierS = "suppliers"
|
||||
SubSResourceS = "resources"
|
||||
SubSAttributeS = "attributes"
|
||||
CGRResourcesAllowed = "cgr_resources_allowed"
|
||||
DIRECTION = "Call-Direction"
|
||||
SUBJECT = "variable_" + utils.CGR_SUBJECT
|
||||
ACCOUNT = "variable_" + utils.CGR_ACCOUNT
|
||||
DESTINATION = "variable_" + utils.CGR_DESTINATION
|
||||
REQTYPE = "variable_" + utils.CGR_REQTYPE //prepaid or postpaid
|
||||
CATEGORY = "variable_" + utils.CGR_CATEGORY
|
||||
VAR_CGR_SUPPLIER = "variable_" + utils.CGR_SUPPLIER
|
||||
UUID = "Unique-ID" // -Unique ID for this call leg
|
||||
CSTMID = "variable_" + utils.CGR_TENANT
|
||||
CALL_DEST_NR = "Caller-Destination-Number"
|
||||
SIP_REQ_USER = "variable_sip_req_user"
|
||||
PARK_TIME = "Caller-Profile-Created-Time"
|
||||
SETUP_TIME = "Caller-Channel-Created-Time"
|
||||
ANSWER_TIME = "Caller-Channel-Answered-Time"
|
||||
END_TIME = "Caller-Channel-Hangup-Time"
|
||||
DURATION = "variable_billsec"
|
||||
NAME = "Event-Name"
|
||||
HEARTBEAT = "HEARTBEAT"
|
||||
ANSWER = "CHANNEL_ANSWER"
|
||||
HANGUP = "CHANNEL_HANGUP_COMPLETE"
|
||||
PARK = "CHANNEL_PARK"
|
||||
AUTH_OK = "AUTH_OK"
|
||||
DISCONNECT = "SWITCH DISCONNECT"
|
||||
MANAGER_REQUEST = "MANAGER_REQUEST"
|
||||
USERNAME = "Caller-Username"
|
||||
FS_IPv4 = "FreeSWITCH-IPv4"
|
||||
HANGUP_CAUSE = "Hangup-Cause"
|
||||
PDD_MEDIA_MS = "variable_progress_mediamsec"
|
||||
PDD_NOMEDIA_MS = "variable_progressmsec"
|
||||
IGNOREPARK = "variable_cgr_ignorepark"
|
||||
FS_VARPREFIX = "variable_"
|
||||
VarCGRSubsystems = "variable_cgr_subsystems"
|
||||
SubSAccountS = "accounts"
|
||||
SubSSupplierS = "suppliers"
|
||||
SubSResourceS = "resources"
|
||||
SubSAttributeS = "attributes"
|
||||
CGRResourceAllocation = "cgr_resource_allocation"
|
||||
|
||||
VAR_CGR_DISCONNECT_CAUSE = "variable_" + utils.CGR_DISCONNECT_CAUSE
|
||||
VAR_CGR_CMPUTELCR = "variable_" + utils.CGR_COMPUTELCR
|
||||
@@ -171,13 +171,13 @@ func (fsev FSEvent) GetReqType(fieldName string) string {
|
||||
}
|
||||
func (fsev FSEvent) MissingParameter(timezone string) string {
|
||||
if strings.TrimSpace(fsev.GetDirection(utils.META_DEFAULT)) == "" {
|
||||
return utils.DIRECTION
|
||||
return utils.Direction
|
||||
}
|
||||
if strings.TrimSpace(fsev.GetAccount(utils.META_DEFAULT)) == "" {
|
||||
return utils.Account
|
||||
}
|
||||
if strings.TrimSpace(fsev.GetSubject(utils.META_DEFAULT)) == "" {
|
||||
return utils.SUBJECT
|
||||
return utils.Subject
|
||||
}
|
||||
if strings.TrimSpace(fsev.GetDestination(utils.META_DEFAULT)) == "" {
|
||||
return utils.Destination
|
||||
|
||||
@@ -115,14 +115,14 @@ func TestSSv1ItAuth(t *testing.T) {
|
||||
authUsage := 5 * time.Minute
|
||||
args := &sessionmanager.V1AuthorizeArgs{
|
||||
GetMaxUsage: true,
|
||||
AuthorizeResources: false,
|
||||
GetSuppliers: false,
|
||||
AuthorizeResources: true,
|
||||
GetSuppliers: true,
|
||||
GetAttributes: true,
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "TestSSv1ItAuth",
|
||||
Event: map[string]interface{}{
|
||||
utils.ACCID: "TestSSv1ItAuth",
|
||||
utils.ACCID: "TestSSv1It",
|
||||
utils.RequestType: utils.META_PREPAID,
|
||||
utils.Account: "1001",
|
||||
utils.Destination: "1002",
|
||||
@@ -138,10 +138,9 @@ func TestSSv1ItAuth(t *testing.T) {
|
||||
if *rply.MaxUsage != authUsage {
|
||||
t.Errorf("Unexpected MaxUsage: %v", rply.MaxUsage)
|
||||
}
|
||||
/*if !*rply.ResourcesAuthorized {
|
||||
t.Errorf("Unexpected ResourceAuthorized: %v", rply.ResourcesAuthorized)
|
||||
if *rply.ResourceAllocation == "" {
|
||||
t.Errorf("Unexpected ResourceAllocation: %s", *rply.ResourceAllocation)
|
||||
}
|
||||
|
||||
eSplrs := &engine.SortedSuppliers{
|
||||
ProfileID: "SPL_ACNT_1001",
|
||||
Sorting: utils.MetaWeight,
|
||||
@@ -163,7 +162,6 @@ func TestSSv1ItAuth(t *testing.T) {
|
||||
if !reflect.DeepEqual(eSplrs, rply.Suppliers) {
|
||||
t.Errorf("expecting: %+v, received: %+v", utils.ToJSON(eSplrs), utils.ToJSON(rply.Suppliers))
|
||||
}
|
||||
*/
|
||||
eAttrs := &engine.AttrSProcessEventReply{
|
||||
MatchedProfile: "ATTR_ACNT_1001",
|
||||
AlteredFields: []string{"OfficeGroup"},
|
||||
@@ -174,7 +172,58 @@ func TestSSv1ItAuth(t *testing.T) {
|
||||
"Account": "1001", "Destination": "1002",
|
||||
"EventName": "CgrAuthorization",
|
||||
"OfficeGroup": "Marketing",
|
||||
"OriginID": "TestSSv1ItAuth",
|
||||
"OriginID": "TestSSv1It",
|
||||
"RequestType": "*prepaid",
|
||||
"SetupTime": "2018-01-07T17:00:00Z",
|
||||
"Usage": 300000000000.0,
|
||||
},
|
||||
},
|
||||
}
|
||||
if !reflect.DeepEqual(eAttrs, rply.Attributes) {
|
||||
t.Errorf("expecting: %+v, received: %+v", utils.ToJSON(eAttrs), utils.ToJSON(rply.Attributes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSSv1ItInitiateSession(t *testing.T) {
|
||||
initUsage := 5 * time.Minute
|
||||
args := &sessionmanager.V1InitSessionArgs{
|
||||
InitSession: true,
|
||||
AllocateResources: true,
|
||||
GetAttributes: true,
|
||||
CGREvent: utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: "TestSSv1ItInitiateSession",
|
||||
Event: map[string]interface{}{
|
||||
utils.ACCID: "TestSSv1It",
|
||||
utils.RequestType: utils.META_PREPAID,
|
||||
utils.Account: "1001",
|
||||
utils.Destination: "1002",
|
||||
utils.SetupTime: time.Date(2018, time.January, 7, 16, 60, 0, 0, time.UTC),
|
||||
utils.Usage: initUsage,
|
||||
},
|
||||
},
|
||||
}
|
||||
var rply sessionmanager.V1InitSessionReply
|
||||
if err := sSv1BiRpc.Call(utils.SessionSv1InitiateSession,
|
||||
args, &rply); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if *rply.MaxUsage != initUsage {
|
||||
t.Errorf("Unexpected MaxUsage: %v", rply.MaxUsage)
|
||||
}
|
||||
if *rply.ResourceAllocation != "RES_ACNT_1001" {
|
||||
t.Errorf("Unexpected ResourceAllocation: %s", *rply.ResourceAllocation)
|
||||
}
|
||||
eAttrs := &engine.AttrSProcessEventReply{
|
||||
MatchedProfile: "ATTR_ACNT_1001",
|
||||
AlteredFields: []string{"OfficeGroup"},
|
||||
CGREvent: &utils.CGREvent{Tenant: "cgrates.org",
|
||||
ID: "TestSSv1ItInitiateSession",
|
||||
Context: utils.StringPointer(utils.MetaSessionS),
|
||||
Event: map[string]interface{}{
|
||||
"Account": "1001", "Destination": "1002",
|
||||
"OfficeGroup": "Marketing",
|
||||
"OriginID": "TestSSv1It",
|
||||
"RequestType": "*prepaid",
|
||||
"SetupTime": "2018-01-07T17:00:00Z",
|
||||
"Usage": 300000000000.0,
|
||||
|
||||
@@ -1326,10 +1326,10 @@ type V1AuthorizeArgs struct {
|
||||
}
|
||||
|
||||
type V1AuthorizeReply struct {
|
||||
MaxUsage *time.Duration
|
||||
ResourcesAuthorized *bool
|
||||
Suppliers *engine.SortedSuppliers
|
||||
Attributes *engine.AttrSProcessEventReply
|
||||
MaxUsage *time.Duration
|
||||
ResourceAllocation *string
|
||||
Suppliers *engine.SortedSuppliers
|
||||
Attributes *engine.AttrSProcessEventReply
|
||||
}
|
||||
|
||||
// BiRPCV1Authorize performs authorization for CGREvent based on specific components
|
||||
@@ -1353,17 +1353,17 @@ func (smg *SMGeneric) BiRPCv1AuthorizeEvent(clnt *rpc2.Client,
|
||||
if err != nil {
|
||||
return utils.NewErrMandatoryIeMissing(utils.ACCID)
|
||||
}
|
||||
var allowed bool
|
||||
var allocMsg string
|
||||
attrRU := utils.ArgRSv1ResourceUsage{
|
||||
CGREvent: args.CGREvent,
|
||||
UsageID: originID,
|
||||
Units: 1,
|
||||
}
|
||||
if err = smg.resS.Call(utils.ResourceSv1AllowUsage,
|
||||
attrRU, &allowed); err != nil {
|
||||
if err = smg.resS.Call(utils.ResourceSv1AuthorizeResources,
|
||||
attrRU, &allocMsg); err != nil {
|
||||
return utils.NewErrResourceS(err)
|
||||
}
|
||||
authReply.ResourcesAuthorized = &allowed
|
||||
authReply.ResourceAllocation = &allocMsg
|
||||
}
|
||||
if args.GetSuppliers {
|
||||
if smg.splS == nil {
|
||||
@@ -1403,9 +1403,9 @@ type V1InitSessionArgs struct {
|
||||
}
|
||||
|
||||
type V1InitSessionReply struct {
|
||||
MaxUsage time.Duration
|
||||
ResAllocMessage string
|
||||
Attributes *engine.AttrSProcessEventReply
|
||||
MaxUsage *time.Duration
|
||||
ResourceAllocation *string
|
||||
Attributes *engine.AttrSProcessEventReply
|
||||
}
|
||||
|
||||
// BiRPCV2InitiateSession initiates a new session, returns the maximum duration the session can last
|
||||
@@ -1429,17 +1429,19 @@ func (smg *SMGeneric) BiRPCv1InitiateSession(clnt *rpc2.Client,
|
||||
attrRU, &allocMessage); err != nil {
|
||||
return err
|
||||
}
|
||||
rply.ResAllocMessage = allocMessage
|
||||
rply.ResourceAllocation = &allocMessage
|
||||
}
|
||||
if args.InitSession {
|
||||
if smg.rals == nil {
|
||||
return utils.NewErrNotConnected(utils.RALService)
|
||||
}
|
||||
if rply.MaxUsage, err = smg.InitiateSession(args.CGREvent.Event, clnt); err != nil {
|
||||
if maxUsage, err := smg.InitiateSession(args.CGREvent.Event, clnt); err != nil {
|
||||
if err != rpcclient.ErrSessionNotFound {
|
||||
err = utils.NewErrServerError(err)
|
||||
}
|
||||
return
|
||||
return err
|
||||
} else {
|
||||
rply.MaxUsage = &maxUsage
|
||||
}
|
||||
}
|
||||
if args.GetAttributes {
|
||||
@@ -1447,7 +1449,7 @@ func (smg *SMGeneric) BiRPCv1InitiateSession(clnt *rpc2.Client,
|
||||
return utils.NewErrNotConnected(utils.AttributeS)
|
||||
}
|
||||
if args.CGREvent.Context == nil {
|
||||
args.CGREvent.Context = utils.StringPointer(utils.MetaSMG)
|
||||
args.CGREvent.Context = utils.StringPointer(utils.MetaSessionS)
|
||||
}
|
||||
var rplyEv engine.AttrSProcessEventReply
|
||||
if err = smg.attrS.Call(utils.AttributeSv1ProcessEvent,
|
||||
|
||||
@@ -529,6 +529,8 @@ const (
|
||||
Cost = "Cost"
|
||||
RatingPlanID = "RatingPlanID"
|
||||
AttributeS = "AttributeS"
|
||||
MetaSessionS = "*sessions"
|
||||
FreeSWITCHAgent = "FreeSWITCHAgent"
|
||||
)
|
||||
|
||||
//Meta
|
||||
@@ -585,10 +587,22 @@ const (
|
||||
|
||||
//ResourceS APIs
|
||||
const (
|
||||
ResourceSv1GetResourcesForEvent = "ResourceSv1.GetResourcesForEvent"
|
||||
ResourceSv1AllocateResource = "ResourceSv1.AllocateResource"
|
||||
ResourceSv1ReleaseResource = "ResourceSv1.ReleaseResource"
|
||||
ResourceSv1AuthorizeResources = "ResourceSv1.AuthorizeResources"
|
||||
ResourceSv1GetResourcesForEvent = "ResourceSv1.GetResourcesForEvent"
|
||||
ResourceSv1AllocateResources = "ResourceSv1.AllocateResources"
|
||||
ResourceSv1ReleaseResources = "ResourceSv1.ReleaseResources"
|
||||
)
|
||||
|
||||
const (
|
||||
SessionSv1AuthorizeEvent = "SessionSv1.AuthorizeEvent"
|
||||
SessionSv1InitiateSession = "SessionSv1.InitiateSession"
|
||||
SessionSv1UpdateSession = "SessionSv1.UpdateSession"
|
||||
SessionSv1TerminateSession = "SessionSv1.TerminateSession"
|
||||
SessionSv1ProcessCDR = "SessionSv1.ProcessCDR"
|
||||
SessionSv1DisconnectSession = "SessionSv1.DisconnectSession"
|
||||
SMGenericV1InitiateSession = "SMGenericV1.InitiateSession"
|
||||
SMGenericV2InitiateSession = "SMGenericV2.InitiateSession"
|
||||
SMGenericV2UpdateSession = "SMGenericV2.UpdateSession"
|
||||
)
|
||||
|
||||
//CSV file name
|
||||
|
||||
Reference in New Issue
Block a user