Generate 'hosts' attribute for the slice
[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 Find the IP address assigned to a virtual interface in the topology
72 (for creating /etc/hosts)
73 """
74 def get_virt_ip(myid, nodeid):
75     if myid < nodeid:
76         virtip = "10.%d.%d.2" % (myid, nodeid)
77     else:
78         virtip = "10.%d.%d.3" % (nodeid, myid)
79     return virtip
80
81
82 """
83 Create a dictionary of site records keyed by site ID
84 """
85 def get_sites():
86     tmp = []
87     for site in GetSites():
88         t = site['site_id'], site
89         tmp.append(t)
90     return dict(tmp)
91
92
93 """
94 Create a dictionary of node records keyed by node ID
95 """
96 def get_nodes():
97     tmp = []
98     for node in GetNodes():
99         t = node['node_id'], node
100         tmp.append(t)
101     return dict(tmp)
102
103     
104 check_adjacencies()
105
106 """ Need global topology information """
107 sites = get_sites()
108 nodes = get_nodes()
109
110 for slice in GetSlices():
111     """ Create dictionary of the slice's attributes """
112     attrs ={}
113     topo_attr = {}
114     for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
115         attrs[attribute['name']] = attribute['slice_attribute_id']
116         if attribute['name'] == 'topo_rspec' and attribute['node_id']:
117             topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
118             
119     if 'egre_key' in attrs:
120         #print "Virtual topology for %s:" % slice['name']
121         slicenodes = set(slice['node_ids'])
122         hosts = "127.0.0.1\t\tlocalhost\n"
123         """
124         For each node in the slice, check whether nodes at adjacent sites
125         are also in the slice's node set.  If so, add a virtual link to 
126         the rspec.  
127         """
128         for node in slicenodes:
129             topo = []
130             for adj in adjacencies[get_site(node)]:
131                 for adj_node in get_sitenodes(adj):
132                     if node != adj_node and adj_node in slicenodes:
133                         link = adj_node, get_ipaddr(adj_node), "1Mbit"
134                         topo.append(link)
135                         shortname = nodes[node]['hostname'].replace('.vini-veritas.net', '')
136                         hosts += "%s\t\t%s\n" % (get_virt_ip(node, adj_node),
137                                                   shortname)
138             topo_str = "%s" % topo
139             #print node, topo_str
140             if node in topo_attr:
141                 UpdateSliceAttribute(topo_attr[node], topo_str)
142                 del topo_attr[node]
143             else:
144                 id = slice['slice_id']
145                 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
146
147         #print hosts
148         if 'hosts' in attrs:
149             UpdateSliceAttribute(attrs['hosts'], hosts)
150         else:
151             id = slice['slice_id']
152             AddSliceAttribute(id, 'hosts', hosts)
153     #else:
154         #print "No EGRE key for %s" % slice['name']
155
156     """ Remove old topo_rspec entries """
157     for node in topo_attr:
158         DeleteSliceAttribute(topo_attr[node])
159
160