Skip to content

Commit

Permalink
Adjust finding flow table identifier to the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Mentel authored and mentels committed Mar 11, 2015
1 parent 57ac719 commit 24b2872
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
32 changes: 25 additions & 7 deletions src/dofl_identifier.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,36 @@ flow_mod(Dpid, OFVersion, FlowMod) ->
flow_table(DatapahtId, {_Matches, _Actions, Opts}) ->
TableNo = proplists:get_value(table_id, Opts),
TableIdFun =
fun(Dpid, _, undefined, []) ->
{continue, Dpid};
(Identifier, IdMetadata, _LinkMetadata, Dpid) ->
case [maps:get(P, IdMetadata) || P <- [type, table_no]] of
[of_flow_table, TableNo] ->
fun(Dpid, _, [], _) when Dpid =:= DatapahtId ->
{continue, []};
(Identifier, IdMetadataInfo, _, _) ->
case table_found(IdMetadataInfo, TableNo) of
true ->
{stop, Identifier};
_ ->
{skip, Dpid}
{skip, []}
end
end,
dby:search(TableIdFun, [], DatapahtId, [breadth,{max_depth, 1}]).
dby:search(TableIdFun, [], [], [breadth, {max_depth, 1}]).

%%%=============================================================================
%%% Internal functions
%%%=============================================================================

table_found(IdMetadataInfo, TableNo) ->
case get_metadata_value(type, IdMetadataInfo) of
of_flow_table ->
TableNo =:= get_metadata_value(table_no, IdMetadataInfo);
_ ->
false
end.

get_metadata_value(Key, Metadatainfo) ->
KeyMap = maps:get(atom_to_binary(Key, utf8), Metadatainfo),
Value = maps:get(value, KeyMap),
case is_binary(Value) of
true ->
binary_to_atom(Value, utf8);
_ ->
Value
end.
38 changes: 29 additions & 9 deletions test/dofl_identifier_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ should_query_flow_table_id(_Config) ->
%% THEN
assert_dobby_search_fun_correct(?DPID, TableNo).

%%%=============================================================================
%%% Assertions
%%%=============================================================================

assert_dobby_search_fun_correct(Dpid, TableNo) ->
SearchFun = meck:capture(first, dby, search, 4, _ArgNo = 1),
Path = switch_to_flow_table_path(Dpid),
SearchResult = SearchFun(_FlowTableId = ?TABLE_IDENTIFIER,
flow_table_metadata_info(TableNo),
Path,
[]),
?assertEqual({stop, ?TABLE_IDENTIFIER}, SearchResult).

%%%=============================================================================
%%% Internal functions
%%%=============================================================================
Expand All @@ -58,14 +71,21 @@ mock_dobby() ->
unmock_dobby() ->
ok = meck:unload(dby).

assert_dobby_search_fun_correct(Dpid, TableNo) ->
SearchFun = meck:capture(first, dby, search, ['_', '_', Dpid, '_'],
_ArgNo = 1),
NullLink = {Dpid, null, undefined},
SearchResult = SearchFun(?TABLE_IDENTIFIER,
#{type => of_flow_table, table_no => TableNo},
#{type => of_resource},
[NullLink]),
?assertEqual({stop, ?TABLE_IDENTIFIER}, SearchResult).
switch_to_flow_table_path(Dpid) ->
[{_SwIdentifier = Dpid,
_SwIdMetadataInfo = metadata([{type, of_switch}]),
_SwToFlowTableMetadataInfo = metadata([{type, of_resource}])}].

flow_table_metadata_info(TableNo) ->
metadata([{type, of_flow_table}, {table_no, TableNo}]).

metadata(Proplist) ->
lists:foldl(fun({K, V}, AccMap) ->
IM = inner_metadata(V),
maps:put(atom_to_binary(K, utf8), IM, AccMap)
end, #{}, Proplist).

inner_metadata(V) when is_atom(V) ->
inner_metadata(atom_to_binary(V, utf8));
inner_metadata(V) ->
#{value => V, publisher_id => <<"ID">>, timestamp => <<"TSTM">>}.
2 changes: 1 addition & 1 deletion test/dofl_with_server_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ transition(PrevIdMetadataInfo, IdMetadataInfo) ->
TypeMap = maps:get(<<"type">>, MetadataInfo),
maps:get(<<"value">>, TypeMap)
end || MetadataInfo <- [PrevIdMetadataInfo, IdMetadataInfo]],
{atom_to_binary(PrevT, utf8), atom_to_binary(T, utf8)}.
{binary_to_atom(PrevT, utf8), binary_to_atom(T, utf8)}.

is_transition_allowed(Transition, AllowedTransitions) ->
lists:member(Transition, AllowedTransitions).
Expand Down

0 comments on commit 24b2872

Please sign in to comment.