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):
18 aNodes = get_sitenodes(a)
19 bNodes = get_sitenodes(b)
26 topo[nodeA][nodeB] = idx
27 topo[nodeB][nodeA] = idx
35 return nodes[nodeid]['site_id']
36 raise Exception("Nodeid %s not found." % nodeid)
39 def get_ipaddr(nodeid):
41 return socket.gethostbyname(nodes[nodeid]['hostname'])
42 raise Exception("Nodeid %s not found." % nodeid)
45 def get_sitenodes(siteid):
47 return sites[siteid]['node_ids']
48 raise Exception("Siteid %s not found." % siteid)
52 Find the IP address assigned to a virtual interface in the topology
53 (for creating /etc/hosts)
55 def get_virt_ip(myid, linkid):
56 return "192.168.%d.%d" % (linkid, myid)
58 def get_virt_net(linkid):
59 return "192.168.%d.0/24" % linkid
62 Create a dictionary of site records keyed by site ID
66 for site in GetSites():
67 t = site['site_id'], site
73 Create a dictionary of node records keyed by node ID
77 for node in GetNodes():
78 t = node['node_id'], node
86 """ Need global topology information """
90 adj_matrix = get_adjacency_matrix(links)
92 for slice in GetSlices():
93 # Create dictionary of the slice's attributes
96 for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
97 attrs[attribute['name']] = attribute['slice_attribute_id']
98 if attribute['name'] == 'topo_rspec' and attribute['node_id']:
99 topo_attr[attribute['node_id']] = attribute['slice_attribute_id']
101 if dryrun and slice['name'] == 'pl_trellis':
102 attrs['egre_key'] = 101
104 if 'egre_key' in attrs:
105 #print "Virtual topology for %s:" % slice['name']
106 slicenodes = set(slice['node_ids'])
107 hosts = "127.0.0.1\t\tlocalhost\n"
109 For each node in the slice, check whether there are any adjacent
110 nodes also in the sliceset using the adjacency matrix.
111 For each pair of adjacent nodes, add to nodes' rspecs.
115 for b in adj_matrix[a]:
119 link_id = adj_matrix[a][b]
120 my_ip = get_virt_ip(a, link_id)
121 remote_ip = get_virt_ip(b, link_id)
122 net = get_virt_net(link_id)
123 link = b, get_ipaddr(b), "1Mbit", my_ip, remote_ip, net
125 shortname = nodes[a]['hostname'].replace('.vini-veritas.net', '')
126 hosts += "%s\t\t%s\n" % (my_ip, shortname)
129 topo_str = "%s" % topo[node]
132 elif node in topo_attr:
133 UpdateSliceAttribute(topo_attr[node], topo_str)
136 id = slice['slice_id']
137 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
141 elif 'hosts' in attrs:
142 UpdateSliceAttribute(attrs['hosts'], hosts)
144 id = slice['slice_id']
145 AddSliceAttribute(id, 'hosts', hosts)
148 print "No EGRE key for %s" % slice['name']
150 # Remove old topo_rspec entries
153 for node in topo_attr:
154 DeleteSliceAttribute(topo_attr[node])