|
| 1 | +%%%============================================================================= |
| 2 | +%%% @copyright (C) 2015, Erlang Solutions Ltd |
| 3 | +%%% @author Szymon Mentel <szymon.mentel@erlang-solutions.com> |
| 4 | +%%% @doc Test utilies |
| 5 | +%%% @end |
| 6 | +%%%============================================================================= |
| 7 | +-module(dof_test_utils). |
| 8 | +-copyright("2015, Erlang Solutions Ltd."). |
| 9 | + |
| 10 | +%% API |
| 11 | +-export([figure4/0]). |
| 12 | + |
| 13 | +-include_lib("eunit/include/eunit.hrl"). |
| 14 | + |
| 15 | +%%%============================================================================= |
| 16 | +%%% External functions |
| 17 | +%%%============================================================================= |
| 18 | + |
| 19 | +%% Represents graph from figure 4 in |
| 20 | +%% https://docs.google.com/a/erlang-solutions.com/document/d/1HmdKtNvAR1f8JQeyY13kzGMUjdAgPDE9tuszo2OFXwM/edit#heading=h.ofp54z1pu56t |
| 21 | +figure4() -> |
| 22 | + Graph = create_graph(), |
| 23 | + [OFP1, OFP2] = |
| 24 | + [begin |
| 25 | + Ep = add_identifier({N, endpoint}, Graph), |
| 26 | + Ofp1 = add_identifier({{N, 1}, of_port}, Graph), |
| 27 | + OfsMetadata = #{datapath_id => no_to_dpid(N), ip => ip(N)}, |
| 28 | + Ofs = add_identifier({N, of_switch, OfsMetadata}, Graph), |
| 29 | + Ofp2 = add_identifier({{N, 2}, of_port}, Graph), |
| 30 | + add_link({Ep, Ofp1, connected_to}, Graph), |
| 31 | + add_link({Ofp1, Ofs, port_of}, Graph), |
| 32 | + add_link({Ofp2, Ofs, port_of}, Graph), |
| 33 | + Ofp2 |
| 34 | + end || N <- [1,2]], |
| 35 | + add_link({OFP1, OFP2, connected_to}, Graph), |
| 36 | + Graph. |
| 37 | + |
| 38 | +%%%============================================================================= |
| 39 | +%%% Internal functions |
| 40 | +%%%============================================================================= |
| 41 | + |
| 42 | +create_graph() -> |
| 43 | + digraph:new(). |
| 44 | + |
| 45 | +add_identifier({N, Type}, Graph) -> |
| 46 | + digraph:add_vertex(Graph, id(N, Type), #{type => Type}); |
| 47 | +add_identifier({N, Type, Metadata}, Graph) -> |
| 48 | + digraph:add_vertex(Graph, id(N, Type), |
| 49 | + maps:put(type, Type, Metadata)). |
| 50 | + |
| 51 | +add_link({Id1, Id2, Type}, Graph) -> |
| 52 | + digraph:add_edge(Graph, Id1, Id2, #{type => Type}), |
| 53 | + digraph:add_edge(Graph, Id2, Id1, #{type => Type}); |
| 54 | +add_link({Id1, Id2, Type, Metadata}, Graph) -> |
| 55 | + digraph:add_edge(Graph, Id1, Id2, maps:put(type, Type, Metadata)), |
| 56 | + digraph:add_edge(Graph, Id2, Id1, maps:put(type, Type, Metadata)). |
| 57 | + |
| 58 | +id(N, endpoint) -> |
| 59 | + list_to_binary("EP" ++ integer_to_list(N)); |
| 60 | +id({Sw, N}, of_port) -> |
| 61 | + list_to_binary("OFS"++ integer_to_list(Sw) ++ "/OFP" ++ integer_to_list(N)); |
| 62 | +id(N, of_switch) -> |
| 63 | + list_to_binary("OFS" ++ integer_to_list(N)). |
| 64 | + |
| 65 | +no_to_dpid(N) -> |
| 66 | + "00:00:00:00:00:01:00:0" ++ integer_to_list(N). |
| 67 | + |
| 68 | +dpid_to_no(Dpid) -> |
| 69 | + [No | _ ] = lists:reverse(Dpid), |
| 70 | + binary_to_integer(<<No>>). |
| 71 | + |
| 72 | +ip(N) -> |
| 73 | + {10, 0, 0, N}. |
| 74 | + |
| 75 | +path(<<"EP1">>, <<"EP2">>) -> |
| 76 | + [<<"EP1">>,<<"OFS1/OFP1">>,<<"OFS1">>, |
| 77 | + <<"OFS1/OFP2">>,<<"OFS2/OFP2">>, |
| 78 | + <<"OFS2">>,<<"OFS2/OFP1">>,<<"EP2">>]; |
| 79 | +path(A = <<"EP2">>, B = <<"EP1">>) -> |
| 80 | + lists:reverse(path(B,A)). |
| 81 | + |
| 82 | + |
| 83 | +%%%============================================================================= |
| 84 | +%%% Tests |
| 85 | +%%%============================================================================= |
| 86 | + |
| 87 | +figure4_graph_test() -> |
| 88 | + %% GIVEN |
| 89 | + [EP1, EP2] = [id(N, endpoint) || N <- [1,2]], |
| 90 | + |
| 91 | + %% WHEN |
| 92 | + Graph = figure4(), |
| 93 | + |
| 94 | + %% THEN |
| 95 | + ?assertEqual(path(EP1, EP2), digraph:get_path(Graph, EP1, EP2)), |
| 96 | + ?assertEqual(path(EP2, EP1), digraph:get_path(Graph, EP2, EP1)). |
0 commit comments