X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fplc%2Fnetwork.py;h=3e9603b2dd2622e4d9d471130242005d671d66e2;hb=0cf0d31c313a366e3f272f830bdb4f2a7308e11f;hp=73745f6a74c442f02dc1f81d97e4c8df8f7ac353;hpb=552134a790b56cba8230d0eedb0b52909ea20b72;p=sfa.git diff --git a/sfa/plc/network.py b/sfa/plc/network.py index 73745f6a..3e9603b2 100644 --- a/sfa/plc/network.py +++ b/sfa/plc/network.py @@ -30,10 +30,10 @@ class Iface: self.hostname = iface['hostname'] self.primary = iface['is_primary'] - """ - Just print out bwlimit right now - """ def toxml(self, xml): + """ + Just print out bwlimit right now + """ if self.bwlimit: with xml.bw_limit(units="kbps"): xml << str(self.bwlimit / 1000) @@ -72,7 +72,9 @@ class Node: with xml.node(id = self.idtag): with xml.hostname: xml << self.hostname - self.get_primary_iface().toxml(xml) + iface = self.get_primary_iface() + if iface: + iface.toxml(xml) if self.sliver: self.sliver.toxml(xml) @@ -113,6 +115,7 @@ class Slice: self.network = network self.id = slice['slice_id'] self.name = slice['name'] + self.peer_id = slice['peer_id'] self.node_ids = set(slice['node_ids']) self.slice_tag_ids = slice['slice_tag_ids'] @@ -122,78 +125,85 @@ class Slice: def get_multi_tag(self, tagname, node = None): tags = [] for i in self.slice_tag_ids: - tag = self.network.lookupSliceTag(i) - if tag.tagname == tagname: - if not (node and node.id != tag.node_id): - tags.append(tag) + try: + tag = self.network.lookupSliceTag(i) + if tag.tagname == tagname: + if not (node and node.id != tag.node_id): + tags.append(tag) + except InvalidRSpec, e: + # As they're not needed, we ignore some tag types from + # GetSliceTags call. See Slicetag.ignore_tags + pass return tags """ Use with tags that have only one instance """ def get_tag(self, tagname, node = None): - for i in self.slice_tag_ids: - tag = self.network.lookupSliceTag(i) - if tag.tagname == tagname: - if (not node) or (node.id == tag.node_id): - return tag + try: + for i in self.slice_tag_ids: + tag = self.network.lookupSliceTag(i) + if tag.tagname == tagname: + if (not node) or (node.id == tag.node_id): + return tag + except InvalidRSpec, e: + # As they're not needed, we ignore some tag types from + # GetSliceTags call. See Slicetag.ignore_tags + pass return None def get_nodes(self): n = [] for id in self.node_ids: - n.append(self.network.nodes[id]) + if id in self.network.nodes: + n.append(self.network.nodes[id]) return n - + # Add a new slice tag - def add_tag(self, tagname, value, node = None): - record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value} - if node: - record['node_id'] = node.id - else: - record['node_id'] = None - tag = Slicetag(record) + def add_tag(self, tagname, value, node = None, role_id = 40): + 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 self.slice_tag_ids.append(tag.id) - tag.changed = True - tag.updated = True return tag # Update a slice tag if it exists, else add it - def update_tag(self, tagname, value, node = None): + def update_tag(self, tagname, value, node = None, role_id = 40): tag = self.get_tag(tagname, node) - if tag and tag.value == value: - value = "no change" - elif tag: - tag.value = value - tag.changed = True + 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) - tag.updated = True + tag = self.add_tag(tagname, value, node, role_id) + return tag - def update_multi_tag(self, tagname, value, node = None): + def update_multi_tag(self, tagname, value, node = None, role_id = 40): tags = self.get_multi_tag(tagname, node) for tag in tags: if tag and tag.value == value: - value = "no change" break else: - tag = self.add_tag(tagname, value, node) - tag.updated = True + tag = self.add_tag(tagname, value, node, role_id) + return tag def tags_to_xml(self, xml, node = None): tagtypes = self.network.getTagTypes() for tt in tagtypes: - if tt.multi: - tags = self.get_multi_tag(tt.tagname, node) - for tag in tags: - if not tag.deleted: ### Debugging - xml << (tag.tagname, tag.value) - else: - tag = self.get_tag(tt.tagname, node) - if tag: - if not tag.deleted: ### Debugging - xml << (tag.tagname, tag.value) + if tt.in_rspec: + if tt.multi: + tags = self.get_multi_tag(tt.tagname, node) + for tag in tags: + if not tag.was_deleted(): ### Debugging + xml << (tag.tagname, tag.value) + else: + tag = self.get_tag(tt.tagname, node) + if tag: + if not tag.was_deleted(): ### Debugging + xml << (tag.tagname, tag.value) def toxml(self, xml): with xml.sliver_defaults: @@ -202,54 +212,104 @@ class Slice: class Slicetag: newid = -1 - def __init__(self, tag): + filter_fields = ['slice_tag_id','slice_id','tagname','value','node_id','category','min_role_id'] + ignore_tags = ['hmac','ssh_key'] + def __init__(self, tag = None): + if not tag: + return self.id = tag['slice_tag_id'] - if not self.id: - # Make one up for the time being... - self.id = Slicetag.newid - Slicetag.newid -= 1 self.slice_id = tag['slice_id'] self.tagname = tag['tagname'] self.value = tag['value'] self.node_id = tag['node_id'] - self.updated = False - self.changed = False - self.deleted = False - + 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 + def initialize(self, tagname, value, node, network): + tt = network.lookupTagType(tagname) + self.id = Slicetag.newid + Slicetag.newid -=1 + self.slice_id = network.slice.id + self.tagname = tagname + self.value = value + if node: + self.node_id = node.id + 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 + self.status = "change" + else: + self.status = "updated" + # Mark a tag as deleted def delete(self): - self.deleted = True - self.updated = True + self.status = "delete" + + def was_added(self): + return (self.id < 0) + + def was_changed(self): + return (self.status == "change") + + def was_deleted(self): + return (self.status == "delete") + + def was_updated(self): + return (self.status != None) def write(self, api): - if self.changed: - if int(self.id) > 0: - api.plshell.UpdateSliceTag(api.plauth, self.id, self.value) - else: - api.plshell.AddSliceTag(api.plauth, self.slice_id, - self.tagname, self.value, self.node_id) - elif self.deleted and int(self.id) > 0: + 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) class TagType: + ignore_tags = ['hmac','ssh_key'] def __init__(self, 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': + self.in_rspec = True if self.tagname in ['codemux', 'ip_addresses', 'vsys']: self.multi = True - else: - self.multi = False + 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 = "PlanetLab"): + """ + 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, type = "SFA"): self.api = api self.type = type self.sites = self.get_sites(api) @@ -259,8 +319,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')) @@ -276,8 +336,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')) @@ -293,8 +353,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')) @@ -335,7 +395,7 @@ class Network: def lookupTagType(self, name): val = None try: - val = self.tagstypes[name] + val = self.tagtypes[name] except: raise InvalidRSpec("tag %s not found" % name) return val @@ -347,27 +407,42 @@ class Network: return tags def __process_attributes(self, element, node=None): - # Do we need to check caller's role before update??? + """ + Process the elements under or + """ + if element is None: + return + tagtypes = self.getTagTypes() for tt in tagtypes: - if tt.multi: - for e in element.iterfind("./" + tt.tagname): - self.slice.update_multi_tag(tt.tagname, e.text, node) - else: - e = element.find("./" + tt.tagname) - if e is not None: - self.slice.update_tag(tt.tagname, e.text, node) + if tt.in_rspec: + if tt.multi: + for e in element.iterfind("./" + tt.tagname): + self.slice.update_multi_tag(tt.tagname, e.text, node) + else: + e = element.find("./" + tt.tagname) + 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: message = str(sys.exc_info()[1]) raise InvalidRSpec(message) + # Filter out stuff that's not for us + rspec = tree.getroot() + for network in rspec.iterfind("./network"): + if network.get("name") != self.api.hrn: + rspec.remove(network) + for request in rspec.iterfind("./request"): + if request.get("name") != self.api.hrn: + rspec.remove(request) + if schema: # Validate the incoming request against the RelaxNG schema relaxng_doc = etree.parse(schema) @@ -378,10 +453,9 @@ class Network: message = "%s (line %s)" % (error.message, error.line) raise InvalidRSpec(message) - rspec = tree.getroot() self.rspec = rspec - defaults = rspec.find("./network/sliver_defaults") + defaults = rspec.find(".//sliver_defaults") self.__process_attributes(defaults) # Find slivers under node elements @@ -409,10 +483,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") @@ -420,28 +494,30 @@ 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): + # 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: - if not tag.updated: - tag.delete() tag.write(self.api) - """ - 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): - name = "Public_" + self.type if self.slice: - element = xml.network(name=name, slice=self.slice.hrn) + element = xml.network(name=self.api.hrn, slice=self.slice.hrn) else: - element = xml.network(name=name) + element = xml.network(name=self.api.hrn) with element: if self.slice: @@ -452,10 +528,10 @@ class Network: header = '\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}): t = site['site_id'], Site(self, site) @@ -463,54 +539,53 @@ class Network: 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}): 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): 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(api.plauth, {'~tagname':Slicetag.ignore_tags}, Slicetag.filter_fields): 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): - if tag['category'] == 'slice/rspec': - t = tag['tagname'], TagType(tag) - tmp.append(t) + for tag in api.plshell.GetTagTypes(api.plauth, {'~tagname':TagType.ignore_tags}): + 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]) - if slice: + if len(slice): self.slice = Slice(self, slicename, slice[0]) return self.slice else: