Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / ryu / ryu / tests / packet_data_generator / src / x_flower_packet.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_flower_packet).
18 -export([message/3, message_extract/1, encode/1, decode/1]).
19
20 -include_lib("flower/include/flower_packet.hrl").
21
22 message(OFPVersion, Xid, Body) ->
23     #ovs_msg{version=OFPVersion, xid=Xid, msg=Body}.
24
25 message_extract(Msg) ->
26     #ovs_msg{version=OFPVersion, xid=Xid, msg=Body} = Msg,
27     {OFPVersion, Xid, Body}.
28
29 guess_type(Msg) ->
30     % eg. {ofp_packet_out, ...} -> packet_out
31     RecType = element(1, Msg),
32     case RecType of
33         ofp_switch_features ->
34             features_reply;
35         _ ->
36             RecTypeStr = atom_to_list(RecType),
37             TypeStr = lists:nthtail(4, RecTypeStr),
38             list_to_atom(TypeStr)
39     end.
40
41 encode(Msg) ->
42     Msg2 = case Msg of
43         #ovs_msg{msg = features_request} ->
44             Msg#ovs_msg{type = features_request, msg = <<>>};
45         _ ->
46             Msg#ovs_msg{type = guess_type(Msg#ovs_msg.msg)}
47     end,
48     %io:format("encoding ~p~n", [Msg2]),
49     BinMsg = flower_packet:encode(Msg2),
50     {ok, BinMsg}.
51
52 decode(BinMsg) ->
53     {[Msg], <<>>} = flower_packet:decode(BinMsg),
54     %io:format("decoded ~p~n", [Msg]),
55     Msg2 = case Msg of
56         #ovs_msg{type = features_request} ->
57             Msg#ovs_msg{msg = features_request};
58         _ ->
59             Msg
60     end,
61     {ok, Msg2, <<>>}.