Scripts placed on scripts directory
[vsorcdistro/.git] / ryu / ryu / tests / packet_data_generator / src / x.erl
1 %% Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 %% Copyright (C) 2013 YAMAMOTO Takashi <yamamoto at valinux co jp>
3 %%
4 %% Licensed under the Apache License, Version 2.0 (the "License");
5 %% you may not use this file except in compliance with the License.
6 %% You may obtain a copy of the License at
7 %%
8 %%    http://www.apache.org/licenses/LICENSE-2.0
9 %%
10 %% Unless required by applicable law or agreed to in writing, software
11 %% distributed under the License is distributed on an "AS IS" BASIS,
12 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 %% implied.
14 %% See the License for the specific language governing permissions and
15 %% limitations under the License.
16
17 -module(x).
18 -export([do/2, x/0]).
19
20 % eg. 1 -> of10
21 ofp_version_string(Vers) ->
22     ["of", integer_to_list(9 + Vers)].
23
24 do(skip, {OFPVersion, N}) ->
25     {OFPVersion, N + 1};
26 do(Body, {OFPVersion, N}) ->
27     Mod = case OFPVersion of
28         1 -> x_flower_packet;
29         _ -> x_of_protocol
30     end,
31     Name = case Body of
32         B when is_tuple(B) ->
33             atom_to_list(element(1, B));
34         _ ->
35             atom_to_list(Body)
36     end,
37     io:format("processing ~B ~B ~s~n", [OFPVersion, N, Name]),
38     Msg = Mod:message(OFPVersion, 0, Body),
39     case Mod:encode(Msg) of
40         {ok, BinMsg} -> ok;
41         {error, Error} -> io:format("~p ~p~n", [Error, Msg]), BinMsg = hoge
42     end,
43     {ok, F} = file:open(["../packet_data/",
44         ofp_version_string(OFPVersion), "/", integer_to_list(OFPVersion), "-",
45         integer_to_list(N), "-", Name, ".packet"], [write, binary]),
46
47     % sanity check
48     % this is fragile because of order of flags.
49     % ofp flags are unorderd but of_protocol keeps them in a list.
50     {ok, Msg2, <<>>} = Mod:decode(BinMsg),
51     {OFPVersion, 0, Body2} = Mod:message_extract(Msg2),
52     case Body == Body2 of
53         false -> io:format("~p~n", [Body]), io:format("~p~n", [Body2]);
54         _ -> hoge
55     end,
56     Body = Body2,
57
58     ok = file:write(F, BinMsg),
59     ok = file:close(F),
60     {OFPVersion, N + 1}.
61
62 x() ->
63     lists:map(fun(Mod) -> Mod:x() end, [x1, x3, x4, x5]).