Skip to content

Commit

Permalink
simpler fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo committed Jan 4, 2019
1 parent 752d427 commit 9e0ab62
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 156 deletions.
6 changes: 3 additions & 3 deletions applications/ecallmgr/src/ecallmgr_fs_fetch.erl
Expand Up @@ -129,7 +129,6 @@ code_change(_OldVsn, State, _Extra) ->
-spec handle_fetch_req(atom(), kz_json:object()) -> fs_sendmsg_ret().
handle_fetch_req(Node, JObj) ->
kz_util:put_callid(JObj),
%% lager:debug_unsafe("FETCH : ~s", [kz_json:encode(JObj, ['pretty'])]),
FetchId = kzd_fetch:fetch_uuid(JObj),
CoreUUID = kzd_fetch:core_uuid(JObj),
Key = kzd_fetch:fetch_key_value(JObj),
Expand All @@ -155,6 +154,7 @@ handle_fetch_req(Node, JObj) ->
_ -> 'ok'
end.

not_found(#{node := Node, fetch_id := FetchId, section := Section}) ->
-spec not_found(map()) -> fs_sendmsg_ret().
not_found(Ctx) ->
{'ok', XmlResp} = ecallmgr_fs_xml:not_found(),
freeswitch:fetch_reply(Node, FetchId, Section, iolist_to_binary(XmlResp)).
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(XmlResp)}).
29 changes: 15 additions & 14 deletions applications/ecallmgr/src/ecallmgr_fs_fetch_channels.erl
Expand Up @@ -30,7 +30,7 @@ init() ->


