Skip to content

Commit

Permalink
Refactor nicira_reg_load.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Nov 17, 2015
1 parent 987e2ca commit 2601ca0
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 56 deletions.
49 changes: 17 additions & 32 deletions features/open_flow13/nicira_reg_load.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ Feature: Pio::NiciraRegLoad
"""
Then it should finish successfully
And the action has the following fields and values:
| field | value |
| action_type.to_hex | 0xffff |
| action_length | 24 |
| experimenter_id.to_hex | 0x2320 |
| experimenter_type | 7 |
| offset | 0 |
| n_bits | 32 |
| destination | :reg0 |
| destination_internal | 65540 |
| value.to_hex | 0xdeadbeef |
| field | value |
| offset | 0 |
| n_bits | 32 |
| destination | :reg0 |
| value.to_hex | 0xdeadbeef |

Scenario: new(0xdeadbeef, :metadata)
When I try to create an OpenFlow action with:
Expand All @@ -26,31 +21,21 @@ Feature: Pio::NiciraRegLoad
"""
Then it should finish successfully
And the action has the following fields and values:
| field | value |
| action_type.to_hex | 0xffff |
| action_length | 24 |
| experimenter_id.to_hex | 0x2320 |
| experimenter_type | 7 |
| offset | 0 |
| n_bits | 64 |
| destination | :metadata |
| destination_internal | 2147484680 |
| value.to_hex | 0xdeadbeef |
| field | value |
| offset | 0 |
| n_bits | 64 |
| destination | :metadata |
| value.to_hex | 0xdeadbeef |

Scenario: new(0xdeadbeef, :metadata, offset: 32, n_bits: 32)
Scenario: new(0xdeadbeef, :reg0, offset: 16, n_bits: 16)
When I try to create an OpenFlow action with:
"""
Pio::NiciraRegLoad.new(0xdeadbeef, :metadata, offset: 32, n_bits: 32)
Pio::NiciraRegLoad.new(0xdeadbeef, :reg0, offset: 16, n_bits: 16)
"""
Then it should finish successfully
And the action has the following fields and values:
| field | value |
| action_type.to_hex | 0xffff |
| action_length | 24 |
| experimenter_id.to_hex | 0x2320 |
| experimenter_type | 7 |
| offset | 32 |
| n_bits | 32 |
| destination | :metadata |
| destination_internal | 2147484680 |
| value.to_hex | 0xdeadbeef |
| field | value |
| offset | 16 |
| n_bits | 16 |
| destination | :reg0 |
| value.to_hex | 0xdeadbeef |
67 changes: 43 additions & 24 deletions lib/pio/open_flow13/nicira_reg_load.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
require 'active_support/core_ext/string/inflections'
require 'pio/open_flow/action'
require 'pio/open_flow/nicira_action'
require 'pio/open_flow13/match'

module Pio
module OpenFlow13
# NXAST_REG_LOAD action
class NiciraRegLoad < OpenFlow::Action
action_header action_type: 0xffff, action_length: 24
uint32 :experimenter_id, value: 0x2320
uint16 :experimenter_type, value: 7
bit10 :offset_internal, initial_value: 0
bit6 :n_bits_internal
uint32 :destination_internal
uint64 :value_internal

attr_reader :destination
class NiciraRegLoad < OpenFlow::NiciraAction
nicira_action_header action_type: 0xffff,
action_length: 24,
subtype: 7
bit10 :_offset, initial_value: 0
bit6 :_n_bits
struct :_destination do
uint16 :oxm_class
bit9 :oxm_field
bit7 :oxm_length
end
uint64 :_value

# rubocop:disable AbcSize
# rubocop:disable LineLength
def initialize(value, destination, options = {})
@destination = destination
oxm_klass = Match.const_get(destination.to_s.split('_').map(&:capitalize).join)
super(value_internal: value,
offset_internal: options[:offset] || 0,
n_bits_internal: options[:n_bits] ? options[:n_bits] - 1 : oxm_klass.new.length * 8 - 1,
destination_internal: ((oxm_klass.superclass.const_get(:OXM_CLASS) << 16) | (oxm_klass.const_get(:OXM_FIELD) << 9) | oxm_klass.new.length))
super(_value: value,
_offset: options[:offset] || 0,
_n_bits: (options[:n_bits] || oxm_length * 8) - 1,
_destination: { oxm_class: oxm_class,
oxm_field: oxm_field,
oxm_length: oxm_length })
end
# rubocop:enable AbcSize
# rubocop:enable LineLength

