Skip to content

Commit

Permalink
Add Pio::OpenFlow10::PacketIn#to_ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jul 11, 2016
1 parent 6897791 commit 6de1242
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ AllCops:

Metrics/LineLength:
Exclude:
- 'fixtures/**/*.rb'
- 'spec/support/*.rb'

Style/StringLiterals:
EnforcedStyle: single_quotes

Expand Down
8 changes: 4 additions & 4 deletions features/open_flow10/exact_match.feature
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
Feature: ExactMatch
Scenario: new (from ARP request Packet In)
When I create an exact match from "open_flow10/packet_in_arp_request.raw"
When I create an exact match from "open_flow10/packet_in_arp_request.rb"
Then the message has the following fields and values:
| field | value |
| wildcards | {} |
| in_port | 1 |
| source_mac_address | ac:5d:10:31:37:79 |
| source_mac_address | fa:ce:b0:00:00:cc |
| destination_mac_address | ff:ff:ff:ff:ff:ff |
| vlan_vid | 65535 |
| vlan_priority | 0 |
| ether_type | 2054 |
| tos | 0 |
| ip_protocol | 1 |
| source_ip_address | 192.168.2.254 |
| destination_ip_address | 192.168.2.5 |
| source_ip_address | 192.168.0.1 |
| destination_ip_address | 192.168.0.2 |
| transport_source_port | 0 |
| transport_destination_port | 0 |

Expand Down
33 changes: 32 additions & 1 deletion features/open_flow10/packet_in.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Feature: PacketIn

Scenario: new
When I create an OpenFlow message with:
"""
"""ruby
arp_request = Pio::Arp::Request.new(
source_mac: 'fa:ce:b0:00:00:cc',
sender_protocol_address: '192.168.0.1',
Expand All @@ -30,3 +30,34 @@ Feature: PacketIn
| data.class | Pio::Arp::Request |
| source_mac | fa:ce:b0:00:00:cc |
| destination_mac | ff:ff:ff:ff:ff:ff |

Scenario: convert PacketIn to Ruby code
When I eval the following Ruby code:
"""ruby
arp_request = Pio::Arp::Request.new(
source_mac: 'fa:ce:b0:00:00:cc',
sender_protocol_address: '192.168.0.1',
target_protocol_address: '192.168.0.2'
)
Pio::PacketIn.new(transaction_id: 0,
buffer_id: 0xffffff00,
in_port: 1,
reason: :no_match,
raw_data: arp_request.to_binary).to_ruby
"""
Then the result of eval should be:
"""ruby
[
0x01, # version
0x0a, # type
0x00, 0x52, # _length
0x00, 0x00, 0x00, 0x00, # transaction_id
0xff, 0xff, 0xff, 0x00, # buffer_id
0x00, 0x40, # total_length
0x00, 0x01, # in_port
0x00, # reason
0x00, # padding
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa, 0xce, 0xb0, 0x00, 0x00, 0xcc, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xfa, 0xce, 0xb0, 0x00, 0x00, 0xcc, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # raw_data
].pack('C82')
"""
11 changes: 9 additions & 2 deletions features/step_definitions/packet_data_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
end

When(/^I create an exact match from "(.*?)"$/) do |path|
packet_in = Pio::PacketIn.read(IO.read(expand_path("%/#{path}")))
@result = Pio::ExactMatch.new(packet_in)
raw_data = case File.extname(path)
when '.raw'
IO.read(expand_path("%/#{path}"))
when '.rb'
Pio.module_eval(IO.read(expand_path("%/#{path}")))
else
raise
end
@result = Pio::ExactMatch.new(Pio::PacketIn.read(raw_data))
end

Then(/^the message should be a "([^"]*)"$/) do |expected_klass|
Expand Down
Binary file removed fixtures/open_flow10/packet_in_arp_request.raw
Binary file not shown.
12 changes: 12 additions & 0 deletions fixtures/open_flow10/packet_in_arp_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
0x01, # version
0x0a, # type
0x00, 0x52, # _length
0x00, 0x00, 0x00, 0x00, # transaction_id
0xff, 0xff, 0xff, 0x00, # buffer_id
0x00, 0x40, # total_length
0x00, 0x01, # in_port
0x00, # reason
0x00, # padding
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa, 0xce, 0xb0, 0x00, 0x00, 0xcc, 0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0xfa, 0xce, 0xb0, 0x00, 0x00, 0xcc, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # raw_data
].pack('C82')
2 changes: 2 additions & 0 deletions lib/pio/open_flow/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'pio/open_flow/flags'
require 'pio/open_flow/header'
require 'pio/parse_error'
require 'pio/ruby_dumper'

module Pio
module OpenFlow
Expand Down Expand Up @@ -33,6 +34,7 @@ def self.method_missing(method, *args, &block)
klass = Class.new(BinData::Record)
const_set :Format, klass
klass.class_eval do
include RubyDumper
define_method(:header_length) { 8 }
define_method(:length) { _length }
end
Expand Down
5 changes: 4 additions & 1 deletion lib/pio/open_flow10/packet_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class PacketIn < OpenFlow::Message
uint16 :in_port
reason :reason
uint8 :padding
hide :padding
string :raw_data, read_length: :total_length

def data
Expand All @@ -25,6 +24,10 @@ def lldp?
data.is_a? Lldp
end

def to_ruby
@format.to_ruby
end

def method_missing(method, *args)
data.__send__(method, *args).snapshot
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pio/open_flow10/packet_in/reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def get
def set(value)
self.reason = REASONS.fetch(value)
end

def to_hex
reason.to_hex
end
end
end
end
Expand Down

0 comments on commit 6de1242

Please sign in to comment.