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']
33 Just print out bwlimit right now
37 with xml.bw_limit(units="kbps"):
38 xml << str(self.bwlimit / 1000)
42 def __init__(self, network, node, bps = 1000 * 1000000):
43 self.network = network
44 self.id = node['node_id']
45 self.idtag = "n%s" % self.id
46 self.hostname = node['hostname']
47 self.site_id = node['site_id']
48 self.iface_ids = node['interface_ids']
50 self.whitelist = node['slice_ids_whitelist']
54 for id in self.iface_ids:
55 i.append(self.network.lookupIface(id))
56 # Only return the first interface
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 if self.network.type == "VINI":
77 xml << str(int(self.bps/1000))
78 for iface in self.get_ifaces():
81 self.sliver.toxml(xml)
85 def __init__(self, network, site):
86 self.network = network
87 self.id = site['site_id']
88 self.idtag = "s%s" % self.id
89 self.node_ids = site['node_ids']
91 self.name = site['abbreviated_name']
92 self.tag = site['login_base']
93 self.public = site['is_public']
94 self.enabled = site['enabled']
96 self.whitelist = False
98 def get_sitenodes(self):
100 for i in self.node_ids:
101 n.append(self.network.lookupNode(i))
104 def toxml(self, xml):
105 if not (self.public and self.enabled and self.node_ids):
107 with xml.site(id = self.idtag):
110 for node in self.get_sitenodes():
115 def __init__(self, network, hrn, slice):
117 self.network = network
118 self.id = slice['slice_id']
119 self.name = slice['name']
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_tags(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 n.append(self.network.nodes[id])
152 # Add a new slice tag
153 def add_tag(self, tagname, value, node = None):
154 record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value}
156 record['node_id'] = node.id
158 record['node_id'] = None
159 tag = Slicetag(record)
160 self.network.slicetags[tag.id] = tag
161 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)
178 def tags_to_xml(self, xml, node = None):
179 tag = self.get_tag("cpu_pct", node)
181 with xml.cpu_percent:
183 tags = self.get_tags("vsys", node)
188 def toxml(self, xml):
189 with xml.sliver_defaults:
190 self.tags_to_xml(xml)
195 def __init__(self, tag):
196 self.id = tag['slice_tag_id']
198 # Make one up for the time being...
199 self.id = Slicetag.newid
201 self.slice_id = tag['slice_id']
202 self.tagname = tag['tagname']
203 self.value = tag['value']
204 self.node_id = tag['node_id']
209 # Mark a tag as deleted
214 def write(self, api):
217 api.plshell.UpdateSliceTag(api.plauth, self.id, self.value)
219 api.plshell.AddSliceTag(api.plauth, self.slice_id,
220 self.tagname, self.value, self.node_id)
221 elif self.deleted and int(self.id) > 0:
222 api.plshell.DeleteSliceTag(api.plauth, self.id)
226 A Network is a compound object consisting of:
227 * a dictionary mapping site IDs to Site objects
228 * a dictionary mapping node IDs to Node objects
229 * a dictionary mapping interface IDs to Iface objects
232 def __init__(self, api, type = "PlanetLab"):
235 self.sites = self.get_sites(api)
236 self.nodes = self.get_nodes(api)
237 self.ifaces = self.get_ifaces(api)
238 self.tags = self.get_slice_tags(api)
241 """ Lookup site based on id or idtag value """
242 def lookupSite(self, id):
244 if isinstance(id, basestring):
245 id = int(id.lstrip('s'))
249 raise KeyError("site ID %s not found" % id)
255 sites.append(self.sites[s])
258 """ Lookup node based on id or idtag value """
259 def lookupNode(self, id):
261 if isinstance(id, basestring):
262 id = int(id.lstrip('n'))
266 raise KeyError("node ID %s not found" % id)
272 nodes.append(self.nodes[n])
275 """ Lookup iface based on id or idtag value """
276 def lookupIface(self, id):
278 if isinstance(id, basestring):
279 id = int(id.lstrip('i'))
281 val = self.ifaces[id]
283 raise KeyError("interface ID %s not found" % id)
288 for i in self.ifaces:
289 ifaces.append(self.ifaces[i])
292 def nodesWithSlivers(self):
300 def lookupSliceTag(self, id):
305 raise KeyError("slicetag ID %s not found" % id)
308 def getSliceTags(self):
311 tags.append(self.tags[t])
315 Annotate the objects in the Network with information from the RSpec
317 def addRSpec(self, xml):
319 for node in self.getNodes():
320 nodedict[node.idtag] = node
324 tree = etree.parse(StringIO(xml))
327 # Validate the incoming request against the RelaxNG schema
328 relaxng_doc = etree.parse(self.schema)
329 relaxng = etree.RelaxNG(relaxng_doc)
331 if not relaxng(tree):
332 error = relaxng.error_log.last_error
333 message = "%s (line %s)" % (error.message, error.line)
334 raise InvalidRSpec(message)
336 rspec = tree.getroot()
338 # Find slivers under node elements
339 for sliver in rspec.iterfind("./network/site/node/sliver"):
340 elem = sliver.getparent()
341 node = nodedict[elem.get("id")]
342 slicenodes[node.id] = node
345 # Find slivers that specify nodeid
346 for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
347 node = nodedict[sliver.get("nodeid")]
348 slicenodes[node.id] = node
354 Annotate the objects in the Network with information from the slice
359 raise Error("no slice associated with network")
361 for node in slice.get_nodes():
365 Write any slice tags that have been added or modified back to the DB
367 def updateSliceTags(self, slice):
368 # Update slice tags in database
369 for tag in self.getSliceTags():
370 if tag.slice_id == slice.id:
371 if tag.tagname == 'topo_rspec' and not tag.updated:
376 Produce XML directly from the topology specification.
379 xml = XMLBuilder(format = True, tab_step = " ")
380 with xml.RSpec(type=self.type):
381 name = "Public_" + self.type
383 element = xml.network(name=name, slice=self.slice.hrn)
385 element = xml.network(name=name)
389 self.slice.toxml(xml)
390 for site in self.getSites():
393 header = '<?xml version="1.0"?>\n'
394 return header + str(xml)
397 Create a dictionary of site objects keyed by site ID
399 def get_sites(self, api):
401 for site in api.plshell.GetSites(api.plauth):
402 t = site['site_id'], Site(self, site)
408 Create a dictionary of node objects keyed by node ID
410 def get_nodes(self, api):
412 for node in api.plshell.GetNodes(api.plauth):
413 t = node['node_id'], Node(self, node)
418 Create a dictionary of node objects keyed by node ID
420 def get_ifaces(self, api):
422 for iface in api.plshell.GetInterfaces(api.plauth):
423 t = iface['interface_id'], Iface(self, iface)
428 Create a dictionary of slicetag objects keyed by slice tag ID
430 def get_slice_tags(self, api):
432 for tag in api.plshell.GetSliceTags(api.plauth):
433 t = tag['slice_tag_id'], Slicetag(tag)
438 Return a Slice object for a single slice
440 def get_slice(self, api, hrn):
441 slicename = hrn_to_pl_slicename(hrn)
442 slice = api.plshell.GetSlices(api.plauth, [slicename])
444 self.slice = Slice(self, slicename, slice[0])