Make PLCAPI calls as user in Aggregate Manager
[sfa.git] / sfa / plc / network.py
index 94f6e04..3b16226 100644 (file)
@@ -150,10 +150,8 @@ class Slice:
         return n
 
     # Add a new slice tag   
-    def add_tag(self, tagname, value, node = None, role_id = 40):
+    def add_tag(self, tagname, value, node = None):
         tt = self.network.lookupTagType(tagname)
-        if not tt.permit_update(role_id):
-            raise InvalidRSpec("permission denied to modify '%s' tag" % tagname)
         tag = Slicetag()
         tag.initialize(tagname, value, node, self.network)
         self.network.tags[tag.id] = tag
@@ -161,23 +159,21 @@ class Slice:
         return tag
     
     # Update a slice tag if it exists, else add it             
-    def update_tag(self, tagname, value, node = None, role_id = 40):
+    def update_tag(self, tagname, value, node = None):
         tag = self.get_tag(tagname, node)
         if tag:
-            if not tag.permit_update(role_id, value):
-                raise InvalidRSpec("permission denied to modify '%s' tag" % tagname)
             tag.change(value)
         else:
-            tag = self.add_tag(tagname, value, node, role_id)
+            tag = self.add_tag(tagname, value, node)
         return tag
             
-    def update_multi_tag(self, tagname, value, node = None, role_id = 40):
+    def update_multi_tag(self, tagname, value, node = None):
         tags = self.get_multi_tag(tagname, node)
         for tag in tags:
             if tag and tag.value == value:
                 break
         else:
-            tag = self.add_tag(tagname, value, node, role_id)
+            tag = self.add_tag(tagname, value, node)
         return tag
             
     def tags_to_xml(self, xml, node = None):
@@ -211,7 +207,6 @@ class Slicetag:
         self.value = tag['value']
         self.node_id = tag['node_id']
         self.category = tag['category']
-        self.min_role_id = tag['min_role_id']
         self.status = None
 
     # Create a new slicetag that will be written to the DB later
@@ -227,16 +222,8 @@ class Slicetag:
         else:
             self.node_id = None
         self.category = tt.category
-        self.min_role_id = tt.min_role_id
         self.status = "new"
 
-    def permit_update(self, role_id, value = None):
-        if value and self.value == value:
-            return True
-        if role_id > self.min_role_id:
-            return False
-        return True
-        
     def change(self, value):
         if self.value != value:
             self.value = value
@@ -260,14 +247,18 @@ class Slicetag:
     def was_updated(self):
         return (self.status != None)
     
-    def write(self, api):
-        if self.was_added():
-            api.plshell.AddSliceTag(api.plauth, self.slice_id, 
-                                    self.tagname, self.value, self.node_id)
-        elif self.was_changed():
-            api.plshell.UpdateSliceTag(api.plauth, self.id, self.value)
-        elif self.was_deleted():
-            api.plshell.DeleteSliceTag(api.plauth, self.id)
+    def write(self, api, user_plauth):
+        try:
+            if self.was_added():
+                api.plshell.AddSliceTag(user_plauth, self.slice_id, 
+                                        self.tagname, self.value, self.node_id)
+            elif self.was_changed():
+                api.plshell.UpdateSliceTag(user_plauth, self.id, self.value)
+            elif self.was_deleted():
+                api.plshell.DeleteSliceTag(user_plauth, self.id)
+        except:
+            raise InvalidRSpec("user cannot modify '%s' tag" % self.tagname)
+
 
 
 class TagType:
@@ -275,7 +266,6 @@ class TagType:
         self.id = tagtype['tag_type_id']
         self.category = tagtype['category']
         self.tagname = tagtype['tagname']
-        self.min_role_id = tagtype['min_role_id']
         self.multi = False
         self.in_rspec = False
         if self.category == 'slice/rspec':
@@ -283,21 +273,17 @@ class TagType:
         if self.tagname in ['codemux', 'ip_addresses', 'vsys']:
             self.multi = True
 
-    def permit_update(self, role_id):
-        if role_id > self.min_role_id:
-            return False
-        return True
-        
 
-"""
-A Network is a compound object consisting of:
-* a dictionary mapping site IDs to Site objects
-* a dictionary mapping node IDs to Node objects
-* a dictionary mapping interface IDs to Iface objects
-"""
 class Network:
-    def __init__(self, api, type = "SFA"):
+    """
+    A Network is a compound object consisting of:
+    * a dictionary mapping site IDs to Site objects
+    * a dictionary mapping node IDs to Node objects
+    * a dictionary mapping interface IDs to Iface objects
+    """
+    def __init__(self, api, user_plauth, type = "SFA"):
         self.api = api
+        self.user_plauth = user_plauth
         self.type = type
         self.sites = self.get_sites(api)
         self.nodes = self.get_nodes(api)
@@ -306,8 +292,8 @@ class Network:
         self.tagtypes = self.get_tag_types(api)
         self.slice = None
     
-    """ Lookup site based on id or idtag value """
     def lookupSite(self, id):
+        """ Lookup site based on id or idtag value """
         val = None
         if isinstance(id, basestring):
             id = int(id.lstrip('s'))
@@ -323,8 +309,8 @@ class Network:
             sites.append(self.sites[s])
         return sites
         
-    """ Lookup node based on id or idtag value """
     def lookupNode(self, id):
+        """ Lookup node based on id or idtag value """
         val = None
         if isinstance(id, basestring):
             id = int(id.lstrip('n'))