-spec channel_req(map()) -> 'ok'.
channel_req(#{node := Node, fetch_id := FetchId, payload := JObj}) ->
channel_req(#{payload := JObj} = Ctx) ->
ToUser = kz_json:get_value(<<"refer-to-user">>, JObj),
TargetUUID = kz_json:get_ne_binary_value(<<"replaces-call-id">>, JObj),
UUID = kz_json:get_ne_binary_value(<<"refer-from-channel-id">>, JObj),
Expand All @@ -41,7 +41,7 @@ channel_req(#{node := Node, fetch_id := FetchId, payload := JObj}) ->
case Channel =/= 'undefined'
andalso TargetChannel =/= 'undefined'
of
'false' -> channel_not_found(Node, FetchId);
'false' -> channel_not_found(Ctx);
'true' ->
[Uri] = kzsip_uri:uris(props:get_value(<<"switch_url">>, TargetChannel)),
URL = kzsip_uri:ruri(
Expand All @@ -51,11 +51,12 @@ channel_req(#{node := Node, fetch_id := FetchId, payload := JObj}) ->
}),
CCVs = ecallmgr_fs_channel:channel_ccvs(Channel),
ForChannelCCVs = ecallmgr_fs_channel:channel_ccvs(ForChannel),
build_channel_resp(FetchId, JObj, Node, URL, TargetChannel, CCVs, ForChannelCCVs)
DialPrefix = channel_resp_dialprefix(JObj, Channel, CCVs, ForChannelCCVs),
build_channel_resp(Ctx#{url => URL, dial_prefix => DialPrefix})
end.

-spec build_channel_resp(kz_term:ne_binary(), kz_json:object(), atom(), kz_term:ne_binary(), kz_term:proplist(), kz_term:proplist(), kz_term:proplist()) -> 'ok'.
build_channel_resp(FetchId, JObj, Node, URL, Channel, ChannelVars, ForChannelCCVs) ->
-spec build_channel_resp(map()) -> 'ok'.
build_channel_resp(#{url := URL, dial_prefix := DialPrefix} = Ctx) ->
%% NOTE
%% valid properties to return are
%% sip-url , dial-prefix, absolute-dial-string, sip-profile (defaulted to current channel profile)
Expand All @@ -64,9 +65,9 @@ build_channel_resp(FetchId, JObj, Node, URL, Channel, ChannelVars, ForChannelCCV
%% else => %ssofia/%s/%s [dial-prefix, sip-profile, sip-url]
Resp = props:filter_undefined(
[{<<"sip-url">>, URL}
,{<<"dial-prefix">>, channel_resp_dialprefix(JObj, Channel, ChannelVars, ForChannelCCVs)}
,{<<"dial-prefix">>, DialPrefix}
]),
try_channel_resp(FetchId, Node, Resp).
try_channel_resp(Ctx, Resp).

-spec channel_resp_dialprefix(kz_json:object(), kz_term:proplist(), kz_term:proplist(), kz_term:proplist()) -> kz_term:ne_binary().
channel_resp_dialprefix(JObj, Channel, ChannelVars, ForChannelCCVs) ->
Expand Down Expand Up @@ -107,19 +108,19 @@ fs_props_to_binary([{Hk,Hv}|T]) ->
Rest = << <<",", K/binary, "='", (kz_term:to_binary(V))/binary, "'">> || {K,V} <- T >>,
<<"[", Hk/binary, "='", (kz_term:to_binary(Hv))/binary, "'", Rest/binary, "]">>.

-spec try_channel_resp(kz_term:ne_binary(), atom(), kz_term:proplist()) -> 'ok'.
try_channel_resp(FetchId, Node, Props) ->
-spec try_channel_resp(map(), kz_term:proplist()) -> 'ok'.
try_channel_resp(#{node := Node} = Ctx, Props) ->
try ecallmgr_fs_xml:sip_channel_xml(Props) of
{'ok', ConfigXml} ->
lager:debug("sending sofia XML to ~s: ~s", [Node, ConfigXml]),
freeswitch:fetch_reply(Node, FetchId, 'channels', erlang:iolist_to_binary(ConfigXml))
freeswitch:fetch_reply(Ctx#{reply => erlang:iolist_to_binary(ConfigXml)})
catch
_E:_R ->
lager:info("sofia profile resp failed to convert to XML (~s): ~p", [_E, _R]),
channel_not_found(Node, FetchId)
channel_not_found(Ctx)
end.

-spec channel_not_found(atom(), kz_term:ne_binary()) -> 'ok'.
channel_not_found(Node, FetchId) ->
-spec channel_not_found(map()) -> 'ok'.
channel_not_found(Ctx) ->
{'ok', Resp} = ecallmgr_fs_xml:not_found(),
freeswitch:fetch_reply(Node, FetchId, 'channels', iolist_to_binary(Resp)).
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(Resp)}).
Expand Up @@ -31,12 +31,12 @@ init() ->
'ok'.

-spec acl(map()) -> fs_sendmsg_ret().
acl(#{node := Node, fetch_id := Id}) ->
acl(#{node := Node, fetch_id := Id}=Ctx) ->
kz_util:put_callid(Id),
ACLs = ecallmgr_fs_acls:get(),
ConfigXML = generate_acl_xml(ACLs),
lager:debug_unsafe("sending acl XML to ~s: ~s", [Node, ConfigXML]),
freeswitch:fetch_reply(Node, Id, 'configuration', ConfigXML).
freeswitch:fetch_reply(Ctx#{reply => ConfigXML}).

-spec generate_acl_xml(kz_json:object()) -> kz_term:ne_binary().
generate_acl_xml(SysconfResp) ->
Expand Down
Expand Up @@ -29,9 +29,9 @@ init() ->
'ok'.

-spec conference(map()) -> fs_sendmsg_ret().
conference(#{node := Node, fetch_id := Id, payload := JObj}) ->
conference(#{node := Node, fetch_id := Id, payload := JObj}=Ctx) ->
kz_util:put_callid(Id),
fetch_conference_config(Node, Id, kz_api:event_name(JObj), JObj).
fetch_conference_config(Node, Id, kz_api:event_name(JObj), JObj, Ctx).

-spec fix_conference_profile(kz_json:object()) -> kz_json:object().
fix_conference_profile(Resp) ->
Expand Down Expand Up @@ -79,19 +79,19 @@ maybe_convert_sound(<<"dnuos-", _/binary>>, Key, Value, Profile) ->
maybe_convert_sound(_, _Key, _Value, Profile) ->
Profile.

-spec fetch_conference_config(atom(), kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object()) -> fs_sendmsg_ret().
fetch_conference_config(Node, Id, <<"COMMAND">>, JObj) ->
-spec fetch_conference_config(atom(), kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), map()) -> fs_sendmsg_ret().
fetch_conference_config(Node, Id, <<"COMMAND">>, JObj, Ctx) ->
Profile = kz_json:get_value(<<"profile_name">>, JObj),
Conference = kz_json:get_value(<<"conference_name">>, JObj),
AccountId = kzd_fetch:account_id(JObj),
maybe_fetch_conference_profile(Node, Id, Profile, Conference, AccountId);
fetch_conference_config(Node, Id, <<"REQUEST_PARAMS">>, JObj) ->
maybe_fetch_conference_profile(Node, Id, Profile, Conference, AccountId, Ctx);
fetch_conference_config(Node, Id, <<"REQUEST_PARAMS">>, JObj, Ctx) ->
Action = kz_json:get_value(<<"Action">>, JObj),
ConfName = kz_json:get_value(<<"Conf-Name">>, JObj),
lager:debug("request conference:~p params:~p", [ConfName, Action]),
fetch_conference_params(Node, Id, Action, ConfName, JObj).
fetch_conference_params(Node, Id, Action, ConfName, JObj, Ctx).

fetch_conference_params(Node, Id, <<"request-controls">>, ConfName, JObj) ->
fetch_conference_params(Node, Id, <<"request-controls">>, ConfName, JObj, Ctx) ->
Controls = kz_json:get_value(<<"Controls">>, JObj),
Profile = kz_json:get_value(<<"Conf-Profile">>, JObj),
lager:debug("request controls:~p for profile: ~p", [Controls, Profile]),
Expand All @@ -110,11 +110,11 @@ fetch_conference_params(Node, Id, <<"request-controls">>, ConfName, JObj) ->
,ecallmgr_fs_node:fetch_timeout(Node)
),
{'ok', Xml} = handle_conference_params_response(Resp),
send_conference_profile_xml(Node, Id, Xml);
fetch_conference_params(Node, Id, Action, ConfName, _Data) ->
send_conference_profile_xml(Xml, Ctx);
fetch_conference_params(_Node, _Id, Action, ConfName, _Data, Ctx) ->
lager:debug("undefined request_params action:~p conference:~p", [Action, ConfName]),
{'ok', XmlResp} = ecallmgr_fs_xml:not_found(),
send_conference_profile_xml(Node, Id, XmlResp).
send_conference_profile_xml(XmlResp, Ctx).

handle_conference_params_response({'ok', Resp}) ->
lager:debug("replying with xml response for conference params request"),
Expand All @@ -126,22 +126,22 @@ handle_conference_params_response(_Error) ->
lager:debug("failed to lookup conference params, error:~p", [_Error]),
ecallmgr_fs_xml:not_found().

-spec maybe_fetch_conference_profile(atom(), kz_term:ne_binary(), kz_term:api_binary(), kz_term:api_binary(), kz_term:api_binary()) -> fs_sendmsg_ret().
maybe_fetch_conference_profile(Node, Id, _, _, 'undefined') ->
-spec maybe_fetch_conference_profile(atom(), kz_term:ne_binary(), kz_term:api_binary(), kz_term:api_binary(), kz_term:api_binary(), map()) -> fs_sendmsg_ret().
maybe_fetch_conference_profile(_Node, _Id, _, _, 'undefined', Ctx) ->
lager:debug("failed to lookup conference profile for undefined account-id"),
{'ok', XmlResp} = ecallmgr_fs_xml:not_found(),
send_conference_profile_xml(Node, Id, XmlResp);
send_conference_profile_xml(XmlResp, Ctx);

maybe_fetch_conference_profile(Node, Id, 'undefined', _Conference, _AccountId) ->
maybe_fetch_conference_profile(_Node, _Id, 'undefined', _Conference, _AccountId, Ctx) ->
lager:debug("failed to lookup undefined profile conference"),
{'ok', XmlResp} = ecallmgr_fs_xml:not_found(),
send_conference_profile_xml(Node, Id, XmlResp);
send_conference_profile_xml(XmlResp, Ctx);

maybe_fetch_conference_profile(Node, Id, Profile, Conference, AccountId) ->
fetch_conference_profile(Node, Id, Profile, Conference, AccountId).
maybe_fetch_conference_profile(Node, Id, Profile, Conference, AccountId, Ctx) ->
fetch_conference_profile(Node, Id, Profile, Conference, AccountId, Ctx).

-spec fetch_conference_profile(atom(), kz_term:ne_binary(), kz_term:api_binary(), kz_term:api_binary(), kz_term:api_binary()) -> fs_sendmsg_ret().
fetch_conference_profile(Node, Id, Profile, Conference, AccountId) ->
-spec fetch_conference_profile(atom(), kz_term:ne_binary(), kz_term:api_binary(), kz_term:api_binary(), kz_term:api_binary(), map()) -> fs_sendmsg_ret().
fetch_conference_profile(Node, Id, Profile, Conference, AccountId, Ctx) ->
Cmd = [{<<"Request">>, <<"Conference">>}
,{<<"Profile">>, Profile}
,{<<"Conference-ID">>, Conference}
Expand Down Expand Up @@ -173,9 +173,9 @@ fetch_conference_profile(Node, Id, Profile, Conference, AccountId) ->
{'ok', Resp} = ecallmgr_fs_xml:not_found(),
Resp
end,
send_conference_profile_xml(Node, Id, XmlResp).
send_conference_profile_xml(XmlResp, Ctx).

-spec send_conference_profile_xml(atom(), kz_term:ne_binary(), iolist()) -> fs_sendmsg_ret().
send_conference_profile_xml(Node, Id, XmlResp) ->
-spec send_conference_profile_xml(iolist(), map()) -> fs_sendmsg_ret().
send_conference_profile_xml(XmlResp, #{node := Node} = Ctx) ->
lager:debug("sending conference profile XML to ~s: ~s", [Node, XmlResp]),
freeswitch:fetch_reply(Node, Id, 'configuration', iolist_to_binary(XmlResp)).
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(XmlResp)}).
50 changes: 25 additions & 25 deletions applications/ecallmgr/src/ecallmgr_fs_fetch_configuration_kazoo.erl
Expand Up @@ -31,41 +31,41 @@ init() ->
'ok'.

-spec kazoo(map()) -> fs_sendmsg_ret().
kazoo(#{node := Node, fetch_id := Id, payload := JObj}) ->
kazoo(#{node := Node, fetch_id := Id, payload := JObj} = Ctx) ->
kz_util:put_callid(Id),
lager:debug("received configuration request for kazoo configuration ~p , ~p", [Node, Id]),
fetch_mod_kazoo_config(Node, Id, kz_api:event_name(JObj), JObj).
fetch_mod_kazoo_config(kz_api:event_name(JObj), Ctx).


-spec fetch_mod_kazoo_config(atom(), kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object()) -> fs_sendmsg_ret().
fetch_mod_kazoo_config(Node, Id, <<"COMMAND">>, _JObj) ->
-spec fetch_mod_kazoo_config(kz_term:ne_binary(), map()) -> fs_sendmsg_ret().
fetch_mod_kazoo_config(<<"COMMAND">>, #{payload := _JObj} = Ctx) ->
lager:debug_unsafe("kazoo conf request : ~s", [kz_json:encode(_JObj, ['pretty'])]),
config_req_not_handled(Node, Id, <<"kazoo.conf">>);
fetch_mod_kazoo_config(Node, Id, <<"REQUEST_PARAMS">>, JObj) ->
kazoo_req_not_handled(Ctx);
fetch_mod_kazoo_config(<<"REQUEST_PARAMS">>, #{payload := JObj} = Ctx) ->
lager:debug_unsafe("kazoo conf request params: ~s", [kz_json:encode(JObj, ['pretty'])]),
Action = kz_json:get_ne_binary_value(<<"Action">>, JObj),
fetch_mod_kazoo_config_action(Node, Id, Action, JObj);
fetch_mod_kazoo_config(Node, Id, Event, _JObj) ->
fetch_mod_kazoo_config_action(Action, Ctx);
fetch_mod_kazoo_config(Event, #{node := Node} = Ctx) ->
lager:debug("unhandled mod kazoo config event : ~p : ~p", [Node, Event]),
config_req_not_handled(Node, Id, <<"kazoo.conf">>).
kazoo_req_not_handled(Ctx).

-spec config_req_not_handled(atom(), kz_term:ne_binary(), kz_term:ne_binary()) -> fs_sendmsg_ret().
config_req_not_handled(Node, Id, Conf) ->
{'ok', NotHandled} = ecallmgr_fs_xml:not_found(),
lager:debug("ignoring conf ~s: ~s", [Conf, Id]),
freeswitch:fetch_reply(Node, Id, 'configuration', iolist_to_binary(NotHandled)).

-spec fetch_mod_kazoo_config_action(atom(), kz_term:ne_binary(), kz_term:api_ne_binary(), kz_json:object()) ->
fs_sendmsg_ret().
fetch_mod_kazoo_config_action(Node, Id, <<"request-filter">>, _Data) ->
-spec fetch_mod_kazoo_config_action(kz_term:api_ne_binary(), map()) -> fs_sendmsg_ret().
fetch_mod_kazoo_config_action(<<"request-filter">>, Ctx) ->
{'ok', Xml} = ecallmgr_fs_xml:event_filters_resp_xml(?FS_EVENT_FILTERS),
lager:debug("replying with xml response for request-filter params request"),
freeswitch:fetch_reply(Node, Id, 'configuration', iolist_to_binary(Xml));
fetch_mod_kazoo_config_action(Node, Id, <<"request-handlers">>, _Data) ->
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(Xml)});
fetch_mod_kazoo_config_action(<<"request-handlers">>, Ctx) ->
%% TODO get handlers/definitions/events
config_req_not_handled(Node, Id, <<"kazoo.conf">>);
fetch_mod_kazoo_config_action(Node, Id, 'undefined', _Data) ->
config_req_not_handled(Node, Id, <<"kazoo.conf">>);
fetch_mod_kazoo_config_action(Node, Id, Action, _Data) ->
kazoo_req_not_handled(Ctx);
fetch_mod_kazoo_config_action('undefined', Ctx) ->
kazoo_req_not_handled(Ctx);
fetch_mod_kazoo_config_action(Action, #{node := Node} = Ctx) ->
lager:debug("unhandled mod kazoo config action : ~p : ~p", [Node, Action]),
config_req_not_handled(Node, Id, <<"kazoo.conf">>).
kazoo_req_not_handled(Ctx).

-spec kazoo_req_not_handled(map()) -> fs_sendmsg_ret().
kazoo_req_not_handled(#{node := Node, fetch_id := Id} = Ctx) ->
{'ok', NotHandled} = ecallmgr_fs_xml:not_found(),
lager:debug("ignoring kazoo conf ~s: ~s", [Node, Id]),
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(NotHandled)}).

Expand Up @@ -31,25 +31,25 @@ init() ->
'ok'.

-spec sofia(map()) -> fs_sendmsg_ret().
sofia(#{node := Node, fetch_id := Id}) ->
sofia(#{node := Node, fetch_id := Id}=Ctx) ->
kz_util:put_callid(Id),
case kapps_config:is_true(?APP_NAME, <<"sofia_conf">>, 'false') of
'false' ->
lager:info("sofia conf disabled"),
{'ok', Resp} = ecallmgr_fs_xml:not_found(),
freeswitch:fetch_reply(Node, Id, 'configuration', iolist_to_binary(Resp));
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(Resp)});
'true' ->
Profiles = kapps_config:get_json(?APP_NAME, <<"fs_profiles">>, kz_json:new()),
DefaultProfiles = default_sip_profiles(Node),
try ecallmgr_fs_xml:sip_profiles_xml(kz_json:merge(DefaultProfiles, Profiles)) of
{'ok', ConfigXml} ->
lager:debug("sending sofia XML to ~s: ~s", [Node, ConfigXml]),
freeswitch:fetch_reply(Node, Id, 'configuration', erlang:iolist_to_binary(ConfigXml))
freeswitch:fetch_reply(Ctx#{reply => erlang:iolist_to_binary(ConfigXml)})
catch
_E:_R ->
lager:info("sofia profile resp failed to convert to XML (~s): ~p", [_E, _R]),
{'ok', Resp} = ecallmgr_fs_xml:not_found(),
freeswitch:fetch_reply(Node, Id, 'configuration', iolist_to_binary(Resp))
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(Resp)})
end
end.

Expand Down
18 changes: 9 additions & 9 deletions applications/ecallmgr/src/ecallmgr_fs_fetch_dialplan.erl
Expand Up @@ -69,7 +69,7 @@ timeout(#{payload := JObj}=Map) ->
T4 = T3 - T0,
T5 = T1 - T4,
T6 = T5 div 1000,
Map#{timeout => T6 - 500}.
Map#{timeout => T6 - 750}.

call_id(#{call_id := _CallId}=Map) -> Map;
call_id(#{payload := JObj}=Map) ->
Expand Down Expand Up @@ -105,8 +105,8 @@ wait_for_route_resp(#{timeout := Timeout}=Map) ->
{'route_resp', Resp, Props} ->
case kz_api:defer_response(Resp) of
true ->
lager:debug("received deferred reply - waiting for others"),
NewTimeout = Timeout - kz_time:elapsed_ms(Now),
lager:debug("received deferred reply - waiting for others for ~B ms", [NewTimeout]),
wait_for_route_resp(Map#{timeout => NewTimeout, reply => #{payload => Resp, props => Props}});
false ->
lager:info("received route reply"),
Expand Down Expand Up @@ -151,22 +151,22 @@ wait_for_authz(#{authz_worker := {Pid, Ref}, authz_timeout := Timeout, reply :=
lager:warning("timeout waiting for authz reply from worker ~p", [Pid])
end.

send_reply(#{node := Node, fetch_id := FetchId, reply := #{payload := Reply}}=Map) ->
Props = maps:to_list(Map),
send_reply(#{node := Node, fetch_id := FetchId, reply := #{payload := Reply}}=Ctx) ->
Props = maps:to_list(Ctx),
{'ok', XML} = ecallmgr_fs_xml:route_resp_xml('dialplan', Reply, Props),
lager:debug("sending xml dialplan reply for request ~s",[FetchId]),
freeswitch:fetch_reply(Node, FetchId, 'dialplan', iolist_to_binary(XML)),
lager:debug("sending xml dialplan reply for request ~s tp ~s",[FetchId, Node]),
freeswitch:fetch_reply(Ctx#{reply => iolist_to_binary(XML)}),
case kz_api:defer_response(Reply)
orelse kz_json:get_ne_binary_value(<<"Method">>, Reply) /= <<"park">>
of
true -> ok;
false -> wait_for_route_winner(Map)
false -> wait_for_route_winner(Ctx)
end.

wait_for_route_winner(Map) ->
wait_for_route_winner(Ctx) ->
receive
{'route_winner', JObj, Props} ->
activate_call_control(Map#{winner => #{payload => JObj, props => Props}})
activate_call_control(Ctx#{winner => #{payload => JObj, props => Props}})
after ?ROUTE_WINNER_TIMEOUT ->
lager:warning("timeout after ~B receiving route winner", [?ROUTE_WINNER_TIMEOUT])
end.
Expand Down

0 comments on commit 9e0ab62

Please sign in to comment.