-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdofl_identifier.erl
More file actions
75 lines (61 loc) · 2.45 KB
/
dofl_identifier.erl
File metadata and controls
75 lines (61 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
%%%=============================================================================
%%% @copyright (C) 2015, Erlang Solutions Ltd
%%% @author Szymon Mentel <szymon.mentel@erlang-solutions.com>
%%% @doc <Module purpose>
%%% @end
%%%=============================================================================
-module(dofl_identifier).
-copyright("2015, Erlang Solutions Ltd.").
-export([]).
%% API
-export([net_flow/2,
flow_mod/3,
flow_table/2]).
-include_lib("dobby_clib/include/dobby.hrl").
-include("dobby_oflib.hrl").
%%%=============================================================================
%%% External functions
%%%=============================================================================
-spec net_flow(dby_identifier(), dby_identifier()) -> dby_endpoint().
net_flow(Src, Dst) ->
{<<"NF:", Src/binary, ":", Dst/binary>>, [{type, of_net_flow}]}.
-spec flow_mod(dby_identifier(), of_version(), flow_mod()) -> dby_endpoint().
flow_mod(Dpid, OFVersion, FlowMod) ->
{_Matches, _Instructions, Opts} = FlowMod,
Cookie = proplists:get_value(cookie, Opts),
{Cookie, [{type, of_flow_mod}, {dpid, Dpid}, {of_version, OFVersion}]}.
-spec flow_table(dby_identifier(), flow_mod()) -> dby_identifier().
flow_table(DatapahtId, {_Matches, _Actions, Opts}) ->
TableNo = proplists:get_value(table_id, Opts),
TableIdFun =
fun(Dpid, _, [], _) when Dpid =:= DatapahtId ->
{continue, []};
(Identifier, IdMetadataInfo, _, _) ->
case table_found(IdMetadataInfo, TableNo) of
true ->
{stop, Identifier};
_ ->
{skip, []}
end
end,
dby:search(TableIdFun, [], [], [breadth,{max_depth, 1}]).
%%%=============================================================================
%%% Internal functions
%%%=============================================================================
table_found(IdMetadataInfo, TableNo) ->
case get_metadata_value(type, IdMetadataInfo) of
of_flow_table ->
TN = get_metadata_value(table_no, IdMetadataInfo),
TN =:= TableNo;
_ ->
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.