quality: Add test cases for flow_stats messages

This commit is contained in:
Eishun Kondoh 2019-05-05 20:45:35 +09:00
parent 7055dfd93b
commit 2557778042
4 changed files with 256 additions and 61 deletions

View file

@ -11,12 +11,19 @@ defmodule Openflow.Multipart.Flow.Reply do
alias __MODULE__
def ofp_type, do: 18
@type t :: %Reply{
version: 4,
datapath_id: String.t() | nil,
aux_id: 0..0xFF | nil,
xid: 0..0xFFFFFFFF,
flags: [:more],
flows: [Openflow.Multipart.FlowStats.t()]
}
def new(flows \\ []) do
%Reply{flows: flows}
end
@spec ofp_type() :: 19
def ofp_type, do: 19
@spec read(<<_::16, _::_*384>>) :: t()
def read(<<flows_bin::bytes>>) do
flows = Openflow.Multipart.FlowStats.read(flows_bin)
%Reply{flows: flows}
@ -54,27 +61,53 @@ defmodule Openflow.Multipart.FlowStats do
alias __MODULE__
def read(binary) do
do_read([], binary)
end
@type t :: %FlowStats{
table_id: 0..0xFF,
duration_sec: 0..0xFFFFFFFF,
duration_nsec: 0..0xFFFFFFFF,
priority: 0..0xFFFF,
idle_timeout: 0..0xFFFF,
hard_timeout: 0..0xFFFF,
flags: [:send_flow_rem | :delete_learned | :write_result],
cookie: 0..0xFFFFFFFFFFFFFFFF,
packet_count: 0..0xFFFFFFFFFFFFFFFF,
byte_count: 0..0xFFFFFFFFFFFFFFFF,
match: Openflow.Match.new(),
instructions: [Openflow.Instruction.instruction()]
}
@spec read(<<_::_*8>>) :: [t()]
def read(binary), do: do_read([], binary)
# private functions
defp do_read(acc, ""), do: Enum.reverse(acc)
@spec do_read(acc :: [t()], binary()) :: [t()]
defp do_read(acc, ""), do: acc
@spec do_read(acc :: [t()], <<_::16, _::_*8>>) :: [t()]
defp do_read(acc, <<length::16, _tail::bytes>> = binary) do
<<flow_stats_bin::size(length)-bytes, rest::bytes>> = binary
do_read([codec(flow_stats_bin) | acc], rest)
do_read(acc ++ [codec(flow_stats_bin)], rest)
end
defp codec(
<<_length::16, table_id_int::8, 0::8, duration_sec::32, duration_nsec::32, priority::16,
idle::16, hard::16, flags_int::16, _::size(4)-unit(8), cookie::64, packet_count::64,
byte_count::64, tail::bytes>>
) do
{match, instructions_bin} = Openflow.Match.read(tail)
table_id = Openflow.Utils.get_enum(table_id_int, :table_id)
defp codec(<<
_length::16,
table_id::8,
0::8,
duration_sec::32,
duration_nsec::32,
priority::16,
idle::16,
hard::16,
flags_int::16,
_::size(4)-unit(8),
cookie::64,
packet_count::64,
byte_count::64,
tail::bytes
>>) do
flags = Openflow.Enums.int_to_flags(flags_int, :flow_mod_flags)
{match, instructions_bin} = Openflow.Match.read(tail)
instructions = Openflow.Instruction.read(instructions_bin)
%FlowStats{