Class: Phut::OpenVswitch

Inherits:
Object
  • Object
show all
Extended by:
Finder, ShellRunner
Includes:
ShellRunner
Defined in:
lib/phut/open_vswitch.rb

Overview

Open vSwitch controller rubocop:disable ClassLength

Direct Known Subclasses

Vswitch

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

sh, sudo

Methods included from Finder

find_by, find_by!

Constructor Details

#initialize(dpid:, openflow_version: 1.0, name: nil, tcp_port: 6653) ⇒ OpenVswitch

Returns a new instance of OpenVswitch.



53
54
55
56
57
58
59
# File 'lib/phut/open_vswitch.rb', line 53

def initialize(dpid:, openflow_version: 1.0, name: nil, tcp_port: 6653)
  @dpid = dpid
  @name = name
  @openflow_version = openflow_version
  @tcp_port = tcp_port
  @vsctl = Vsctl.new(name: default_name, bridge: default_bridge)
end

Class Method Details

.allObject



35
36
37
38
39
# File 'lib/phut/open_vswitch.rb', line 35

def self.all
  Vsctl.list_br(bridge_prefix).map do |bridge_attrs|
    new(bridge_attrs)
  end.sort_by(&:dpid)
end

.create(args) ⇒ Object



21
22
23
24
25
# File 'lib/phut/open_vswitch.rb', line 21

def self.create(args)
  found = find_by(name: args[:name]) || find_by(dpid: args[:dpid])
  raise "a Vswitch #{found.inspect} already exists" if found
  new(args).__send__ :start
end

.destroy(name) ⇒ Object



27
28
29
# File 'lib/phut/open_vswitch.rb', line 27

def self.destroy(name)
  find_by!(name: name).destroy
end

.destroy_allObject



31
32
33
# File 'lib/phut/open_vswitch.rb', line 31

def self.destroy_all
  all.each(&:destroy)
end

.dump_flows(name) ⇒ Object



45
46
47
# File 'lib/phut/open_vswitch.rb', line 45

def self.dump_flows(name)
  find_by!(name: name).dump_flows
end

.select(&block) ⇒ Object



41
42
43
# File 'lib/phut/open_vswitch.rb', line 41

def self.select(&block)
  all.select(&block)
end

Instance Method Details

#<=>(other) ⇒ Object

rubocop:enable MethodLength rubocop:enable LineLength



118
119
120
# File 'lib/phut/open_vswitch.rb', line 118

def <=>(other)
  dpid <=> other.dpid
end

#dump_flowsObject

rubocop:disable MethodLength rubocop:disable LineLength



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/phut/open_vswitch.rb', line 101

def dump_flows
  openflow_version = case @vsctl.openflow_version
                     when 1.0
                       :OpenFlow10
                     when 1.3
                       :OpenFlow13
                     else
                       raise "Unknown OpenFlow version: #{@vsctl.openflow_version}"
                     end
  sudo("ovs-ofctl dump-flows #{bridge} -O #{openflow_version}").
    split("\n").inject('') do |memo, each|
    memo + (/^(NXST|OFPST)_FLOW reply/ =~ each ? '' : each.lstrip + "\n")
  end
end

#inspectObject



65
66
67
68
69
70
# File 'lib/phut/open_vswitch.rb', line 65

def inspect
  "#<Vswitch name: \"#{name}\", "\
  "dpid: #{@dpid.to_hex}, "\
  "openflow_version: #{openflow_version}, "\
  "tcp_port: #{tcp_port}>"
end

#nameObject



72
73
74
75
# File 'lib/phut/open_vswitch.rb', line 72

def name
  /^#{bridge_prefix}(\S+)$/ =~ bridge
  Regexp.last_match(1)
end

#run(port) ⇒ Object



94
95
96
97
# File 'lib/phut/open_vswitch.rb', line 94

def run(port)
  raise "An Open vSwitch #{inspect} is already running" if @vsctl.tcp_port
  @vsctl.tcp_port = port
end

#to_sObject



61
62
63
# File 'lib/phut/open_vswitch.rb', line 61

def to_s
  "vswitch (name = #{name}, dpid = #{format('%#x', @dpid)})"
end