Revert create-topo-attributes.py
[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 from topology import links
12
13 """
14 Generate site adjacency map from list of links
15 """
16 def gen_adjacencies(links):
17     adj = {}
18     for (a, b) in links:
19         if a in adj:
20             adj[a].append(b)
21         else:
22             adj[a] = [a, b]
23         if b in adj:
24             adj[b].append(a)
25         else:
26             adj[b] = [b, a]
27     return adj
28
29
30 """
31 Test whether two sites are adjacent to each other in the adjacency graph.
32 """
33 def is_adjacent(adjacencies, s1, s2):
34     set1 = set(adjacencies[s1])
35     set2 = set(adjacencies[s2])
36
37     if s1 in set2 and s2 in set1:
38         return True
39     elif not s1 in set2 and not s2 in set1:
40         return False
41     else:
42         raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
43
44
45 """
46 Check the adjacency graph for discrepancies.
47 """
48 def check_adjacencies(adjacencies):
49     for site in adjacencies:
50         for adj in adjacencies[site]:
51             try:
52                 test = is_adjacent(adjacencies, site, adj)
53             except Exception, e:
54                 print "Error: ", e, " Fix adjacencies!"
55     return
56
57
58 def get_site(nodeid):
59     if nodes[nodeid]:
60         return nodes[nodeid]['site_id']
61     raise Exception("Nodeid %s not found." % nodeid)
62
63
64 def get_ipaddr(nodeid):
65     if nodes[nodeid]:
66         return socket.gethostbyname(nodes[nodeid]['hostname'])
67     raise Exception("Nodeid %s not found." % nodeid)
68
69
70 def get_sitenodes(siteid):
71     if sites[siteid]:
72         return sites[siteid]['node_ids']
73     raise Exception("Siteid %s not found." % siteid)
74
75
76 """
77 Find the IP address assigned to a virtual interface in the topology
78 (for creating /etc/hosts)
79 """
80 def get_virt_ip(myid, nodeid):
81     if myid < nodeid:
82         virtip = "10.%d.%d.2" % (myid, nodeid)
83     else:
84         virtip = "10.%d.%d.3" % (nodeid, myid)
85     return virtip
86
87
88 """
89 Create a dictionary of site records keyed by site ID
90 """
91 def get_sites():
92     tmp = []
93     for site in GetSites():
94         t = site['site_id'], site
95         tmp.append(t)
96     return dict(tmp)
97
98
99 """
100 Create a dictionary of node records keyed by node ID
101 """
102 def get_nodes():
103     tmp = []
104     for node in GetNodes():
105         t = node['node_id'], node
106         tmp.append(t)
107     return dict(tmp)
108
109 adjacencies = gen_adjacencies(links)    
110 check_adjacencies(adjacencies)
111
112 """ Need global topology information """
113 sites = get_sites()
114 nodes = get_nodes()
115
116 for slice in GetSlices():
117     """ Create dictionary of the slice's attributes """
118     attrs ={}
119     topo_attr = {}
120     for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
121         attrs[attribute['name']] = attribute['slice_attribute_id']
122         if attribute['name'] == 'topo_rspec' and attribute['node_id']:
123             topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
124             
125     if 'egre_key' in attrs:
126         #print "Virtual topology for %s:" % slice['name']
127         slicenodes = set(slice['node_ids'])
128         hosts = "127.0.0.1\t\tlocalhost\n"
129         """
130         For each node in the slice, check whether nodes at adjacent sites
131         are also in the slice's node set.  If so, add a virtual link to 
132         the rspec.  
133         """
134         for node in slicenodes:
135             topo = []
136             for adj in adjacencies[get_site(node)]:
137                 for adj_node in get_sitenodes(adj):
138                     if node != adj_node and adj_node in slicenodes:
139                         link = adj_node, get_ipaddr(adj_node), "1Mbit"
140                         topo.append(link)
141                         shortname = nodes[node]['hostname'].replace('.vini-veritas.net', '')
142                         hosts += "%s\t\t%s\n" % (get_virt_ip(node, adj_node),
143                                                   shortname)
144             topo_str = "%s" % topo
145             #print node, topo_str
146             if node in topo_attr:
147                 UpdateSliceAttribute(topo_attr[node], topo_str)
148                 del topo_attr[node]
149             else:
150                 id = slice['slice_id']
151                 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
152
153         #print hosts
154         if 'hosts' in attrs:
155             UpdateSliceAttribute(attrs['hosts'], hosts)
156         else:
157             id = slice['slice_id']
158             AddSliceAttribute(id, 'hosts', hosts)
159     #else:
160         #print "No EGRE key for %s" % slice['name']
161
162     """ Remove old topo_rspec entries """
163     for node in topo_attr:
164         DeleteSliceAttribute(topo_attr[node])
165
166