Revert create-topo-attributes.py
authorFaiyaz Ahmed <faiyaza@cs.princeton.edu>
Mon, 9 Mar 2009 20:56:05 +0000 (20:56 +0000)
committerFaiyaz Ahmed <faiyaza@cs.princeton.edu>
Mon, 9 Mar 2009 20:56:05 +0000 (20:56 +0000)
create topology module
initial commit of vinilinkspec.  Creates a linkspec dict for the vini geni aggregate.

create-topo-attributes.py
topology.py [new file with mode: 0755]
vinilinkspec.py [new file with mode: 0755]

index 0f26bbd..05b5aba 100755 (executable)
@@ -1,58 +1,19 @@
-#!/usr/bin/python
-
 # $Id$
 # $URL$
 
 # $Id$
 # $URL$
 
+"""
+Scan the VINI Central database and create topology "rspec" attributes for
+slices that have an EGRE key.  This script to be run from a cron job.
+"""
 
 import string
 import socket
 
 import string
 import socket
-import sys
-import optparse
-from time import time
-
-# Load shell with default configuration
-sys.path.append('/usr/share/plc_api')
-from PLC.Shell import Shell
-plc = Shell(globals())
-
-parser = optparse.OptionParser()
-parser.add_option('-l', '--linkspec', action='store_true', 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
-
+from topology import links
 
 
+"""
+Generate site adjacency map from list of links
+"""
 def gen_adjacencies(links):
 def gen_adjacencies(links):
-    """
-    Generate site adjacency map from list of links
-    """
     adj = {}
     for (a, b) in links:
         if a in adj:
     adj = {}
     for (a, b) in links:
         if a in adj:
@@ -66,10 +27,10 @@ def gen_adjacencies(links):
     return adj
 
 
     return adj
 
 
+"""
+Test whether two sites are adjacent to each other in the adjacency graph.
+"""
 def is_adjacent(adjacencies, s1, s2):
 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])
 
     set1 = set(adjacencies[s1])
     set2 = set(adjacencies[s2])
 
@@ -81,10 +42,10 @@ def is_adjacent(adjacencies, s1, s2):
         raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
 
 
         raise Exception("Adjacency mismatch, sites %d and %d." % (s1, s2))
 
 
+"""
+Check the adjacency graph for discrepancies.
+"""
 def check_adjacencies(adjacencies):
 def check_adjacencies(adjacencies):
-    """
-    Check the adjacency graph for discrepancies.
-    """
     for site in adjacencies:
         for adj in adjacencies[site]:
             try:
     for site in adjacencies:
         for adj in adjacencies[site]:
             try:
@@ -112,11 +73,11 @@ def get_sitenodes(siteid):
     raise Exception("Siteid %s not found." % 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):
 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:
     if myid < nodeid:
         virtip = "10.%d.%d.2" % (myid, nodeid)
     else:
@@ -124,158 +85,82 @@ def get_virt_ip(myid, nodeid):
     return virtip
 
 
     return virtip
 
 
+"""
+Create a dictionary of site records keyed by site ID
+"""
 def get_sites():
 def get_sites():
-    """
-    Create a dictionary of site records keyed by site ID
-    """
-    tmp = {}
+    tmp = []
     for site in GetSites():
     for site in GetSites():
-        tmp[site['site_id']] = site
-    return tmp
+        t = site['site_id'], site
+        tmp.append(t)
+    return dict(tmp)
 
 
 
 
+"""
+Create a dictionary of node records keyed by node ID
+"""
 def get_nodes():
 def get_nodes():
-    """
-    Create a dictionary of node records keyed by node ID
-    """
-    tmp = {}
+    tmp = []
     for node in GetNodes():
     for node in GetNodes():
