diff --git a/lib/openflow/multipart/aggregate/reply.ex b/lib/openflow/multipart/aggregate/reply.ex index 7b710c2..dfecbf0 100644 --- a/lib/openflow/multipart/aggregate/reply.ex +++ b/lib/openflow/multipart/aggregate/reply.ex @@ -13,9 +13,59 @@ defmodule Openflow.Multipart.Aggregate.Reply do alias __MODULE__ - def ofp_type, do: 18 + @type t :: %Reply{ + version: 4, + xid: 0..0xFFFFFFFF, + datapath_id: String.t() | nil, + aux_id: 0..0xFF, + flags: [], + packet_count: 0..0xFFFFFFFFFFFFFFFF, + byte_count: 0..0xFFFFFFFFFFFFFFFF, + flow_count: 0..0xFFFFFFFF + } - def read(<>) do - %Reply{packet_count: packet_count, byte_count: byte_count, flow_count: flow_count} + @spec ofp_type() :: 19 + def ofp_type, do: 19 + + @spec new( + xid: 0..0xFFFFFFFF, + flags: [], + packet_count: 0..0xFFFFFFFFFFFFFFFF, + byte_count: 0..0xFFFFFFFFFFFFFFFF, + flow_count: 0..0xFFFFFFFF + ) :: t() + def new(options \\ []) do + %Reply{ + xid: options[:xid] || 0, + flags: options[:flags] || [], + packet_count: options[:packet_count] || 0, + byte_count: options[:byte_count] || 0, + flow_count: options[:flow_count] || 0 + } + end + + @spec to_binary(t()) :: <<_::192>> + def to_binary(aggregate) do + << + Openflow.Multipart.Reply.header(aggregate)::bytes, + aggregate.packet_count::64, + aggregate.byte_count::64, + aggregate.flow_count::32, + 0::size(4)-unit(8) + >> + end + + @spec read(<<_::192>>) :: t() + def read(<< + packet_count::64, + byte_count::64, + flow_count::32, + _::size(4)-unit(8) + >>) do + %Reply{ + packet_count: packet_count, + byte_count: byte_count, + flow_count: flow_count + } end end diff --git a/test/lib/openflow/ofp_aggregate_test.exs b/test/lib/openflow/ofp_aggregate_test.exs new file mode 100644 index 0000000..a6c7a08 --- /dev/null +++ b/test/lib/openflow/ofp_aggregate_test.exs @@ -0,0 +1,40 @@ +defmodule OfpAggregateTest do + use ExUnit.Case + + describe "Openflow.Multipart.Aggregate.Request" do + test "with default values" do + aggregate = + %Openflow.Multipart.Aggregate.Request{} + |> Map.to_list() + |> Openflow.Multipart.Aggregate.Request.new() + |> Openflow.to_binary() + |> Openflow.read() + |> Kernel.elem(1) + + assert aggregate.xid == 0 + assert aggregate.table_id == :all + assert aggregate.out_port == :any + assert aggregate.out_group == :any + assert aggregate.cookie == 0 + assert aggregate.cookie_mask == 0 + assert aggregate.match == [] + end + end + + describe "Openflow.Multipart.Aggregate.Reply" do + test "with default values" do + aggregate = + %Openflow.Multipart.Aggregate.Reply{} + |> Map.to_list() + |> Openflow.Multipart.Aggregate.Reply.new() + |> Openflow.to_binary() + |> Openflow.read() + |> Kernel.elem(1) + + assert aggregate.xid == 0 + assert aggregate.packet_count == 0 + assert aggregate.byte_count == 0 + assert aggregate.flow_count == 0 + end + end +end