Skip to content

Commit a00b97a

Browse files
author
Szymon Mentel
committed
Add retrieving flow tables' identifiers from dobby
When publishing a Net Flow the dobby_oflib creates links between Flow Mods and corresponding Flow Tables. To accomplish that flow tables' identifiers are needed.
1 parent 8dba591 commit a00b97a

4 files changed

Lines changed: 104 additions & 5 deletions

File tree

src/dofl_identifier.erl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,23 @@ flow_mod(Dpid, OFVersion, FlowMod) ->
3535
{Cookie, #{type => of_flow_mod, dpid => Dpid, of_version => OFVersion}}.
3636

3737

38-
-spec flow_table(Dpid :: binary(), FlowMod :: flow_mod())
38+
-spec flow_table(DatapathId :: binary(), FlowMod :: flow_mod())
3939
-> FlowTableId :: idenfier().
4040

41-
flow_table(Dpid, FlowMod) ->
42-
<<Dpid/binary>>.
41+
flow_table(DatapahtId, {_Matches, _Actions, Opts}) ->
42+
TableNo = proplists:get_value(table_id, Opts),
43+
TableIdFun =
44+
fun(Dpid, _, undefined, []) ->
45+
{continue, Dpid};
46+
(Identifier, IdMetadata, _LinkMetadata, Dpid) ->
47+
case [maps:get(P, IdMetadata) || P <- [type, table_no]] of
48+
[of_flow_table, TableNo] ->
49+
{stop, Identifier};
50+
_ ->
51+
{skip, Dpid}
52+
end
53+
end,
54+
dby:search(TableIdFun, [], DatapahtId, [breadth,{max_depth, 1}]).
4355

4456
%%%=============================================================================
4557
%%% Internal functions

test/dobby_oflib_SUITE.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ init_per_suite(Config) ->
3030

3131
end_per_suite(_Config) ->
3232
unmock_dobby(),
33+
unmock_flow_table_identifiers(),
3334
ok.
3435

3536
all() ->
@@ -116,6 +117,9 @@ mock_flow_table_identifiers() ->
116117
<<Dpid/binary, ":", TableNoBin/binary>>
117118
end).
118119

120+
unmock_flow_table_identifiers() ->
121+
ok = meck:unload(dofl_identifier).
122+
119123
flow_mod_identifier({Dpid, OFVersion, _FlowTableId, FlowMod}) ->
120124
{_Matches, _Instructions, Opts} = FlowMod,
121125
Cookie = proplists:get_value(cookie, Opts),
@@ -130,7 +134,6 @@ link_metadata(Type, {NetFlowId, Src}) ->
130134
link_metadata(Type, bidirectional) ->
131135
#{type => Type}.
132136

133-
134137
reconstruct_flow_path(FlowPath0) ->
135138
Fun = fun({Dpid, {OFVersion, FlowMods}}) ->
136139
[reconstruct_flow_mod(Dpid, OFVersion, FM) || FM <- FlowMods]

test/dofl_identifier_SUITE.erl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
%%%=============================================================================
2+
%%% @copyright (C) 2015, Erlang Solutions Ltd
3+
%%% @author Szymon Mentel <szymon.mentel@erlang-solutions.com>
4+
%%% @doc <Suite purpose>
5+
%%% @end
6+
%%%=============================================================================
7+
-module(dofl_identifier_SUITE).
8+
-copyright("2015, Erlang Solutions Ltd.").
9+
10+
%% Note: This directive should only be used in test suites.
11+
-compile(export_all).
12+
-include_lib("common_test/include/ct.hrl").
13+
-include_lib("eunit/include/eunit.hrl").
14+
15+
-define(TABLE_IDENTIFIER, <<"TableId">>).
16+
-define(DPID, <<"00:00:00:00:00:01:00:01">>).
17+
18+
%%%=============================================================================
19+
%%% Callbacks
20+
%%%=============================================================================
21+
22+
23+
suite() ->
24+
[{timetrap,{minutes,10}}].
25+
26+
init_per_suite(Config) ->
27+
mock_dobby(),
28+
Config.
29+
30+
end_per_suite(_Config) ->
31+
unmock_dobby(),
32+
ok.
33+
34+
all() ->
35+
[should_query_flow_table_id].
36+
37+
%%%=============================================================================
38+
%%% Testcases
39+
%%%=============================================================================
40+
41+
should_query_flow_table_id(_Config) ->
42+
%% GIVEN
43+
FlowMod = dofl_test_utils:flow_mod(),
44+
TableNo = dofl_test_utils:flow_mod_to_table_no(FlowMod),
45+
46+
%% WHEN
47+
dofl_identifier:flow_table(?DPID, FlowMod),
48+
49+
%% THEN
50+
assert_dobby_search_fun_correct(?DPID, TableNo).
51+
52+
%%%=============================================================================
53+
%%% Internal functions
54+
%%%=============================================================================
55+
56+
mock_dobby() ->
57+
ok = meck:expect(dby, search, 4, _Ret = ?TABLE_IDENTIFIER).
58+
unmock_dobby() ->
59+
ok = meck:unload(dby).
60+
61+
assert_dobby_search_fun_correct(Dpid, TableNo) ->
62+
SearchFun = meck:capture(first, dby, search, ['_', '_', Dpid, '_'],
63+
_ArgNo = 1),
64+
NullLink = {Dpid, null, undefined},
65+
SearchResult = SearchFun(?TABLE_IDENTIFIER,
66+
#{type => of_flow_table, table_no => TableNo},
67+
#{type => of_resource},
68+
[NullLink]),
69+
?assertEqual({stop, ?TABLE_IDENTIFIER}, SearchResult).
70+
71+

test/dofl_test_utils.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
%% API
1111
-export([figure4/0,
1212
flow_path/0,
13-
flow_path_to_identifiers/1]).
13+
flow_path_to_identifiers/1,
14+
flow_mod/0,
15+
flow_mod_to_table_no/1]).
1416

1517
-include_lib("eunit/include/eunit.hrl").
1618

@@ -61,6 +63,17 @@ flow_path() ->
6163
{cookie_mask, <<0,0,0,0,0,0,0,0>>}]}]}
6264
}].
6365

66+
flow_mod() ->
67+
{[{in_port,2}],
68+
[{apply_actions,[{output,1,no_buffer}]}],
69+
[{table_id, 0}, {priority, 100},
70+
{idle_timeout, 0}, {idle_timeout, 0},
71+
{cookie, <<0,0,0,0,0,0,0,102>>},
72+
{cookie_mask, <<0,0,0,0,0,0,0,0>>}]}.
73+
74+
flow_mod_to_table_no({_Matches, _Actions, Opts}) ->
75+
proplists:get_value(table_id, Opts).
76+
6477
%% @doc
6578
%% Transletes `FlowPath' as returned by ?MODULE:figure4() into a list
6679
%% of subsequent identifiers.

0 commit comments

Comments
 (0)