@@ -340,8 +326,8 @@ class Network:
             nodes.append(self.nodes[n])
         return nodes
     
-    """ Lookup iface based on id or idtag value """
     def lookupIface(self, id):
+        """ Lookup iface based on id or idtag value """
         val = None
         if isinstance(id, basestring):
             id = int(id.lstrip('i'))
@@ -393,13 +379,13 @@ class Network:
             tags.append(self.tagtypes[t])
         return tags
     
-    """
-    Process the elements under <sliver_defaults> or <sliver>
-    """
     def __process_attributes(self, element, node=None):
+        """
+        Process the elements under <sliver_defaults> or <sliver>
+        """
         if element is None:
             return 
-
+        
         tagtypes = self.getTagTypes()
         for tt in tagtypes:
             if tt.in_rspec:
@@ -411,10 +397,10 @@ class Network:
                     if e is not None:
                         self.slice.update_tag(tt.tagname, e.text, node)
 
-    """
-    Annotate the objects in the Network with information from the RSpec
-    """
     def addRSpec(self, xml, schema=None):
+        """
+        Annotate the objects in the Network with information from the RSpec
+        """
         try:
             tree = etree.parse(StringIO(xml))
         except etree.XMLSyntaxError:
@@ -470,10 +456,10 @@ class Network:
 
         return
 
-    """
-    Annotate the objects in the Network with information from the slice
-    """
     def addSlice(self):
+        """
+        Annotate the objects in the Network with information from the slice
+        """
         slice = self.slice
         if not slice:
             raise InvalidRSpec("no slice associated with network")
@@ -481,24 +467,24 @@ class Network:
         for node in slice.get_nodes():
             node.add_sliver()
 
-    """
-    Write any slice tags that have been added or modified back to the DB
-    """
     def updateSliceTags(self):
+        """
+        Write any slice tags that have been added or modified back to the DB
+        """
         for tag in self.getSliceTags():
-            if tag.category == 'slice/rspec' and not tag.was_updated() and tag.permit_update(None, 40):
+            if tag.category == 'slice/rspec' and not tag.was_updated():
                 # The user wants to delete this tag
                 tag.delete()
 
         # Update slice tags in database
         for tag in self.getSliceTags():
             if tag.slice_id == self.slice.id:
-                tag.write(self.api) 
+                tag.write(self.api, self.user_plauth
 
-    """
-    Produce XML directly from the topology specification.
-    """
     def toxml(self):
+        """
+        Produce XML directly from the topology specification.
+        """
         xml = XMLBuilder(format = True, tab_step = "  ")
         with xml.RSpec(type=self.type):
             if self.slice:
@@ -515,63 +501,64 @@ class Network:
         header = '<?xml version="1.0"?>\n'
         return header + str(xml)
 
-    """
-    Create a dictionary of site objects keyed by site ID
-    """
     def get_sites(self, api):
+        """
+        Create a dictionary of site objects keyed by site ID
+        """
         tmp = []
-        for site in api.plshell.GetSites(api.plauth, {'peer_id': None}):
+        for site in api.plshell.GetSites(self.user_plauth, {'peer_id': None}):
             t = site['site_id'], Site(self, site)
             tmp.append(t)
+            
         return dict(tmp)
 
 
-    """
-    Create a dictionary of node objects keyed by node ID
-    """
     def get_nodes(self, api):
+        """
+        Create a dictionary of node objects keyed by node ID
+        """
         tmp = []
-        for node in api.plshell.GetNodes(api.plauth, {'peer_id': None}):
+        for node in api.plshell.GetNodes(self.user_plauth, {'peer_id': None}):
             t = node['node_id'], Node(self, node)
             tmp.append(t)
         return dict(tmp)
 
-    """
-    Create a dictionary of node objects keyed by node ID
-    """
     def get_ifaces(self, api):
+        """
+        Create a dictionary of node objects keyed by node ID
+        """
         tmp = []
-        for iface in api.plshell.GetInterfaces(api.plauth):
+        for iface in api.plshell.GetInterfaces(self.user_plauth):
             t = iface['interface_id'], Iface(self, iface)
             tmp.append(t)
         return dict(tmp)
 
-    """
-    Create a dictionary of slicetag objects keyed by slice tag ID
-    """
     def get_slice_tags(self, api):
+        """
+        Create a dictionary of slicetag objects keyed by slice tag ID
+        """
         tmp = []
-        for tag in api.plshell.GetSliceTags(api.plauth):
+        for tag in api.plshell.GetSliceTags(self.user_plauth):
             t = tag['slice_tag_id'], Slicetag(tag)
             tmp.append(t)
         return dict(tmp)
     
-    """
-    Create a list of tagtype obects keyed by tag name
-    """
     def get_tag_types(self, api):
+        """
+        Create a list of tagtype obects keyed by tag name
+        """
         tmp = []
-        for tag in api.plshell.GetTagTypes(api.plauth):
+        for tag in api.plshell.GetTagTypes(self.user_plauth):
             t = tag['tagname'], TagType(tag)
             tmp.append(t)
         return dict(tmp)
     
-    """
-    Return a Slice object for a single slice
-    """
     def get_slice(self, api, hrn):
+        """
+        Return a Slice object for a single slice
+        """
         slicename = hrn_to_pl_slicename(hrn)
-        slice = api.plshell.GetSlices(api.plauth, [slicename])
+        slice = api.plshell.GetSlices(self.user_plauth, [slicename])
         if len(slice):
             self.slice = Slice(self, slicename, slice[0])
             return self.slice