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):
105 with xml.site(id = self.idtag):
108 for node in self.get_sitenodes():
113 def __init__(self, network, hrn, slice):
115 self.network = network
116 self.id = slice['slice_id']
117 self.name = slice['name']
118 self.node_ids = set(slice['node_ids'])
119 self.slice_tag_ids = slice['slice_tag_ids']
122 Use with tags that can have more than one instance
124 def get_multi_tag(self, tagname, node = None):
126 for i in self.slice_tag_ids:
127 tag = self.network.lookupSliceTag(i)
128 if tag.tagname == tagname:
129 if not (node and node.id != tag.node_id):
134 Use with tags that have only one instance
136 def get_tag(self, tagname, node = None):
137 for i in self.slice_tag_ids:
138 tag = self.network.lookupSliceTag(i)
139 if tag.tagname == tagname:
140 if (not node) or (node.id == tag.node_id):
146 for id in self.node_ids:
147 n.append(self.network.nodes[id])
150 # Add a new slice tag
151 def add_tag(self, tagname, value, node = None, category = 'slice/rspec'):
152 record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value,
155 record['node_id'] = node.id
157 record['node_id'] = None
158 tag = Slicetag(record)
159 self.network.tags[tag.id] = tag
160 self.slice_tag_ids.append(tag.id)
166 # Update a slice tag if it exists, else add it
167 def update_tag(self, tagname, value, node = None):
168 tag = self.get_tag(tagname, node)
169 if tag and tag.value == value:
175 tag = self.add_tag(tagname, value, node)
180 def update_multi_tag(self, tagname, value, node = None):
181 tags = self.get_multi_tag(tagname, node)
183 if tag and tag.value == value:
187 tag = self.add_tag(tagname, value, node)
192 def tags_to_xml(self, xml, node = None):
193 tagtypes = self.network.getTagTypes()
197 tags = self.get_multi_tag(tt.tagname, node)
199 if not tag.deleted: ### Debugging
200 xml << (tag.tagname, tag.value)
202 tag = self.get_tag(tt.tagname, node)
204 if not tag.deleted: ### Debugging
205 xml << (tag.tagname, tag.value)
207 def toxml(self, xml):
208 with xml.sliver_defaults:
209 self.tags_to_xml(xml)
214 def __init__(self, tag):
215 self.id = tag['slice_tag_id']
217 # Make one up for the time being...
218 self.id = Slicetag.newid
220 self.slice_id = tag['slice_id']
221 self.tagname = tag['tagname']
222 self.value = tag['value']
223 self.node_id = tag['node_id']
224 self.category = tag['category']
228 self.writable = False
229 if self.category == 'slice/rspec':
232 # Mark a tag as deleted
237 def write(self, api):
238 if not self.writable:
243 api.plshell.UpdateSliceTag(api.plauth, self.id, self.value)
245 api.plshell.AddSliceTag(api.plauth, self.slice_id,
246 self.tagname, self.value, self.node_id)
247 elif self.deleted and int(self.id) > 0:
248 api.plshell.DeleteSliceTag(api.plauth, self.id)
252 def __init__(self, tagtype):
253 self.id = tagtype['tag_type_id']
254 self.category = tagtype['category']
255 self.tagname = tagtype['tagname']
257 self.in_rspec = False
258 if self.category == 'slice/rspec':
260 if self.tagname in ['codemux', 'ip_addresses', 'vsys']:
265 A Network is a compound object consisting of:
266 * a dictionary mapping site IDs to Site objects
267 * a dictionary mapping node IDs to Node objects
268 * a dictionary mapping interface IDs to Iface objects
271 def __init__(self, api, type = "PlanetLab"):
274 self.sites = self.get_sites(api)
275 self.nodes = self.get_nodes(api)
276 self.ifaces = self.get_ifaces(api)
277 self.tags = self.get_slice_tags(api)
278 self.tagtypes = self.get_tag_types(api)
281 """ Lookup site based on id or idtag value """
282 def lookupSite(self, id):
284 if isinstance(id, basestring):
285 id = int(id.lstrip('s'))
289 raise InvalidRSpec("site ID %s not found" % id)
295 sites.append(self.sites[s])
298 """ Lookup node based on id or idtag value """
299 def lookupNode(self, id):
301 if isinstance(id, basestring):
302 id = int(id.lstrip('n'))
306 raise InvalidRSpec("node ID %s not found" % id)
312 nodes.append(self.nodes[n])
315 """ Lookup iface based on id or idtag value """
316 def lookupIface(self, id):
318 if isinstance(id, basestring):
319 id = int(id.lstrip('i'))
321 val = self.ifaces[id]
323 raise InvalidRSpec("interface ID %s not found" % id)
328 for i in self.ifaces:
329 ifaces.append(self.ifaces[i])
332 def nodesWithSlivers(self):
340 def lookupSliceTag(self, id):
345 raise InvalidRSpec("slicetag ID %s not found" % id)
348 def getSliceTags(self):
351 tags.append(self.tags[t])
354 def lookupTagType(self, name):
357 val = self.tagstypes[name]
359 raise InvalidRSpec("tag %s not found" % name)
362 def getTagTypes(self):
364 for t in self.tagtypes:
365 tags.append(self.tagtypes[t])
368 def __process_attributes(self, element, node=None):
372 # Do we need to check caller's role before update???
373 tagtypes = self.getTagTypes()
377 for e in element.iterfind("./" + tt.tagname):
378 self.slice.update_multi_tag(tt.tagname, e.text, node)
380 e = element.find("./" + tt.tagname)
382 self.slice.update_tag(tt.tagname, e.text, node)
385 Annotate the objects in the Network with information from the RSpec
387 def addRSpec(self, xml, schema=None):
389 tree = etree.parse(StringIO(xml))
390 except etree.XMLSyntaxError:
391 message = str(sys.exc_info()[1])
392 raise InvalidRSpec(message)
395 # Validate the incoming request against the RelaxNG schema
396 relaxng_doc = etree.parse(schema)
397 relaxng = etree.RelaxNG(relaxng_doc)
399 if not relaxng(tree):
400 error = relaxng.error_log.last_error
401 message = "%s (line %s)" % (error.message, error.line)
402 raise InvalidRSpec(message)
404 rspec = tree.getroot()
407 defaults = rspec.find("./network/sliver_defaults")
408 self.__process_attributes(defaults)
410 # Find slivers under node elements
411 for sliver in rspec.iterfind("./network/site/node/sliver"):
412 elem = sliver.getparent()
414 node = self.lookupNode(elem.get("id"))
416 # Don't worry about nodes from other aggregates
420 self.__process_attributes(sliver, node)
422 # Find slivers that specify nodeid
423 for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
425 node = self.lookupNode(sliver.get("nodeid"))
427 # Don't worry about nodes from other aggregates
431 self.__process_attributes(sliver, node)
436 Annotate the objects in the Network with information from the slice
441 raise InvalidRSpec("no slice associated with network")
443 for node in slice.get_nodes():
447 Write any slice tags that have been added or modified back to the DB
449 def updateSliceTags(self):
450 # Update slice tags in database
451 for tag in self.getSliceTags():
452 if tag.slice_id == self.slice.id:
458 Produce XML directly from the topology specification.
461 xml = XMLBuilder(format = True, tab_step = " ")
462 with xml.RSpec(type=self.type):
463 name = "Public_" + self.type
465 element = xml.network(name=name, slice=self.slice.hrn)
467 element = xml.network(name=name)
471 self.slice.toxml(xml)
472 for site in self.getSites():
475 header = '<?xml version="1.0"?>\n'
476 return header + str(xml)
479 Create a dictionary of site objects keyed by site ID
481 def get_sites(self, api):
483 for site in api.plshell.GetSites(api.plauth, {'peer_id': None}):
484 t = site['site_id'], Site(self, site)
490 Create a dictionary of node objects keyed by node ID
492 def get_nodes(self, api):
494 for node in api.plshell.GetNodes(api.plauth, {'peer_id': None}):
495 t = node['node_id'], Node(self, node)
500 Create a dictionary of node objects keyed by node ID
502 def get_ifaces(self, api):
504 for iface in api.plshell.GetInterfaces(api.plauth):
505 t = iface['interface_id'], Iface(self, iface)
510 Create a dictionary of slicetag objects keyed by slice tag ID
512 def get_slice_tags(self, api):
514 for tag in api.plshell.GetSliceTags(api.plauth):
515 t = tag['slice_tag_id'], Slicetag(tag)
520 Create a list of tagtype obects keyed by tag name
522 def get_tag_types(self, api):
524 for tag in api.plshell.GetTagTypes(api.plauth):
525 t = tag['tagname'], TagType(tag)
530 Return a Slice object for a single slice
532 def get_slice(self, api, hrn):
533 slicename = hrn_to_pl_slicename(hrn)
534 slice = api.plshell.GetSlices(api.plauth, [slicename])
536 self.slice = Slice(self, slicename, slice[0])