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.
11 from topology import links
14 def get_adjacency_matrix(links):
17 aNodes = get_sitenodes(a)
18 bNodes = get_sitenodes(b)
25 topo[nodeA][nodeB] = 1
26 topo[nodeB][nodeA] = 1
33 return nodes[nodeid]['site_id']
34 raise Exception("Nodeid %s not found." % nodeid)
37 def get_ipaddr(nodeid):
39 return socket.gethostbyname(nodes[nodeid]['hostname'])
40 raise Exception("Nodeid %s not found." % nodeid)
43 def get_sitenodes(siteid):
45 return sites[siteid]['node_ids']
46 raise Exception("Siteid %s not found." % siteid)
50 Find the IP address assigned to a virtual interface in the topology
51 (for creating /etc/hosts).
52 Each virtual link is on a /30 subnet.
54 def get_linkid(myid, remoteid):
56 linkid = (myid<<7) + remoteid
58 linkid = (remoteid<<7) + myid
61 def get_nodeid(myid, remoteid):
68 def get_virt_ip(myid, remoteid):
69 linkid = get_linkid(myid, remoteid)
70 nodeid = get_nodeid(myid, remoteid)
72 second = ((linkid & 0x3f)<<2) + nodeid
73 return "192.168.%d.%d" % (first, second)
75 def get_virt_net(myid, remoteid):
76 linkid = get_linkid(myid, remoteid)
78 second = (linkid & 0x3f)<<2
79 return "192.168.%d.%d/30" % (first, second)
82 Create a dictionary of site records keyed by site ID
86 for site in GetSites():
87 t = site['site_id'], site
93 Create a dictionary of node records keyed by node ID
97 for node in GetNodes():
98 t = node['node_id'], node
106 """ Need global topology information """
110 adj_matrix = get_adjacency_matrix(links)
112 for slice in GetSlices():
113 # Create dictionary of the slice's attributes
116 for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
117 attrs[attribute['name']] = attribute['slice_attribute_id']
118 if attribute['name'] == 'topo_rspec' and attribute['node_id']:
119 topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
121 if dryrun and slice['name'] == 'pl_trellis':
122 attrs['egre_key'] = 101
124 if 'egre_key' in attrs:
125 #print "Virtual topology for %s:" % slice['name']
126 slicenodes = set(slice['node_ids'])
127 hosts = "127.0.0.1\t\tlocalhost\n"
129 For each node in the slice, check whether there are any adjacent
130 nodes also in the sliceset using the adjacency matrix.
131 For each pair of adjacent nodes, add to nodes' rspecs.
135 for b in adj_matrix[a]:
139 my_ip = get_virt_ip(a, b)
140 remote_ip = get_virt_ip(b, a)
141 net = get_virt_net(a, b)
142 link = b, get_ipaddr(b), "1Mbit", my_ip, remote_ip, net
144 shortname = nodes[a]['hostname'].replace('.vini-veritas.net', '')
145 hosts += "%s\t\t%s\n" % (my_ip, shortname)
148 topo_str = "%s" % topo[node]
151 elif node in topo_attr:
152 UpdateSliceAttribute(topo_attr[node], topo_str)
155 id = slice['slice_id']
156 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
160 elif 'hosts' in attrs:
161 UpdateSliceAttribute(attrs['hosts'], hosts)
163 id = slice['slice_id']
164 AddSliceAttribute(id, 'hosts', hosts)
167 print "No EGRE key for %s" % slice['name']
169 # Remove old topo_rspec entries
172 for node in topo_attr:
173 DeleteSliceAttribute(topo_attr[node])