Skip to content

Commit

Permalink
Refactor SetVlanVid class
Browse files Browse the repository at this point in the history
Delete SetVlanVid::Format class Using Pio::OpenFlow::Action.
Refs #253
  • Loading branch information
yasuhito committed Oct 26, 2015
1 parent 259ff45 commit c49ac95
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
6 changes: 3 additions & 3 deletions features/open_flow10/flow_mod.feature
Expand Up @@ -76,7 +76,7 @@ Feature: Pio::FlowMod
| out_port | 65535 |
| flags | [:send_flow_rem] |
| actions.length | 12 |
| actions.first.class | Pio::SetVlanVid |
| actions.first.class | Pio::OpenFlow10::SetVlanVid |
| actions.first.vlan_id | 10 |

Scenario: read (Flow Mod Modify)
Expand Down Expand Up @@ -113,7 +113,7 @@ Feature: Pio::FlowMod
| out_port | 65535 |
| flags | [:send_flow_rem] |
| actions.length | 12 |
| actions.first.class | Pio::SetVlanVid |
| actions.first.class | Pio::OpenFlow10::SetVlanVid |
| actions.first.vlan_id | 10 |

Scenario: read (Flow Mod Modify Strict)
Expand Down Expand Up @@ -150,7 +150,7 @@ Feature: Pio::FlowMod
| out_port | 65535 |
| flags | [:send_flow_rem] |
| actions.length | 12 |
| actions.first.class | Pio::SetVlanVid |
| actions.first.class | Pio::OpenFlow10::SetVlanVid |
| actions.first.vlan_id | 10 |

Scenario: read (Flow Mod Delete)
Expand Down
4 changes: 2 additions & 2 deletions features/open_flow10/set_vlan_vid.feature
@@ -1,10 +1,10 @@
@open_flow10
Feature: Pio::SetVlanVid
Feature: Pio::OpenFlow10::SetVlanVid

Scenario: new(1)
When I try to create an OpenFlow action with:
"""
Pio::SetVlanVid.new(1)
Pio::OpenFlow10::SetVlanVid.new(1)
"""
Then it should finish successfully
And the action has the following fields and values:
Expand Down
2 changes: 1 addition & 1 deletion lib/pio/open_flow10/actions.rb
Expand Up @@ -16,7 +16,7 @@ module OpenFlow
class Actions < BinData::Primitive
ACTION_CLASS = {
0 => Pio::OpenFlow10::SendOutPort,
1 => Pio::SetVlanVid,
1 => Pio::OpenFlow10::SetVlanVid,
2 => Pio::SetVlanPriority,
3 => Pio::StripVlanHeader,
4 => Pio::SetEtherSourceAddress,
Expand Down
26 changes: 16 additions & 10 deletions lib/pio/open_flow10/set_vlan_vid.rb
@@ -1,18 +1,24 @@
require 'pio/open_flow/action'
require 'pio/open_flow10/set_vlan'

module Pio
# An action to modify the VLAN ID of a packet.
class SetVlanVid < SetVlan
def_format :vlan_id, 1
module OpenFlow10
# An action to modify the VLAN ID of a packet.
class SetVlanVid < OpenFlow::Action
action_header action_type: 1, action_length: 8
uint16 :vlan_id
string :padding, length: 2
hide :padding

def initialize(number)
vlan_id = number.to_i
unless vlan_id >= 1 && vlan_id <= 4095
fail ArgumentError, 'VLAN ID must be between 1 and 4095 inclusive'
def initialize(number)
vlan_id = number.to_i
unless vlan_id >= 1 && vlan_id <= 4095
fail ArgumentError, 'VLAN ID must be between 1 and 4095 inclusive'
end
super(vlan_id: vlan_id)
rescue NoMethodError
raise TypeError, 'VLAN ID must be an Integer.'
end
@format = Format.new(vlan_id: vlan_id)
rescue NoMethodError
raise TypeError, 'VLAN ID must be an Integer.'
end
end
end
8 changes: 4 additions & 4 deletions spec/pio/open_flow10/packet_out_spec.rb
Expand Up @@ -137,15 +137,15 @@
transaction_id: 0x16,
buffer_id: 0xffffffff,
in_port: 0xffff,
actions: Pio::SetVlanVid.new(10),
actions: Pio::OpenFlow10::SetVlanVid.new(10),
raw_data: data_dump
}
end

Then { result.message_length == 0x58 }
Then { result.actions_len == 0x8 }
Then { result.actions.length == 1 }
Then { result.actions[0].is_a? Pio::SetVlanVid }
Then { result.actions[0].is_a? Pio::OpenFlow10::SetVlanVid }
Then { result.actions[0].vlan_id == 10 }
end

Expand Down Expand Up @@ -336,7 +336,7 @@
buffer_id: 0xffffffff,
in_port: 0xffff,
actions: [Pio::OpenFlow10::SendOutPort.new(2),
Pio::SetVlanVid.new(10)],
Pio::OpenFlow10::SetVlanVid.new(10)],
raw_data: data_dump
}
end
Expand All @@ -347,7 +347,7 @@
Then { result.actions[0].is_a? Pio::OpenFlow10::SendOutPort }
Then { result.actions[0].port == 2 }
Then { result.actions[0].max_length == 2**16 - 1 }
Then { result.actions[1].is_a? Pio::SetVlanVid }
Then { result.actions[1].is_a? Pio::OpenFlow10::SetVlanVid }
Then { result.actions[1].vlan_id == 10 }
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/pio/open_flow10/set_vlan_vid_spec.rb
@@ -1,8 +1,8 @@
require 'pio/open_flow10/set_vlan_vid'

describe Pio::SetVlanVid do
describe Pio::OpenFlow10::SetVlanVid do
describe '.new' do
When(:set_vlan_vid) { Pio::SetVlanVid.new(vlan_id) }
When(:set_vlan_vid) { Pio::OpenFlow10::SetVlanVid.new(vlan_id) }

context 'with 10' do
When(:vlan_id) { 10 }
Expand Down

0 comments on commit c49ac95

Please sign in to comment.