380a147a1b98be9edd56b365cd1d38a5f3e43027
[nodemanager-topo.git] / create-topo-attributes.py
1 # $Id$
2 # $URL$
3
4 """
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.
7 """
8
9 import string
10 import socket
11
12 """
13 Map sites to adjacent sites in topology.  Generated manually :-(
14 A site is adjacent to itself.
15 """
16 adjacencies = {
17     1: [1], 2: [2,12], 3: [3], 4: [4,5,6,7,9,10], 5: [4,5,6,8], 
18     6: [4,5,6,10], 7: [4,7,8], 8: [5,7,8], 9: [4,9,10], 10: [4,6,9,10], 
19     11: [11,13,15,16,17], 12: [2,12,13], 13: [11,12,13,15], 14: [14], 
20     15: [11,13,15,19], 16: [11,16], 17: [11,17,19,22], 18: [18], 
21     19: [15,17,19,20], 20: [19,20,21,22], 21: [20,21,22], 22: [17,20,21,22]
22     }
23
24 """
25 Test whether two sites are adjacent to each other in the adjacency graph.
26 """
27 def is_adjacent(s1, s2):
28     set1 = set(adjacencies[s1])
29     set2 = set(adjacencies[s2])
30
31     if s1 in set2 and s2 in set1:
32         return True
33     elif not s1 in set2 and not s2 in set1:
34         return False
35     else:
36         raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
37
38
39 """
40 Check the adjacency graph for discrepancies.
41 """
42 def check_adjacencies():
43     for site in adjacencies:
44         for adj in adjacencies[site]:
45             try:
46                 test = is_adjacent(site, adj)
47             except Exception, e:
48                 print "Error: ", e, " Fix adjacencies!"
49     return
50
51
52 def get_site(nodeid):
53     if nodes[nodeid]:
54         return nodes[nodeid]['site_id']
55     raise Exception("Nodeid %s not found." % nodeid)
56
57
58 def get_ipaddr(nodeid):
59     if nodes[nodeid]:
60         return socket.gethostbyname(nodes[nodeid]['hostname'])
61     raise Exception("Nodeid %s not found." % nodeid)
62
63
64 def get_sitenodes(siteid):
65     if sites[siteid]:
66         return sites[siteid]['node_ids']
67     raise Exception("Siteid %s not found." % siteid)
68
69
70 """
71 Create a dictionary of site records keyed by site ID
72 """
73 def get_sites():
74     tmp = []
75     for site in GetSites():
76         t = site['site_id'], site
77         tmp.append(t)
78     return dict(tmp)
79
80
81 """
82 Create a dictionary of node records keyed by node ID
83 """
84 def get_nodes():
85     tmp = []
86     for node in GetNodes():
87         t = node['node_id'], node
88         tmp.append(t)
89     return dict(tmp)
90
91     
92 check_adjacencies()
93
94 """ Need global topology information """
95 sites = get_sites()
96 nodes = get_nodes()
97
98 for slice in GetSlices():
99     """ Create dictionary of the slice's attributes """
100     attrs = []
101     topo_attr = {}
102     for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
103         attrs.append(attribute['name'])
104         if attribute['name'] == 'topo_rspec' and attribute['node_id']:
105             topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
106             
107     if 'egre_key' in attrs:
108         print "Virtual topology for %s:" % slice['name']
109         slicenodes = set(slice['node_ids'])
110         """
111         For each node in the slice, check whether nodes at adjacent sites
112         are also in the slice's node set.  If so, add a virtual link to 
113         the rspec.  
114         """
115         for node in slicenodes:
116             topo = []
117             site = get_site(node)
118             for adj in adjacencies[site]:
119                 for adj_node in get_sitenodes(adj):
120                     if node != adj_node and adj_node in slicenodes:
121                         link = adj_node, get_ipaddr(adj_node)
122                         topo.append(link)
123             topo_str = "%s" % topo
124             print node, topo_str
125             if node in topo_attr:
126                 UpdateSliceAttribute(topo_attr[node], topo_str)
127                 del topo_attr[node]
128             else:
129                 id = slice['slice_id']
130                 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
131
132         """ Remove old topo_rspec entries """
133         for node in topo_attr:
134             DeleteSliceAttribute(topo_attr[node])
135
136     else:
137         print "No EGRE key for %s" % slice['name']