Openflow parser
This commit is contained in:
parent
70b0d8919e
commit
fc02a678de
338 changed files with 9081 additions and 0 deletions
29
lib/openflow/action.ex
Normal file
29
lib/openflow/action.ex
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
defmodule Openflow.Action do
|
||||
|
||||
def read(action_bin) do
|
||||
do_read([], action_bin)
|
||||
end
|
||||
|
||||
def to_binary(actions) when is_list(actions) do
|
||||
to_binary(<<>>, actions)
|
||||
end
|
||||
def to_binary(action) do
|
||||
to_binary([action])
|
||||
end
|
||||
|
||||
# private functions
|
||||
|
||||
defp do_read(acc, <<>>), do: Enum.reverse(acc)
|
||||
defp do_read(acc, <<0::32, _::bytes>>), do: Enum.reverse(acc)
|
||||
defp do_read(acc, <<type::16, length::16, _::bytes>> = binary) do
|
||||
<<action_bin::size(length)-bytes, rest::bytes>> = binary
|
||||
codec = Openflow.Enums.to_atom(type, :action_type)
|
||||
do_read([codec.read(action_bin)|acc], rest)
|
||||
end
|
||||
|
||||
defp to_binary(acc, []), do: acc
|
||||
defp to_binary(acc, [action|rest]) do
|
||||
codec = action.__struct__
|
||||
to_binary(<<acc::bytes, (codec.to_binary(action))::bytes>>, rest)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue