Decided to make this an API call.
[nodemanager-topo.git] / create-topo-attributes.py
index 380a147..c446b94 100755 (executable)
@@ -8,46 +8,27 @@ slices that have an EGRE key.  This script to be run from a cron job.
 
 import string
 import socket
-
-"""
-Map sites to adjacent sites in topology.  Generated manually :-(
-A site is adjacent to itself.
-"""
-adjacencies = {
-    1: [1], 2: [2,12], 3: [3], 4: [4,5,6,7,9,10], 5: [4,5,6,8], 
-    6: [4,5,6,10], 7: [4,7,8], 8: [5,7,8], 9: [4,9,10], 10: [4,6,9,10], 
-    11: [11,13,15,16,17], 12: [2,12,13], 13: [11,12,13,15], 14: [14], 
-    15: [11,13,15,19], 16: [11,16], 17: [11,17,19,22], 18: [18], 
-    19: [15,17,19,20], 20: [19,20,21,22], 21: [20,21,22], 22: [17,20,21,22]
-    }
-
-"""
-Test whether two sites are adjacent to each other in the adjacency graph.
-"""
-def is_adjacent(s1, s2):
-    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))
-
-
-"""
-Check the adjacency graph for discrepancies.
-"""
-def check_adjacencies():
-    for site in adjacencies:
-        for adj in adjacencies[site]:
-            try:
-                test = is_adjacent(site, adj)
-            except Exception, e:
-                print "Error: ", e, " Fix adjacencies!"
-    return
-
+from topology import links
+
+
+def get_adjacency_matrix(links):
+    topo = {}
+    idx = 1
+    for (a, b) in links:
+        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] = idx
+                topo[nodeB][nodeA] = idx
+        idx += 1
+    return topo
+                    
+            
 
 def get_site(nodeid):
     if nodes[nodeid]:
@@ -67,6 +48,16 @@ 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, linkid):
+    return "192.168.%d.%d" % (linkid, myid)
+
+def get_virt_net(linkid):
+    return "192.168.%d.0/24" % linkid
+
 """
 Create a dictionary of site records keyed by site ID
 """
@@ -88,50 +79,78 @@ def get_nodes():
         tmp.append(t)
     return dict(tmp)
 
-    
-check_adjacencies()
+
+# For debugging
+dryrun = 1
 
 """ Need global topology information """
 sites = get_sites()
 nodes = get_nodes()
 
+adj_matrix = get_adjacency_matrix(links)
+
 for slice in GetSlices():
-    """ Create dictionary of the slice's attributes """
-    attrs = []
+    # Create dictionary of the slice's attributes 
+    attrs ={}
     topo_attr = {}
     for attribute in GetSliceAttributes(slice['slice_attribute_ids']):
-        attrs.append(attribute['name'])
+        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 dryrun and slice['name'] == 'pl_trellis':
+        attrs['egre_key'] = 101
+        
     if 'egre_key' in attrs:
-        print "Virtual topology for %s:" % slice['name']
+        #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 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.
         """
-        for node in slicenodes:
-            topo = []
-            site = get_site(node)
-            for adj in adjacencies[site]:
-                for adj_node in get_sitenodes(adj):
-                    if node != adj_node and adj_node in slicenodes:
-                        link = adj_node, get_ipaddr(adj_node)
-                        topo.append(link)
-            topo_str = "%s" % topo
-            print node, topo_str
-            if node in topo_attr:
+        topo = {}
+        for a in slicenodes:
+            for b in adj_matrix[a]:
+                if b in slicenodes:
+                    if a not in topo:
+                        topo[a] = []
+                    link_id = adj_matrix[a][b]
+                    my_ip = get_virt_ip(a, link_id)
+                    remote_ip = get_virt_ip(b, link_id)
+                    net = get_virt_net(link_id)
+                    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:
                 UpdateSliceAttribute(topo_attr[node], topo_str)
                 del topo_attr[node]
             else:
                 id = slice['slice_id']
                 AddSliceAttribute(id, 'topo_rspec', topo_str, node)
 
-        """ Remove old topo_rspec entries """
+        if dryrun:
+            print hosts
+        elif 'hosts' in attrs:
+            UpdateSliceAttribute(attrs['hosts'], hosts)
+        else:
+            id = slice['slice_id']
+            AddSliceAttribute(id, 'hosts', hosts)
+    else:
+        if dryrun:
+            print "No EGRE key for %s" % slice['name']
+
+    # Remove old topo_rspec entries
+    
+    if not dryrun:
         for node in topo_attr:
             DeleteSliceAttribute(topo_attr[node])
 
-    else:
-        print "No EGRE key for %s" % slice['name']
+