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 self.get_primary_iface().toxml(xml)
77 self.sliver.toxml(xml)
81 def __init__(self, network, site):
82 self.network = network
83 self.id = site['site_id']
84 self.idtag = "s%s" % self.id
85 self.node_ids = site['node_ids']
87 self.name = site['abbreviated_name']
88 self.tag = site['login_base']
89 self.public = site['is_public']
90 self.enabled = site['enabled']
92 self.whitelist = False
94 def get_sitenodes(self):
96 for i in self.node_ids:
97 n.append(self.network.lookupNode(i))
100 def toxml(self, xml):
101 if not (self.public and self.enabled and self.node_ids):
103 with xml.site(id = self.idtag):
106 for node in self.get_sitenodes():
111 def __init__(self, network, hrn, slice):
113 self.network = network
114 self.id = slice['slice_id']
115 self.name = slice['name']
116 self.node_ids = set(slice['node_ids'])
117 self.slice_tag_ids = slice['slice_tag_ids']
120 Use with tags that can have more than one instance
122 def get_multi_tag(self, tagname, node = None):
124 for i in self.slice_tag_ids:
125 tag = self.network.lookupSliceTag(i)
126 if tag.tagname == tagname:
127 if not (node and node.id != tag.node_id):
132 Use with tags that have only one instance
134 def get_tag(self, tagname, node = None):
135 for i in self.slice_tag_ids:
136 tag = self.network.lookupSliceTag(i)
137 if tag.tagname == tagname:
138 if (not node) or (node.id == tag.node_id):
144 for id in self.node_ids:
145 n.append(self.network.nodes[id])
148 # Add a new slice tag
149 def add_tag(self, tagname, value, node = None, category = 'slice/rspec'):
150 record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value,
153 record['node_id'] = node.id
155 record['node_id'] = None
156 tag = Slicetag(record)
157 self.network.tags[tag.id] = tag
158 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):
166 tag = self.get_tag(tagname, node)
167 if tag and tag.value == value:
173 tag = self.add_tag(tagname, value, node)
178 def update_multi_tag(self, tagname, value, node = None):
179 tags = self.get_multi_tag(tagname, node)
181 if tag and tag.value == value:
185 tag = self.add_tag(tagname, value, node)
190 def tags_to_xml(self, xml, node = None):
191 tagtypes = self.network.getTagTypes()
195 tags = self.get_multi_tag(tt.tagname, node)
197 if not tag.deleted: ### Debugging
198 xml << (tag.tagname, tag.value)
200 tag = self.get_tag(tt.tagname, node)
202 if not tag.deleted: ### Debugging
203 xml << (tag.tagname, tag.value)
205 def toxml(self, xml):
206 with xml.sliver_defaults:
207 self.tags_to_xml(xml)
212 def __init__(self, tag):
213 self.id = tag['slice_tag_id']
215 # Make one up for the time being...
216 self.id = Slicetag.newid
218 self.slice_id = tag['slice_id']
219 self.tagname = tag['tagname']
220 self.value = tag['value']
221 self.node_id = tag['node_id']
222 self.category = tag['category']
226 self.writable = False
227 if self.category == 'slice/rspec':
230 # Mark a tag as deleted
235 def write(self, api):
236 if not self.writable:
241 api.plshell.UpdateSliceTag(api.plauth, self.id, self.value)
243 api.plshell.AddSliceTag(api.plauth, self.slice_id,
244 self.tagname, self.value, self.node_id)
245 elif self.deleted and int(self.id) > 0:
246 api.plshell.DeleteSliceTag(api.plauth, self.id)
250 def __init__(self, tagtype):
251 self.id = tagtype['tag_type_id']
252 self.category = tagtype['category']
253 self.tagname = tagtype['tagname']
255 self.in_rspec = False
256 if self.category == 'slice/rspec':
258 if self.tagname in ['codemux', 'ip_addresses', 'vsys']:
263 A Network is a compound object consisting of:
264 * a dictionary mapping site IDs to Site objects
265 * a dictionary mapping node IDs to Node objects
266 * a dictionary mapping interface IDs to Iface objects
269 def __init__(self, api, type = "PlanetLab"):
272 self.sites = self.get_sites(api)
273 self.nodes = self.get_nodes(api)
274 self.ifaces = self.get_ifaces(api)
275 self.tags = self.get_slice_tags(api)
276 self.tagtypes = self.get_tag_types(api)
279 """ Lookup site based on id or idtag value """
280 def lookupSite(self, id):
282 if isinstance(id, basestring):
283 id = int(id.lstrip('s'))
287 raise InvalidRSpec("site ID %s not found" % id)
293 sites.append(self.sites[s])
296 """ Lookup node based on id or idtag value """
297 def lookupNode(self, id):
299 if isinstance(id, basestring):
300 id = int(id.lstrip('n'))
304 raise InvalidRSpec("node ID %s not found" % id)
310 nodes.append(self.nodes[n])
313 """ Lookup iface based on id or idtag value """
314 def lookupIface(self, id):
316 if isinstance(id, basestring):
317 id = int(id.lstrip('i'))
319 val = self.ifaces[id]
321 raise InvalidRSpec("interface ID %s not found" % id)
326 for i in self.ifaces:
327 ifaces.append(self.ifaces[i])
330 def nodesWithSlivers(self):
338 def lookupSliceTag(self, id):
343 raise InvalidRSpec("slicetag ID %s not found" % id)
346 def getSliceTags(self):
349 tags.append(self.tags[t])
352 def lookupTagType(self, name):
355 val = self.tagstypes[name]
357 raise InvalidRSpec("tag %s not found" % name)
360 def getTagTypes(self):
362 for t in self.tagtypes:
363 tags.append(self.tagtypes[t])
366 def __process_attributes(self, element, node=None):
370 # Do we need to check caller's role before update???
371 tagtypes = self.getTagTypes()
375 for e in element.iterfind("./" + tt.tagname):
376 self.slice.update_multi_tag(tt.tagname, e.text, node)
378 e = element.find("./" + tt.tagname)
380 self.slice.update_tag(tt.tagname, e.text, node)
383 Annotate the objects in the Network with information from the RSpec
385 def addRSpec(self, xml, schema=None):
387 tree = etree.parse(StringIO(xml))
388 except etree.XMLSyntaxError:
389 message = str(sys.exc_info()[1])
390 raise InvalidRSpec(message)
393 # Validate the incoming request against the RelaxNG schema
394 relaxng_doc = etree.parse(schema)
395 relaxng = etree.RelaxNG(relaxng_doc)
397 if not relaxng(tree):
398 error = relaxng.error_log.last_error
399 message = "%s (line %s)" % (error.message, error.line)
400 raise InvalidRSpec(message)
402 rspec = tree.getroot()
405 defaults = rspec.find("./network/sliver_defaults")
406 self.__process_attributes(defaults)
408 # Find slivers under node elements
409 for sliver in rspec.iterfind("./network/site/node/sliver"):
410 elem = sliver.getparent()
412 node = self.lookupNode(elem.get("id"))
414 # Don't worry about nodes from other aggregates
418 self.__process_attributes(sliver, node)
420 # Find slivers that specify nodeid
421 for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
423 node = self.lookupNode(sliver.get("nodeid"))
425 # Don't worry about nodes from other aggregates
429 self.__process_attributes(sliver, node)
434 Annotate the objects in the Network with information from the slice
439 raise InvalidRSpec("no slice associated with network")
441 for node in slice.get_nodes():
445 Write any slice tags that have been added or modified back to the DB
447 def updateSliceTags(self):
448 # Update slice tags in database
449 for tag in self.getSliceTags():
450 if tag.slice_id == self.slice.id:
456 Produce XML directly from the topology specification.
459 xml = XMLBuilder(format = True, tab_step = " ")
460 with xml.RSpec(type=self.type):
461 name = "Public_" + self.type
463 element = xml.network(name=name, slice=self.slice.hrn)
465 element = xml.network(name=name)
469 self.slice.toxml(xml)
470 for site in self.getSites():
473 header = '<?xml version="1.0"?>\n'
474 return header + str(xml)
477 Create a dictionary of site objects keyed by site ID
479 def get_sites(self, api):
481 for site in api.plshell.GetSites(api.plauth, {'peer_id': None}):
482 t = site['site_id'], Site(self, site)
488 Create a dictionary of node objects keyed by node ID
490 def get_nodes(self, api):
492 for node in api.plshell.GetNodes(api.plauth, {'peer_id': None}):
493 t = node['node_id'], Node(self, node)
498 Create a dictionary of node objects keyed by node ID
500 def get_ifaces(self, api):
502 for iface in api.plshell.GetInterfaces(api.plauth):
503 t = iface['interface_id'], Iface(self, iface)
508 Create a dictionary of slicetag objects keyed by slice tag ID
510 def get_slice_tags(self, api):
512 for tag in api.plshell.GetSliceTags(api.plauth):
513 t = tag['slice_tag_id'], Slicetag(tag)
518 Create a list of tagtype obects keyed by tag name
520 def get_tag_types(self, api):
522 for tag in api.plshell.GetTagTypes(api.plauth):
523 t = tag['tagname'], TagType(tag)
528 Return a Slice object for a single slice
530 def get_slice(self, api, hrn):
531 slicename = hrn_to_pl_slicename(hrn)
532 slice = api.plshell.GetSlices(api.plauth, [slicename])
534 self.slice = Slice(self, slicename, slice[0])