Skip to content

Commit

Permalink
SBI:: use structed types
Browse files Browse the repository at this point in the history
Replace plain binaries with structured types.
  • Loading branch information
RoadRunnr authored and vkatsuba committed Jul 26, 2021
1 parent e566beb commit 2794795
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
72 changes: 31 additions & 41 deletions apps/ergw_sbi_client/src/ergw_sbi_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

-ignore_xref([?MODULE]).

-include_lib("gtplib/include/gtp_packet.hrl").

%%% ============================================================================
%%% API functions
%%% ============================================================================
Expand Down Expand Up @@ -78,21 +80,40 @@ from_ip({_, _, _, _} = IP) ->
from_ip({_, _, _, _,_, _, _, _} = IP) ->
#{<<"ipv6Addr">> => iolist_to_binary(inet:ntoa(IP))}.

split_location_info(<<MCC:3/bytes, MNC:3/bytes, Rest/binary>> = Bin, Size)
when byte_size(Bin) == Size ->
{{MCC, MNC}, Rest};
split_location_info(<<MCC:3/bytes, MNC:2/bytes, Rest/binary>>, _Size) ->
{{MCC, MNC}, Rest}.

from_session(Session) ->
maps:fold(fun from_session/3, #{}, Session).

from_plmnid({MCC, MNC}) ->
#{<<"mcc">> => MCC, <<"mnc">> => MNC}.

from_uli('CGI', #cgi{plmn_id = PlmnId, lac = LAC, ci = CI}, Req) ->
Id = #{<<"plmnId">> => from_plmnid(PlmnId),
<<"lac">> => LAC,
<<"cellId">> => CI},
put_kv([<<"userLocationInfo">>, <<"cgi">>], Id, Req);
from_uli('SAI', #sai{plmn_id = PlmnId, lac = LAC, sac = SAC}, Req) ->
Id = #{<<"plmnId">> => from_plmnid(PlmnId),
<<"lac">> => LAC,
<<"sac">> => SAC},
put_kv([<<"userLocationInfo">>, <<"sai">>], Id, Req);
from_uli('RAI', #rai{plmn_id = PlmnId, lac = LAC, rac = RAC}, Req) ->
Id = #{<<"plmnId">> => from_plmnid(PlmnId),
<<"lac">> => LAC,
<<"rac">> => RAC},
put_kv([<<"userLocationInfo">>, <<"rai">>], Id, Req);
from_uli('LAI', #lai{plmn_id = PlmnId, lac = LAC}, Req) ->
Id = #{<<"plmnId">> => from_plmnid(PlmnId),
<<"lac">> => LAC},
put_kv([<<"userLocationInfo">>, <<"lai">>], Id, Req);
from_uli(_, _, Req) ->
Req.

from_session('APN', [H|_] = V, Req) when is_binary(H) ->
Req#{<<"accessPointName">> => iolist_to_binary(lists:join($., V))};
from_session('3GPP-IMSI', V, Req) ->
Req#{<<"supi">> => <<"imsi-", V/binary>>};
from_session('3GPP-SGSN-MCC-MNC', <<MCC:3/bytes, MNC/binary>>, Req) ->
put_kv([<<"vPlmn">>, <<"plmnId">>], #{<<"mcc">> => MCC, <<"mnc">> => MNC}, Req);
from_session('3GPP-SGSN-MCC-MNC', PlmnId, Req) ->
put_kv([<<"vPlmn">>, <<"plmnId">>], from_plmnid(PlmnId), Req);
from_session('3GPP-SGSN-Address', V, Req) ->
put_kv([<<"vPlmn">>, <<"cpAddress">>], from_ip(V), Req);
from_session('3GPP-SGSN-IPv6-Address', V, Req) ->
Expand All @@ -110,39 +131,8 @@ from_session('3GPP-MSISDN', V, Req) ->
from_session('3GPP-IMEISV' , V, Req) ->
Req#{<<"pei">> => <<"imeisv-", V/binary>>};

from_session('CGI', V, Req) when is_binary(V) ->
case split_location_info(V, 10) of
{{MCC, MNC}, <<LAC:16, CI:16>>} ->
Id = #{<<"plmnId">> => #{<<"mcc">> => MCC, <<"mnc">> => MNC},
<<"lac">> => LAC,
<<"cellId">> => CI},
put_kv([<<"userLocationInfo">>, <<"cgi">>], Id, Req);
_ ->
%% unable do decode CGI
Req
end;
from_session('SAI', V, Req) when is_binary(V) ->
case split_location_info(V, 10) of
{{MCC, MNC}, <<LAC:16, SAC:16>>} ->
Id = #{<<"plmnId">> => #{<<"mcc">> => MCC, <<"mnc">> => MNC},
<<"lac">> => LAC,
<<"sac">> => SAC},
put_kv([<<"userLocationInfo">>, <<"sai">>], Id, Req);
_ ->
%% unable do decode SAI
Req
end;
from_session('RAI', V, Req) when is_binary(V) ->
case split_location_info(V, 9) of
{{MCC, MNC}, <<LAC:16, RAC:8>>} ->
Id = #{<<"plmnId">> => #{<<"mcc">> => MCC, <<"mnc">> => MNC},
<<"lac">> => LAC,
<<"rac">> => RAC},
put_kv([<<"userLocationInfo">>, <<"rai">>], Id, Req);
_ ->
%% unable do decode RAI
Req
end;
from_session('User-Location-Info', V, Req) when is_map(V) ->
maps:fold(fun from_uli/3, Req, V);

