5 Scan the VINI Central database and create topology "rspec" attributes for
6 slices that have an EGRE key. This script to be run from a cron job.
14 parser = optparse.OptionParser()
15 parser.add_option('-l', '--linkspec', action='store', dest='genlinkspec', default=False, help='Generate linkspec dict.')
16 (options, args) = parser.parse_args()
19 Links in the physical topology, gleaned from looking at the Internet2
20 and NLR topology maps. Link (a, b) connects sites with IDs a and b.
22 links = [(2, 12), # I2 Princeton - New York
23 (4, 5), # NLR Chicago - Houston
24 (4, 6), # NLR Chicago - Atlanta
25 (4, 7), # NLR Chicago - Seattle
26 (4, 9), # NLR Chicago - New York
27 (4, 10), # NLR Chicago - Wash DC
28 (5, 6), # NLR Houston - Atlanta
29 (5, 8), # NLR Houston - Los Angeles
30 (6, 10), # NLR Atlanta - Wash DC
31 (7, 8), # NLR Seattle - Los Angeles
32 (9, 10), # NLR New York - Wash DC
33 (11, 13), # I2 Chicago - Wash DC
34 (11, 15), # I2 Chicago - Atlanta
35 (11, 16), # I2 Chicago - CESNET
36 (11, 17), # I2 Chicago - Kansas City
37 (12, 13), # I2 New York - Wash DC
38 (13, 15), # I2 Wash DC - Atlanta
39 (15, 19), # I2 Atlanta - Houston
40 (17, 19), # I2 Kansas City - Houston
41 (17, 22), # I2 Kansas City - Salt Lake City
42 (19, 20), # I2 Houston - Los Angeles
43 (20, 21), # I2 Los Angeles - Seattle
44 (20, 22), # I2 Los Angeles - Salt Lake City
45 (21, 22)] # I2 Seattle - Salt Lake City
48 def gen_adjacencies(links):
50 Generate site adjacency map from list of links
65 def is_adjacent(adjacencies, s1, s2):
67 Test whether two sites are adjacent to each other in the adjacency graph.
69 set1 = set(adjacencies[s1])
70 set2 = set(adjacencies[s2])
72 if s1 in set2 and s2 in set1:
74 elif not s1 in set2 and not s2 in set1:
77 raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
80 def check_adjacencies(adjacencies):
82 Check the adjacency graph for discrepancies.
84 for site in adjacencies:
85 for adj in adjacencies[site]:
87 test = is_adjacent(adjacencies, site, adj)
89 print "Error: ", e, " Fix adjacencies!"
95 return nodes[nodeid]['site_id']
96 raise Exception("Nodeid %s not found." % nodeid)
99 def get_ipaddr(nodeid):
101 return socket.gethostbyname(nodes[nodeid]['hostname'])
102 raise Exception("Nodeid %s not found." % nodeid)
105 def get_sitenodes(siteid):
107 return sites[siteid]['node_ids']
108 raise Exception("Siteid %s not found." % siteid)
111 def get_virt_ip(myid, nodeid):
113 Find the IP address assigned to a virtual interface in the topology
114 (for creating /etc/hosts)
117 virtip = "10.%d.%d.2" % (myid, nodeid)
119 virtip = "10.%d.%d.3" % (nodeid, myid)
125 Create a dictionary of site records keyed by site ID
128 for site in GetSites():
129 t = site['site_id'], site
136 Create a dictionary of node records keyed by node ID
139 for node in GetNodes():
140 t = node['node_id'], node
147 Return dict with keys from [a] w/ vals from [b]
151 for i in range(0,len(a)):
154 except Exception("Length error.")
158 def ifSpecDict(nodedict):
160 Generate ifspec dict for given node dict.
162 ifspecattrs = ['name',
171 nodenetworks = GetNodeNetworks(nodedict['nodenetwork_ids'])
172 # some nodes have more than 1 public interface.
173 for nodenetwork in nodenetworks:
174 ifspecs.append( toDict(ifspecattrs,
175 [nodenetwork['hostname'],
178 None, '0', '1Gbps', False]))
184 Create dict for physical topology.
186 # list of attributes in the LinkSpec
187 # (https://svn.planet-lab.org/svn/geniwrapper/trunk/rspec/model/planetlab.{ecore,xsd})
188 linkspecattrs = ['type',
193 'endpoint', # <-- ifspec(S)?
201 adjacencies = gen_adjacencies(links)
202 check_adjacencies(adjacencies)
204 """ Need global topology information """
211 for slice in GetSlices():
212 """ Create dictionary of the slice's attributes """
215 for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
216 attrs[attribute['name']] = attribute['slice_attribute_id']
217 if attribute['name'] == 'topo_rspec' and attribute['node_id']:
218 topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
220 if 'egre_key' in attrs:
221 #print "Virtual topology for %s:" % slice['name']
222 slicenodes = set(slice['node_ids'])
223 hosts = "127.0.0.1\t\tlocalhost\n"
225 For each node in the slice, check whether nodes at adjacent sites
226 are also in the slice's node set. If so, add a virtual link to
229 for node in slicenodes:
231 for adj in adjacencies[get_site(node)]:
232 for adj_node in get_sitenodes(adj):
233 if node != adj_node and adj_node in slicenodes:
234 link = adj_node, get_ipaddr(adj_node), "1Mbit"
236 shortname = nodes[node]['hostname'].replace('.vini-veritas.net', '')
237 hosts += "%s\t\t%s\n" % (get_virt_ip(node, adj_node),
239 topo_str = "%s" % topo
240 #print node, topo_str
241 if node in topo_attr:
242 UpdateSliceAttribute(topo_attr[node], topo_str)
245 id = slice['slice_id']
246 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
250 UpdateSliceAttribute(attrs['hosts'], hosts)
252 id = slice['slice_id']
253 AddSliceAttribute(id, 'hosts', hosts)
255 #print "No EGRE key for %s" % slice['name']
257 """ Remove old topo_rspec entries """
258 for node in topo_attr:
259 DeleteSliceAttribute(topo_attr[node])
263 if __name__ == '__main__':
264 if options.genlinkspec: linkspec()