1 from __future__ import with_statement
4 from sfa.util.namespace import *
5 from sfa.util.faults import *
6 from xmlbuilder import XMLBuilder
9 from StringIO import StringIO
13 def __init__(self, node):
15 self.network = node.network
16 self.slice = node.network.slice
20 self.slice.tags_to_xml(xml, self.node)
24 def __init__(self, network, iface):
25 self.network = network
26 self.id = iface['interface_id']
27 self.idtag = "i%s" % self.id
28 self.ipv4 = iface['ip']
29 self.bwlimit = iface['bwlimit']
30 self.hostname = iface['hostname']
31 self.primary = iface['is_primary']
34 Just print out bwlimit right now
38 with xml.bw_limit(units="kbps"):
39 xml << str(self.bwlimit / 1000)
43 def __init__(self, network, node):
44 self.network = network
45 self.id = node['node_id']
46 self.idtag = "n%s" % self.id
47 self.hostname = node['hostname']
48 self.site_id = node['site_id']
49 self.iface_ids = node['interface_ids']
51 self.whitelist = node['slice_ids_whitelist']
53 def get_primary_iface(self):
54 for id in self.iface_ids:
55 iface = self.network.lookupIface(id)
61 return self.network.lookupSite(self.site_id)
64 self.sliver = Sliver(self)
67 slice = self.network.slice
68 if self.whitelist and not self.sliver:
69 if not slice or slice.id not in self.whitelist:
72 with xml.node(id = self.idtag):
75 iface = self.get_primary_iface()
79 self.sliver.toxml(xml)
83 def __init__(self, network, site):
84 self.network = network
85 self.id = site['site_id']
86 self.idtag = "s%s" % self.id
87 self.node_ids = site['node_ids']
89 self.name = site['abbreviated_name']
90 self.tag = site['login_base']
91 self.public = site['is_public']
92 self.enabled = site['enabled']
94 self.whitelist = False
96 def get_sitenodes(self):
98 for i in self.node_ids:
99 n.append(self.network.lookupNode(i))
102 def toxml(self, xml):
103 if not (self.public and self.enabled and self.node_ids):
106 with xml.site(id = self.idtag):
109 for node in self.get_sitenodes():
114 def __init__(self, network, hrn, slice):
116 self.network = network
117 self.id = slice['slice_id']
118 self.name = slice['name']
119 self.peer_id = slice['peer_id']
120 self.node_ids = set(slice['node_ids'])
121 self.slice_tag_ids = slice['slice_tag_ids']
124 Use with tags that can have more than one instance
126 def get_multi_tag(self, tagname, node = None):
128 for i in self.slice_tag_ids:
129 tag = self.network.lookupSliceTag(i)
130 if tag.tagname == tagname:
131 if not (node and node.id != tag.node_id):
136 Use with tags that have only one instance
138 def get_tag(self, tagname, node = None):
139 for i in self.slice_tag_ids:
140 tag = self.network.lookupSliceTag(i)
141 if tag.tagname == tagname:
142 if (not node) or (node.id == tag.node_id):
148 for id in self.node_ids:
149 if id in self.network.nodes:
150 n.append(self.network.nodes[id])
153 # Add a new slice tag
154 def add_tag(self, tagname, value, node = None, role_id = 40):
155 tt = self.network.lookupTagType(tagname)
156 if not tt.permit_update(role_id):
157 raise InvalidRSpec("permission denied to modify '%s' tag" % tagname)
159 tag.initialize(tagname, value, node, self.network)
160 self.network.tags[tag.id] = tag
161 self.slice_tag_ids.append(tag.id)
164 # Update a slice tag if it exists, else add it
165 def update_tag(self, tagname, value, node = None, role_id = 40):
166 tag = self.get_tag(tagname, node)
168 if not tag.permit_update(role_id, value):
169 raise InvalidRSpec("permission denied to modify '%s' tag" % tagname)
172 tag = self.add_tag(tagname, value, node, role_id)
175 def update_multi_tag(self, tagname, value, node = None, role_id = 40):
176 tags = self.get_multi_tag(tagname, node)
178 if tag and tag.value == value:
181 tag = self.add_tag(tagname, value, node, role_id)
184 def tags_to_xml(self, xml, node = None):
185 tagtypes = self.network.getTagTypes()
189 tags = self.get_multi_tag(tt.tagname, node)
191 if not tag.was_deleted(): ### Debugging
192 xml << (tag.tagname, tag.value)
194 tag = self.get_tag(tt.tagname, node)
196 if not tag.was_deleted(): ### Debugging
197 xml << (tag.tagname, tag.value)
199 def toxml(self, xml):
200 with xml.sliver_defaults:
201 self.tags_to_xml(xml)
206 def __init__(self, tag = None):
209 self.id = tag['slice_tag_id']
210 self.slice_id = tag['slice_id']
211 self.tagname = tag['tagname']
212 self.value = tag['value']
213 self.node_id = tag['node_id']
214 self.category = tag['category']
215 self.min_role_id = tag['min_role_id']
218 # Create a new slicetag that will be written to the DB later
219 def initialize(self, tagname, value, node, network):
220 tt = network.lookupTagType(tagname)
221 self.id = Slicetag.newid
223 self.slice_id = network.slice.id
224 self.tagname = tagname
227 self.node_id = node.id
230 self.category = tt.category
231 self.min_role_id = tt.min_role_id
234 def permit_update(self, role_id, value = None):
235 if value and self.value == value:
237 if role_id > self.min_role_id:
241 def change(self, value):
242 if self.value != value:
244 self.status = "change"
246 self.status = "updated"
248 # Mark a tag as deleted
250 self.status = "delete"
255 def was_changed(self):
256 return (self.status == "change")
258 def was_deleted(self):
259 return (self.status == "delete")
261 def was_updated(self):
262 return (self.status != None)
264 def write(self, api):
266 api.plshell.AddSliceTag(api.plauth, self.slice_id,
267 self.tagname, self.value, self.node_id)
268 elif self.was_changed():
269 api.plshell.UpdateSliceTag(api.plauth, self.id, self.value)
270 elif self.was_deleted():
271 api.plshell.DeleteSliceTag(api.plauth, self.id)
275 def __init__(self, tagtype):
276 self.id = tagtype['tag_type_id']
277 self.category = tagtype['category']
278 self.tagname = tagtype['tagname']
279 self.min_role_id = tagtype['min_role_id']
281 self.in_rspec = False
282 if self.category == 'slice/rspec':
284 if self.tagname in ['codemux', 'ip_addresses', 'vsys']:
287 def permit_update(self, role_id):
288 if role_id > self.min_role_id:
294 A Network is a compound object consisting of:
295 * a dictionary mapping site IDs to Site objects
296 * a dictionary mapping node IDs to Node objects
297 * a dictionary mapping interface IDs to Iface objects
300 def __init__(self, api, type = "SFA"):
303 self.sites = self.get_sites(api)
304 self.nodes = self.get_nodes(api)
305 self.ifaces = self.get_ifaces(api)
306 self.tags = self.get_slice_tags(api)
307 self.tagtypes = self.get_tag_types(api)
310 """ Lookup site based on id or idtag value """
311 def lookupSite(self, id):
313 if isinstance(id, basestring):
314 id = int(id.lstrip('s'))
318 raise InvalidRSpec("site ID %s not found" % id)
324 sites.append(self.sites[s])
327 """ Lookup node based on id or idtag value """
328 def lookupNode(self, id):
330 if isinstance(id, basestring):
331 id = int(id.lstrip('n'))
335 raise InvalidRSpec("node ID %s not found" % id)
341 nodes.append(self.nodes[n])
344 """ Lookup iface based on id or idtag value """
345 def lookupIface(self, id):
347 if isinstance(id, basestring):
348 id = int(id.lstrip('i'))
350 val = self.ifaces[id]
352 raise InvalidRSpec("interface ID %s not found" % id)
357 for i in self.ifaces:
358 ifaces.append(self.ifaces[i])
361 def nodesWithSlivers(self):
369 def lookupSliceTag(self, id):
374 raise InvalidRSpec("slicetag ID %s not found" % id)
377 def getSliceTags(self):
380 tags.append(self.tags[t])
383 def lookupTagType(self, name):
386 val = self.tagtypes[name]
388 raise InvalidRSpec("tag %s not found" % name)
391 def getTagTypes(self):
393 for t in self.tagtypes:
394 tags.append(self.tagtypes[t])
398 Process the elements under <sliver_defaults> or <sliver>
400 def __process_attributes(self, element, node=None):
404 tagtypes = self.getTagTypes()
408 for e in element.iterfind("./" + tt.tagname):
409 self.slice.update_multi_tag(tt.tagname, e.text, node)
411 e = element.find("./" + tt.tagname)
413 self.slice.update_tag(tt.tagname, e.text, node)
416 Annotate the objects in the Network with information from the RSpec
418 def addRSpec(self, xml, schema=None):
420 tree = etree.parse(StringIO(xml))
421 except etree.XMLSyntaxError:
422 message = str(sys.exc_info()[1])
423 raise InvalidRSpec(message)
425 # Filter out stuff that's not for us
426 rspec = tree.getroot()
427 for network in rspec.iterfind("./network"):
428 if network.get("name") != self.api.hrn:
429 rspec.remove(network)
430 for request in rspec.iterfind("./request"):
431 if request.get("name") != self.api.hrn:
432 rspec.remove(request)
435 # Validate the incoming request against the RelaxNG schema
436 relaxng_doc = etree.parse(schema)
437 relaxng = etree.RelaxNG(relaxng_doc)
439 if not relaxng(tree):
440 error = relaxng.error_log.last_error
441 message = "%s (line %s)" % (error.message, error.line)
442 raise InvalidRSpec(message)
446 defaults = rspec.find(".//sliver_defaults")
447 self.__process_attributes(defaults)
449 # Find slivers under node elements
450 for sliver in rspec.iterfind("./network/site/node/sliver"):
451 elem = sliver.getparent()
453 node = self.lookupNode(elem.get("id"))
455 # Don't worry about nodes from other aggregates
459 self.__process_attributes(sliver, node)
461 # Find slivers that specify nodeid
462 for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
464 node = self.lookupNode(sliver.get("nodeid"))
466 # Don't worry about nodes from other aggregates
470 self.__process_attributes(sliver, node)
475 Annotate the objects in the Network with information from the slice
480 raise InvalidRSpec("no slice associated with network")
482 for node in slice.get_nodes():
486 Write any slice tags that have been added or modified back to the DB
488 def updateSliceTags(self):
489 for tag in self.getSliceTags():
490 if tag.category == 'slice/rspec' and not tag.was_updated() and tag.permit_update(None, 40):
491 # The user wants to delete this tag
494 # Update slice tags in database
495 for tag in self.getSliceTags():
496 if tag.slice_id == self.slice.id:
500 Produce XML directly from the topology specification.
503 xml = XMLBuilder(format = True, tab_step = " ")
504 with xml.RSpec(type=self.type):
506 element = xml.network(name=self.api.hrn, slice=self.slice.hrn)
508 element = xml.network(name=self.api.hrn)
512 self.slice.toxml(xml)
513 for site in self.getSites():
516 header = '<?xml version="1.0"?>\n'
517 return header + str(xml)
520 Create a dictionary of site objects keyed by site ID
522 def get_sites(self, api):
524 for site in api.plshell.GetSites(api.plauth, {'peer_id': None}):
525 t = site['site_id'], Site(self, site)
531 Create a dictionary of node objects keyed by node ID
533 def get_nodes(self, api):
535 for node in api.plshell.GetNodes(api.plauth, {'peer_id': None}):
536 t = node['node_id'], Node(self, node)
541 Create a dictionary of node objects keyed by node ID
543 def get_ifaces(self, api):
545 for iface in api.plshell.GetInterfaces(api.plauth):
546 t = iface['interface_id'], Iface(self, iface)
551 Create a dictionary of slicetag objects keyed by slice tag ID
553 def get_slice_tags(self, api):
555 for tag in api.plshell.GetSliceTags(api.plauth):
556 t = tag['slice_tag_id'], Slicetag(tag)
561 Create a list of tagtype obects keyed by tag name
563 def get_tag_types(self, api):
565 for tag in api.plshell.GetTagTypes(api.plauth):
566 t = tag['tagname'], TagType(tag)
571 Return a Slice object for a single slice
573 def get_slice(self, api, hrn):
574 slicename = hrn_to_pl_slicename(hrn)
575 slice = api.plshell.GetSlices(api.plauth, [slicename])
577 self.slice = Slice(self, slicename, slice[0])