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()
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):
150 record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value}
152 record['node_id'] = node.id
154 record['node_id'] = None
155 tag = Slicetag(record)
156 self.network.tags[tag.id] = tag
157 self.slice_tag_ids.append(tag.id)
162 # Update a slice tag if it exists, else add it
163 def update_tag(self, tagname, value, node = None):
164 tag = self.get_tag(tagname, node)
165 if tag and tag.value == value:
171 tag = self.add_tag(tagname, value, node)
174 def update_multi_tag(self, tagname, value, node = None):
175 tags = self.get_multi_tag(tagname, node)
177 if tag and tag.value == value:
181 tag = self.add_tag(tagname, value, node)
184 def tags_to_xml(self, xml, node = None):
185 tagtypes = self.network.getTagTypes()
188 tags = self.get_multi_tag(tt.tagname, node)
190 if not tag.deleted: ### Debugging
191 xml << (tag.tagname, tag.value)
193 tag = self.get_tag(tt.tagname, node)
195 if not tag.deleted: ### Debugging
196 xml << (tag.tagname, tag.value)
198 def toxml(self, xml):
199 with xml.sliver_defaults:
200 self.tags_to_xml(xml)
205 def __init__(self, tag):
206 self.id = tag['slice_tag_id']
208 # Make one up for the time being...
209 self.id = Slicetag.newid
211 self.slice_id = tag['slice_id']
212 self.tagname = tag['tagname']
213 self.value = tag['value']
214 self.node_id = tag['node_id']
219 # Mark a tag as deleted
224 def write(self, api):
227 api.plshell.UpdateSliceTag(api.plauth, self.id, self.value)
229 api.plshell.AddSliceTag(api.plauth, self.slice_id,
230 self.tagname, self.value, self.node_id)
231 elif self.deleted and int(self.id) > 0:
232 api.plshell.DeleteSliceTag(api.plauth, self.id)
236 def __init__(self, tagtype):
237 self.id = tagtype['tag_type_id']
238 self.tagname = tagtype['tagname']
239 if self.tagname in ['codemux', 'ip_addresses', 'vsys']:
246 A Network is a compound object consisting of:
247 * a dictionary mapping site IDs to Site objects
248 * a dictionary mapping node IDs to Node objects
249 * a dictionary mapping interface IDs to Iface objects
252 def __init__(self, api, type = "PlanetLab"):
255 self.sites = self.get_sites(api)
256 self.nodes = self.get_nodes(api)
257 self.ifaces = self.get_ifaces(api)
258 self.tags = self.get_slice_tags(api)
259 self.tagtypes = self.get_tag_types(api)
262 """ Lookup site based on id or idtag value """
263 def lookupSite(self, id):
265 if isinstance(id, basestring):
266 id = int(id.lstrip('s'))
270 raise InvalidRSpec("site ID %s not found" % id)
276 sites.append(self.sites[s])
279 """ Lookup node based on id or idtag value """
280 def lookupNode(self, id):
282 if isinstance(id, basestring):
283 id = int(id.lstrip('n'))
287 raise InvalidRSpec("node ID %s not found" % id)
293 nodes.append(self.nodes[n])
296 """ Lookup iface based on id or idtag value """
297 def lookupIface(self, id):
299 if isinstance(id, basestring):
300 id = int(id.lstrip('i'))
302 val = self.ifaces[id]
304 raise InvalidRSpec("interface ID %s not found" % id)
309 for i in self.ifaces:
310 ifaces.append(self.ifaces[i])
313 def nodesWithSlivers(self):
321 def lookupSliceTag(self, id):
326 raise InvalidRSpec("slicetag ID %s not found" % id)
329 def getSliceTags(self):
332 tags.append(self.tags[t])
335 def lookupTagType(self, name):
338 val = self.tagstypes[name]
340 raise InvalidRSpec("tag %s not found" % name)
343 def getTagTypes(self):
345 for t in self.tagtypes:
346 tags.append(self.tagtypes[t])
349 def __process_attributes(self, element, node=None):
350 # Do we need to check caller's role before update???
351 tagtypes = self.getTagTypes()
354 for e in element.iterfind("./" + tt.tagname):
355 self.slice.update_multi_tag(tt.tagname, e.text, node)
357 e = element.find("./" + tt.tagname)
359 self.slice.update_tag(tt.tagname, e.text, node)
362 Annotate the objects in the Network with information from the RSpec
364 def addRSpec(self, xml, schema=None):
366 tree = etree.parse(StringIO(xml))
367 except etree.XMLSyntaxError:
368 message = str(sys.exc_info()[1])
369 raise InvalidRSpec(message)
372 # Validate the incoming request against the RelaxNG schema
373 relaxng_doc = etree.parse(schema)
374 relaxng = etree.RelaxNG(relaxng_doc)
376 if not relaxng(tree):
377 error = relaxng.error_log.last_error
378 message = "%s (line %s)" % (error.message, error.line)
379 raise InvalidRSpec(message)
381 rspec = tree.getroot()
384 defaults = rspec.find("./network/sliver_defaults")
385 self.__process_attributes(defaults)
387 # Find slivers under node elements
388 for sliver in rspec.iterfind("./network/site/node/sliver"):
389 elem = sliver.getparent()
391 node = self.lookupNode(elem.get("id"))
393 # Don't worry about nodes from other aggregates
397 self.__process_attributes(sliver, node)
399 # Find slivers that specify nodeid
400 for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
402 node = self.lookupNode(sliver.get("nodeid"))
404 # Don't worry about nodes from other aggregates
408 self.__process_attributes(sliver, node)
413 Annotate the objects in the Network with information from the slice
418 raise InvalidRSpec("no slice associated with network")
420 for node in slice.get_nodes():
424 Write any slice tags that have been added or modified back to the DB
426 def updateSliceTags(self):
427 # Update slice tags in database
428 for tag in self.getSliceTags():
429 if tag.slice_id == self.slice.id:
435 Produce XML directly from the topology specification.
438 xml = XMLBuilder(format = True, tab_step = " ")
439 with xml.RSpec(type=self.type):
440 name = "Public_" + self.type
442 element = xml.network(name=name, slice=self.slice.hrn)
444 element = xml.network(name=name)
448 self.slice.toxml(xml)
449 for site in self.getSites():
452 header = '<?xml version="1.0"?>\n'
453 return header + str(xml)
456 Create a dictionary of site objects keyed by site ID
458 def get_sites(self, api):
460 for site in api.plshell.GetSites(api.plauth, {'peer_id': None}):
461 t = site['site_id'], Site(self, site)
467 Create a dictionary of node objects keyed by node ID
469 def get_nodes(self, api):
471 for node in api.plshell.GetNodes(api.plauth, {'peer_id': None}):
472 t = node['node_id'], Node(self, node)
477 Create a dictionary of node objects keyed by node ID
479 def get_ifaces(self, api):
481 for iface in api.plshell.GetInterfaces(api.plauth):
482 t = iface['interface_id'], Iface(self, iface)
487 Create a dictionary of slicetag objects keyed by slice tag ID
489 def get_slice_tags(self, api):
491 for tag in api.plshell.GetSliceTags(api.plauth):
492 t = tag['slice_tag_id'], Slicetag(tag)
497 Create a list of tagtype obects keyed by tag name
499 def get_tag_types(self, api):
501 for tag in api.plshell.GetTagTypes(api.plauth):
502 if tag['category'] == 'slice/rspec':
503 t = tag['tagname'], TagType(tag)
508 Return a Slice object for a single slice
510 def get_slice(self, api, hrn):
511 slicename = hrn_to_pl_slicename(hrn)
512 slice = api.plshell.GetSlices(api.plauth, [slicename])
514 self.slice = Slice(self, slicename, slice[0])