Class: Dot

Inherits:
Object
  • Object
show all
Defined in:
generators/gen_dot_topology.rb

Overview

Generate a Graphviz dot file, showing the switches and the interconnections between them. Does not go further, and run dot to generate a png.

Instance Method Summary (collapse)

Constructor Details

- (Dot) initialize(switches, target_file)

Creates dot (Graphviz) diagram as a png.

Parameters:

  • switches (Hash)

    Hash indexed by name of the switch, pointing to Switch records for the switches we defined in conf/switches.json.

  • target_file (String)

    The basename of the files we write to.



7
8
9
10
11
12
13
14
15
16
17
# File 'generators/gen_dot_topology.rb', line 7

def initialize(switches, target_file)
  @switches = switches
  @links = {}
  File.open(target_file, "w") do |fd|
    put_header(fd) #dot file lead in.
    put_switches(fd) #The switch definitions
    put_rank(fd) #Group the core switches
    put_links(fd) #Output the interswitch link definitions
    fd.puts "}" #Closing } for the dot file.
  end
end

Instance Method Details

- (Object) put_header(fd)

Dot file header

Parameters:

  • fd (File)

    File descriptor for the file we write the header into.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'generators/gen_dot_topology.rb', line 21

def put_header(fd)
  fd.puts <<-EOF
digraph g {
ranksep = 2.0; splines=false;
graph [
];
node [
fontsize = "16"
shape = "ellipse"
];
edge [
];
EOF
end

Output link relationships between the switches

Parameters:

  • fd (File)

    File descriptor for the file we write the link relationship definitions into.



77
78
79
80
# File 'generators/gen_dot_topology.rb', line 77

def put_links(fd)
  i = 0
  @links.each { |k,v| fd.puts "#{v} [ id = #{i} ];"; i = i + 1}
end

- (Object) put_rank(fd)

Ensure the core switches are grouped together

Parameters:

  • fd (File)

    File descriptor for the file we write into.



70
71
72
73
# File 'generators/gen_dot_topology.rb', line 70

def put_rank(fd)
  fd.puts '{ rank=same; "bnt-a2-001-m" ; "bnt-a2-002-m"; }' #External BNTs
  fd.puts '{ rank=same; "bnt-c2-003-m" ; "bnt-c2-004-m"; }' #Private Core
end

- (Object) put_switches(fd)

Dot file body, for each switch

Parameters:

  • fd (File)

    File descriptor for the file we write the swtich definitions into.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'generators/gen_dot_topology.rb', line 38

def put_switches(fd)
  @switches.each do |n, s|
    s.ports.each do |k,p|
      if p.remote_switch_name != nil && @switches[p.remote_switch_name] != nil  && @switches[p.remote_switch_name].ports[p.port_number] == nil
        #puts "#{p.remote_switch_name} #{p.port_number} #{p.remote_port_name}"
        @switches[p.remote_switch_name].ports[p.port_number] = Port.new(p.port_number)
        @switches[p.remote_switch_name].ports[p.port_number].port_name = "#{p.port_number}/#{p.remote_port_name}"
        @switches[p.remote_switch_name].ports[p.port_number].remote_switch_name = s.name
        @switches[p.remote_switch_name].ports[p.port_number].remote_port_name = p.port_name
        @switches[p.remote_switch_name].ports[p.port_number].remote_port_number = p.port_number
        @switches[p.remote_switch_name].ports[p.port_number].remote_mac = s.mac
      end
    end
  end
  @switches.each do |n, s|
    fd.puts "\"#{s.name}\" ["
    fd.print "label = \""
    out = []
    out << "<h0> #{s.name}"
    s.ports.each do |k,p|
      if p.remote_switch_name != nil && @switches[p.remote_switch_name] != nil && p.remote_port_name !~ /MGT[AB]/ && p.port_name !~ /MGT[AB]/
        out << "<p#{p.port_number}> #{p.port_name}"
        @links["#{s.name}:p#{p.port_number}"] = "\"#{s.name}\":p#{p.port_number} -> \"#{p.remote_switch_name}\":p#{p.remote_port_number}"   #if @links["#{p.remote_switch_name}:p#{p.remote_port_number}"] == nil
      end
    end
    fd.puts "#{out.join(' | ')}\""
    fd.puts "shape = \"record\"\n];"
  end
end