Assign EGRE keys and vsys attributes
authorAndy Bavier <acb@cs.princeton.edu>
Tue, 26 May 2009 21:04:46 +0000 (21:04 +0000)
committerAndy Bavier <acb@cs.princeton.edu>
Tue, 26 May 2009 21:04:46 +0000 (21:04 +0000)
create-topo-attributes.py

index 02bed13..91ae976 100755 (executable)
@@ -94,33 +94,75 @@ class Slice:
         self.name = slice['name']
         self.node_ids = set(slice['node_ids'])
         self.slice_tag_ids = slice['slice_tag_ids']
-        self.attrs = {}
     
-    def get_tag(self, tagname, slicetags):
-        if tagname not in self.attrs:
-            for i in self.slice_tag_ids:
-                if slicetags[i].tagname == tagname:
-                    self.attrs[tagname] = slicetags[i]
-                    break
-            else:
-                self.attrs[tagname] = None
-        return self.attrs[tagname]
+    def get_tag(self, tagname, slicetags, node = None):
+        for i in self.slice_tag_ids:
+            tag = slicetags[i]
+            if tag.tagname == tagname:
+                if (not node) or (node.id == tag.node_id):
+                    return tag
+        else:
+            return None
         
     def get_nodes(self, nodes):
         n = []
         for id in self.node_ids:
             n.append(nodes[id])
         return n
-    
-    def get_rspec(self, slicetags, node):
-        for i in self.slice_tag_ids:
-            tag = slicetags[i]
-            if tag.tagname == 'topo_rspec' and node.id == tag.node_id:
-                return tag
+             
+             
+    def add_tag(self, tagname, value, slicetags, dryrun, node = None):
+        if node:
+            id = AddSliceTag(self.id, tagname, value, node.id)
         else:
-            return None
+            id = AddSliceTag(self.id, tagname, value)
+            
+        record = GetSliceTags([id])[0]
+        tag = Slicetag(record)
+        slicetags[id] = tag
+        self.slice_tag_ids.append(id)
+        
+        return tag
                 
+    def update_tag(self, tagname, value, slicetags, dryrun, node = None):
+        tag = self.get_tag(tagname, slicetags, node)
+        
+        if tag and tag.value == value:
+            value = "no change"
+        elif not dryrun:
+            if tag:
+                UpdateSliceTag(tag.id, value)
+            else:
+                tag = self.add_tag(tagname, value, slicetags, dryrun, node)
+            
+        if tag: 
+            tag.updated = 1
+            id = tag.id
+        else:
+            id = 'new'
+            
+        if dryrun:
+            if node:
+                print "[%s] %s: %s (%s)" % (id, tagname, value, node.shortname)
+            else:
+                print "[%s] %s: %s" % (id, tagname, value)
 
+    """
+    Update the vsys/setup-link and vsys/setup-nat slice tags.
+    """
+    def add_vsys_tags(self, slicetags, dryrun):
+        link = nat = False
+        for i in self.slice_tag_ids:
+            tag = slicetags[i]
+            if tag.tagname == 'vsys':
+                if tag.value == 'setup-link':
+                    link = True
+                elif tag.value == 'setup-nat':
+                    nat = True
+        if not link:
+            self.add_tag('vsys', 'setup-link', slicetags, dryrun)
+        if not nat:
+            self.add_tag('vsys', 'setup-nat', slicetags, dryrun)
 
 class Slicetag:
     def __init__(self, tag):
@@ -175,7 +217,25 @@ def get_slice_tags():
         tmp.append(t)
     return dict(tmp)
     
-    
+"""
+Find a free EGRE key
+"""
+def free_egre_key(slicetags):
+    for i in slicetags:
+        used = set()
+        tag = slicetags[i]
+        if tag.tagname == 'egre_key':
+            used.add(int(tag.value))
+                
+    for i in range(1, 256):
+        if i not in used:
+            key = i
+            break
+    else:
+        raise KeyError("No more EGRE keys available")
+        
+    return "%s" % key
+   
 # For debugging
 dryrun = 0
 
@@ -203,11 +263,20 @@ for i in slices:
         If no 'netns' attribute, add netns/1
         For 'vsys', add vsys/setup-link and vsys/setup-nat
         """
+        if not slice.get_tag('egre_key', slicetags):
+            key = free_egre_key(slicetags)
+            slice.update_tag('egre_key', key, slicetags, dryrun)
+            
+    if topo_type == 'vsys' and slice.get_tag('egre_key', slicetags):
+        slice.add_vsys_tags(slicetags, dryrun)
         
-    if slice.get_tag('egre_key', slicetags) and topo_type == 'iias':
+    if topo_type == 'iias' and slice.get_tag('egre_key', slicetags):
         if dryrun:
             print "Virtual topology for %s:" % slice.name
             
+        if not slice.get_tag('netns', slicetags):
+            slice.update_tag('netns', '1', slicetags, dryrun)
+            
         hosts = "127.0.0.1\t\tlocalhost\n"
         """
         For each node in the slice, check whether the slice is running on any
@@ -219,42 +288,11 @@ for i in slices:
             for adj in adj_nodes:
                 node.add_rspec(adj)
                 hosts += "%s\t\t%s\n" % (node.get_virt_ip(adj), node.shortname)
-                
-            old_rspec = slice.get_rspec(slicetags, node)
             if node.rspecs:
                 topo_str = "%s" % node.rspecs
-
-                if old_rspec:
-                    old_rspec.updated = 1
-                    id = old_rspec.id
-                    
-                if old_rspec and old_rspec.value == topo_str:
-                        topo_str = "no change"
-                elif not dryrun:
-                    if old_rspec:
-                        UpdateSliceTag(old_rspec.id, topo_str)
-                    else:
-                        AddSliceTag(slice.id, 'topo_rspec', topo_str, node.id)
-                        
-                if dryrun:
-                    if not id:
-                        id = 'new'
-                    print "[%s] rspec for %s: %s" % (id, node.shortname, topo_str)
+                slice.update_tag('topo_rspec', topo_str, slicetags, dryrun, node)
                     
-        hosttag = slice.get_tag('hosts', slicetags)
-        if hosttag:
-            hosttag.updated = 1
-        if hosttag and hosts == hosttag.value:
-            hosts = "/etc/hosts: no change"
-        elif not dryrun:
-            if hosttag:
-                UpdateSliceTag(hosttag.id, hosts)
-            else:
-                AddSliceTag(slice.id, 'hosts', hosts)
-            
-        if dryrun:
-            print hosts
-
+        slice.update_tag('hosts', hosts, slicetags, dryrun)
     else:
         if dryrun:
             print "Slice %s not using IIAS" % slice.name
@@ -265,8 +303,12 @@ for i in slicetags:
     if (tag.tagname == 'topo_rspec' or tag.tagname == 'hosts') and not tag.updated:
         if dryrun:
             slice = slices[tag.slice_id].name
-            node = nodes[tag.node_id].hostname
-            print "Deleting topo_rspec tag %s (%s, %s)" % (tag.id, slice, node)
+            if tag.node_id:
+                node = nodes[tag.node_id].hostname
+                print "Deleting tag %s (%s, %s)" % (tag.id, slice, node)
+            else:
+                print "Deleting tag %s (%s)" % (tag.id, slice)
+
         else:
             DeleteSliceTag(tag.id)