Internal/OpenFlow/OrbitSwitches/scripts: gridmap.rb

File gridmap.rb, 3.0 KB (added by akoshibe, 12 years ago)
Line 
1=begin
2
3gridmap.rb - keeps track of nodes on the grid, mapping [x,y] to switch.
4Functions found here:
5
6Switch
7 ::has_node?(n) - checks if node n is on this switch
8 ::get_port(n) - looks up which port node n is connected to
9 ::get_ip() - return doted decimal IP of switch
10
11 'id' is last octet of switch IP.
12
13Grid
14 ::get_node_id(row, col) - find node ID of node at [row,col]
15 ::switch_of(x,<y>) - find switch that node x or [x,y] is conneced to,
16 returns [switch ID, switchport] pair.
17
18=end
19
20
21#port number a range of 48 values between 1 and 400, to match grid.
22#ID is last octet of IP address 172.16.20.*
23class Switch
24 attr_accessor :upper, :lower, :pmap, :id
25 NUM_PORTS = 48
26 BASE_ADDR = "172.16.20."
27
28 #Take into account that even ports on top, odd on bottom. 48 Nodes
29 #are wired in zigzag fashion, e.g. top ports <-> first 24, bottom
30 #to last 24.
31 def initialize(min, max, id)
32 unless (max - min + 1) > NUM_PORTS
33 @upper = (min..(min + NUM_PORTS/2 - 1))
34 @lower = ((min + NUM_PORTS/2)..max)
35 @pmap = @lower.entries.zip(@upper.entries).flatten
36 else
37 raise "Switch::initialize: tried adding too many ports"
38 end
39 @id = id
40 end
41
42 #check if node ID id is on switch
43 def has_node?(n)
44 return @pmap.include?(n)
45 end
46
47 def get_ip()
48 return BASE_ADDR + (id.to_s)
49 end
50
51 #return true port number (1..48) associated with an ID.
52 def get_port(n)
53 p = @pmap.index(n)
54
55 unless p.nil? then return p + 1 else raise "Switch::get_port: node not found" end
56 end
57end
58
59#map [x,y] to (1..400) and a switch. Hard-coded because I am bad.
60class Grid
61 Rows = 20
62 Cols = 20
63
64 SW_LIST = [ Switch.new(1, 48, 1),
65 Switch.new(49, 96, 2),
66 Switch.new(97, 144, 3),
67 Switch.new(145, 192, 4),
68 Switch.new(193, 240, 5),
69 Switch.new(241, 288, 6),
70 Switch.new(289, 336, 7),
71 Switch.new(337, 384, 8),
72 Switch.new(385, 432, 253) ]
73
74 #node number of first row of grid.
75 FIRST = [400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 60, 40, 20]
76
77 def initialize()
78 end
79
80 #(row - 1) is offset of the ID from the first node of the col-th column.
81 def get_node_id(row, col)
82 return FIRST[col - 1] - (row - 1)
83 end
84
85 #get [switch id, switchport] pair for a node
86 def switch_of(row, col=nil)
87 sw_port = nil
88 sw_id = nil
89 node = nil
90
91 if col.nil? then node = row else node = get_node_id(row, col) end
92
93 SW_LIST.each do |sw|
94 if sw.has_node?(node)
95 sw_port = sw.get_port(node)
96 sw_id = sw.id
97 break
98 end
99 end
100
101 if (sw_port.nil?) or (sw_id.nil?)
102 return nil
103 end
104
105 return [sw_id, sw_port]
106 end
107end
108
109