of-wire json test: a program to generate test data for unit tests

Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
YAMAMOTO Takashi 2013-07-23 08:02:53 +09:00 committed by FUJITA Tomonori
parent e6dd2e8683
commit d0cd6e1533
9 changed files with 11157 additions and 0 deletions

View File

@ -0,0 +1,5 @@
generate:
rebar get-deps
rebar compile
erl -pa ebin deps/*/ebin -s x x -s init stop

View File

@ -0,0 +1,32 @@
%% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
%% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
%% implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
{deps,
[{of_protocol, ".*",
% {git, "http://github.com/FlowForwarding/of_protocol.git",
% {branch, "master"}}
% use a local repo until the following changes are merged:
% https://github.com/FlowForwarding/of_protocol/pull/17
% https://github.com/FlowForwarding/of_protocol/pull/18
% https://github.com/FlowForwarding/of_protocol/pull/19
% https://github.com/FlowForwarding/of_protocol/pull/20
% https://github.com/FlowForwarding/of_protocol/pull/23
% https://github.com/FlowForwarding/of_protocol/pull/28
{git, "http://github.com/yamt/of_protocol.git", {branch, "ryu"}}
},
{flower, ".*",
{git, "http://github.com/travelping/flower.git", {branch, "master"}}}
]}.

View File

@ -0,0 +1,24 @@
%% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
%% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
%% implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
{application, er,
[{description, "generate test data"},
{vsn, "1.0"},
{applications,
[kernel,
stdlib,
of_protocol]}]}.

View File

@ -0,0 +1,63 @@
%% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
%% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
%% implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(x).
-export([do/2, x/0]).
% eg. 1 -> of10
ofp_version_string(Vers) ->
["of", integer_to_list(9 + Vers)].
do(skip, {OFPVersion, N}) ->
{OFPVersion, N + 1};
do(Body, {OFPVersion, N}) ->
Mod = case OFPVersion of
1 -> x_flower_packet;
_ -> x_of_protocol
end,
Name = case Body of
B when is_tuple(B) ->
atom_to_list(element(1, B));
_ ->
atom_to_list(Body)
end,
io:format("processing ~B ~B ~s~n", [OFPVersion, N, Name]),
Msg = Mod:message(OFPVersion, 0, Body),
case Mod:encode(Msg) of
{ok, BinMsg} -> ok;
{error, Error} -> io:format("~p ~p~n", [Error, Msg]), BinMsg = hoge
end,
{ok, F} = file:open(["../packet_data/",
ofp_version_string(OFPVersion), "/", integer_to_list(OFPVersion), "-",
integer_to_list(N), "-", Name, ".packet"], [write, binary]),
% sanity check
% this is fragile because of order of flags.
% ofp flags are unorderd but of_protocol keeps them in a list.
{ok, Msg2, <<>>} = Mod:decode(BinMsg),
{OFPVersion, 0, Body2} = Mod:message_extract(Msg2),
case Body == Body2 of
false -> io:format("~p~n", [Body]), io:format("~p~n", [Body2]);
_ -> hoge
end,
Body = Body2,
ok = file:write(F, BinMsg),
ok = file:close(F),
{OFPVersion, N + 1}.
x() ->
lists:map(fun(Mod) -> Mod:x() end, [x1, x3, x4]).

View File

@ -0,0 +1,90 @@
%% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
%% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
%% implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(x1).
-export([x/0]).
-include_lib("flower/include/flower_packet.hrl").
cookie(Bin) ->
<<Int:64>> = Bin,
Int.
x() ->
List = [
skip,
#ofp_packet_out{
buffer_id = ?OFP_NO_BUFFER,in_port = controller,
actions =
[#ofp_action_output{port = all,max_len = 65535}],
data =
<<242,11,164,208,63,112,242,11,164,125,248,234,8,0,69,0,
0,84,248,26,0,0,255,1,175,139,10,0,0,1,10,0,0,2,8,0,2,
8,247,96,0,0,49,214,2,0,0,0,0,0,171,141,45,49,0,0,0,0,
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,0,0,0,0,0,0,
0,0>>},
#ofp_flow_mod{
cookie = cookie(<<0,0,0,0,0,0,0,0>>),
command = add,idle_timeout = 0,
hard_timeout = 0,priority = 123,buffer_id = 65535,
out_port = all,flags = [],
match =
#ofp_match{
wildcards = 16#3ffff7,
dl_dst = <<"\362\v\244}\370\352">>,
% XXX ryu and flower have different defaults for the
% followin fields.
in_port = 0,
dl_src = <<(-1):6/unit:8>>
},
actions = [#ofp_action_output{port = 6,max_len = 65535}]},
skip,
#ofp_packet_in{
buffer_id = 2,total_len=42,reason = action,in_port = 99,
data =
<<255,255,255,255,255,255,242,11,164,125,248,234,8,6,0,
1,8,0,6,4,0,1,242,11,164,125,248,234,10,0,0,1,0,0,0,0,
0,0,10,0,0,3>>},
features_request,
#ofp_switch_features{ % features_reply
datapath_id = 16#ff12345678,n_buffers = 0,n_tables = 255,
capabilities =
[arp_match_ip,ip_reasm,stp,flow_stats],
actions =
[enqueue,set_nw_src,set_vlan_vid,output],
ports =
[#ofp_phy_port{
port_no = 7,hw_addr = <<"\362\v\244\320?p">>,
name = <<80,111,114,116,7>>,
config = [],
state = [stp_block],
curr = [autoneg,copper,'100mb_fd'],
advertised = [autoneg,copper],
supported = [autoneg,copper,'100mb_fd'],
peer = [autoneg,copper,'100mb_fd']},
#ofp_phy_port{
port_no = 6,hw_addr = <<"\362\v\244}\370\352">>,
name = <<80,111,114,116,6>>,
config = [],
state = [stp_listen],
curr = [autoneg,copper,'100mb_fd'],
advertised = [autoneg,copper],
supported = [autoneg,copper,'100mb_fd'],
peer = [autoneg,copper,'100mb_fd']}]}
],
lists:foldl(fun x:do/2, {1, 0}, List).

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,61 @@
%% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
%% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
%% implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(x_flower_packet).
-export([message/3, message_extract/1, encode/1, decode/1]).
-include_lib("flower/include/flower_packet.hrl").
message(OFPVersion, Xid, Body) ->
#ovs_msg{version=OFPVersion, xid=Xid, msg=Body}.
message_extract(Msg) ->
#ovs_msg{version=OFPVersion, xid=Xid, msg=Body} = Msg,
{OFPVersion, Xid, Body}.
guess_type(Msg) ->
% eg. {ofp_packet_out, ...} -> packet_out
RecType = element(1, Msg),
case RecType of
ofp_switch_features ->
features_reply;
_ ->
RecTypeStr = atom_to_list(RecType),
TypeStr = lists:nthtail(4, RecTypeStr),
list_to_atom(TypeStr)
end.
encode(Msg) ->
Msg2 = case Msg of
#ovs_msg{msg = features_request} ->
Msg#ovs_msg{type = features_request, msg = <<>>};
_ ->
Msg#ovs_msg{type = guess_type(Msg#ovs_msg.msg)}
end,
%io:format("encoding ~p~n", [Msg2]),
BinMsg = flower_packet:encode(Msg2),
{ok, BinMsg}.
decode(BinMsg) ->
{[Msg], <<>>} = flower_packet:decode(BinMsg),
%io:format("decoded ~p~n", [Msg]),
Msg2 = case Msg of
#ovs_msg{type = features_request} ->
Msg#ovs_msg{msg = features_request};
_ ->
Msg
end,
{ok, Msg2, <<>>}.

View File

@ -0,0 +1,33 @@
%% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
%% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
%% implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(x_of_protocol).
-export([message/3, message_extract/1, encode/1, decode/1]).
-include_lib("of_protocol/include/of_protocol.hrl").
message(OFPVersion, Xid, Body) ->
#ofp_message{version=OFPVersion, xid=Xid, body=Body}.
message_extract(Msg) ->
#ofp_message{version=OFPVersion, xid=Xid, body=Body} = Msg,
{OFPVersion, Xid, Body}.
encode(Msg) ->
of_protocol:encode(Msg).
decode(BinMsg) ->
of_protocol:decode(BinMsg).