Skip to content

Commit

Permalink
Add hello13_version_bitmap.raw parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Apr 7, 2015
1 parent 9ecd4f1 commit f6c5533
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
30 changes: 22 additions & 8 deletions features/hello13_read.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ Feature: Pio::Hello13.read
When I try to parse the file with "Hello13" class
Then it should finish successfully
And the parsed data have the following field and value:
| field | value |
| class | Pio::Hello13 |
| ofp_version | 4 |
| message_type | 0 |
| message_length | 8 |
| transaction_id | 0 |
| xid | 0 |
| elements | [] |
| field | value |
| class | Pio::Hello13 |
| ofp_version | 4 |
| message_type | 0 |
| message_length | 8 |
| transaction_id | 0 |
| xid | 0 |
| supported_versions | [] |

Scenario: hello13_version_bitmap.raw
Given a packet data file "hello13_version_bitmap.raw"
When I try to parse the file with "Hello13" class
Then it should finish successfully
And the parsed data have the following field and value:
| field | value |
| class | Pio::Hello13 |
| ofp_version | 4 |
| message_type | 0 |
| message_length | 16 |
| transaction_id | 0 |
| xid | 0 |
| supported_versions | [:open_flow10, :open_flow13] |
45 changes: 42 additions & 3 deletions lib/pio/hello13.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
require 'bindata'

module Pio
# OpenFlow 1.3 Hello Element
# ofp_hello_elem_header and value
class HelloElement < BinData::Record
endian :big

uint16 :element_type
uint16 :element_length
choice :element_value, selection: :chooser do
string 'unknown', read_length: -> { element_length - 4 }
uint32 'version_bitmap'
end

def version_bitmap?
element_type == 1
end

private

def chooser
version_bitmap? ? 'version_bitmap' : 'unknown'
end
end

# OpenFlow 1.3 Hello message
Expand All @@ -11,14 +29,35 @@ class Hello13 < BinData::Record

uint8 :ofp_version, initial_value: 4
uint8 :message_type
uint16 :message_length, value: 8
uint16 :message_length, initial_value: 8
uint32 :transaction_id
array :elements, type: :hello_element, length: 0
array :elements,
type: :hello_element,
read_until: :eof

def xid
transaction_id
end

def supported_versions
supported_versions_list.map do |each|
"open_flow1#{each - 1}".to_sym
end
end

alias_method :to_binary, :to_binary_s

private

def supported_versions_list
(1..32).each_with_object([]) do |each, result|
result << each if (version_bitmap >> each & 1) == 1
end
end

def version_bitmap
bitmap = elements.select(&:version_bitmap?).first
bitmap ? bitmap.element_value : 0
end
end
end

0 comments on commit f6c5533

Please sign in to comment.