quality/tres: Add handler testcases

This commit is contained in:
Eishun Kondoh 2019-05-08 01:31:27 +09:00
parent 0a6d7f31d6
commit 4965bc60ec
9 changed files with 273 additions and 98 deletions

View file

@ -43,29 +43,4 @@ defmodule Openflow.PacketIn do
data: data
}
end
def to_binary(%PacketIn{} = packet_in) do
%PacketIn{
buffer_id: buffer_id,
total_len: total_len,
reason: reason,
table_id: table_id,
cookie: cookie,
in_port: in_port,
match: match_fields,
data: data
} = packet_in
buffer_id_int = Openflow.Utils.get_enum(buffer_id, :buffer_id)
reason_int = Openflow.Utils.get_enum(reason, :packet_in_reason)
table_id_int = Openflow.Utils.get_enum(table_id, :table_id)
match_fields_bin =
[{:in_port, in_port} | match_fields]
|> Openflow.Match.new()
|> Openflow.Match.to_binary()
<<buffer_id_int::32, total_len::16, reason_int::8, table_id_int::8, cookie::64,
match_fields_bin::bytes, 0::size(2)-unit(8), data::bytes>>
end
end

View file

@ -14,19 +14,8 @@ defmodule Openflow.Role.Reply do
def ofp_type, do: 25
def new(options \\ []) do
role = Keyword.get(options, :role, :nochange)
generation_id = Keyword.get(options, :generation_id, 0)
%Reply{role: role, generation_id: generation_id}
end
def read(<<role_int::32, 0::size(4)-unit(8), generation_id::64>>) do
role = Openflow.Enums.to_atom(role_int, :controller_role)
%Reply{role: role, generation_id: generation_id}
end
def to_binary(%Reply{role: role, generation_id: generation_id}) do
role_int = Openflow.Enums.to_int(role, :controller_role)
<<role_int::32, 0::size(4)-unit(8), generation_id::64>>
end
end

View file

@ -21,11 +21,6 @@ defmodule Openflow.Role.Request do
%Request{xid: xid, role: role, generation_id: generation_id}
end
def read(<<role_int::32, 0::size(4)-unit(8), generation_id::64>>) do
role = Openflow.Enums.to_atom(role_int, :controller_role)
%Request{role: role, generation_id: generation_id}
end
def to_binary(%Request{role: role, generation_id: generation_id}) do
role_int = Openflow.Enums.to_int(role, :controller_role)
<<role_int::32, 0::size(4)-unit(8), generation_id::64>>

View file

@ -7,33 +7,50 @@ defmodule Tres.ExampleHandler do
defmodule State do
defstruct datapath_id: nil,
aux_id: nil,
conn_ref: nil
queue: :queue.new(),
client: nil
end
# API functions
@spec send(String.t(), map()) :: :ok
def send(datapath_id, msg), do: send_message(msg, datapath_id)
@spec get(datapath_id :: String.t()) :: map() | nil
def get(datapath_id) do
datapath_id
|> lookup_handler_pid()
|> GenServer.call(:get)
end
def start_link(datapath, args) do
GenServer.start_link(__MODULE__, [datapath, args])
end
# GenServer callbacks
def init([{datapath_id, aux_id}, _args]) do
info(
"Switch Ready: " <>
"datapath_id: #{datapath_id} " <> "aux_id: #{aux_id} " <> "on #{inspect(self())}"
)
_ = send_desc_stats_request(datapath_id)
_ = send_port_desc_stats_request(datapath_id)
state = %State{datapath_id: datapath_id, aux_id: aux_id}
{:ok, state}
:ok = info("datapath connected: #{datapath_id}")
:ok = send_flow_mod_add(datapath_id, priority: 0)
{:ok, %State{datapath_id: datapath_id, aux_id: aux_id}}
end
def handle_info(%PortDesc.Reply{datapath_id: datapath_id} = desc, state) do
handle_port_desc_stats_reply(desc, datapath_id)
{:noreply, state}
def handle_call(:get, from, %State{queue: {[], []}} = state) do
{:noreply, %{state | client: from}}
end
def handle_info(%Desc.Reply{datapath_id: datapath_id} = desc, state) do
handle_desc_stats_reply(desc, datapath_id)
{:noreply, state}
def handle_call(:get, _from, %State{} = state) do
{{:value, msg}, new_queue} = :queue.out(state.queue)
{:reply, msg, %{state | queue: new_queue}}
end
def handle_info(%{datapath_id: _datapath_id} = msg, %State{client: nil} = state) do
{:noreply, %{state | queue: :queue.in(msg, state.queue)}}
end
def handle_info(%{datapath_id: _datapath_id} = msg, state) do
GenServer.reply(state.client, msg)
{:noreply, %{state | client: nil}}
end
def handle_info({:switch_disconnected, _reason}, state) do
@ -41,47 +58,10 @@ defmodule Tres.ExampleHandler do
{:stop, :normal, state}
end
def handle_info({:switch_hang, _datapath_id}, state) do
:ok = warn("Switch possible hang: datapath_id: #{state.datapath_id}")
{:noreply, state}
end
# `Catch all` function is required.
def handle_info(info, state) do
:ok = warn("unhandled message #{inspect(info)}: #{state.datapath_id}")
{:noreply, state}
end
# private functions
defp send_desc_stats_request(datapath_id) do
Desc.Request.new()
|> send_message(datapath_id)
end
defp send_port_desc_stats_request(datapath_id) do
PortDesc.Request.new()
|> send_message(datapath_id)
end
defp handle_desc_stats_reply(desc, datapath_id) do
info(
"Switch Desc: " <>
"mfr = #{desc.mfr_desc} " <>
"hw = #{desc.hw_desc} " <> "sw = #{desc.sw_desc} " <> "for #{datapath_id}"
)
end
defp handle_port_desc_stats_reply(port_desc, datapath_id) do
for port <- port_desc.ports do
info(
"Switch has port: " <>
"number = #{port.number} " <>
"hw_addr = #{port.hw_addr} " <>
"name = #{port.name} " <>
"config = #{inspect(port.config)} " <>
"current_speed = #{port.current_speed} " <> "on #{datapath_id}"
)
end
end
end