attr_reader :destination

def offset
offset_internal
_offset
end

def n_bits
n_bits_internal + 1
_n_bits + 1
end

def value
value_internal
_value
end

private

def oxm_class
destination_oxm_class.superclass.const_get(:OXM_CLASS)
end

def oxm_field
destination_oxm_class.const_get(:OXM_FIELD)
end

def oxm_length
destination_oxm_class.new.length
end

def destination_oxm_class
Match.const_get(@destination.to_s.split('_').map(&:capitalize).join)
end
end
end
Expand Down
71 changes: 71 additions & 0 deletions spec/pio/open_flow13/nicira_reg_load_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'pio/open_flow13/nicira_reg_load'

describe Pio::OpenFlow13::NiciraRegLoad do
describe '.new' do
When(:nicira_reg_load) do
Pio::OpenFlow13::NiciraRegLoad.new(value, destination, options)
end

context 'with 0xdeadbeef, :reg0' do
Given(:value) { 0xdeadbeef }
Given(:destination) { :reg0 }
Given(:options) { {} }

Invariant do
nicira_reg_load.n_bits == nicira_reg_load._destination[:oxm_length] * 8
end

Then { nicira_reg_load.action_type == 0xffff }
Then { nicira_reg_load.action_length == 24 }
Then { nicira_reg_load.vendor == 0x2320 }
Then { nicira_reg_load.subtype == 7 }
Then { nicira_reg_load.offset == 0 }
Then { nicira_reg_load.n_bits == 32 }
Then { nicira_reg_load.destination == :reg0 }
Then { nicira_reg_load._destination[:oxm_class] == 1 }
Then { nicira_reg_load._destination[:oxm_field] == 0 }
Then { nicira_reg_load._destination[:oxm_length] == 4 }
Then { nicira_reg_load.value == 0xdeadbeef }
end

context 'with 0xdeadbeef, :metadata' do
Given(:value) { 0xdeadbeef }
Given(:destination) { :metadata }
Given(:options) { {} }

Invariant do
nicira_reg_load.n_bits == nicira_reg_load._destination[:oxm_length] * 8
end

Then { nicira_reg_load.action_type == 0xffff }
Then { nicira_reg_load.action_length == 24 }
Then { nicira_reg_load.vendor == 0x2320 }
Then { nicira_reg_load.subtype == 7 }
Then { nicira_reg_load.offset == 0 }
Then { nicira_reg_load.n_bits == 64 }
Then { nicira_reg_load.destination == :metadata }
Then { nicira_reg_load._destination[:oxm_class] == 0x8000 }
Then { nicira_reg_load._destination[:oxm_field] == 2 }
Then { nicira_reg_load._destination[:oxm_length] == 8 }
Then { nicira_reg_load.value == 0xdeadbeef }
end

context 'with 0xdeadbeef, :reg0, offset: 16, n_bits: 16' do
Given(:value) { 0xdeadbeef }
Given(:destination) { :reg0 }
Given(:options) { { offset: 16, n_bits: 16 } }

Then { nicira_reg_load.action_type == 0xffff }
Then { nicira_reg_load.action_length == 24 }
Then { nicira_reg_load.vendor == 0x2320 }
Then { nicira_reg_load.subtype == 7 }
Then { nicira_reg_load.offset == 16 }
Then { nicira_reg_load.n_bits == 16 }
Then { nicira_reg_load.destination == :reg0 }
Then { nicira_reg_load._destination[:oxm_class] == 1 }
Then { nicira_reg_load._destination[:oxm_field] == 0 }
Then { nicira_reg_load._destination[:oxm_length] == 4 }
Then { nicira_reg_load.value == 0xdeadbeef }
end
end
end

0 comments on commit 2601ca0

Please sign in to comment.