class Havox::Topology

Constants

EDGE_REGEX
NODE_REGEX

Attributes

edges[R]
nodes[R]

Public Class Methods

new(file_path) click to toggle source
# File lib/havox/classes/topology.rb, line 8
def initialize(file_path)
  @file_path = file_path
  @nodes = []
  @edges = []
  parse_dot_file
end

Public Instance Methods

host_names() click to toggle source
# File lib/havox/classes/topology.rb, line 15
def host_names
  @nodes.select(&:host?).map(&:name)
end
switch_hosts() click to toggle source
# 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_ips() click to toggle source
# 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

Private Instance Methods

hashed_attributes(raw_attrs) click to toggle source
# File lib/havox/classes/topology.rb, line 66
def hashed_attributes(raw_attrs)
  hash = {}
  raw_attrs.gsub('"', '').split(',').each do |str|
    field, value = str.strip.split(/\s*=\s*/)
    hash[field.to_sym] = value
  end
  hash
end
parse_dot_file() click to toggle source
# File lib/havox/classes/topology.rb, line 41
def parse_dot_file
  File.read(@file_path).each_line do |line|
    parse_node(line)
    parse_edge(line)
  end
end
parse_edge(line) click to toggle source
# File lib/havox/classes/topology.rb, line 56
def parse_edge(line)
  match = line.match(EDGE_REGEX)
  unless match.nil?
    attributes = hashed_attributes(match[:attributes].to_s)
    node_from = @nodes.find { |n| n.name.eql?(match[:from].to_s) }
    node_to = @nodes.find { |n| n.name.eql?(match[:to].to_s) }
    @edges << Havox::Edge.new(node_from, node_to, attributes)
  end
end
parse_node(line) click to toggle source
# File lib/havox/classes/topology.rb, line 48
def parse_node(line)
  match = line.match(NODE_REGEX)
  unless match.nil?
    attributes = hashed_attributes(match[:attributes].to_s)
    @nodes << Havox::Node.new(match[:name].to_s, attributes)
  end
end