Class: Phut::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/phut/configuration.rb

Overview

Parsed DSL data.

Instance Method Summary collapse

Constructor Details

#initialize(logger = NullLogger.new) ⇒ Configuration

Returns a new instance of Configuration.



7
8
9
10
11
# File 'lib/phut/configuration.rb', line 7

def initialize(logger = NullLogger.new)
  @all = {}
  @links = []
  @logger = logger
end

Instance Method Details



79
80
81
# File 'lib/phut/configuration.rb', line 79

def add_link(name_a, name_b)
  @links << VirtualLink.new(name_a, name_b, @logger)
end

#add_netns(name, attrs) ⇒ Object



74
75
76
77
# File 'lib/phut/configuration.rb', line 74

def add_netns(name, attrs)
  check_name_conflict name
  @all[name] = Netns.new(attrs, name, @logger)
end

#add_vhost(name, attrs) ⇒ Object



68
69
70
71
72
# File 'lib/phut/configuration.rb', line 68

def add_vhost(name, attrs)
  check_name_conflict name
  @all[name] =
    Vhost.new(attrs[:ip], attrs[:mac], attrs[:promisc], name, @logger)
end

#add_vswitch(name, attrs) ⇒ Object



62
63
64
65
66
# File 'lib/phut/configuration.rb', line 62

def add_vswitch(name, attrs)
  check_name_conflict name
  @all[name] =
    OpenVswitch.new(attrs[:dpid], attrs[:port_number], name, @logger)
end

#fetch(key) ⇒ Object

rubocop:disable MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/phut/configuration.rb', line 14

def fetch(key)
  case key
  when String
    @all.fetch(key)
  when Array
    @links.each do |each|
      return each if [each.name_a, each.name_b].sort == key.sort
    end
    fail "link #{key.join ' '} not found."
  else
    fail "Invalid key: #{key.inspect}"
  end
end

#netnssObject



44
45
46
# File 'lib/phut/configuration.rb', line 44

def netnss
  @all.values.select { |each| each.is_a? Netns }
end

#runObject



48
49
50
51
52
53
# File 'lib/phut/configuration.rb', line 48

def run
  @links.each(&:run)
  vhosts.each { |each| each.run vhosts }
  netnss.each(&:run)
  vswitches.each(&:run)
end

#stopObject



55
56
57
58
59
60
# File 'lib/phut/configuration.rb', line 55

def stop
  vswitches.each(&:maybe_stop)
  vhosts.each(&:maybe_stop)
  netnss.each(&:maybe_stop)
  @links.each(&:maybe_stop)
end

#update_connectionsObject

rubocop:enable MethodLength



29
30
31
32
33
34
# File 'lib/phut/configuration.rb', line 29

def update_connections
  update_vswitch_ports
  update_vhost_interfaces
  update_netns_interfaces
  self
end

#vhostsObject



40
41
42
# File 'lib/phut/configuration.rb', line 40

def vhosts
  @all.values.select { |each| each.is_a? Vhost }
end

#vswitchesObject



36
37
38
# File 'lib/phut/configuration.rb', line 36

def vswitches
  @all.values.select { |each| each.is_a? OpenVswitch }
end