diff --git a/src/dofl_identifier.erl b/src/dofl_identifier.erl index a1796f2..800daba 100644 --- a/src/dofl_identifier.erl +++ b/src/dofl_identifier.erl @@ -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. diff --git a/test/dofl_identifier_SUITE.erl b/test/dofl_identifier_SUITE.erl index dea0158..c3d518d 100644 --- a/test/dofl_identifier_SUITE.erl +++ b/test/dofl_identifier_SUITE.erl @@ -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 %%%============================================================================= @@ -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">>}. diff --git a/test/dofl_with_server_SUITE.erl b/test/dofl_with_server_SUITE.erl index d325993..abfb6e3 100644 --- a/test/dofl_with_server_SUITE.erl +++ b/test/dofl_with_server_SUITE.erl @@ -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).