from_session('QoS-Information' = K, V, Req) ->
Req#{K => V};
Expand Down
23 changes: 20 additions & 3 deletions apps/ergw_sbi_client/test/ergw_sbi_client_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ session() ->
#{'Username' => <<"111111111111111/3520990017614823/440000000000/ATOM/TEXT/[email protected]">>,
'Framed-Protocol' => 'GPRS-PDP-Context',
'Multi-Session-Id' => 15958412130338583375391261628607396908,
'TAI' => <<3,2,22,214,217>>,
'3GPP-SGSN-Address' => {127,127,127,127},
'ECGI' => <<3,2,22,8,71,9,92>>,
'3GPP-RAT-Type' => 1,
'Framed-IPv6-Pool' => <<"pool-A">>,
'3GPP-Charging-Id' => 1981737279,
Expand All @@ -80,6 +78,14 @@ session() ->
#{'ext-macro-eNB' =>
#ext_macro_enb{plmn_id = {<<"001">>, <<"001">>},
id = rand:uniform(16#1fffff)},
'SAI' =>
#sai{plmn_id = {<<"001">>, <<"001">>},
lac = rand:uniform(16#ffff),
sac = rand:uniform(16#ffff)},
'RAI' =>
#rai{plmn_id = {<<"001">>, <<"001">>},
lac = rand:uniform(16#ffff),
rac = rand:uniform(16#ffff)},
'TAI' =>
#tai{plmn_id = {<<"001">>, <<"001">>},
tac = rand:uniform(16#ffff)}},
Expand All @@ -100,7 +106,7 @@ session() ->
'NAS-Identifier' => <<"NAS-Identifier">>,
'3GPP-NSAPI' => 5,
'3GPP-PDP-Type' => 'IPv4',
'3GPP-SGSN-MCC-MNC' => <<"001001">>,
'3GPP-SGSN-MCC-MNC' => {<<"001">>,<<"001">>},
'3GPP-IMSI-MCC-MNC' => <<"11111">>,

%% TBD:
Expand Down Expand Up @@ -131,6 +137,17 @@ from_session(_Config) ->
<<"pei">> := <<"imeisv-3520990017614823">>,
<<"protocolType">> := <<"GTPv2">>,
<<"supi">> := <<"imsi-111111111111111">>,
<<"userLocationInfo">> :=
#{<<"rai">> :=
#{<<"lac">> := _,
<<"plmnId">> :=
#{<<"mcc">> := <<"001">>,<<"mnc">> := <<"001">>},
<<"rac">> := _},
<<"sai">> :=
#{<<"lac">> := _,
<<"plmnId">> :=
#{<<"mcc">> := <<"001">>,<<"mnc">> := <<"001">>},
<<"sac">> := _}},
<<"vPlmn">> :=
#{<<"cpAddress">> := #{<<"ipv4Addr">> := <<"127.127.127.127">>},
<<"plmnId">> := #{<<"mcc">> := <<"001">>, <<"mnc">> := <<"001">>},
Expand Down

0 comments on commit 2794795

Please sign in to comment.