Skip to content

Commit

Permalink
WHISTLE-788: start of refactor for j5_call_monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson authored and James Aimonetti committed May 18, 2012
1 parent 534127d commit b4981ea
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 87 deletions.
19 changes: 13 additions & 6 deletions ecallmgr/src/ecallmgr_authz.erl
Expand Up @@ -34,16 +34,16 @@ maybe_authorize_channel(Props, Node) ->
lager:debug("channel does not require authorization, allowing call", []),
true;
true ->
case authorize(Props) of
case authorize(Props, Node) of
true -> true;
false ->
kill_uuid(Props, Node),
false
end
end.

-spec authorize/1 :: (proplist()) -> 'ok'.
authorize(Props) ->
-spec authorize/2 :: (proplist(), atom()) -> 'ok'.
authorize(Props, Node) ->
lager:debug("channel authorization request started"),
ReqResp = wh_amqp_worker:call(?ECALLMGR_AMQP_POOL
,request(Props)
Expand All @@ -54,16 +54,23 @@ authorize(Props) ->
lager:debug("authz request lookup failed: ~p", [_R]),
default();
{ok, RespJObj} ->
handle_authz_response(RespJObj)
handle_authz_response(RespJObj, Node)
end.

-spec handle_authz_response/1 :: (wh_json:json_object()) -> boolean().
handle_authz_response(JObj) ->
-spec handle_authz_response/2 :: (wh_json:json_object(), atom()) -> boolean().
handle_authz_response(JObj, Node) ->
case wh_util:is_true(wh_json:get_value(<<"Is-Authorized">>, JObj)) of
true ->
lager:debug("channel authorization received affirmative response, allowing call", []),
wapi_authz:publish_win(wh_json:get_value(<<"Server-ID">>, JObj)
,wh_json:delete_key(<<"Event-Name">>, JObj)),
case wh_json:get_value(<<"Type">>, JObj) of
<<"per_minute">> ->
CallId = wh_json:get_value(<<"Call-ID">>, JObj),
lager:debug("call authorized as per-minute, ensuring call event process for ~s exists", [CallId]),
{ok, _} = ecallmgr_call_sup:start_event_process(Node, CallId);
_Else -> ok
end,
true;
false ->
lager:debug("channel authorization received negative response", []),
Expand Down
3 changes: 2 additions & 1 deletion lib/whistle-1.0.0/src/api/wapi_authz.erl
Expand Up @@ -48,9 +48,10 @@

%% Authorization Responses
-define(AUTHZ_RESP_HEADERS, [<<"Msg-ID">>, <<"Call-ID">>, <<"Is-Authorized">>]).
-define(OPTIONAL_AUTHZ_RESP_HEADERS, [<<"Custom-Channel-Vars">>]).
-define(OPTIONAL_AUTHZ_RESP_HEADERS, [<<"Custom-Channel-Vars">>, <<"Type">>]).
-define(AUTHZ_RESP_VALUES, [{<<"Event-Category">>, ?EVENT_CATEGORY}
,{<<"Event-Name">>, <<"authz_resp">>}
,{<<"Type">>, [<<"flat_rate">>, <<"per_minute">>]}
,{<<"Is-Authorized">>, [<<"true">>, <<"false">>]}
]).
-define(AUTHZ_RESP_TYPES, [{<<"Custom-Channel-Vars">>, ?IS_JSON_OBJECT}]).
Expand Down
89 changes: 40 additions & 49 deletions whistle_apps/apps/jonny5/src/j5_authz_req.erl
Expand Up @@ -15,54 +15,41 @@
handle_req(JObj, Props) ->
true = wapi_authz:req_v(JObj),
wh_util:put_callid(JObj),
Q = props:get_value(queue, Props),
AccountId = wh_json:get_value(<<"Account-ID">>, JObj),
Limits = j5_util:get_limits(AccountId),
Routines = [fun(_) ->
case calls_at_limit(Limits, JObj) of
false -> ok;
true ->
lager:debug("account is over the calls limit", []),
{error, call_limit}
false -> {ok, under_limit};
true -> {error, call_limit}
end
end
,fun({error, _}=E) -> E;
(ok) ->
case resource_consumption_at_limit(Limits, JObj) of
false -> ok;
true ->
lager:debug("account is over the resource consumption limit", []),
{error, resource_consumption_limit}
end
({ok, _}) ->
case resource_consumption_at_limit(Limits, JObj) of
false -> {ok, under_limit};
true -> {error, resource_consumption_limit}
end
end
,fun({error, _}=E) -> E;
(ok) ->
case trunks_at_limit(Limits, JObj) of
false ->
lager:debug("using flat-rate to authorize call"),
ok;
true ->
lager:debug("account is over the trunk consumption limit", []),
{error, trunk_limit}
end
({ok, _}) ->
case trunks_at_limit(Limits, JObj) of
false ->
Q = props:get_value(queue, Props),
{ok, flat_rate, Q};
true -> {error, trunk_limit}
end
end
,fun({error, trunk_limit}) ->
case credit_id_available(Limits, JObj) of
true ->
lager:debug("using credit to authorize call"),
ok;
false ->
lager:debug("credit is not available", []),
{error, trunk_limit}
case credit_is_available(Limits, JObj) of
{ok, Pid} ->
Q = gen_listener:queue_name(Pid),
{ok, per_minute, Q};
false -> {error, trunk_limit}
end;
(Else) -> Else
end
],
case lists:foldl(fun(F, A) -> F(A) end, ok, Routines) of
ok -> send_resp(JObj, Q, true);
{error, _} -> send_resp(JObj, Q, false)
end,
ok.
send_resp(JObj, lists:foldl(fun(F, A) -> F(A) end, ok, Routines)).

-spec calls_at_limit/2 :: (#limits{}, wh_json:json_object()) -> boolean().
calls_at_limit(#limits{calls=-1}, _) ->
Expand All @@ -85,38 +72,32 @@ trunks_at_limit(Limits, JObj) ->
OutboundResources = wh_json:get_integer_value([<<"Usage">>, <<"Outbound-Resources">>], JObj, 0),
consume_twoway_limits(Limits, RemainingInbound + OutboundResources) < 0.

-spec credit_id_available/2 :: (#limits{}, wh_json:json_object()) -> boolean().
credit_id_available(Limits, JObj) ->
-spec credit_is_available/2 :: (#limits{}, wh_json:json_object()) -> false | {'ok', pid()}.
credit_is_available(Limits, JObj) ->
AccountId = wh_json:get_value(<<"Account-ID">>, JObj),
Balance = j5_util:current_balance(AccountId),
case prepay_is_available(Limits, Balance, JObj) of
true -> true;
{ok, _}=Ok -> Ok;
false -> postpay_is_available(Limits, Balance, JObj)
end.

-spec prepay_is_available/3 :: (#limits{}, integer(), wh_json:json_object()) -> boolean().
-spec prepay_is_available/3 :: (#limits{}, integer(), wh_json:json_object()) -> false | {'ok', pid()}.
prepay_is_available(#limits{allow_prepay=false}, _, _) ->
false;
prepay_is_available(#limits{allow_prepay=true, reserve_amount=ReserveAmount}, Balance,JObj) ->
case (Balance - ReserveAmount) > 0 of
false -> false;
true ->
CallId = wh_json:get_value(<<"Call-ID">>, JObj),
wh_cache:store_local(?JONNY5_CACHE, ?MONITOR_CALL(CallId), {prepay, ReserveAmount, JObj}, 5),
true
true -> j5_call_monitor_sup:start_monitor(prepay, ReserveAmount, JObj)
end.

-spec postpay_is_available/3 :: (#limits{}, integer(), wh_json:json_object()) -> boolean().
-spec postpay_is_available/3 :: (#limits{}, integer(), wh_json:json_object()) -> false | {'ok', pid()}.
postpay_is_available(#limits{allow_postpay=false}, _, _) ->
false;
postpay_is_available(#limits{allow_postpay=true, max_postpay_amount=MaxPostpay
,reserve_amount=ReserveAmount}, Balance, JObj) ->
case (Balance - ReserveAmount) > MaxPostpay of
false -> false;
true ->
CallId = wh_json:get_value(<<"Call-ID">>, JObj),
wh_cache:store_local(?JONNY5_CACHE, ?MONITOR_CALL(CallId), {postpay, ReserveAmount, JObj}, 5),
true
true -> j5_call_monitor_sup:start_monitor(postpay, ReserveAmount, JObj)
end.

-spec consume_inbound_limits/2 :: (#limits{}, wh_json:json_object()) -> integer().
Expand All @@ -137,9 +118,19 @@ consume_twoway_limits(#limits{twoway_trunks=Trunks}, Resources) ->
Count -> Count
end.

-spec send_resp/3 :: (wh_json:json_object(), ne_binary(), boolean()) -> 'ok'.
send_resp(JObj, Q, IsAuthorized) ->
Resp = [{<<"Is-Authorized">>, wh_util:to_binary(IsAuthorized)}
-spec send_resp/2 :: (wh_json:json_object(), {'ok', 'credit' | 'flatrate', ne_binary()} | {'error', _}) -> 'ok'.
send_resp(JObj, {error, _R}) ->
lager:debug("call is unauthorize due to ~s", [_R]),
Resp = [{<<"Is-Authorized">>, <<"false">>}
,{<<"Msg-ID">>, wh_json:get_value(<<"Msg-ID">>, JObj)}
,{<<"Call-ID">>, wh_json:get_value(<<"Call-ID">>, JObj)}
| wh_api:default_headers(<<>>, ?APP_NAME, ?APP_VERSION)
],
wapi_authz:publish_resp(wh_json:get_value(<<"Server-ID">>, JObj), Resp);
send_resp(JObj, {ok, Type, Q}) ->
lager:debug("call is authorized as ~s", [Type]),
Resp = [{<<"Is-Authorized">>, <<"true">>}
,{<<"Type">>, wh_util:to_binary(Type)}
,{<<"Msg-ID">>, wh_json:get_value(<<"Msg-ID">>, JObj)}
,{<<"Call-ID">>, wh_json:get_value(<<"Call-ID">>, JObj)}
| wh_api:default_headers(Q, ?APP_NAME, ?APP_VERSION)
Expand Down
23 changes: 0 additions & 23 deletions whistle_apps/apps/jonny5/src/j5_authz_win.erl

This file was deleted.

0 comments on commit b4981ea

Please sign in to comment.