# $URL$
"""
-Scan the VINI Central database and create topology "rspec" attributes for
+Scan the VINI Central database and create topology "rspec" tags for
slices that have an EGRE key. This script to be run from a cron job.
"""
import string
import socket
-import sys
-import optparse
+from topology import links
-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
-and NLR topology maps. Link (a, b) connects sites with IDs a and b.
-"""
-links = [(2, 12), # I2 Princeton - New York
- (4, 5), # NLR Chicago - Houston
- (4, 6), # NLR Chicago - Atlanta
- (4, 7), # NLR Chicago - Seattle
- (4, 9), # NLR Chicago - New York
- (4, 10), # NLR Chicago - Wash DC
- (5, 6), # NLR Houston - Atlanta
- (5, 8), # NLR Houston - Los Angeles
- (6, 10), # NLR Atlanta - Wash DC
- (7, 8), # NLR Seattle - Los Angeles
- (9, 10), # NLR New York - Wash DC
- (11, 13), # I2 Chicago - Wash DC
- (11, 15), # I2 Chicago - Atlanta
- (11, 16), # I2 Chicago - CESNET
- (11, 17), # I2 Chicago - Kansas City
- (12, 13), # I2 New York - Wash DC
- (13, 15), # I2 Wash DC - Atlanta
- (15, 19), # I2 Atlanta - Houston
- (17, 19), # I2 Kansas City - Houston
- (17, 22), # I2 Kansas City - Salt Lake City
- (19, 20), # I2 Houston - Los Angeles
- (20, 21), # I2 Los Angeles - Seattle
- (20, 22), # I2 Los Angeles - Salt Lake City
- (21, 22)] # I2 Seattle - Salt Lake City
-
-
-def gen_adjacencies(links):
- """
- Generate site adjacency map from list of links
- """
- adj = {}
+def get_adjacency_matrix(links):
+ topo = {}
for (a, b) in links:
- if a in adj:
- adj[a].append(b)
- else:
- adj[a] = [a, b]
- if b in adj:
- adj[b].append(a)
- else:
- adj[b] = [b, a]
- return adj
-
-
-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])
-
- if s1 in set2 and s2 in set1:
- return True
- elif not s1 in set2 and not s2 in set1:
- return False
- else:
- raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
-
-
-def check_adjacencies(adjacencies):
- """
- Check the adjacency graph for discrepancies.
- """
- for site in adjacencies:
- for adj in adjacencies[site]:
- try:
- test = is_adjacent(adjacencies, site, adj)
- except Exception, e:
- print "Error: ", e, " Fix adjacencies!"
- return
-
+ aNodes = get_sitenodes(a)
+ bNodes = get_sitenodes(b)
+ for nodeA in aNodes:
+ for nodeB in bNodes:
+ if nodeA not in topo:
+ topo[nodeA] = {}
+ if nodeB not in topo:
+ topo[nodeB] = {}
+ topo[nodeA][nodeB] = 1
+ topo[nodeB][nodeA] = 1
+ return topo
+
+
def get_site(nodeid):
if nodes[nodeid]:
raise Exception("Siteid %s not found." % siteid)
-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)
+"""
+Find the IP address assigned to a virtual interface in the topology
+(for creating /etc/hosts).
+Each virtual link is on a /30 subnet.
+"""
+def get_linkid(myid, remoteid):
+ if myid < remoteid:
+ linkid = (myid<<7) + remoteid
else:
- virtip = "10.%d.%d.3" % (nodeid, myid)
- return virtip
+ linkid = (remoteid<<7) + myid
+ return linkid
+def get_nodeid(myid, remoteid):
+ if myid < remoteid:
+ nodeid = 1
+ else:
+ nodeid = 2
+ return nodeid
+
+def get_virt_ip(myid, remoteid):
+ linkid = get_linkid(myid, remoteid)
+ nodeid = get_nodeid(myid, remoteid)
+ first = linkid >> 6
+ second = ((linkid & 0x3f)<<2) + nodeid
+ return "192.168.%d.%d" % (first, second)
+
+def get_virt_net(myid, remoteid):
+ linkid = get_linkid(myid, remoteid)
+ first = linkid >> 6
+ second = (linkid & 0x3f)<<2
+ return "192.168.%d.%d/30" % (first, second)
+"""
+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
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
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
+# For debugging
+dryrun = 1
-
-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()
-
-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)
+adj_matrix = get_adjacency_matrix(links)
+
+for slice in GetSlices():
+ # Create dictionary of the slice's tags
+ attrs ={}
+ topo_attr = {}
+ for tag in GetSliceTags(slice['slice_tag_ids']):
+ attrs[tag['tagname']] = tag['slice_tag_id']
+ if tag['tagname'] == 'topo_rspec' and tag['node_id']:
+ topo_attr[tag['node_id']] = tag['slice_tag_id']
+
+ if dryrun and slice['name'] == 'pl_trellis':
+ attrs['egre_key'] = 101
+
+ 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 there are any adjacent
+ nodes also in the sliceset using the adjacency matrix.
+ For each pair of adjacent nodes, add to nodes' rspecs.
+ """
+ topo = {}
+ for a in slicenodes:
+ for b in adj_matrix[a]:
+ if b in slicenodes:
+ if a not in topo:
+ topo[a] = []
+ my_ip = get_virt_ip(a, b)
+ remote_ip = get_virt_ip(b, a)
+ net = get_virt_net(a, b)
+ link = b, get_ipaddr(b), "1Mbit", my_ip, remote_ip, net
+ topo[a].append(link)
+ shortname = nodes[a]['hostname'].replace('.vini-veritas.net', '')
+ hosts += "%s\t\t%s\n" % (my_ip, shortname)
+
+ for node in topo:
+ topo_str = "%s" % topo[node]
+ if dryrun:
+ print node, topo_str
+ elif node in topo_attr:
+ UpdateSliceTag(topo_attr[node], topo_str)
+ del topo_attr[node]
else:
id = slice['slice_id']
- AddSliceAttribute(id, 'hosts', hosts)
- #else:
- #print "No EGRE key for %s" % slice['name']
+ AddSliceTag(id, 'topo_rspec', topo_str, node)
+
+ if dryrun:
+ print hosts
+ elif 'hosts' in attrs:
+ UpdateSliceTag(attrs['hosts'], hosts)
+ else:
+ id = slice['slice_id']
+ AddSliceTag(id, 'hosts', hosts)
+ else:
+ if dryrun:
+ print "No EGRE key for %s" % slice['name']
+
+ # Remove old topo_rspec entries
- """ Remove old topo_rspec entries """
+ if not dryrun:
for node in topo_attr:
- DeleteSliceAttribute(topo_attr[node])
-
-
+ DeleteSliceTag(topo_attr[node])
-if __name__ == '__main__':
- if options.genlinkspec: linkspec()
- else: main()
-