Skip to content

Commit 91173d4

Browse files
author
Szymon Mentel
committed
Extract publishing to dobby to a separate module
1 parent 52f4247 commit 91173d4

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

src/dobby_oflib.erl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,7 @@ flow_table_identifier({Dpid, _OFVersion, FlowMod}) ->
107107
dofl_identifier:flow_table(Dpid, FlowMod).
108108

109109
publish(PublisherId, Src, Dst, LinkMetadata) ->
110-
%% HACK: New API of dobby is out of sync with dobby_clib; but we
111-
%% want to mock dby:publish/5 for tests
112-
try dby:publish(PublisherId, Src, Dst, LinkMetadata, [persistent])
113-
catch
114-
error:undef ->
115-
Data = [{Src, Dst, LinkMetadata}],
116-
Opts = [persistent],
117-
gen_server:call({global,dobby},
118-
{dby_publish, [PublisherId, Data, Opts]})
119-
end.
110+
dofl_publish:do(PublisherId, Src, Dst, LinkMetadata).
120111

121112
reconstruct_flow_path(FlowPath0) ->
122113
Fun = fun({Dpid, {OFVersion, FlowMods}}) ->

src/dofl_publish.erl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
%%%=============================================================================
2+
%%% @copyright (C) 2015, Erlang Solutions Ltd
3+
%%% @author Szymon Mentel <szymon.mentel@erlang-solutions.com>
4+
%%% @doc Module encapsulating publishing to the dobby database
5+
%%% @end
6+
%%%=============================================================================
7+
-module(dofl_publish).
8+
-copyright("2015, Erlang Solutions Ltd.").
9+
10+
%% API
11+
-export([do/4]).
12+
13+
-include_lib("dobby_clib/include/dobby.hrl").
14+
-include("dobby_oflib.hrl").
15+
16+
%%%=============================================================================
17+
%%% External functions
18+
%%%=============================================================================
19+
20+
do(PublisherId, Src, Dst, LinkMetadata) ->
21+
do_publish(PublisherId,
22+
binarize(Src),
23+
binarize(Dst),
24+
binarize_metadata(LinkMetadata)).
25+
26+
%%%=============================================================================
27+
%%% Internal functions
28+
%%%=============================================================================
29+
30+
binarize(Identifier) when is_binary(Identifier) ->
31+
Identifier;
32+
binarize({Identifier, Metadata}) when is_binary(Identifier) ->
33+
{Identifier, binarize_metadata(Metadata)}.
34+
35+
binarize_metadata(Metadata) when is_list(Metadata)->
36+
[{binarize_term(K), binarize_term(V)} || {K, V} <- Metadata].
37+
38+
binarize_term(T) when is_atom(T) ->
39+
atom_to_binary(T, utf8);
40+
binarize_term(T)
41+
when is_binary(T);
42+
is_number(T);
43+
is_function(T);
44+
is_list(T);
45+
T =:= delete;
46+
T =:= nochange ->
47+
T.
48+
49+
do_publish(PublisherId, Src, Dst, LinkMetadata) ->
50+
dby:publish(PublisherId, Src, Dst, LinkMetadata, [persistent]).

0 commit comments

Comments
 (0)