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.peer_id = slice['peer_id']
119 self.node_ids = set(slice['node_ids'])
120 self.slice_tag_ids = slice['slice_tag_ids']
123 Use with tags that can have more than one instance
125 def get_multi_tag(self, tagname, node = None):
127 for i in self.slice_tag_ids:
128 tag = self.network.lookupSliceTag(i)
129 if tag.tagname == tagname:
130 if not (node and node.id != tag.node_id):
135 Use with tags that have only one instance
137 def get_tag(self, tagname, node = None):
138 for i in self.slice_tag_ids:
139 tag = self.network.lookupSliceTag(i)
140 if tag.tagname == tagname:
141 if (not node) or (node.id == tag.node_id):
147 for id in self.node_ids:
148 if id in self.network.nodes:
149 n.append(self.network.nodes[id])
152 # Add a new slice tag
153 def add_tag(self, tagname, value, node = None):
154 tt = self.network.lookupTagType(tagname)
156 tag.initialize(tagname, value, node, self.network)
157 self.network.tags[tag.id] = tag
158 self.slice_tag_ids.append(tag.id)
161 # Update a slice tag if it exists, else add it
162 def update_tag(self, tagname, value, node = None):
163 tag = self.get_tag(tagname, node)
167 tag = self.add_tag(tagname, value, node)
170 def update_multi_tag(self, tagname, value, node = None):
171 tags = self.get_multi_tag(tagname, node)
173 if tag and tag.value == value:
176 tag = self.add_tag(tagname, value, node)
179 def tags_to_xml(self, xml, node = None):
180 tagtypes = self.network.getTagTypes()
184 tags = self.get_multi_tag(tt.tagname, node)
186 if not tag.was_deleted(): ### Debugging
187 xml << (tag.tagname, tag.value)
189 tag = self.get_tag(tt.tagname, node)
191 if not tag.was_deleted(): ### Debugging
192 xml << (tag.tagname, tag.value)
194 def toxml(self, xml):
195 with xml.sliver_defaults:
196 self.tags_to_xml(xml)
201 def __init__(self, tag = None):
204 self.id = tag['slice_tag_id']
205 self.slice_id = tag['slice_id']
206 self.tagname = tag['tagname']
207 self.value = tag['value']
208 self.node_id = tag['node_id']
209 self.category = tag['category']
212 # Create a new slicetag that will be written to the DB later
213 def initialize(self, tagname, value, node, network):
214 tt = network.lookupTagType(tagname)
215 self.id = Slicetag.newid
217 self.slice_id = network.slice.id
218 self.tagname = tagname
221 self.node_id = node.id
224 self.category = tt.category
227 def change(self, value):
228 if self.value != value:
230 self.status = "change"
232 self.status = "updated"
234 # Mark a tag as deleted
236 self.status = "delete"
241 def was_changed(self):
242 return (self.status == "change")
244 def was_deleted(self):
245 return (self.status == "delete")
247 def was_updated(self):
248 return (self.status != None)
250 def write(self, api, user_plauth):
253 api.plshell.AddSliceTag(user_plauth, self.slice_id,
254 self.tagname, self.value, self.node_id)
255 elif self.was_changed():
256 api.plshell.UpdateSliceTag(user_plauth, self.id, self.value)
257 elif self.was_deleted():
258 api.plshell.DeleteSliceTag(user_plauth, self.id)
260 raise InvalidRSpec("user cannot modify '%s' tag" % self.tagname)
265 def __init__(self, tagtype):
266 self.id = tagtype['tag_type_id']
267 self.category = tagtype['category']
268 self.tagname = tagtype['tagname']
270 self.in_rspec = False
271 if self.category == 'slice/rspec':
273 if self.tagname in ['codemux', 'ip_addresses', 'vsys']:
279 A Network is a compound object consisting of:
280 * a dictionary mapping site IDs to Site objects
281 * a dictionary mapping node IDs to Node objects
282 * a dictionary mapping interface IDs to Iface objects
284 def __init__(self, api, user_plauth, type = "SFA"):
286 self.user_plauth = user_plauth
288 self.sites = self.get_sites(api)
289 self.nodes = self.get_nodes(api)
290 self.ifaces = self.get_ifaces(api)
291 self.tags = self.get_slice_tags(api)
292 self.tagtypes = self.get_tag_types(api)
295 def lookupSite(self, id):
296 """ Lookup site based on id or idtag value """
298 if isinstance(id, basestring):
299 id = int(id.lstrip('s'))
303 raise InvalidRSpec("site ID %s not found" % id)
309 sites.append(self.sites[s])
312 def lookupNode(self, id):
313 """ Lookup node based on id or idtag value """
315 if isinstance(id, basestring):
316 id = int(id.lstrip('n'))
320 raise InvalidRSpec("node ID %s not found" % id)
326 nodes.append(self.nodes[n])
329 def lookupIface(self, id):
330 """ Lookup iface based on id or idtag value """
332 if isinstance(id, basestring):
333 id = int(id.lstrip('i'))
335 val = self.ifaces[id]
337 raise InvalidRSpec("interface ID %s not found" % id)
342 for i in self.ifaces:
343 ifaces.append(self.ifaces[i])
346 def nodesWithSlivers(self):
354 def lookupSliceTag(self, id):
359 raise InvalidRSpec("slicetag ID %s not found" % id)
362 def getSliceTags(self):
365 tags.append(self.tags[t])
368 def lookupTagType(self, name):
371 val = self.tagtypes[name]
373 raise InvalidRSpec("tag %s not found" % name)
376 def getTagTypes(self):
378 for t in self.tagtypes:
379 tags.append(self.tagtypes[t])
382 def __process_attributes(self, element, node=None):
384 Process the elements under <sliver_defaults> or <sliver>
389 tagtypes = self.getTagTypes()
393 for e in element.iterfind("./" + tt.tagname):
394 self.slice.update_multi_tag(tt.tagname, e.text, node)
396 e = element.find("./" + tt.tagname)
398 self.slice.update_tag(tt.tagname, e.text, node)
400 def addRSpec(self, xml, schema=None):
402 Annotate the objects in the Network with information from the RSpec
405 tree = etree.parse(StringIO(xml))
406 except etree.XMLSyntaxError:
407 message = str(sys.exc_info()[1])
408 raise InvalidRSpec(message)
410 # Filter out stuff that's not for us
411 rspec = tree.getroot()
412 for network in rspec.iterfind("./network"):
413 if network.get("name") != self.api.hrn:
414 rspec.remove(network)
415 for request in rspec.iterfind("./request"):
416 if request.get("name") != self.api.hrn:
417 rspec.remove(request)
420 # Validate the incoming request against the RelaxNG schema
421 relaxng_doc = etree.parse(schema)
422 relaxng = etree.RelaxNG(relaxng_doc)
424 if not relaxng(tree):
425 error = relaxng.error_log.last_error
426 message = "%s (line %s)" % (error.message, error.line)
427 raise InvalidRSpec(message)
431 defaults = rspec.find(".//sliver_defaults")
432 self.__process_attributes(defaults)
434 # Find slivers under node elements
435 for sliver in rspec.iterfind("./network/site/node/sliver"):
436 elem = sliver.getparent()
438 node = self.lookupNode(elem.get("id"))
440 # Don't worry about nodes from other aggregates
444 self.__process_attributes(sliver, node)
446 # Find slivers that specify nodeid
447 for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
449 node = self.lookupNode(sliver.get("nodeid"))
451 # Don't worry about nodes from other aggregates
455 self.__process_attributes(sliver, node)
461 Annotate the objects in the Network with information from the slice
465 raise InvalidRSpec("no slice associated with network")
467 for node in slice.get_nodes():
470 def updateSliceTags(self):
472 Write any slice tags that have been added or modified back to the DB
474 for tag in self.getSliceTags():
475 if tag.category == 'slice/rspec' and not tag.was_updated():
476 # The user wants to delete this tag
479 # Update slice tags in database
480 for tag in self.getSliceTags():
481 if tag.slice_id == self.slice.id:
482 tag.write(self.api, self.user_plauth)
486 Produce XML directly from the topology specification.
488 xml = XMLBuilder(format = True, tab_step = " ")
489 with xml.RSpec(type=self.type):
491 element = xml.network(name=self.api.hrn, slice=self.slice.hrn)
493 element = xml.network(name=self.api.hrn)
497 self.slice.toxml(xml)
498 for site in self.getSites():
501 header = '<?xml version="1.0"?>\n'
502 return header + str(xml)
504 def get_sites(self, api):
506 Create a dictionary of site objects keyed by site ID
509 for site in api.plshell.GetSites(self.user_plauth, {'peer_id': None}):
510 t = site['site_id'], Site(self, site)
516 def get_nodes(self, api):
518 Create a dictionary of node objects keyed by node ID
521 for node in api.plshell.GetNodes(self.user_plauth, {'peer_id': None}):
522 t = node['node_id'], Node(self, node)
526 def get_ifaces(self, api):
528 Create a dictionary of node objects keyed by node ID
531 for iface in api.plshell.GetInterfaces(self.user_plauth):
532 t = iface['interface_id'], Iface(self, iface)
536 def get_slice_tags(self, api):
538 Create a dictionary of slicetag objects keyed by slice tag ID
541 for tag in api.plshell.GetSliceTags(self.user_plauth):
542 t = tag['slice_tag_id'], Slicetag(tag)
546 def get_tag_types(self, api):
548 Create a list of tagtype obects keyed by tag name
551 for tag in api.plshell.GetTagTypes(self.user_plauth):
552 t = tag['tagname'], TagType(tag)
556 def get_slice(self, api, hrn):
558 Return a Slice object for a single slice
560 slicename = hrn_to_pl_slicename(hrn)
561 slice = api.plshell.GetSlices(self.user_plauth, [slicename])
563 self.slice = Slice(self, slicename, slice[0])