-        tmp[node['node_id']] = node
-    return tmp
-
-
-def ifSpecDict(nodedict):
-    """
-    Generate ifspec dict for given node dict.
-    """
-    # ifspecattrs = ['name',
-    #            'addr',
-    #            'type', 
-    #            'init_params', 
-    #            'bw', 
-    #            'min_alloc', 
-    #            'max_alloc', 
-    #            'ip_spoof']
-    # Assume only 1 node network per node.
-    nodenetwork = GetNodeNetworks(nodedict['nodenetwork_ids'])[0]
-    ifspecs = {'name': nodenetwork['hostname'],
-               'addr': nodenetwork['ip'],
-               'type': nodenetwork['type'],
-               'init_params': None,
-               'bw': '1Gps',
-               'min_alloc': 0,
-               'max_alloc': '1Gbps',
-               'ip_spoof': 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']
-    # list of linkspecs.  1 per adjacency 
-    linkspecs = []
-    # links maps sites.  Get nodes in site, make linkspecs for each.
-    for (i, j) in links:
-        print "sites = (%s, %s) nodes = %s" %(i,j, GetNodes({'site_id':[i,j]}, ['node_id']))
-        nodeset = GetNodes({'site_id':[i,j]})
-        ifspecs = []
-        for node in nodeset: 
-            ifspecs.append(ifSpecDict(node))
-        linkspecs.append({\
-                'type': 'ipv4',
-                'init_params': None,
-                'bw': '1Gbps',
-                'min_alloc': '0',
-                'bw': '1Gbps',
-                'endpoints': ifspecs,
-                'start_time': int(time()),
-                'duration': '-1'})
-    return linkspecs
-
+        t = node['node_id'], node
+        tmp.append(t)
+    return dict(tmp)
 
 adjacencies = gen_adjacencies(links)    
 check_adjacencies(adjacencies)
 
 adjacencies = gen_adjacencies(links)    
 check_adjacencies(adjacencies)
-    
+
 """ Need global topology information """
 sites = get_sites()
 nodes = get_nodes()
 """ Need global topology information """
 sites = get_sites()
 nodes = get_nodes()
-        
 
 
-def main():
-    """
-    Scan the VINI Central database and create topology "rspec" attributes for
-    slices that have an EGRE key.  This script to be run from a cron job.
-    """
-  
-    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)
+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']
             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, '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])
+
 
 
-if __name__ == '__main__':
-    if options.genlinkspec: 
-        import pprint
-        pp = pprint
-        print "LinkSpecs ="
-        pp.pprint(linkSpecDict())
-    else:
-        print "Main" 
-        main()
diff --git a/topology.py b/topology.py
new file mode 100755 (executable)
index 0000000..b4ba5bf
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/python
+
+# $Id$
+# $URL$
+
+#
+# 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
diff --git a/vinilinkspec.py b/vinilinkspec.py
new file mode 100755 (executable)
index 0000000..5e8911b
--- /dev/null
@@ -0,0 +1,91 @@
+#!/usr/bin/python
+
+# $Id$
+# $URL$
+
+
+import sys
+import optparse
+from topology import links
+from time import time
+
+# Load shell with default configuration
+sys.path.append('/usr/share/plc_api')
+from PLC.Shell import Shell
+plc = Shell(globals())
+
+parser = optparse.OptionParser()
+parser.add_option('-v', '--verbose', action='store_true', dest='DEBUG', default=False, help='Run in verbose mode.')
+(options, args) = parser.parse_args()
+
+
+def ifSpecDict(nodedict):
+    """
+    Generate ifspec dict for given node dict.
+    """
+    # ifspecattrs = ['name',
+    #            'addr',
+    #            'type', 
+    #            'init_params', 
+    #            'bw', 
+    #            'min_alloc', 
+    #            'max_alloc', 
+    #            'ip_spoof']
+    # Assume only 1 node network per node.
+    nodenetwork = GetNodeNetworks(nodedict['nodenetwork_ids'])[0]
+    ifspecs = {'name': nodenetwork['hostname'],
+               'addr': nodenetwork['ip'],
+               'type': nodenetwork['type'],
+               'init_params': None,
+               'bw': '1Gps',
+               'min_alloc': 0,
+               'max_alloc': '1Gbps',
+               'ip_spoof': 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']
+    # list of linkspecs.  1 per adjacency 
+    linkspecs = []
+    # links maps sites.  Get nodes in site, make linkspecs for each.
+    for (i, j) in links:
+        if options.DEBUG:
+            print "sites = (%s, %s) nodes = %s" %(i,j, GetNodes({'site_id':[i,j]}, ['node_id']))
+        nodeset = GetNodes({'site_id':[i,j]})
+        ifspecs = []
+        for node in nodeset: 
+            ifspecs.append(ifSpecDict(node))
+        linkspecs.append({\
+                'type': 'ipv4',
+                'init_params': None,
+                'bw': '1Gbps',
+                'min_alloc': '0',
+                'bw': '1Gbps',
+                'endpoints': ifspecs,
+                'start_time': int(time()),
+                'duration': '-1'})
+    return linkspecs
+
+
+def main():
+    pp = pprint
+    print "LinkSpecs ="
+    pp.pprint(linkSpecDict())
+      
+
+if __name__ == '__main__':
+    import pprint
+    main()