Class: Havox::Topology

Inherits:
Object
  • Object
show all
Defined in:
lib/havox/classes/topology.rb

Constant Summary collapse

NODE_REGEX =
/^\s*(?<name>\w+)\s?\[(?<attributes>.*)\];$/i
EDGE_REGEX =
/^\s*(?<from>\w+)\s?->\s?(?<to>\w+)\s?\[(?<attributes>.*)\];$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Topology

Returns a new instance of Topology.



8
9
10
11
12
13
# File 'lib/havox/classes/topology.rb', line 8

def initialize(file_path)
  @file_path = file_path
  @nodes = []
  @edges = []
  parse_dot_file
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



3
4
5
# File 'lib/havox/classes/topology.rb', line 3

def edges
  @edges
end

#nodesObject (readonly)

Returns the value of attribute nodes.



3
4
5
# File 'lib/havox/classes/topology.rb', line 3

def nodes
  @nodes
end

Instance Method Details

#host_namesObject



15
16
17
# File 'lib/havox/classes/topology.rb', line 15

def host_names
  @nodes.select(&:host?).map(&:name)
end

#switch_hostsObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/havox/classes/topology.rb', line 26

def switch_hosts
  exit_edges = @edges.select { |e| e.from.switch? && e.to.host? }
  hash = {}
  exit_edges.each do |e|
    if hash[e.from.name].nil?
      hash[e.from.name] = [e.to.name]
    else
      hash[e.from.name] << e.to.name
    end
  end
  hash
end

#switch_ipsObject



19
20
21
22
23
24
# File 'lib/havox/classes/topology.rb', line 19

def switch_ips
  switches = @nodes.select(&:switch?)
  switch_ip_hash = {}
  switches.each { |s| switch_ip_hash[s.name] = s.attributes[:ip] }
  switch_ip_hash
end