From 947e9934f4f0dc03c810abd4d8c9b8762580d2b7 Mon Sep 17 00:00:00 2001 From: Faiyaz Ahmed Date: Fri, 6 Mar 2009 22:29:26 +0000 Subject: [PATCH] Generates linkspecs and ifspecs from the topology list. --- create-topo-attributes.py | 206 +++++++++++++++++++++++++------------- 1 file changed, 139 insertions(+), 67 deletions(-) diff --git a/create-topo-attributes.py b/create-topo-attributes.py index 324d09f..03c5903 100755 --- a/create-topo-attributes.py +++ b/create-topo-attributes.py @@ -8,6 +8,12 @@ slices that have an EGRE key. This script to be run from a cron job. import string import socket +import sys +import optparse + +parser = optparse.OptionParser() +parser.add_option('-l', '--linkspec', action='store', dest='genlinkspec', default=False, help='Generate linkspec dict.') +(options, args) = parser.parse_args() """ Links in the physical topology, gleaned from looking at the Internet2 @@ -39,10 +45,10 @@ links = [(2, 12), # I2 Princeton - New York (21, 22)] # I2 Seattle - Salt Lake City -""" -Generate site adjacency map from list of links -""" def gen_adjacencies(links): + """ + Generate site adjacency map from list of links + """ adj = {} for (a, b) in links: if a in adj: @@ -56,10 +62,10 @@ def gen_adjacencies(links): return adj -""" -Test whether two sites are adjacent to each other in the adjacency graph. -""" def is_adjacent(adjacencies, s1, s2): + """ + Test whether two sites are adjacent to each other in the adjacency graph. + """ set1 = set(adjacencies[s1]) set2 = set(adjacencies[s2]) @@ -71,10 +77,10 @@ def is_adjacent(adjacencies, s1, s2): raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2)) -""" -Check the adjacency graph for discrepancies. -""" def check_adjacencies(adjacencies): + """ + Check the adjacency graph for discrepancies. + """ for site in adjacencies: for adj in adjacencies[site]: try: @@ -102,11 +108,11 @@ def get_sitenodes(siteid): raise Exception("Siteid %s not found." % siteid) -""" -Find the IP address assigned to a virtual interface in the topology -(for creating /etc/hosts) -""" def get_virt_ip(myid, nodeid): + """ + Find the IP address assigned to a virtual interface in the topology + (for creating /etc/hosts) + """ if myid < nodeid: virtip = "10.%d.%d.2" % (myid, nodeid) else: @@ -114,10 +120,10 @@ def get_virt_ip(myid, nodeid): return virtip -""" -Create a dictionary of site records keyed by site ID -""" def get_sites(): + """ + Create a dictionary of site records keyed by site ID + """ tmp = [] for site in GetSites(): t = site['site_id'], site @@ -125,71 +131,137 @@ def get_sites(): return dict(tmp) -""" -Create a dictionary of node records keyed by node ID -""" def get_nodes(): + """ + Create a dictionary of node records keyed by node ID + """ tmp = [] for node in GetNodes(): t = node['node_id'], node tmp.append(t) return dict(tmp) + +def toDict(a,b): + """ + Return dict with keys from [a] w/ vals from [b] + """ + if len(a) == len(b): + c = {} + for i in range(0,len(a)): + c[a[i]] = b[i] + else: + except Exception("Length error.") + return c + + +def ifSpecDict(nodedict): + """ + Generate ifspec dict for given node dict. + """ + ifspecattrs = ['name', + 'addr', + 'type', + 'init_params', + 'bw', + 'min_alloc', + 'max_alloc', + 'ip_spoof'] + ifspecs= [] + nodenetworks = GetNodeNetworks(nodedict['nodenetwork_ids']) + # some nodes have more than 1 public interface. + for nodenetwork in nodenetworks: + ifspecs.append( toDict(ifspecattrs, + [nodenetwork['hostname'], + nodenetwork['ip'], + nodenetwork['type'], + None, '0', '1Gbps', False])) + return ifspecs + + +def linkSpecDict(): + """ + Create dict for physical topology. + """ + # list of attributes in the LinkSpec + # (https://svn.planet-lab.org/svn/geniwrapper/trunk/rspec/model/planetlab.{ecore,xsd}) + linkspecattrs = ['type', + 'init_params', + 'bw', + 'min_alloc', + 'max_alloc', + 'endpoint', # <-- ifspec(S)? + 'start_time', + 'duration'] + nodes = get_nodes + for (i, j) in links: + ifSpecDict(nodes[i]) + + adjacencies = gen_adjacencies(links) check_adjacencies(adjacencies) - + """ Need global topology information """ sites = get_sites() nodes = get_nodes() + -for slice in GetSlices(): - """ Create dictionary of the slice's attributes """ - attrs ={} - topo_attr = {} - for attribute in GetSliceAttributes(slice['slice_attribute_ids']): - attrs[attribute['name']] = attribute['slice_attribute_id'] - if attribute['name'] == 'topo_rspec' and attribute['node_id']: - topo_attr[attribute['node_id']] = attribute['slice_attribute_id'] - - if 'egre_key' in attrs: - #print "Virtual topology for %s:" % slice['name'] - slicenodes = set(slice['node_ids']) - hosts = "127.0.0.1\t\tlocalhost\n" - """ - For each node in the slice, check whether nodes at adjacent sites - are also in the slice's node set. If so, add a virtual link to - the rspec. - """ - for node in slicenodes: - topo = [] - for adj in adjacencies[get_site(node)]: - for adj_node in get_sitenodes(adj): - if node != adj_node and adj_node in slicenodes: - link = adj_node, get_ipaddr(adj_node), "1Mbit" - topo.append(link) - shortname = nodes[node]['hostname'].replace('.vini-veritas.net', '') - hosts += "%s\t\t%s\n" % (get_virt_ip(node, adj_node), - shortname) - topo_str = "%s" % topo - #print node, topo_str - if node in topo_attr: - UpdateSliceAttribute(topo_attr[node], topo_str) - del topo_attr[node] +def main(): + + for slice in GetSlices(): + """ Create dictionary of the slice's attributes """ + attrs ={} + topo_attr = {} + for attribute in GetSliceAttributes(slice['slice_attribute_ids']): + attrs[attribute['name']] = attribute['slice_attribute_id'] + if attribute['name'] == 'topo_rspec' and attribute['node_id']: + topo_attr[attribute['node_id']] = attribute['slice_attribute_id'] + + if 'egre_key' in attrs: + #print "Virtual topology for %s:" % slice['name'] + slicenodes = set(slice['node_ids']) + hosts = "127.0.0.1\t\tlocalhost\n" + """ + For each node in the slice, check whether nodes at adjacent sites + are also in the slice's node set. If so, add a virtual link to + the rspec. + """ + for node in slicenodes: + topo = [] + for adj in adjacencies[get_site(node)]: + for adj_node in get_sitenodes(adj): + if node != adj_node and adj_node in slicenodes: + link = adj_node, get_ipaddr(adj_node), "1Mbit" + topo.append(link) + shortname = nodes[node]['hostname'].replace('.vini-veritas.net', '') + hosts += "%s\t\t%s\n" % (get_virt_ip(node, adj_node), + shortname) + topo_str = "%s" % topo + #print node, topo_str + if node in topo_attr: + UpdateSliceAttribute(topo_attr[node], topo_str) + del topo_attr[node] + else: + id = slice['slice_id'] + AddSliceAttribute(id, 'topo_rspec', topo_str, node) + + #print hosts + if 'hosts' in attrs: + UpdateSliceAttribute(attrs['hosts'], hosts) else: id = slice['slice_id'] - AddSliceAttribute(id, 'topo_rspec', topo_str, node) - - #print hosts - if 'hosts' in attrs: - UpdateSliceAttribute(attrs['hosts'], hosts) - else: - id = slice['slice_id'] - AddSliceAttribute(id, 'hosts', hosts) - #else: - #print "No EGRE key for %s" % slice['name'] - - """ Remove old topo_rspec entries """ - for node in topo_attr: - DeleteSliceAttribute(topo_attr[node]) + AddSliceAttribute(id, 'hosts', hosts) + #else: + #print "No EGRE key for %s" % slice['name'] + + """ Remove old topo_rspec entries """ + for node in topo_attr: + DeleteSliceAttribute(topo_attr[node]) + + +if __name__ == '__main__': + if options.genlinkspec: linkspec() + else: main() + -- 2.43.0