diff --git a/agents/astagent.go b/agents/astagent.go index 33847904d..d6741a43a 100644 --- a/agents/astagent.go +++ b/agents/astagent.go @@ -214,7 +214,7 @@ func (sma *AsteriskAgent) handleStasisStart(ev *SMAsteriskEvent) { } } if authReply.Routes != nil { - for i, route := range authReply.Routes.SortedRoutes { + for i, route := range authReply.Routes.Routes { if !sma.setChannelVar(ev.ChannelID(), CGRRoute+strconv.Itoa(i+1), route.RouteID) { return diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 8b2acd7a9..2c8f56c77 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -346,14 +346,12 @@ func (dRoute *DispatcherRouteSv1) Ping(args *utils.CGREvent, reply *string) erro } // GetRoutes implements RouteSv1GetRoutes -func (dRoute *DispatcherRouteSv1) GetRoutes(args *engine.ArgsGetRoutes, - reply *engine.SortedRoutes) error { +func (dRoute *DispatcherRouteSv1) GetRoutes(args *engine.ArgsGetRoutes, reply *engine.SortedRoutesSet) error { return dRoute.dRoute.RouteSv1GetRoutes(args, reply) } // GetRouteProfilesForEvent returns a list of route profiles that match for Event -func (dRoute *DispatcherRouteSv1) GetRouteProfilesForEvent(args *utils.CGREvent, - reply *[]*engine.RouteProfile) error { +func (dRoute *DispatcherRouteSv1) GetRouteProfilesForEvent(args *utils.CGREvent, reply *[]*engine.RouteProfile) error { return dRoute.dRoute.RouteSv1GetRouteProfilesForEvent(args, reply) } diff --git a/apier/v1/routes.go b/apier/v1/routes.go index 47534e912..69ab8aa4a 100644 --- a/apier/v1/routes.go +++ b/apier/v1/routes.go @@ -128,20 +128,17 @@ type RouteSv1 struct { } // Call implements rpcclient.ClientConnector interface for internal RPC -func (rS *RouteSv1) Call(serviceMethod string, - args interface{}, reply interface{}) error { +func (rS *RouteSv1) Call(serviceMethod string, args interface{}, reply interface{}) error { return utils.APIerRPCCall(rS, serviceMethod, args, reply) } // GetRoutes returns sorted list of routes for Event -func (rS *RouteSv1) GetRoutes(args *engine.ArgsGetRoutes, - reply *engine.SortedRoutes) error { +func (rS *RouteSv1) GetRoutes(args *engine.ArgsGetRoutes, reply *engine.SortedRoutesSet) error { return rS.rS.V1GetRoutes(args, reply) } // GetRouteProfilesForEvent returns a list of route profiles that match for Event -func (rS *RouteSv1) GetRouteProfilesForEvent(args *utils.CGREvent, - reply *[]*engine.RouteProfile) error { +func (rS *RouteSv1) GetRouteProfilesForEvent(args *utils.CGREvent, reply *[]*engine.RouteProfile) error { return rS.rS.V1GetRouteProfilesForEvent(args, reply) } diff --git a/apier/v1/routes_it_test.go b/apier/v1/routes_it_test.go index 8f9019fd7..0555250d7 100644 --- a/apier/v1/routes_it_test.go +++ b/apier/v1/routes_it_test.go @@ -169,7 +169,7 @@ func testV1RouteGetWeightRoutes(t *testing.T) { ProfileID: "ROUTE_WEIGHT_1", Sorting: utils.MetaWeight, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ @@ -221,7 +221,7 @@ func testV1RouteGetLeastCostRoutes(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: utils.MetaLC, Count: 3, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", SortingData: map[string]interface{}{ @@ -275,7 +275,7 @@ func testV1RouteGetLeastCostRoutesWithoutUsage(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: utils.MetaLC, Count: 3, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", SortingData: map[string]interface{}{ @@ -331,7 +331,7 @@ func testV1RouteGetLeastCostRoutesWithMaxCost(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", SortingData: map[string]interface{}{ @@ -402,7 +402,7 @@ func testV1RouteGetLeastCostRoutesWithMaxCost2(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", SortingData: map[string]interface{}{ @@ -449,7 +449,7 @@ func testV1RouteGetHighestCostRoutes(t *testing.T) { ProfileID: "ROUTE_HIGHESTCOST_1", Sorting: utils.MetaHC, Count: 3, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ @@ -661,8 +661,8 @@ func testV1RouteGetQOSRoutes(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_QOS_1" { @@ -692,8 +692,8 @@ func testV1RouteGetQOSRoutes2(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_QOS_2" { @@ -723,8 +723,8 @@ func testV1RouteGetQOSRoutes3(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_QOS_3" { @@ -754,8 +754,8 @@ func testV1RouteGetQOSRoutesFiltred(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_QOS_FILTRED" { @@ -789,8 +789,8 @@ func testV1RouteGetQOSRoutesFiltred2(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_QOS_FILTRED2" { @@ -819,7 +819,7 @@ func testV1RouteGetRouteWithoutFilter(t *testing.T) { ProfileID: "ROUTE_WEIGHT_2", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -1147,7 +1147,7 @@ func testV1RoutesOneRouteWithoutDestination(t *testing.T) { ProfileID: "ROUTE_DESTINATION", Sorting: utils.MetaLC, Count: 1, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "local", SortingData: map[string]interface{}{ @@ -1224,7 +1224,7 @@ func testV1RouteMultipleRouteSameID(t *testing.T) { ProfileID: "MULTIPLE_ROUTES", Sorting: utils.MetaLC, Count: 1, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "Route1", SortingData: map[string]interface{}{ @@ -1262,7 +1262,7 @@ func testV1RouteMultipleRouteSameID(t *testing.T) { ProfileID: "MULTIPLE_ROUTES", Sorting: utils.MetaLC, Count: 1, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "Route1", SortingData: map[string]interface{}{ @@ -1360,7 +1360,7 @@ func testV1RouteAccountWithRatingPlan(t *testing.T) { ProfileID: "RouteWithAccAndRP", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "RouteWithAccAndRP", SortingData: map[string]interface{}{ @@ -1381,7 +1381,7 @@ func testV1RouteAccountWithRatingPlan(t *testing.T) { }, } if *encoding == utils.MetaGOB { - eSpls.SortedRoutes = []*engine.SortedRoute{ + eSpls.Routes = []*engine.SortedRoute{ { RouteID: "RouteWithAccAndRP", SortingData: map[string]interface{}{ @@ -1430,7 +1430,7 @@ func testV1RouteAccountWithRatingPlan(t *testing.T) { ProfileID: "RouteWithAccAndRP", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "RouteWithAccAndRP", SortingData: map[string]interface{}{ @@ -1452,7 +1452,7 @@ func testV1RouteAccountWithRatingPlan(t *testing.T) { }, } if *encoding == utils.MetaGOB { - eSpls.SortedRoutes = []*engine.SortedRoute{ + eSpls.Routes = []*engine.SortedRoute{ { RouteID: "RouteWithAccAndRP", SortingData: map[string]interface{}{ @@ -1502,7 +1502,7 @@ func testV1RouteAccountWithRatingPlan(t *testing.T) { ProfileID: "RouteWithAccAndRP", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "RouteWithRP", SortingData: map[string]interface{}{ @@ -1524,7 +1524,7 @@ func testV1RouteAccountWithRatingPlan(t *testing.T) { }, } if *encoding == utils.MetaGOB { - eSpls.SortedRoutes = []*engine.SortedRoute{ + eSpls.Routes = []*engine.SortedRoute{ { RouteID: "RouteWithRP", SortingData: map[string]interface{}{ diff --git a/apier/v1/sessions_process_event_it_test.go b/apier/v1/sessions_process_event_it_test.go index d2ab57f39..236bd680a 100644 --- a/apier/v1/sessions_process_event_it_test.go +++ b/apier/v1/sessions_process_event_it_test.go @@ -199,7 +199,7 @@ func testSSv1ItProcessEventAuth(t *testing.T) { ProfileID: "ROUTE_ACNT_1001", Sorting: utils.MetaWeight, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ diff --git a/apier/v1/sessionsv1_it_test.go b/apier/v1/sessionsv1_it_test.go index 9d5c40f05..5936cdd92 100644 --- a/apier/v1/sessionsv1_it_test.go +++ b/apier/v1/sessionsv1_it_test.go @@ -236,7 +236,7 @@ func testSSv1ItAuth(t *testing.T) { ProfileID: "ROUTE_ACNT_1001", Sorting: utils.MetaWeight, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ diff --git a/console/routes.go b/console/routes.go index 4c5fc6c0a..b77c070cd 100644 --- a/console/routes.go +++ b/console/routes.go @@ -68,6 +68,6 @@ func (self *CmdRoutesSort) PostprocessRpcParams() error { } func (self *CmdRoutesSort) RpcResult() interface{} { - var atr engine.SortedRoutes + var atr []engine.SortedRoutes return &atr } diff --git a/dispatchers/routes.go b/dispatchers/routes.go index 150cf4de5..a7d9217ce 100755 --- a/dispatchers/routes.go +++ b/dispatchers/routes.go @@ -38,8 +38,7 @@ func (dS *DispatcherService) RouteSv1Ping(args *utils.CGREvent, reply *string) ( return dS.Dispatch(args, utils.MetaRoutes, utils.RouteSv1Ping, args, reply) } -func (dS *DispatcherService) RouteSv1GetRoutes(args *engine.ArgsGetRoutes, - reply *engine.SortedRoutes) (err error) { +func (dS *DispatcherService) RouteSv1GetRoutes(args *engine.ArgsGetRoutes, reply *engine.SortedRoutesSet) (err error) { args.CGREvent.Tenant = utils.FirstNonEmpty(args.CGREvent.Tenant, dS.cfg.GeneralCfg().DefaultTenant) if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { if err = dS.authorize(utils.RouteSv1GetRoutes, @@ -51,8 +50,7 @@ func (dS *DispatcherService) RouteSv1GetRoutes(args *engine.ArgsGetRoutes, return dS.Dispatch(args.CGREvent, utils.MetaRoutes, utils.RouteSv1GetRoutes, args, reply) } -func (dS *DispatcherService) RouteSv1GetRouteProfilesForEvent(args *utils.CGREvent, - reply *[]*engine.RouteProfile) (err error) { +func (dS *DispatcherService) RouteSv1GetRouteProfilesForEvent(args *utils.CGREvent, reply *[]*engine.RouteProfile) (err error) { args.Tenant = utils.FirstNonEmpty(args.Tenant, dS.cfg.GeneralCfg().DefaultTenant) if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { if err = dS.authorize(utils.RouteSv1GetRouteProfilesForEvent, diff --git a/dispatchers/routes_it_test.go b/dispatchers/routes_it_test.go index e05e8d524..9d774fe58 100755 --- a/dispatchers/routes_it_test.go +++ b/dispatchers/routes_it_test.go @@ -129,7 +129,7 @@ func testDspSupGetSupFailover(t *testing.T) { ProfileID: "ROUTE_WEIGHT_2", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", RouteParameters: "", @@ -143,7 +143,7 @@ func testDspSupGetSupFailover(t *testing.T) { ProfileID: "ROUTE_ACNT_1002", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", RouteParameters: "", @@ -230,7 +230,7 @@ func testDspSupTestAuthKey2(t *testing.T) { ProfileID: "ROUTE_ACNT_1002", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", RouteParameters: "", @@ -283,7 +283,7 @@ func testDspSupGetSupRoundRobin(t *testing.T) { ProfileID: "ROUTE_WEIGHT_2", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", RouteParameters: "", @@ -297,7 +297,7 @@ func testDspSupGetSupRoundRobin(t *testing.T) { ProfileID: "ROUTE_ACNT_1002", Sorting: utils.MetaLC, Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route1", RouteParameters: "", diff --git a/engine/libroutes.go b/engine/libroutes.go index 7281a8248..12e0177b5 100644 --- a/engine/libroutes.go +++ b/engine/libroutes.go @@ -35,16 +35,16 @@ type SortedRoute struct { // SortedRoutes is returned as part of GetRoutes call type SortedRoutes struct { - ProfileID string // Profile matched - Sorting string // Sorting algorithm - Count int // number of routes returned - SortedRoutes []*SortedRoute // list of route IDs and SortingData data + ProfileID string // Profile matched + Sorting string // Sorting algorithm + Count int // number of routes returned + Routes []*SortedRoute // list of route IDs and SortingData data } // RouteIDs returns a list of route IDs func (sRoutes *SortedRoutes) RouteIDs() (rIDs []string) { - rIDs = make([]string, len(sRoutes.SortedRoutes)) - for i, sRoute := range sRoutes.SortedRoutes { + rIDs = make([]string, len(sRoutes.Routes)) + for i, sRoute := range sRoutes.Routes { rIDs[i] = sRoute.RouteID } return @@ -52,8 +52,8 @@ func (sRoutes *SortedRoutes) RouteIDs() (rIDs []string) { // RoutesWithParams returns a list of routes IDs with Parameters func (sRoutes *SortedRoutes) RoutesWithParams() (sPs []string) { - sPs = make([]string, len(sRoutes.SortedRoutes)) - for i, spl := range sRoutes.SortedRoutes { + sPs = make([]string, len(sRoutes.Routes)) + for i, spl := range sRoutes.Routes { sPs[i] = spl.RouteID if spl.RouteParameters != "" { sPs[i] += utils.InInFieldSep + spl.RouteParameters @@ -64,39 +64,39 @@ func (sRoutes *SortedRoutes) RoutesWithParams() (sPs []string) { // SortWeight is part of sort interface, sort based on Weight func (sRoutes *SortedRoutes) SortWeight() { - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + sort.Slice(sRoutes.Routes, func(i, j int) bool { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) }) } // SortLeastCost is part of sort interface, // sort ascendent based on Cost with fallback on Weight func (sRoutes *SortedRoutes) SortLeastCost() { - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { - if sRoutes.SortedRoutes[i].SortingData[utils.Cost].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Cost].(float64) { - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + sort.Slice(sRoutes.Routes, func(i, j int) bool { + if sRoutes.Routes[i].SortingData[utils.Cost].(float64) == sRoutes.Routes[j].SortingData[utils.Cost].(float64) { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) } - return sRoutes.SortedRoutes[i].SortingData[utils.Cost].(float64) < sRoutes.SortedRoutes[j].SortingData[utils.Cost].(float64) + return sRoutes.Routes[i].SortingData[utils.Cost].(float64) < sRoutes.Routes[j].SortingData[utils.Cost].(float64) }) } // SortHighestCost is part of sort interface, // sort descendent based on Cost with fallback on Weight func (sRoutes *SortedRoutes) SortHighestCost() { - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { - if sRoutes.SortedRoutes[i].SortingData[utils.Cost].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Cost].(float64) { - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + sort.Slice(sRoutes.Routes, func(i, j int) bool { + if sRoutes.Routes[i].SortingData[utils.Cost].(float64) == sRoutes.Routes[j].SortingData[utils.Cost].(float64) { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) } - return sRoutes.SortedRoutes[i].SortingData[utils.Cost].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Cost].(float64) + return sRoutes.Routes[i].SortingData[utils.Cost].(float64) > sRoutes.Routes[j].SortingData[utils.Cost].(float64) }) } @@ -104,20 +104,20 @@ func (sRoutes *SortedRoutes) SortHighestCost() { // sort based on Stats func (sRoutes *SortedRoutes) SortQOS(params []string) { //sort routes - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { + sort.Slice(sRoutes.Routes, func(i, j int) bool { for _, param := range params { //in case we have the same value for the current param we skip to the next one - if sRoutes.SortedRoutes[i].SortingData[param].(float64) == sRoutes.SortedRoutes[j].SortingData[param].(float64) { + if sRoutes.Routes[i].SortingData[param].(float64) == sRoutes.Routes[j].SortingData[param].(float64) { continue } switch param { default: - if sRoutes.SortedRoutes[i].SortingData[param].(float64) > sRoutes.SortedRoutes[j].SortingData[param].(float64) { + if sRoutes.Routes[i].SortingData[param].(float64) > sRoutes.Routes[j].SortingData[param].(float64) { return true } return false case utils.MetaPDD: //in case of pdd the smallest value if the best - if sRoutes.SortedRoutes[i].SortingData[param].(float64) < sRoutes.SortedRoutes[j].SortingData[param].(float64) { + if sRoutes.Routes[i].SortingData[param].(float64) < sRoutes.Routes[j].SortingData[param].(float64) { return true } return false @@ -125,52 +125,52 @@ func (sRoutes *SortedRoutes) SortQOS(params []string) { } //in case that we have the same value for all params we sort base on weight - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) }) } // SortResourceAscendent is part of sort interface, // sort ascendent based on ResourceUsage with fallback on Weight func (sRoutes *SortedRoutes) SortResourceAscendent() { - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { - if sRoutes.SortedRoutes[i].SortingData[utils.ResourceUsage].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.ResourceUsage].(float64) { - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + sort.Slice(sRoutes.Routes, func(i, j int) bool { + if sRoutes.Routes[i].SortingData[utils.ResourceUsage].(float64) == sRoutes.Routes[j].SortingData[utils.ResourceUsage].(float64) { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) } - return sRoutes.SortedRoutes[i].SortingData[utils.ResourceUsage].(float64) < sRoutes.SortedRoutes[j].SortingData[utils.ResourceUsage].(float64) + return sRoutes.Routes[i].SortingData[utils.ResourceUsage].(float64) < sRoutes.Routes[j].SortingData[utils.ResourceUsage].(float64) }) } // SortResourceDescendent is part of sort interface, // sort descendent based on ResourceUsage with fallback on Weight func (sRoutes *SortedRoutes) SortResourceDescendent() { - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { - if sRoutes.SortedRoutes[i].SortingData[utils.ResourceUsage].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.ResourceUsage].(float64) { - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + sort.Slice(sRoutes.Routes, func(i, j int) bool { + if sRoutes.Routes[i].SortingData[utils.ResourceUsage].(float64) == sRoutes.Routes[j].SortingData[utils.ResourceUsage].(float64) { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) } - return sRoutes.SortedRoutes[i].SortingData[utils.ResourceUsage].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.ResourceUsage].(float64) + return sRoutes.Routes[i].SortingData[utils.ResourceUsage].(float64) > sRoutes.Routes[j].SortingData[utils.ResourceUsage].(float64) }) } // SortLoadDistribution is part of sort interface, // sort based on the following formula (float64(ratio + metricVal) / float64(ratio)) -1 with fallback on Weight func (sRoutes *SortedRoutes) SortLoadDistribution() { - sort.Slice(sRoutes.SortedRoutes, func(i, j int) bool { - splIVal := ((sRoutes.SortedRoutes[i].SortingData[utils.Ratio].(float64)+sRoutes.SortedRoutes[i].SortingData[utils.Load].(float64))/sRoutes.SortedRoutes[i].SortingData[utils.Ratio].(float64) - 1.0) - splJVal := ((sRoutes.SortedRoutes[j].SortingData[utils.Ratio].(float64)+sRoutes.SortedRoutes[j].SortingData[utils.Load].(float64))/sRoutes.SortedRoutes[j].SortingData[utils.Ratio].(float64) - 1.0) + sort.Slice(sRoutes.Routes, func(i, j int) bool { + splIVal := ((sRoutes.Routes[i].SortingData[utils.Ratio].(float64)+sRoutes.Routes[i].SortingData[utils.Load].(float64))/sRoutes.Routes[i].SortingData[utils.Ratio].(float64) - 1.0) + splJVal := ((sRoutes.Routes[j].SortingData[utils.Ratio].(float64)+sRoutes.Routes[j].SortingData[utils.Load].(float64))/sRoutes.Routes[j].SortingData[utils.Ratio].(float64) - 1.0) if splIVal == splJVal { - if sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) == sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) { + if sRoutes.Routes[i].SortingData[utils.Weight].(float64) == sRoutes.Routes[j].SortingData[utils.Weight].(float64) { return utils.BoolGenerator().RandomBool() } - return sRoutes.SortedRoutes[i].SortingData[utils.Weight].(float64) > sRoutes.SortedRoutes[j].SortingData[utils.Weight].(float64) + return sRoutes.Routes[i].SortingData[utils.Weight].(float64) > sRoutes.Routes[j].SortingData[utils.Weight].(float64) } return splIVal < splJVal }) @@ -201,8 +201,8 @@ func (sRoutes *SortedRoutes) AsNavigableMap() (nm utils.NavigableMap2) { utils.Sorting: utils.NewNMData(sRoutes.Sorting), utils.Count: utils.NewNMData(sRoutes.Count), } - sr := make(utils.NMSlice, len(sRoutes.SortedRoutes)) - for i, ss := range sRoutes.SortedRoutes { + sr := make(utils.NMSlice, len(sRoutes.Routes)) + for i, ss := range sRoutes.Routes { sr[i] = ss.AsNavigableMap() } nm[utils.SortedRoutes] = &sr @@ -240,8 +240,50 @@ func (ssd RouteSortDispatcher) SortRoutes(prflID, strategy string, if sortedRoutes, err = sd.SortRoutes(prflID, suppls, suplEv, extraOpts); err != nil { return } - if len(sortedRoutes.SortedRoutes) == 0 { + if len(sortedRoutes.Routes) == 0 { return nil, utils.ErrNotFound } return } + +// SortedRoutesSet represents the list of matched routes grouped based of profile +type SortedRoutesSet []*SortedRoutes + +// RouteIDs returns a list of route IDs +func (sRs SortedRoutesSet) RouteIDs() (rIDs []string) { + for _, sR := range sRs { + for _, r := range sR.Routes { + rIDs = append(rIDs, r.RouteID) + } + } + return +} + +// RoutesWithParams returns a list of routes IDs with Parameters +func (sRs SortedRoutesSet) RoutesWithParams() (sPs []string) { + for _, sR := range sRs { + for _, spl := range sR.Routes { + route := spl.RouteID + if spl.RouteParameters != "" { + route += utils.InInFieldSep + spl.RouteParameters + } + sPs = append(sPs, route) + } + } + return +} + +// Digest returns list of routeIDs + parameters for easier outside access +// format route1:route1params,route2:route2params +func (sRs SortedRoutesSet) Digest() string { + return strings.Join(sRs.RoutesWithParams(), utils.FieldsSep) +} + +// AsNavigableMap returns the SortedRoutesSet as NMInterface object +func (sRs SortedRoutesSet) AsNavigableMap() (nm utils.NMSlice) { + nm = make(utils.NMSlice, len(sRs)) + for i, ss := range sRs { + nm[i] = ss.AsNavigableMap() + } + return +} diff --git a/engine/libroutes_test.go b/engine/libroutes_test.go index 54d29d3fa..0d07653ff 100644 --- a/engine/libroutes_test.go +++ b/engine/libroutes_test.go @@ -28,7 +28,7 @@ import ( func TestLibSuppliersSortCost(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -57,7 +57,7 @@ func TestLibSuppliersSortCost(t *testing.T) { } sSpls.SortLeastCost() eOrderedSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route3", SortingData: map[string]interface{}{ @@ -92,7 +92,7 @@ func TestLibSuppliersSortCost(t *testing.T) { func TestLibRoutesSortWeight(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -118,7 +118,7 @@ func TestLibRoutesSortWeight(t *testing.T) { } sSpls.SortWeight() eOrderedSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ @@ -152,7 +152,7 @@ func TestSortedRoutesDigest(t *testing.T) { eSpls := SortedRoutes{ ProfileID: "SPL_WEIGHT_1", Sorting: utils.MetaWeight, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ @@ -180,7 +180,7 @@ func TestSortedRoutesDigest2(t *testing.T) { eSpls := SortedRoutes{ ProfileID: "SPL_WEIGHT_1", Sorting: utils.MetaWeight, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -206,9 +206,9 @@ func TestSortedRoutesDigest2(t *testing.T) { func TestSortedRoutesDigest3(t *testing.T) { eSpls := SortedRoutes{ - ProfileID: "SPL_WEIGHT_1", - Sorting: utils.MetaWeight, - SortedRoutes: []*SortedRoute{}, + ProfileID: "SPL_WEIGHT_1", + Sorting: utils.MetaWeight, + Routes: []*SortedRoute{}, } exp := "" rcv := eSpls.Digest() @@ -219,7 +219,7 @@ func TestSortedRoutesDigest3(t *testing.T) { func TestLibRoutesSortHighestCost(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -248,7 +248,7 @@ func TestLibRoutesSortHighestCost(t *testing.T) { } sSpls.SortHighestCost() eOrderedSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ @@ -284,7 +284,7 @@ func TestLibRoutesSortHighestCost(t *testing.T) { //sort based on *acd and *tcd func TestLibRoutesSortQOS(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { //the average value for route1 for *acd is 0.5 , *tcd 1.1 RouteID: "route1", @@ -320,9 +320,9 @@ func TestLibRoutesSortQOS(t *testing.T) { //sort base on *acd and *tcd sSpls.SortQOS([]string{utils.MetaACD, utils.MetaTCD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route2", "route1", "route3"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -335,7 +335,7 @@ func TestLibRoutesSortQOS(t *testing.T) { //sort based on *acd and *tcd func TestLibRoutesSortQOS2(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { //the average value for route1 for *acd is 0.5 , *tcd 1.1 RouteID: "route1", @@ -369,9 +369,9 @@ func TestLibRoutesSortQOS2(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaACD, utils.MetaTCD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route3", "route2", "route1"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -384,7 +384,7 @@ func TestLibRoutesSortQOS2(t *testing.T) { //sort based on *pdd func TestLibRoutesSortQOS3(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { //the worst value for route1 for *pdd is 0.7 , *tcd 1.1 //route1 and route3 have the same value for *pdd @@ -417,9 +417,9 @@ func TestLibRoutesSortQOS3(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaPDD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route1", "route3", "route2"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -431,7 +431,7 @@ func TestLibRoutesSortQOS3(t *testing.T) { func TestLibRoutesSortQOS4(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -459,9 +459,9 @@ func TestLibRoutesSortQOS4(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaASR, utils.MetaACD, utils.MetaTCD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route1", "route3", "route2"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -473,7 +473,7 @@ func TestLibRoutesSortQOS4(t *testing.T) { func TestLibRoutesSortQOS5(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -504,9 +504,9 @@ func TestLibRoutesSortQOS5(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaTCC, utils.MetaASR, utils.MetaACD, utils.MetaTCD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route2", "route3", "route1"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -518,7 +518,7 @@ func TestLibRoutesSortQOS5(t *testing.T) { func TestLibRoutesSortQOS6(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -543,9 +543,9 @@ func TestLibRoutesSortQOS6(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaACD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route2", "route1", "route3"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -557,7 +557,7 @@ func TestLibRoutesSortQOS6(t *testing.T) { func TestLibRoutesSortQOS7(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -582,9 +582,9 @@ func TestLibRoutesSortQOS7(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaACD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route2", "route3", "route1"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -596,7 +596,7 @@ func TestLibRoutesSortQOS7(t *testing.T) { func TestLibRoutesSortQOS8(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -621,9 +621,9 @@ func TestLibRoutesSortQOS8(t *testing.T) { }, } sSpls.SortQOS([]string{utils.MetaACD}) - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route3", "route2", "route1"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } @@ -635,7 +635,7 @@ func TestLibRoutesSortQOS8(t *testing.T) { func TestLibRoutesSortLoadDistribution(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -663,9 +663,9 @@ func TestLibRoutesSortLoadDistribution(t *testing.T) { }, } sSpls.SortLoadDistribution() - rcv := make([]string, len(sSpls.SortedRoutes)) + rcv := make([]string, len(sSpls.Routes)) eIds := []string{"route2", "route1", "route3"} - for i, spl := range sSpls.SortedRoutes { + for i, spl := range sSpls.Routes { rcv[i] = spl.RouteID } if !reflect.DeepEqual(eIds, rcv) { @@ -682,8 +682,8 @@ func TestLibRoutesLCSameWeight(t *testing.T) { utils.Cost: 0.1, utils.Weight: 10.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortLeastCost() @@ -704,8 +704,8 @@ func TestLibRoutesHCSameWeight(t *testing.T) { utils.Cost: 0.1, utils.Weight: 10.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortHighestCost() @@ -726,8 +726,8 @@ func TestLibRoutesResAscSameWeight(t *testing.T) { utils.ResourceUsage: 5.0, utils.Weight: 10.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortResourceAscendent() @@ -748,8 +748,8 @@ func TestLibRoutesResDescSameWeight(t *testing.T) { utils.ResourceUsage: 5.0, utils.Weight: 10.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortResourceDescendent() @@ -772,8 +772,8 @@ func TestLibRoutesLoadDistSameWeight(t *testing.T) { utils.Load: 3.0, utils.Weight: 10.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortLoadDistribution() @@ -794,8 +794,8 @@ func TestLibRoutesQOSSameWeight(t *testing.T) { utils.Weight: 10.0, utils.MetaACD: -1.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortQOS([]string{utils.MetaACD}) @@ -815,8 +815,8 @@ func TestLibRoutesSameWeight(t *testing.T) { route := &SortedRoute{RouteID: strconv.Itoa(i), SortingData: map[string]interface{}{ utils.Weight: 10.0, }} - sSpls.SortedRoutes = append(sSpls.SortedRoutes, route) - sortedSlice.SortedRoutes = append(sortedSlice.SortedRoutes, route) + sSpls.Routes = append(sSpls.Routes, route) + sortedSlice.Routes = append(sortedSlice.Routes, route) } for i := 0; i < 3; i++ { sSpls.SortWeight() @@ -831,7 +831,7 @@ func TestLibRoutesSameWeight(t *testing.T) { func BenchmarkRouteSortCost(b *testing.B) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -866,7 +866,7 @@ func BenchmarkRouteSortCost(b *testing.B) { func TestRouteIDsGetIDs(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -904,7 +904,7 @@ func TestRouteIDsGetIDs(t *testing.T) { func TestSortHighestCost(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -930,7 +930,7 @@ func TestSortHighestCost(t *testing.T) { func TestSortResourceAscendentDescendent(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ @@ -961,7 +961,7 @@ func TestSortResourceAscendentDescendent(t *testing.T) { } //SortingResourceAscendent/Descendent while ResourceUsages are not equal - sSpls.SortedRoutes[0].SortingData[utils.ResourceUsage] = 11.0 + sSpls.Routes[0].SortingData[utils.ResourceUsage] = 11.0 expSRts = sSpls sSpls.SortResourceAscendent() if !reflect.DeepEqual(expSRts, sSpls) { @@ -976,7 +976,7 @@ func TestSortResourceAscendentDescendent(t *testing.T) { func TestSortLoadDistribution(t *testing.T) { sSpls := &SortedRoutes{ - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "ROUTE1", SortingData: map[string]interface{}{ @@ -1031,7 +1031,7 @@ func TestSortedRoutesAsNavigableMap(t *testing.T) { ProfileID: "TEST_ID1", Sorting: utils.MetaWeight, Count: 100, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "ROUTE1", RouteParameters: "SORTING_PARAMETER", diff --git a/engine/route_highestcost.go b/engine/route_highestcost.go index b87c9682a..9346dea3c 100755 --- a/engine/route_highestcost.go +++ b/engine/route_highestcost.go @@ -38,8 +38,8 @@ type HightCostSorter struct { func (hcs *HightCostSorter) SortRoutes(prflID string, routes map[string]*Route, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: hcs.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + Sorting: hcs.sorting, + Routes: make([]*SortedRoute, 0)} for _, route := range routes { if len(route.RatingPlanIDs) == 0 && len(route.AccountIDs) == 0 { utils.Logger.Warning( @@ -50,7 +50,7 @@ func (hcs *HightCostSorter) SortRoutes(prflID string, routes map[string]*Route, if srtSpl, pass, err := hcs.rS.populateSortingData(ev, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtSpl) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) } } sortedRoutes.SortHighestCost() diff --git a/engine/route_leastcost.go b/engine/route_leastcost.go index c74cad5ef..23a6e48e4 100644 --- a/engine/route_leastcost.go +++ b/engine/route_leastcost.go @@ -38,8 +38,8 @@ type LeastCostSorter struct { func (lcs *LeastCostSorter) SortRoutes(prflID string, routes map[string]*Route, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: lcs.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + Sorting: lcs.sorting, + Routes: make([]*SortedRoute, 0)} for _, s := range routes { if len(s.RatingPlanIDs) == 0 && len(s.AccountIDs) == 0 { utils.Logger.Warning( @@ -50,7 +50,7 @@ func (lcs *LeastCostSorter) SortRoutes(prflID string, routes map[string]*Route, if srtSpl, pass, err := lcs.rS.populateSortingData(ev, s, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtSpl) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) } } sortedRoutes.SortLeastCost() diff --git a/engine/route_load_distribution.go b/engine/route_load_distribution.go index fe23e104f..fb8dbcc88 100644 --- a/engine/route_load_distribution.go +++ b/engine/route_load_distribution.go @@ -39,9 +39,11 @@ type LoadDistributionSorter struct { // SortRoutes . func (ws *LoadDistributionSorter) SortRoutes(prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { - sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: ws.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + sortedRoutes = &SortedRoutes{ + ProfileID: prflID, + Sorting: ws.sorting, + Routes: make([]*SortedRoute, 0), + } for _, route := range routes { // we should have at least 1 statID defined for counting CDR (a.k.a *sum:1) if len(route.StatIDs) == 0 { @@ -61,7 +63,7 @@ func (ws *LoadDistributionSorter) SortRoutes(prflID string, utils.RouteS, route.cacheRoute[utils.MetaRatio], route.ID)) } srtSpl.SortingData[utils.Ratio] = floatRatio - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtSpl) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) } } sortedRoutes.SortLoadDistribution() diff --git a/engine/route_qos.go b/engine/route_qos.go index cb2165b87..a16abb477 100755 --- a/engine/route_qos.go +++ b/engine/route_qos.go @@ -36,13 +36,13 @@ type QOSRouteSorter struct { func (qos *QOSRouteSorter) SortRoutes(prflID string, routes map[string]*Route, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: qos.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + Sorting: qos.sorting, + Routes: make([]*SortedRoute, 0)} for _, route := range routes { if srtSpl, pass, err := qos.rS.populateSortingData(ev, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtSpl) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) } } sortedRoutes.SortQOS(extraOpts.sortingParameters) diff --git a/engine/route_reas.go b/engine/route_reas.go index 5d3119adc..16f57dbf6 100644 --- a/engine/route_reas.go +++ b/engine/route_reas.go @@ -38,8 +38,8 @@ type ResourceAscendentSorter struct { func (ws *ResourceAscendentSorter) SortRoutes(prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: ws.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + Sorting: ws.sorting, + Routes: make([]*SortedRoute, 0)} for _, route := range routes { if len(route.ResourceIDs) == 0 { utils.Logger.Warning( @@ -50,7 +50,7 @@ func (ws *ResourceAscendentSorter) SortRoutes(prflID string, if srtSpl, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtSpl) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) } } sortedRoutes.SortResourceAscendent() diff --git a/engine/route_reds.go b/engine/route_reds.go index 51748eaab..23b84c439 100644 --- a/engine/route_reds.go +++ b/engine/route_reds.go @@ -38,8 +38,8 @@ type ResourceDescendentSorter struct { func (ws *ResourceDescendentSorter) SortRoutes(prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: ws.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + Sorting: ws.sorting, + Routes: make([]*SortedRoute, 0)} for _, route := range routes { if len(route.ResourceIDs) == 0 { utils.Logger.Warning( @@ -50,7 +50,7 @@ func (ws *ResourceDescendentSorter) SortRoutes(prflID string, if srtSpl, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtSpl) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) } } sortedRoutes.SortResourceDescendent() diff --git a/engine/route_weight.go b/engine/route_weight.go index 3f72fb146..b914e58fe 100755 --- a/engine/route_weight.go +++ b/engine/route_weight.go @@ -36,13 +36,13 @@ type WeightSorter struct { func (ws *WeightSorter) SortRoutes(prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, - Sorting: ws.sorting, - SortedRoutes: make([]*SortedRoute, 0)} + Sorting: ws.sorting, + Routes: make([]*SortedRoute, 0)} for _, route := range routes { if srtRoute, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtRoute != nil { - sortedRoutes.SortedRoutes = append(sortedRoutes.SortedRoutes, srtRoute) + sortedRoutes.Routes = append(sortedRoutes.Routes, srtRoute) } } sortedRoutes.SortWeight() diff --git a/engine/routes.go b/engine/routes.go index 14ee5cbb7..e6ef3131b 100644 --- a/engine/routes.go +++ b/engine/routes.go @@ -505,65 +505,6 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, return sortedSpl, true, nil } -// sortedRoutesForEvent will return the list of valid route IDs -// for event based on filters and sorting algorithms -func (rpS *RouteService) sortedRoutesForEvent(tnt string, args *ArgsGetRoutes) (sortedRoutes *SortedRoutes, err error) { - if _, has := args.CGREvent.Event[utils.Usage]; !has { - args.CGREvent.Event[utils.Usage] = time.Minute // make sure we have default set for Usage - } - var rPrfs []*RouteProfile - if rPrfs, err = rpS.matchingRouteProfilesForEvent(tnt, args.CGREvent, true); err != nil { - return - } - rPrfl := rPrfs[0] - extraOpts, err := args.asOptsGetRoutes() // convert routes arguments into internal options used to limit data - if err != nil { - return nil, err - } - extraOpts.sortingParameters = rPrfl.SortingParameters // populate sortingParameters in extraOpts - extraOpts.sortingStragety = rPrfl.Sorting // populate sortingStrategy in extraOpts - - //construct the DP and pass it to filterS - nM := utils.MapStorage{utils.MetaReq: args.Event} - passedRoutes := make(map[string]*Route) - // apply filters for event - for _, route := range rPrfl.Routes { - pass, lazyCheckRules, err := rpS.filterS.LazyPass(tnt, - route.FilterIDs, nM, - []string{utils.DynamicDataPrefix + utils.MetaReq, - utils.DynamicDataPrefix + utils.MetaAccounts, - utils.DynamicDataPrefix + utils.MetaResources, - utils.DynamicDataPrefix + utils.MetaStats}) - if err != nil { - return nil, err - } else if !pass { - continue - } - route.lazyCheckRules = lazyCheckRules - if prev, has := passedRoutes[route.ID]; has && prev.Weight >= route.Weight { - continue - } - passedRoutes[route.ID] = route - } - sortedRoutes, err = rpS.sorter.SortRoutes(rPrfl.ID, rPrfl.Sorting, - passedRoutes, args.CGREvent, extraOpts) - if err != nil { - return nil, err - } - if args.Paginator.Offset != nil { - if *args.Paginator.Offset <= len(sortedRoutes.SortedRoutes) { - sortedRoutes.SortedRoutes = sortedRoutes.SortedRoutes[*args.Paginator.Offset:] - } - } - if args.Paginator.Limit != nil { - if *args.Paginator.Limit <= len(sortedRoutes.SortedRoutes) { - sortedRoutes.SortedRoutes = sortedRoutes.SortedRoutes[:*args.Paginator.Limit] - } - } - sortedRoutes.Count = len(sortedRoutes.SortedRoutes) - return -} - // ArgsGetRoutes the argument for GetRoutes API type ArgsGetRoutes struct { IgnoreErrors bool @@ -630,7 +571,7 @@ type optsGetRoutes struct { } // V1GetRoutes returns the list of valid routes -func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutes) (err error) { +func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutesSet) (err error) { if args.CGREvent == nil { return utils.NewErrMandatoryIeMissing(utils.CGREventString) } @@ -677,7 +618,7 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutes) ( } return err } - *reply = *sSps + *reply = sSps return } @@ -702,3 +643,113 @@ func (rpS *RouteService) V1GetRouteProfilesForEvent(args *utils.CGREvent, reply *reply = sPs return } + +// sortedRoutesForEvent will return the list of valid route IDs +// for event based on filters and sorting algorithms +func (rpS *RouteService) sortedRoutesForProfile(tnt string, rPrfl *RouteProfile, ev *utils.CGREvent, pag utils.Paginator, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { + extraOpts.sortingParameters = rPrfl.SortingParameters // populate sortingParameters in extraOpts + extraOpts.sortingStragety = rPrfl.Sorting // populate sortingStrategy in extraOpts + //construct the DP and pass it to filterS + nM := utils.MapStorage{ + utils.MetaReq: ev.Event, + utils.MetaOpts: ev.Opts, + } + passedRoutes := make(map[string]*Route) + // apply filters for event + for _, route := range rPrfl.Routes { + pass, lazyCheckRules, err := rpS.filterS.LazyPass(tnt, + route.FilterIDs, nM, + []string{utils.DynamicDataPrefix + utils.MetaReq, + utils.DynamicDataPrefix + utils.MetaAccounts, + utils.DynamicDataPrefix + utils.MetaResources, + utils.DynamicDataPrefix + utils.MetaStats}) + if err != nil { + return nil, err + } else if !pass { + continue + } + route.lazyCheckRules = lazyCheckRules + if prev, has := passedRoutes[route.ID]; has && prev.Weight >= route.Weight { + continue + } + passedRoutes[route.ID] = route + } + + if sortedRoutes, err = rpS.sorter.SortRoutes(rPrfl.ID, rPrfl.Sorting, + passedRoutes, ev, extraOpts); err != nil { + return nil, err + } + if pag.Offset != nil { + if *pag.Offset <= len(sortedRoutes.Routes) { + sortedRoutes.Routes = sortedRoutes.Routes[*pag.Offset:] + } + } + if pag.Limit != nil { + if *pag.Limit <= len(sortedRoutes.Routes) { + sortedRoutes.Routes = sortedRoutes.Routes[:*pag.Limit] + } + } + sortedRoutes.Count = len(sortedRoutes.Routes) + return +} + +// sortedRoutesForEvent will return the list of valid route IDs +// for event based on filters and sorting algorithms +func (rpS *RouteService) sortedRoutesForEvent(tnt string, args *ArgsGetRoutes) (sortedRoutes SortedRoutesSet, err error) { + if _, has := args.CGREvent.Event[utils.Usage]; !has { + args.CGREvent.Event[utils.Usage] = time.Minute // make sure we have default set for Usage + } + var rPrfs []*RouteProfile + if rPrfs, err = rpS.matchingRouteProfilesForEvent(tnt, args.CGREvent, true); err != nil { + return + } + prfCount := len(rPrfs) // if the option is not present return for all profiles + if prfCountOpt, err := args.OptAsInt64(utils.OptsRouteProfilesCount); err != nil { + if err != utils.ErrNotFound { // is an conversion error + return nil, err + } + } else if prfCount > int(prfCountOpt) { // it has the option and is smaller that the current number of profiles + prfCount = int(prfCountOpt) + } + var extraOpts *optsGetRoutes + if extraOpts, err = args.asOptsGetRoutes(); err != nil { // convert routes arguments into internal options used to limit data + return + } + + var startIdx, noSrtRoutes int + if args.Paginator.Offset != nil { // save the offset in a varible to not duble check if we have offset and is still not 0 + startIdx = *args.Paginator.Offset + } + sortedRoutes = make(SortedRoutesSet, 0, prfCount) + for _, rPrfl := range rPrfs { + var prfPag utils.Paginator + if args.Paginator.Limit != nil { // we have a limit + if noSrtRoutes >= *args.Paginator.Limit { // the limit was reached return + return + } + if noSrtRoutes+len(rPrfl.Routes) > *args.Paginator.Limit { // the limit will be reached in this profile + limit := *args.Paginator.Limit - noSrtRoutes // make it relative to current profile + prfPag.Limit = &limit // add the limit to the paginator + } + } + if startIdx > 0 { // we have offest + if startIdx -= len(rPrfl.Routes); startIdx >= 0 { // we still have offset so try the next profile + continue + } + // we have offset but is in the range of this profile + offset := -startIdx + prfPag.Offset = &offset + } + var sr *SortedRoutes + if sr, err = rpS.sortedRoutesForProfile(tnt, rPrfl, args.CGREvent, prfPag, extraOpts); err != nil { + return + } + noSrtRoutes += sr.Count + sortedRoutes = append(sortedRoutes, sr) + if len(sortedRoutes) == prfCount { // the profile count was reached + return + } + + } + return +} diff --git a/engine/routes_test.go b/engine/routes_test.go index 2c78782dc..9af3b1af3 100644 --- a/engine/routes_test.go +++ b/engine/routes_test.go @@ -800,7 +800,7 @@ func TestRoutesSortedForEvent(t *testing.T) { ProfileID: "RouteProfile1", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -822,7 +822,7 @@ func TestRoutesSortedForEvent(t *testing.T) { ProfileID: "RouteProfile2", Sorting: utils.MetaWeight, Count: 3, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -859,7 +859,7 @@ func TestRoutesSortedForEvent(t *testing.T) { ProfileID: "RouteProfilePrefix", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -1111,7 +1111,7 @@ func TestRoutesSortedForEventWithLimit(t *testing.T) { ProfileID: "RouteProfile2", Sorting: utils.MetaWeight, Count: 2, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route1", SortingData: map[string]interface{}{ @@ -1372,7 +1372,7 @@ func TestRoutesSortedForEventWithOffset(t *testing.T) { ProfileID: "RouteProfile2", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route3", SortingData: map[string]interface{}{ @@ -1626,7 +1626,7 @@ func TestRoutesSortedForEventWithLimitAndOffset(t *testing.T) { ProfileID: "RouteProfile2", Sorting: utils.MetaWeight, Count: 1, - SortedRoutes: []*SortedRoute{ + Routes: []*SortedRoute{ { RouteID: "route2", SortingData: map[string]interface{}{ diff --git a/general_tests/route_it_test.go b/general_tests/route_it_test.go index 63a993530..8f4f4cb29 100644 --- a/general_tests/route_it_test.go +++ b/general_tests/route_it_test.go @@ -450,8 +450,8 @@ func testV1SplSGetSortedSuppliers(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_ResourceTest" { @@ -534,8 +534,8 @@ func testV1SplSGetSortedSuppliers2(t *testing.T) { ev, &suplsReply); err != nil { t.Error(err) } else { - rcvSupl := make([]string, len(suplsReply.SortedRoutes)) - for i, supl := range suplsReply.SortedRoutes { + rcvSupl := make([]string, len(suplsReply.Routes)) + for i, supl := range suplsReply.Routes { rcvSupl[i] = supl.RouteID } if suplsReply.ProfileID != "ROUTE_ResourceDescendent" { @@ -752,9 +752,9 @@ func testV1SplSGetSoredSuppliersWithLoad(t *testing.T) { t.Errorf("Expecting: ROUTE_LOAD_DIST, received: %s", suplsReply.ProfileID) } - if !reflect.DeepEqual(suplsReply.SortedRoutes, expSuppliers) { + if !reflect.DeepEqual(suplsReply.Routes, expSuppliers) { t.Errorf("Expecting: %+v, \n received: %+v", - utils.ToJSON(expSuppliers), utils.ToJSON(suplsReply.SortedRoutes)) + utils.ToJSON(expSuppliers), utils.ToJSON(suplsReply.Routes)) } } } diff --git a/general_tests/sessionroutes_it_test.go b/general_tests/sessionroutes_it_test.go index cf4de395a..031ce796f 100644 --- a/general_tests/sessionroutes_it_test.go +++ b/general_tests/sessionroutes_it_test.go @@ -145,7 +145,7 @@ func testSesRoutesAuthorizeEvent(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: "*lc", Count: 3, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", RouteParameters: "", @@ -196,7 +196,7 @@ func testSesRoutesAuthorizeEvent(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: "*lc", Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", RouteParameters: "", @@ -278,7 +278,7 @@ func testSesRoutesProcessMessage(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: "*lc", Count: 3, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", RouteParameters: "", @@ -330,7 +330,7 @@ func testSesRoutesProcessMessage(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: "*lc", Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", RouteParameters: "", @@ -414,7 +414,7 @@ func testSesRoutesProcessEvent(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: "*lc", Count: 3, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", RouteParameters: "", @@ -470,7 +470,7 @@ func testSesRoutesProcessEvent(t *testing.T) { ProfileID: "ROUTE_LEASTCOST_1", Sorting: "*lc", Count: 2, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "route3", RouteParameters: "", diff --git a/sessions/sessions.go b/sessions/sessions.go index 44e0349e9..9b5192091 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1856,7 +1856,7 @@ type V1AuthorizeReply struct { Attributes *engine.AttrSProcessEventReply `json:",omitempty"` ResourceAllocation *string `json:",omitempty"` MaxUsage *time.Duration `json:",omitempty"` - Routes *engine.SortedRoutes `json:",omitempty"` + Routes engine.SortedRoutesSet `json:",omitempty"` ThresholdIDs *[]string `json:",omitempty"` StatQueueIDs *[]string `json:",omitempty"` @@ -2003,8 +2003,8 @@ func (sS *SessionS) BiRPCv1AuthorizeEvent(clnt rpcclient.ClientConnector, if err != nil { return err } - if routesReply.SortedRoutes != nil { - authReply.Routes = &routesReply + if routesReply != nil { + authReply.Routes = routesReply } } if args.ProcessThresholds { @@ -2824,7 +2824,7 @@ type V1ProcessMessageReply struct { MaxUsage *time.Duration `json:",omitempty"` ResourceAllocation *string `json:",omitempty"` Attributes *engine.AttrSProcessEventReply `json:",omitempty"` - Routes *engine.SortedRoutes `json:",omitempty"` + Routes engine.SortedRoutesSet `json:",omitempty"` ThresholdIDs *[]string `json:",omitempty"` StatQueueIDs *[]string `json:",omitempty"` @@ -2952,8 +2952,8 @@ func (sS *SessionS) BiRPCv1ProcessMessage(clnt rpcclient.ClientConnector, if err != nil { return err } - if routesReply.SortedRoutes != nil { - rply.Routes = &routesReply + if routesReply != nil { + rply.Routes = routesReply } } if args.Debit { @@ -3004,7 +3004,7 @@ type V1ProcessEventReply struct { Cost map[string]float64 `json:",omitempty"` // Cost is the cost received from Rater, ignoring accounting part ResourceAllocation map[string]string `json:",omitempty"` Attributes map[string]*engine.AttrSProcessEventReply `json:",omitempty"` - Routes map[string]*engine.SortedRoutes `json:",omitempty"` + Routes map[string]engine.SortedRoutesSet `json:",omitempty"` ThresholdIDs map[string][]string `json:",omitempty"` StatQueueIDs map[string][]string `json:",omitempty"` STIRIdentity map[string]string `json:",omitempty"` @@ -3158,7 +3158,7 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.ClientConnector, // get routes if required if argsFlagsWithParams.GetBool(utils.MetaRoutes) { - rply.Routes = make(map[string]*engine.SortedRoutes) + rply.Routes = make(map[string]engine.SortedRoutesSet) // check in case we have options for suppliers flags := argsFlagsWithParams[utils.MetaRoutes] ignoreErrors := flags.Has(utils.MetaIgnoreErrors) @@ -3173,8 +3173,8 @@ func (sS *SessionS) BiRPCv1ProcessEvent(clnt rpcclient.ClientConnector, if err != nil { return err } - if routesReply.SortedRoutes != nil { - rply.Routes[runID] = &routesReply + if routesReply != nil { + rply.Routes[runID] = routesReply } } } @@ -3794,7 +3794,7 @@ func (sS *SessionS) processStats(cgrEv *utils.CGREvent, stsIDs []string, clnb bo // getRoutes will receive the event and send it to SupplierS to find the suppliers func (sS *SessionS) getRoutes(cgrEv *utils.CGREvent, pag utils.Paginator, ignoreErrors bool, - maxCost string, clnb bool) (routesReply engine.SortedRoutes, err error) { + maxCost string, clnb bool) (routesReply engine.SortedRoutesSet, err error) { if len(sS.cgrCfg.SessionSCfg().RouteSConns) == 0 { return routesReply, utils.NewErrNotConnected(utils.RouteS) } diff --git a/sessions/sessions_test.go b/sessions/sessions_test.go index 626b9509a..ec525b243 100644 --- a/sessions/sessions_test.go +++ b/sessions/sessions_test.go @@ -1285,7 +1285,7 @@ func TestSessionSV1AuthorizeReplyAsNavigableMap(t *testing.T) { splrs := &engine.SortedRoutes{ ProfileID: "SPL_ACNT_1001", Sorting: utils.MetaWeight, - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "supplier1", SortingData: map[string]interface{}{ diff --git a/sessions/sessionscover_test.go b/sessions/sessionscover_test.go index f96b67e11..372d93935 100644 --- a/sessions/sessionscover_test.go +++ b/sessions/sessionscover_test.go @@ -2503,7 +2503,7 @@ func TestBiRPCv1AuthorizeEvent2(t *testing.T) { }, utils.RouteSv1GetRoutes: func(args interface{}, reply interface{}) error { routesReply := engine.SortedRoutes{ - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "RouteID", }, @@ -2650,7 +2650,7 @@ func TestBiRPCv1AuthorizeEventWithDigest(t *testing.T) { }, utils.RouteSv1GetRoutes: func(args interface{}, reply interface{}) error { routesReply := engine.SortedRoutes{ - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "RouteID", }, @@ -3024,7 +3024,7 @@ func TestBiRPCv1InitiateSessionWithDigest(t *testing.T) { }, utils.RouteSv1GetRoutes: func(args interface{}, reply interface{}) error { routesReply := engine.SortedRoutes{ - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "RouteID", }, @@ -3594,7 +3594,7 @@ func TestBiRPCv1ProcessMessage2(t *testing.T) { }, utils.RouteSv1GetRoutes: func(args interface{}, reply interface{}) error { rts := &engine.SortedRoutes{ - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "ROUTE_ID", }, @@ -3724,7 +3724,7 @@ func TestBiRPCv1ProcessEvent(t *testing.T) { utils.RouteSv1GetRoutes: func(args interface{}, reply interface{}) error { rts := engine.SortedRoutes{ ProfileID: "ROUTE_PRFID", - SortedRoutes: []*engine.SortedRoute{ + Routes: []*engine.SortedRoute{ { RouteID: "ROUTE_ID", }, diff --git a/utils/cgrevent.go b/utils/cgrevent.go index 4a6d3be15..ac25f8c32 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -65,6 +65,15 @@ func (ev *CGREvent) OptAsString(optName string) (val string, err error) { return IfaceAsString(iface), nil } +// OptAsInt64 returns an option as int64 +func (ev *CGREvent) OptAsInt64(optName string) (int64, error) { + iface, has := ev.Opts[optName] + if !has { + return 0, ErrNotFound + } + return IfaceAsTInt64(iface) +} + // FieldAsTime returns a field as Time instance func (ev *CGREvent) FieldAsTime(fldName string, timezone string) (t time.Time, err error) { iface, has := ev.Event[fldName] diff --git a/utils/consts.go b/utils/consts.go index d87ee7910..f26db2f62 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -2554,6 +2554,7 @@ const ( // Event Opts const ( + OptsRouteProfilesCount = "*routeProfilesCount" OptsRoutesLimit = "*routes_limit" OptsRoutesOffset = "*routes_offset" OptsRatesStartTime = "*ratesStartTime"