slices.verify_slice_attributes(slice, requested_attributes)
# add/remove slice from nodes
- requested_slivers = [str(host) for host in rspec.version.get_nodes_with_slivers()]
+ requested_slivers = [node['component_name'] for node in rspec.version.get_nodes_with_slivers()]
slices.verify_slice_nodes(slice, requested_slivers, peer)
-
- aggregate.prepare_nodes({'hostname': requested_slivers})
- aggregate.prepare_interfaces({'node_id': aggregate.nodes.keys()})
+
+ # add/remove links links
slices.verify_slice_links(slice, rspec.version.get_link_requests(), aggregate)
# handle MyPLC peer association.
from lxml import etree as ET
from sqlobject import *
-from sfa.util.faults import InvalidRSpec,
+from sfa.util.faults import InvalidRSpec
from sfa.util.xrn import urn_to_hrn, Xrn
from sfa.util.plxrn import hrn_to_pl_slicename, slicename_to_hrn
from sfa.util.callids import Callids
#comes with its own logging
#from sfa.util.sfalogging import logger
from sfa.util.version import version_core
-
from sfa.trust.credential import Credential
-
from sfa.server.sfaapi import SfaApi
-
from sfa.plc.aggregate import Aggregate
from sfa.plc.slices import Slice, Slices
+from sfa.rspecs.version_manager import VersionManager
+from sfa.rspecs.rspec import RSpec
# not sure what this used to be nor where it is now defined
#from sfa.rspecs.sfa_rspec import sfa_rspec_version
f.close()
def GetVersion(api):
+
+ version_manager = VersionManager()
+ ad_rspec_versions = []
+ request_rspec_versions = []
+ for rspec_version in version_manager.versions:
+ if rspec_version.content_type in ['*', 'ad']:
+ ad_rspec_versions.append(rspec_version.to_dict())
+ if rspec_version.content_type in ['*', 'request']:
+ request_rspec_versions.append(rspec_version.to_dict())
+ default_rspec_version = version_manager.get_version("sfa 1").to_dict()
xrn=Xrn(api.hrn)
- request_rspec_versions = [dict(sfa_rspec_version)]
- ad_rspec_versions = [dict(sfa_rspec_version)]
version_more = {'interface':'aggregate',
'testbed':'myplc',
'hrn':xrn.get_hrn(),
'request_rspec_versions': request_rspec_versions,
'ad_rspec_versions': ad_rspec_versions,
- 'default_ad_rspec': dict(sfa_rspec_version)
+ 'default_ad_rspec': default_rspec_version
}
return version_core(version_more)
-
### GENI AM API Methods
- def GetVersion(self, api):
- xrn=Xrn(api.hrn)
- request_rspec_versions = [dict(sfa_rspec_version)]
- ad_rspec_versions = [dict(sfa_rspec_version)]
- #TODO: MAX-AM specific
- version_more = {'interface':'aggregate',
- 'testbed':'myplc',
- 'hrn':xrn.get_hrn(),
- 'request_rspec_versions': request_rspec_versions,
- 'ad_rspec_versions': ad_rspec_versions,
- 'default_ad_rspec': dict(sfa_rspec_version)
- }
- return version_core(version_more)
-
def SliverStatus(self, api, slice_xrn, creds, call_id):
if Callids().already_handled(call_id): return {}
return self.slice_status(api, slice_xrn, creds)
import sys
import time
+import traceback
from StringIO import StringIO
from copy import copy
from lxml import etree
except Exception, e:
logger.warn("drop_slicemgr_stats failed: %s " % (str(e)))
- def add_slicemgr_stat(self, rspec, callname, aggname, elapsed, status):
+ def add_slicemgr_stat(self, rspec, callname, aggname, elapsed, status, exc_info=None):
try:
stats_tags = rspec.xml.xpath('//statistics[@call="%s"]' % callname)
if stats_tags:
stats_tag = stats_tags[0]
else:
stats_tag = etree.SubElement(rspec.xml.root, "statistics", call=callname)
-
- etree.SubElement(stats_tag, "aggregate", name=str(aggname), elapsed=str(elapsed), status=str(status))
+
+ stat_tag = etree.SubElement(stats_tag, "aggregate", name=str(aggname), elapsed=str(elapsed), status=str(status))
+
+ if exc_info:
+ exc_tag = etree.SubElement(stat_tag, "exc_info", name=str(exc_info[1]))
+
+ # formats the traceback as one big text blob
+ #exc_tag.text = "\n".join(traceback.format_exception(exc_info[0], exc_info[1], exc_info[2]))
+
+ # formats the traceback as a set of xml elements
+ tb = traceback.extract_tb(exc_info[2])
+ for item in tb:
+ exc_frame = etree.SubElement(exc_tag, "tb_frame", filename=str(item[0]), line=str(item[1]), func=str(item[2]), code=str(item[3]))
+
except Exception, e:
logger.warn("add_slicemgr_stat failed on %s: %s" %(aggname, str(e)))
return {"aggregate": aggregate, "rspec": rspec, "elapsed": time.time()-tStart, "status": "success"}
except Exception, e:
api.logger.log_exc("ListResources failed at %s" %(server.url))
- return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception"}
+ return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception", "exc_info": sys.exc_info()}
if Callids().already_handled(call_id): return ""
result_version = version_manager._get_version(rspec_version.type, rspec_version.version, 'ad')
rspec = RSpec(version=result_version)
for result in results:
- self.add_slicemgr_stat(rspec, "ListResources", result["aggregate"], result["elapsed"], result["status"])
+ self.add_slicemgr_stat(rspec, "ListResources", result["aggregate"], result["elapsed"], result["status"], result.get("exc_info",None))
if result["status"]=="success":
try:
rspec.version.merge(result["rspec"])
args.append(call_id)
rspec = server.CreateSliver(*args)
return {"aggregate": aggregate, "rspec": rspec, "elapsed": time.time()-tStart, "status": "success"}
- except:
+ except:
logger.log_exc('Something wrong in _CreateSliver with URL %s'%server.url)
- return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception"}
+ return {"aggregate": aggregate, "elapsed": time.time()-tStart, "status": "exception", "exc_info": sys.exc_info()}
if Callids().already_handled(call_id): return ""
# Validate the RSpec against PlanetLab's schema --disabled for now
manifest_version = version_manager._get_version(rspec.version.type, rspec.version.version, 'manifest')
result_rspec = RSpec(version=manifest_version)
for result in results:
- self.add_slicemgr_stat(result_rspec, "CreateSliver", result["aggregate"], result["elapsed"], result["status"])
+ self.add_slicemgr_stat(result_rspec, "CreateSliver", result["aggregate"], result["elapsed"], result["status"], result.get("exc_info",None))
if result["status"]=="success":
try:
result_rspec.version.merge(result["rspec"])
+++ /dev/null
-<?xml version="1.0"?>
-<RSpec type="SFA">
- <request name="plc.vini">
- <sliver nodeid="n18"/>
- <sliver nodeid="n20"/>
- <sliver nodeid="n22"/>
- <sliver nodeid="n26"/>
- <sliver nodeid="n28"/>
- <sliver nodeid="n30"/>
- <sliver nodeid="n32"/>
- <sliver nodeid="n34"/>
- <sliver nodeid="n36"/>
- <vlink endpoints="n18 n22"/>
- <vlink endpoints="n18 n26"/>
- <vlink endpoints="n18 n28"/>
- <vlink endpoints="n20 n22"/>
- <vlink endpoints="n22 n26"/>
- <vlink endpoints="n26 n30"/>
- <vlink endpoints="n28 n30"/>
- <vlink endpoints="n28 n32"/>
- <vlink endpoints="n30 n36"/>
- <vlink endpoints="n34 n36"/>
- <vlink endpoints="n32 n36"/>
- <vlink endpoints="n32 n34"/>
- </request>
-</RSpec>
+++ /dev/null
-#!/usr/bin/python
-#
-# Links in the physical topology, gleaned from looking at the Internet2
-# topology map. Link (a, b) connects sites with IDs a and b.
-#
-PhysicalLinks = [(2, 12), # I2 Princeton - New York
- (11, 13), # I2 Chicago - Wash DC
- (11, 15), # I2 Chicago - Atlanta
- (11, 16), # I2 Chicago - CESNET
- (11, 17), # I2 Chicago - Kansas City
- (12, 13), # I2 New York - Wash DC
- (13, 15), # I2 Wash DC - Atlanta
- (14, 15), # Ga Tech - I2 Atlanta
- (15, 19), # I2 Atlanta - Houston
- (17, 19), # I2 Kansas City - Houston
- (17, 22), # I2 Kansas City - Salt Lake City
- (17, 24), # I2 Kansas City - UMKC
- (19, 20), # I2 Houston - Los Angeles
- (20, 21), # I2 Los Angeles - Seattle
- (20, 22), # I2 Los Angeles - Salt Lake City
- (21, 22)] # I2 Seattle - Salt Lake City
-
-
+++ /dev/null
-from __future__ import with_statement
-import re
-import socket
-from sfa.util.faults import *
-from sfa.managers.vini.topology import PhysicalLinks
-from xmlbuilder import XMLBuilder
-from lxml import etree
-import sys
-from StringIO import StringIO
-
-VINI_RELAXNG_SCHEMA = "/var/www/html/schemas/vini.rng"
-
-# Taken from bwlimit.py
-#
-# See tc_util.c and http://physics.nist.gov/cuu/Units/binary.html. Be
-# warned that older versions of tc interpret "kbps", "mbps", "mbit",
-# and "kbit" to mean (in this system) "kibps", "mibps", "mibit", and
-# "kibit" and that if an older version is installed, all rates will
-# be off by a small fraction.
-suffixes = {
- "": 1,
- "bit": 1,
- "kibit": 1024,
- "kbit": 1000,
- "mibit": 1024*1024,
- "mbit": 1000000,
- "gibit": 1024*1024*1024,
- "gbit": 1000000000,
- "tibit": 1024*1024*1024*1024,
- "tbit": 1000000000000,
- "bps": 8,
- "kibps": 8*1024,
- "kbps": 8000,
- "mibps": 8*1024*1024,
- "mbps": 8000000,
- "gibps": 8*1024*1024*1024,
- "gbps": 8000000000,
- "tibps": 8*1024*1024*1024*1024,
- "tbps": 8000000000000
-}
-
-
-def get_tc_rate(s):
- """
- Parses an integer or a tc rate string (e.g., 1.5mbit) into bits/second
- """
-
- if type(s) == int:
- return s
- m = re.match(r"([0-9.]+)(\D*)", s)
- if m is None:
- return -1
- suffix = m.group(2).lower()
- if suffixes.has_key(suffix):
- return int(float(m.group(1)) * suffixes[suffix])
- else:
- return -1
-
-def format_tc_rate(rate):
- """
- Formats a bits/second rate into a tc rate string
- """
-
- if rate >= 1000000000 and (rate % 1000000000) == 0:
- return "%.0fgbit" % (rate / 1000000000.)
- elif rate >= 1000000 and (rate % 1000000) == 0:
- return "%.0fmbit" % (rate / 1000000.)
- elif rate >= 1000:
- return "%.0fkbit" % (rate / 1000.)
- else:
- return "%.0fbit" % rate
-
-
-class Node:
- def __init__(self, node, bps = 1000 * 1000000):
- self.id = node['node_id']
- self.idtag = "n%s" % self.id
- self.hostname = node['hostname']
- self.name = self.shortname = self.hostname.replace('.vini-veritas.net', '')
- self.site_id = node['site_id']
- self.ipaddr = socket.gethostbyname(self.hostname)
- self.bps = bps
- self.links = set()
- self.sliver = False
-
- def get_link_id(self, remote):
- if self.id < remote.id:
- link = (self.id<<7) + remote.id
- else:
- link = (remote.id<<7) + self.id
- return link
-
- def get_iface_id(self, remote):
- if self.id < remote.id:
- iface = 1
- else:
- iface = 2
- return iface
-
- def get_virt_ip(self, remote):
- link = self.get_link_id(remote)
- iface = self.get_iface_id(remote)
- first = link >> 6
- second = ((link & 0x3f)<<2) + iface
- return "192.168.%d.%d" % (first, second)
-
- def get_virt_net(self, remote):
- link = self.get_link_id(remote)
- first = link >> 6
- second = (link & 0x3f)<<2
- return "192.168.%d.%d/30" % (first, second)
-
- def get_site(self, sites):
- return sites[self.site_id]
-
- def get_topo_rspec(self, link):
- if link.end1 == self:
- remote = link.end2
- elif link.end2 == self:
- remote = link.end1
- else:
- raise Error("Link does not connect to Node")
-
- my_ip = self.get_virt_ip(remote)
- remote_ip = remote.get_virt_ip(self)
- net = self.get_virt_net(remote)
- bw = format_tc_rate(link.bps)
- return (remote.id, remote.ipaddr, bw, my_ip, remote_ip, net)
-
- def add_link(self, link):
- self.links.add(link)
-
- def add_tag(self, sites):
- s = self.get_site(sites)
- words = self.hostname.split(".")
- index = words[0].replace("node", "")
- if index.isdigit():
- self.tag = s.tag + index
- else:
- self.tag = None
-
- # Assumes there is at most one Link between two sites
- def get_sitelink(self, node, sites):
- site1 = sites[self.site_id]
- site2 = sites[node.site_id]
- sl = site1.links.intersection(site2.links)
- if len(sl):
- return sl.pop()
- return None
-
- def add_sliver(self):
- self.sliver = True
-
- def toxml(self, xml, hrn):
- if not self.tag:
- return
- with xml.node(id = self.idtag):
- with xml.hostname:
- xml << self.hostname
- with xml.kbps:
- xml << str(int(self.bps/1000))
- if self.sliver:
- with xml.sliver:
- pass
-
-
-class Link:
- def __init__(self, end1, end2, bps = 1000 * 1000000, parent = None):
- self.end1 = end1
- self.end2 = end2
- self.bps = bps
- self.parent = parent
- self.children = []
-
- end1.add_link(self)
- end2.add_link(self)
-
- if self.parent:
- self.parent.children.append(self)
-
- def toxml(self, xml):
- end_ids = "%s %s" % (self.end1.idtag, self.end2.idtag)
-
- if self.parent:
- element = xml.vlink(endpoints=end_ids)
- else:
- element = xml.link(endpoints=end_ids)
-
- with element:
- with xml.description:
- xml << "%s -- %s" % (self.end1.name, self.end2.name)
- with xml.kbps:
- xml << str(int(self.bps/1000))
- for child in self.children:
- child.toxml(xml)
-
-
-class Site:
- def __init__(self, site):
- self.id = site['site_id']
- self.idtag = "s%s" % self.id
- self.node_ids = site['node_ids']
- self.name = site['abbreviated_name'].replace(" ", "_")
- self.tag = site['login_base']
- self.public = site['is_public']
- self.enabled = site['enabled']
- self.links = set()
-
- def get_sitenodes(self, nodes):
- n = []
- for i in self.node_ids:
- n.append(nodes[i])
- return n
-
- def add_link(self, link):
- self.links.add(link)
-
- def toxml(self, xml, hrn, nodes):
- if not (self.public and self.enabled and self.node_ids):
- return
- with xml.site(id = self.idtag):
- with xml.name:
- xml << self.name
-
- for node in self.get_sitenodes(nodes):
- node.toxml(xml, hrn)
-
-
-class Slice:
- def __init__(self, slice):
- self.id = slice['slice_id']
- self.name = slice['name']
- self.node_ids = set(slice['node_ids'])
- self.slice_tag_ids = slice['slice_tag_ids']
-
- def get_tag(self, tagname, slicetags, node = None):
- for i in self.slice_tag_ids:
- tag = slicetags[i]
- if tag.tagname == tagname:
- if (not node) or (node.id == tag.node_id):
- return tag
- else:
- return None
-
- def get_nodes(self, nodes):
- n = []
- for id in self.node_ids:
- n.append(nodes[id])
- return n
-
-
- # Add a new slice tag
- def add_tag(self, tagname, value, slicetags, node = None):
- record = {'slice_tag_id':None, 'slice_id':self.id, 'tagname':tagname, 'value':value}
- if node:
- record['node_id'] = node.id
- else:
- record['node_id'] = None
- tag = Slicetag(record)
- slicetags[tag.id] = tag
- self.slice_tag_ids.append(tag.id)
- tag.changed = True
- tag.updated = True
- return tag
-
- # Update a slice tag if it exists, else add it
- def update_tag(self, tagname, value, slicetags, node = None):
- tag = self.get_tag(tagname, slicetags, node)
- if tag and tag.value == value:
- value = "no change"
- elif tag:
- tag.value = value
- tag.changed = True
- else:
- tag = self.add_tag(tagname, value, slicetags, node)
- tag.updated = True
-
- def assign_egre_key(self, slicetags):
- if not self.get_tag('egre_key', slicetags):
- try:
- key = free_egre_key(slicetags)
- self.update_tag('egre_key', key, slicetags)
- except:
- # Should handle this case...
- pass
- return
-
- def turn_on_netns(self, slicetags):
- tag = self.get_tag('netns', slicetags)
- if (not tag) or (tag.value != '1'):
- self.update_tag('netns', '1', slicetags)
- return
-
- def turn_off_netns(self, slicetags):
- tag = self.get_tag('netns', slicetags)
- if tag and (tag.value != '0'):
- tag.delete()
- return
-
- def add_cap_net_admin(self, slicetags):
- tag = self.get_tag('capabilities', slicetags)
- if tag:
- caps = tag.value.split(',')
- for cap in caps:
- if cap == "CAP_NET_ADMIN":
- return
- else:
- newcaps = "CAP_NET_ADMIN," + tag.value
- self.update_tag('capabilities', newcaps, slicetags)
- else:
- self.add_tag('capabilities', 'CAP_NET_ADMIN', slicetags)
- return
-
- def remove_cap_net_admin(self, slicetags):
- tag = self.get_tag('capabilities', slicetags)
- if tag:
- caps = tag.value.split(',')
- newcaps = []
- for cap in caps:
- if cap != "CAP_NET_ADMIN":
- newcaps.append(cap)
- if newcaps:
- value = ','.join(newcaps)
- self.update_tag('capabilities', value, slicetags)
- else:
- tag.delete()
- return
-
- # Update the vsys/setup-link and vsys/setup-nat slice tags.
- def add_vsys_tags(self, slicetags):
- link = nat = False
- for i in self.slice_tag_ids:
- tag = slicetags[i]
- if tag.tagname == 'vsys':
- if tag.value == 'setup-link':
- link = True
- elif tag.value == 'setup-nat':
- nat = True
- if not link:
- self.add_tag('vsys', 'setup-link', slicetags)
- if not nat:
- self.add_tag('vsys', 'setup-nat', slicetags)
- return
-
-
-class Slicetag:
- newid = -1
- def __init__(self, tag):
- self.id = tag['slice_tag_id']
- if not self.id:
- # Make one up for the time being...
- self.id = Slicetag.newid
- Slicetag.newid -= 1
- self.slice_id = tag['slice_id']
- self.tagname = tag['tagname']
- self.value = tag['value']
- self.node_id = tag['node_id']
- self.updated = False
- self.changed = False
- self.deleted = False
-
- # Mark a tag as deleted
- def delete(self):
- self.deleted = True
- self.updated = True
-
- def write(self, api):
- if self.changed:
- if int(self.id) > 0:
- api.driver.UpdateSliceTag(self.id, self.value)
- else:
- api.driver.AddSliceTag(self.slice_id, self.tagname, self.value, self.node_id)
- elif self.deleted and int(self.id) > 0:
- api.driver.DeleteSliceTag(self.id)
-
-
-"""
-A topology is a compound object consisting of:
-* a dictionary mapping site IDs to Site objects
-* a dictionary mapping node IDs to Node objects
-* the Site objects are connected via SiteLink objects representing
- the physical topology and available bandwidth
-* the Node objects are connected via Link objects representing
- the requested or assigned virtual topology of a slice
-"""
-class Topology:
- def __init__(self, api):
- self.api = api
- self.sites = get_sites(api)
- self.nodes = get_nodes(api)
- self.tags = get_slice_tags(api)
- self.sitelinks = []
- self.nodelinks = []
-
- for (s1, s2) in PhysicalLinks:
- self.sitelinks.append(Link(self.sites[s1], self.sites[s2]))
-
- for id in self.nodes:
- self.nodes[id].add_tag(self.sites)
-
- for t in self.tags:
- tag = self.tags[t]
- if tag.tagname == 'topo_rspec':
- node1 = self.nodes[tag.node_id]
- l = eval(tag.value)
- for (id, realip, bw, lvip, rvip, vnet) in l:
- allocbps = get_tc_rate(bw)
- node1.bps -= allocbps
- try:
- node2 = self.nodes[id]
- if node1.id < node2.id:
- sl = node1.get_sitelink(node2, self.sites)
- sl.bps -= allocbps
- except:
- pass
-
-
- """ Lookup site based on id or idtag value """
- def lookupSite(self, id):
- val = None
- if isinstance(id, basestring):
- id = int(id.lstrip('s'))
- try:
- val = self.sites[id]
- except:
- raise KeyError("site ID %s not found" % id)
- return val
-
- def getSites(self):
- sites = []
- for s in self.sites:
- sites.append(self.sites[s])
- return sites
-
- """ Lookup node based on id or idtag value """
- def lookupNode(self, id):
- val = None
- if isinstance(id, basestring):
- id = int(id.lstrip('n'))
- try:
- val = self.nodes[id]
- except:
- raise KeyError("node ID %s not found" % id)
- return val
-
- def getNodes(self):
- nodes = []
- for n in self.nodes:
- nodes.append(self.nodes[n])
- return nodes
-
- def nodesInTopo(self):
- nodes = []
- for n in self.nodes:
- node = self.nodes[n]
- if node.sliver:
- nodes.append(node)
- return nodes
-
- def lookupSliceTag(self, id):
- val = None
- try:
- val = self.tags[id]
- except:
- raise KeyError("slicetag ID %s not found" % id)
- return val
-
- def getSliceTags(self):
- tags = []
- for t in self.tags:
- tags.append(self.tags[t])
- return tags
-
- def lookupSiteLink(self, node1, node2):
- site1 = self.sites[node1.site_id]
- site2 = self.sites[node2.site_id]
- for link in self.sitelinks:
- if site1 == link.end1 and site2 == link.end2:
- return link
- if site2 == link.end1 and site1 == link.end2:
- return link
- return None
-
-
- def __add_vlink(self, vlink, slicenodes, parent = None):
- n1 = n2 = None
- endpoints = vlink.get("endpoints")
- if endpoints:
- (end1, end2) = endpoints.split()
- n1 = self.lookupNode(end1)
- n2 = self.lookupNode(end2)
- elif parent:
- """ Try to infer the endpoints for the virtual link """
- site_endpoints = parent.get("endpoints")
- (n1, n2) = self.__infer_endpoints(site_endpoints, slicenodes)
- else:
- raise Error("no endpoints given")
-
- #print "Added virtual link: %s -- %s" % (n1.tag, n2.tag)
- bps = int(vlink.findtext("kbps")) * 1000
- sitelink = self.lookupSiteLink(n1, n2)
- if not sitelink:
- raise PermissionError("nodes %s and %s not adjacent" %
- (n1.idtag, n2.idtag))
- self.nodelinks.append(Link(n1, n2, bps, sitelink))
- return
-
- """
- Infer the endpoints of the virtual link. If the slice exists on
- only a single node at each end of the physical link, we'll assume that
- the user wants the virtual link to terminate at these nodes.
- """
- def __infer_endpoints(self, endpoints, slicenodes):
- n = []
- ends = endpoints.split()
- for end in ends:
- found = 0
- site = self.lookupSite(end)
- for id in site.node_ids:
- if id in slicenodes:
- n.append(slicenodes[id])
- found += 1
- if found != 1:
- raise Error("could not infer endpoint for site %s" % site.id)
- #print "Inferred endpoints: %s %s" % (n[0].idtag, n[1].idtag)
- return n
-
- def nodeTopoFromRSpec(self, xml):
- if self.nodelinks:
- raise Error("virtual topology already present")
-
- nodedict = {}
- for node in self.getNodes():
- nodedict[node.idtag] = node
-
- slicenodes = {}
-
- tree = etree.parse(StringIO(xml))
-
- # Validate the incoming request against the RelaxNG schema
- relaxng_doc = etree.parse(VINI_RELAXNG_SCHEMA)
- relaxng = etree.RelaxNG(relaxng_doc)
-
- if not relaxng(tree):
- error = relaxng.error_log.last_error
- message = "%s (line %s)" % (error.message, error.line)
- raise InvalidRSpec(message)
-
- rspec = tree.getroot()
-
- """
- Handle requests where the user has annotated a description of the
- physical resources (nodes and links) with virtual ones (slivers
- and vlinks).
- """
- # Find slivers under node elements
- for sliver in rspec.iterfind("./network/site/node/sliver"):
- elem = sliver.getparent()
- node = nodedict[elem.get("id")]
- slicenodes[node.id] = node
- node.add_sliver()
-
- # Find vlinks under link elements
- for vlink in rspec.iterfind("./network/link/vlink"):
- link = vlink.getparent()
- self.__add_vlink(vlink, slicenodes, link)
-
- """
- Handle requests where the user has listed the virtual resources only
- """
- # Find slivers that specify nodeid
- for sliver in rspec.iterfind("./request/sliver[@nodeid]"):
- node = nodedict[sliver.get("nodeid")]
- slicenodes[node.id] = node
- node.add_sliver()
-
- # Find vlinks that specify endpoints
- for vlink in rspec.iterfind("./request/vlink[@endpoints]"):
- self.__add_vlink(vlink, slicenodes)
-
- return
-
- def nodeTopoFromSliceTags(self, slice):
- if self.nodelinks:
- raise Error("virtual topology already present")
-
- for node in slice.get_nodes(self.nodes):
- node.sliver = True
- linktag = slice.get_tag('topo_rspec', self.tags, node)
- if linktag:
- l = eval(linktag.value)
- for (id, realip, bw, lvip, rvip, vnet) in l:
- if node.id < id:
- bps = get_tc_rate(bw)
- remote = self.lookupNode(id)
- sitelink = self.lookupSiteLink(node, remote)
- self.nodelinks.append(Link(node,remote,bps,sitelink))
-
- def updateSliceTags(self, slice):
- if not self.nodelinks:
- return
-
- slice.update_tag('vini_topo', 'manual', self.tags)
- slice.assign_egre_key(self.tags)
- slice.turn_on_netns(self.tags)
- slice.add_cap_net_admin(self.tags)
-
- for node in slice.get_nodes(self.nodes):
- linkdesc = []
- for link in node.links:
- linkdesc.append(node.get_topo_rspec(link))
- if linkdesc:
- topo_str = "%s" % linkdesc
- slice.update_tag('topo_rspec', topo_str, self.tags, node)
-
- # Update slice tags in database
- for tag in self.getSliceTags():
- if tag.slice_id == slice.id:
- if tag.tagname == 'topo_rspec' and not tag.updated:
- tag.delete()
- tag.write(self.api)
-
- """
- Check the requested topology against the available topology and capacity
- """
- def verifyNodeTopo(self, hrn, topo):
- for link in self.nodelinks:
- if link.bps <= 0:
- raise GeniInvalidArgument(bw, "BW")
-
- n1 = link.end1
- n2 = link.end2
- sitelink = self.lookupSiteLink(n1, n2)
- if not sitelink:
- raise PermissionError("%s: nodes %s and %s not adjacent" % (hrn, n1.tag, n2.tag))
- if sitelink.bps < link.bps:
- raise PermissionError("%s: insufficient capacity between %s and %s" % (hrn, n1.tag, n2.tag))
-
- """
- Produce XML directly from the topology specification.
- """
- def toxml(self, hrn = None):
- xml = XMLBuilder(format = True, tab_step = " ")
- with xml.RSpec(type="VINI"):
- if hrn:
- element = xml.network(name="Public_VINI", slice=hrn)
- else:
- element = xml.network(name="Public_VINI")
-
- with element:
- for site in self.getSites():
- site.toxml(xml, hrn, self.nodes)
- for link in self.sitelinks:
- link.toxml(xml)
-
- header = '<?xml version="1.0"?>\n'
- return header + str(xml)
-
-"""
-Create a dictionary of site objects keyed by site ID
-"""
-def get_sites(api):
- tmp = []
- for site in api.driver.GetSites():
- t = site['site_id'], Site(site)
- tmp.append(t)
- return dict(tmp)
-
-
-"""
-Create a dictionary of node objects keyed by node ID
-"""
-def get_nodes(api):
- tmp = []
- for node in api.driver.GetNodes():
- t = node['node_id'], Node(node)
- tmp.append(t)
- return dict(tmp)
-
-"""
-Create a dictionary of slice objects keyed by slice ID
-"""
-def get_slice(api, slicename):
- slice = api.driver.GetSlices([slicename])
- if slice:
- return Slice(slice[0])
- else:
- return None
-
-"""
-Create a dictionary of slicetag objects keyed by slice tag ID
-"""
-def get_slice_tags(api):
- tmp = []
- for tag in api.driver.GetSliceTags():
- t = tag['slice_tag_id'], Slicetag(tag)
- tmp.append(t)
- return dict(tmp)
-
-"""
-Find a free EGRE key
-"""
-def free_egre_key(slicetags):
- used = set()
- for i in slicetags:
- tag = slicetags[i]
- if tag.tagname == 'egre_key':
- used.add(int(tag.value))
-
- for i in range(1, 256):
- if i not in used:
- key = i
- break
- else:
- raise KeyError("No more EGRE keys available")
-
- return "%s" % key
-
+++ /dev/null
-start = RSpec
-RSpec = element RSpec {
- attribute expires { xsd:NMTOKEN },
- attribute generated { xsd:NMTOKEN },
- attribute type { xsd:NMTOKEN },
- ( network | request )
-}
-network = element network {
- attribute name { xsd:NMTOKEN },
- attribute slice { xsd:NMTOKEN }?,
- sliver_defaults?,
- site+,
- link*
-}
-sliver_defaults = element sliver_defaults {
- sliver_elements
-}
-site = element site {
- attribute id { xsd:ID },
- element name { text },
- node*
-}
-node = element node {
- attribute id { xsd:ID },
- element hostname { text },
- element bw_unallocated { attribute units { xsd:NMTOKEN }, text }?,
- element bw_limit { attribute units { xsd:NMTOKEN }, text }?,
- sliver*
-}
-link = element link {
- attribute endpoints { xsd:IDREFS },
- element description { text }?,
- element bw_unallocated { attribute units { xsd:NMTOKEN }, text },
- vlink*
-}
-request = element request {
- attribute name { xsd:NMTOKEN },
- sliver_defaults?,
- sliver*,
- vlink*
-}
-sliver = element sliver {
- attribute nodeid { xsd:ID }?,
- sliver_elements
-}
-sliver_elements = (
- element capabilities { text }?
- & element codemux { text }*
- & element cpu_pct { text }?
- & element cpu_share { text }?
- & element delegations { text }?
- & element disk_max { text }?
- & element initscript { text }?
- & element ip_addresses {text }*
- & element net_i2_max_kbyte { text }?
- & element net_i2_max_rate { text }?
- & element net_i2_min_rate { text }?
- & element net_i2_share { text }?
- & element net_i2_thresh_kbyte { text }?
- & element net_max_kbyte { text }?
- & element net_max_rate { text }?
- & element net_min_rate { text }?
- & element net_share { text }?
- & element net_thresh_kbyte { text }?
- & element vsys {text}*
- & element vsys_vnet { text }?
-)
-vlink = element vlink {
- attribute endpoints { xsd:IDREFS }?,
- element description { text }?,
- element kbps { text }?
-}
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
- <start>
- <ref name="RSpec"/>
- </start>
- <define name="RSpec">
- <element name="RSpec">
- <attribute name="expires">
- <data type="NMTOKEN"/>
- </attribute>
- <attribute name="generated">
- <data type="NMTOKEN"/>
- </attribute>
- <attribute name="type">
- <data type="NMTOKEN"/>
- </attribute>
- <choice>
- <ref name="network"/>
- <ref name="request"/>
- </choice>
- </element>
- </define>
- <define name="network">
- <element name="network">
- <attribute name="name">
- <data type="NMTOKEN"/>
- </attribute>
- <optional>
- <attribute name="slice">
- <data type="NMTOKEN"/>
- </attribute>
- </optional>
- <optional>
- <ref name="sliver_defaults"/>
- </optional>
- <oneOrMore>
- <ref name="site"/>
- </oneOrMore>
- <zeroOrMore>
- <ref name="link"/>
- </zeroOrMore>
- </element>
- </define>
- <define name="sliver_defaults">
- <element name="sliver_defaults">
- <ref name="sliver_elements"/>
- </element>
- </define>
- <define name="site">
- <element name="site">
- <attribute name="id">
- <data type="ID"/>
- </attribute>
- <element name="name">
- <text/>
- </element>
- <zeroOrMore>
- <ref name="node"/>
- </zeroOrMore>
- </element>
- </define>
- <define name="node">
- <element name="node">
- <attribute name="id">
- <data type="ID"/>
- </attribute>
- <element name="hostname">
- <text/>
- </element>
- <optional>
- <element name="bw_unallocated">
- <attribute name="units">
- <data type="NMTOKEN"/>
- </attribute>
- <text/>
- </element>
- </optional>
- <optional>
- <element name="bw_limit">
- <attribute name="units">
- <data type="NMTOKEN"/>
- </attribute>
- <text/>
- </element>
- </optional>
- <zeroOrMore>
- <ref name="sliver"/>
- </zeroOrMore>
- </element>
- </define>
- <define name="link">
- <element name="link">
- <attribute name="endpoints">
- <data type="IDREFS"/>
- </attribute>
- <optional>
- <element name="description">
- <text/>
- </element>
- </optional>
- <element name="bw_unallocated">
- <attribute name="units">
- <data type="NMTOKEN"/>
- </attribute>
- <text/>
- </element>
- <zeroOrMore>
- <ref name="vlink"/>
- </zeroOrMore>
- </element>
- </define>
- <define name="request">
- <element name="request">
- <attribute name="name">
- <data type="NMTOKEN"/>
- </attribute>
- <optional>
- <ref name="sliver_defaults"/>
- </optional>
- <zeroOrMore>
- <ref name="sliver"/>
- </zeroOrMore>
- <zeroOrMore>
- <ref name="vlink"/>
- </zeroOrMore>
- </element>
- </define>
- <define name="sliver">
- <element name="sliver">
- <optional>
- <attribute name="nodeid">
- <data type="ID"/>
- </attribute>
- </optional>
- <ref name="sliver_elements"/>
- </element>
- </define>
- <define name="sliver_elements">
- <interleave>
- <optional>
- <element name="capabilities">
- <text/>
- </element>
- </optional>
- <zeroOrMore>
- <element name="codemux">
- <text/>
- </element>
- </zeroOrMore>
- <optional>
- <element name="cpu_pct">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="cpu_share">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="delegations">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="disk_max">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="initscript">
- <text/>
- </element>
- </optional>
- <zeroOrMore>
- <element name="ip_addresses">
- <text/>
- </element>
- </zeroOrMore>
- <optional>
- <element name="net_i2_max_kbyte">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_i2_max_rate">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_i2_min_rate">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_i2_share">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_i2_thresh_kbyte">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_max_kbyte">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_max_rate">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_min_rate">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_share">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="net_thresh_kbyte">
- <text/>
- </element>
- </optional>
- <zeroOrMore>
- <element name="vsys">
- <text/>
- </element>
- </zeroOrMore>
- <optional>
- <element name="vsys_vnet">
- <text/>
- </element>
- </optional>
- </interleave>
- </define>
- <define name="vlink">
- <element name="vlink">
- <optional>
- <attribute name="endpoints">
- <data type="IDREFS"/>
- </attribute>
- </optional>
- <optional>
- <element name="description">
- <text/>
- </element>
- </optional>
- <optional>
- <element name="kbps">
- <text/>
- </element>
- </optional>
- </element>
- </define>
-</grammar>
+++ /dev/null
-<?xml version="1.0"?>
-<RSpec type="SFA">
- <network name="plc.vini">
- <site id="s2">
- <name>Princeton</name>
- <node id="n1">
- <hostname>node1.princeton.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">999000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s3">
- <name>PSG</name>
- <node id="n6">
- <hostname>node1.psg.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n25">
- <hostname>node2.psg.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s4">
- <name>NLR Chicago</name>
- <node id="n2">
- <hostname>node1.chic.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n3">
- <hostname>node2.chic.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s5">
- <name>NLR Houston</name>
- <node id="n4">
- <hostname>node1.hous.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n5">
- <hostname>node2.hous.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s6">
- <name>NLR Atlanta</name>
- <node id="n8">
- <hostname>node1.atla.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n9">
- <hostname>node2.atla.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s7">
- <name>NLR Seattle</name>
- <node id="n10">
- <hostname>node1.seat.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n11">
- <hostname>node2.seat.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s8">
- <name>NLR Los Angeles</name>
- <node id="n12">
- <hostname>node1.losa.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n13">
- <hostname>node2.losa.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s9">
- <name>NLR New York</name>
- <node id="n14">
- <hostname>node1.newy.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n15">
- <hostname>node2.newy.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s10">
- <name>NLR Wash DC</name>
- <node id="n16">
- <hostname>node1.wash.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n17">
- <hostname>node2.wash.nlr.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s11">
- <name>I2 Chicago</name>
- <node id="n18">
- <hostname>node1.chic.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">963000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n19">
- <hostname>node2.chic.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s12">
- <name>I2 New York</name>
- <node id="n20">
- <hostname>node1.newy.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n21">
- <hostname>node2.newy.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s13">
- <name>I2 Wash DC</name>
- <node id="n22">
- <hostname>node1.wash.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">964000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n23">
- <hostname>node2.wash.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s14">
- <name>Georgia Tech</name>
- <node id="n45">
- <hostname>node1.gatech.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">999000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s15">
- <name>I2 Atlanta</name>
- <node id="n26">
- <hostname>node1.atla.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">964000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n27">
- <hostname>node2.atla.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s16">
- <name>CESNET</name>
- <node id="n42">
- <hostname>node1.cesnet.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n43">
- <hostname>node2.cesnet.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </node>
- </site>
- <site id="s17">
- <name>I2 Kansas City</name>
- <node id="n28">
- <hostname>node1.kans.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">961000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n29">
- <hostname>node2.kans.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s19">
- <name>I2 Houston</name>
- <node id="n30">
- <hostname>node1.hous.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">964000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n31">
- <hostname>node2.hous.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s20">
- <name>I2 Los Angeles</name>
- <node id="n36">
- <hostname>node1.losa.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">964000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n37">
- <hostname>node2.losa.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s21">
- <name>I2 Seattle</name>
- <node id="n34">
- <hostname>node1.seat.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">975000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n35">
- <hostname>node2.seat.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s22">
- <name>I2 Salt Lake</name>
- <node id="n32">
- <hostname>node1.salt.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">962000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- <node id="n33">
- <hostname>node2.salt.internet2.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- <bw_limit units="kbps">1000000</bw_limit>
- </node>
- </site>
- <site id="s24">
- <name>UMKC</name>
- <node id="n48">
- <hostname>node1.umkc.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">999000</bw_unallocated>
- </node>
- <node id="n50">
- <hostname>node2.umkc.vini-veritas.net</hostname>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </node>
- </site>
- <link endpoints="s2 s12">
- <description>Princeton -- I2 New York</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s4 s5">
- <description>NLR Chicago -- NLR Houston</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s4 s6">
- <description>NLR Chicago -- NLR Atlanta</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s4 s7">
- <description>NLR Chicago -- NLR Seattle</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s4 s9">
- <description>NLR Chicago -- NLR New York</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s4 s10">
- <description>NLR Chicago -- NLR Wash DC</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s5 s6">
- <description>NLR Houston -- NLR Atlanta</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s5 s8">
- <description>NLR Houston -- NLR Los Angeles</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s6 s10">
- <description>NLR Atlanta -- NLR Wash DC</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s6 s14">
- <description>NLR Atlanta -- Georgia Tech</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s7 s8">
- <description>NLR Seattle -- NLR Los Angeles</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s9 s10">
- <description>NLR New York -- NLR Wash DC</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s11 s13">
- <description>I2 Chicago -- I2 Wash DC</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s11 s15">
- <description>I2 Chicago -- I2 Atlanta</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s11 s16">
- <description>I2 Chicago -- CESNET</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s11 s17">
- <description>I2 Chicago -- I2 Kansas City</description>
- <bw_unallocated units="kbps">987000</bw_unallocated>
- </link>
- <link endpoints="s12 s13">
- <description>I2 New York -- I2 Wash DC</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s13 s15">
- <description>I2 Wash DC -- I2 Atlanta</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s14 s15">
- <description>Georgia Tech -- I2 Atlanta</description>
- <bw_unallocated units="kbps">1000000</bw_unallocated>
- </link>
- <link endpoints="s15 s19">
- <description>I2 Atlanta -- I2 Houston</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s17 s19">
- <description>I2 Kansas City -- I2 Houston</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s17 s22">
- <description>I2 Kansas City -- I2 Salt Lake</description>
- <bw_unallocated units="kbps">987000</bw_unallocated>
- </link>
- <link endpoints="s17 s24">
- <description>I2 Kansas City -- UMKC</description>
- <bw_unallocated units="kbps">999000</bw_unallocated>
- </link>
- <link endpoints="s19 s20">
- <description>I2 Houston -- I2 Los Angeles</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s20 s21">
- <description>I2 Los Angeles -- I2 Seattle</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s20 s22">
- <description>I2 Los Angeles -- I2 Salt Lake</description>
- <bw_unallocated units="kbps">988000</bw_unallocated>
- </link>
- <link endpoints="s21 s22">
- <description>I2 Seattle -- I2 Salt Lake</description>
- <bw_unallocated units="kbps">987000</bw_unallocated>
- </link>
- </network>
-</RSpec>
+++ /dev/null
-from __future__ import with_statement
-from sfa.util.faults import *
-from xmlbuilder import XMLBuilder
-from lxml import etree
-import sys
-from sfa.plc.network import *
-from sfa.managers.vini.topology import PhysicalLinks
-
-# Taken from bwlimit.py
-#
-# See tc_util.c and http://physics.nist.gov/cuu/Units/binary.html. Be
-# warned that older versions of tc interpret "kbps", "mbps", "mbit",
-# and "kbit" to mean (in this system) "kibps", "mibps", "mibit", and
-# "kibit" and that if an older version is installed, all rates will
-# be off by a small fraction.
-suffixes = {
- "": 1,
- "bit": 1,
- "kibit": 1024,
- "kbit": 1000,
- "mibit": 1024*1024,
- "mbit": 1000000,
- "gibit": 1024*1024*1024,
- "gbit": 1000000000,
- "tibit": 1024*1024*1024*1024,
- "tbit": 1000000000000,
- "bps": 8,
- "kibps": 8*1024,
- "kbps": 8000,
- "mibps": 8*1024*1024,
- "mbps": 8000000,
- "gibps": 8*1024*1024*1024,
- "gbps": 8000000000,
- "tibps": 8*1024*1024*1024*1024,
- "tbps": 8000000000000
-}
-
-
-def get_tc_rate(s):
- """
- Parses an integer or a tc rate string (e.g., 1.5mbit) into bits/second
- """
-
- if type(s) == int:
- return s
- m = re.match(r"([0-9.]+)(\D*)", s)
- if m is None:
- return -1
- suffix = m.group(2).lower()
- if suffixes.has_key(suffix):
- return int(float(m.group(1)) * suffixes[suffix])
- else:
- return -1
-
-def format_tc_rate(rate):
- """
- Formats a bits/second rate into a tc rate string
- """
-
- if rate >= 1000000000 and (rate % 1000000000) == 0:
- return "%.0fgbit" % (rate / 1000000000.)
- elif rate >= 1000000 and (rate % 1000000) == 0:
- return "%.0fmbit" % (rate / 1000000.)
- elif rate >= 1000:
- return "%.0fkbit" % (rate / 1000.)
- else:
- return "%.0fbit" % rate
-
-
-class ViniNode(Node):
- def __init__(self, network, node, bps = 1000 * 1000000):
- Node.__init__(self, network, node)
- self.bps = bps
- self.links = set()
- self.name = self.hostname.replace('.vini-veritas.net', '')
-
- def get_link_id(self, remote):
- if self.id < remote.id:
- link = (self.id<<7) + remote.id
- else:
- link = (remote.id<<7) + self.id
- return link
-
- def get_iface_id(self, remote):
- if self.id < remote.id:
- iface = 1
- else:
- iface = 2
- return iface
-
- def get_virt_ip(self, remote):
- link = self.get_link_id(remote)
- iface = self.get_iface_id(remote)
- first = link >> 6
- second = ((link & 0x3f)<<2) + iface
- return "192.168.%d.%d" % (first, second)
-
- def get_virt_net(self, remote):
- link = self.get_link_id(remote)
- first = link >> 6
- second = (link & 0x3f)<<2
- return "192.168.%d.%d/30" % (first, second)
-
- def get_topo_rspec(self, link):
- if link.end1 == self:
- remote = link.end2
- elif link.end2 == self:
- remote = link.end1
- else:
- raise Error("Link does not connect to Node")
-
- my_ip = self.get_virt_ip(remote)
- remote_ip = remote.get_virt_ip(self)
- net = self.get_virt_net(remote)
- bw = format_tc_rate(link.bps)
- ipaddr = remote.get_primary_iface().ipv4
- return (remote.id, ipaddr, bw, my_ip, remote_ip, net)
-
- def add_link(self, link):
- self.links.add(link)
-
- # Assumes there is at most one Link between two sites
- def get_sitelink(self, node):
- site1 = self.network.sites[self.site_id]
- site2 = self.network.sites[node.site_id]
- sl = site1.links.intersection(site2.links)
- if len(sl):
- return sl.pop()
- return None
-
- def toxml(self, xml):
- slice = self.network.slice
- if self.whitelist and not self.sliver:
- if not slice or slice.id not in self.whitelist:
- return
-
- with xml.node(id = self.idtag):
- with xml.hostname:
- xml << self.hostname
- with xml.bw_unallocated(units="kbps"):
- xml << str(int(self.bps/1000))
- self.get_primary_iface().toxml(xml)
- if self.sliver:
- self.sliver.toxml(xml)
-
-
-class ViniSite(Site):
- def __init__(self, network, site):
- Site.__init__(self, network, site)
- self.links = set()
-
- def add_link(self, link):
- self.links.add(link)
-
-class ViniSlice(Slice):
- def assign_egre_key(self):
- tag = self.get_tag('egre_key')
- if not tag:
- try:
- key = self.network.free_egre_key()
- except:
- raise InvalidRSpec("ran out of EGRE keys!")
- tag = self.update_tag('egre_key', key, None, 'admin')
- return
-
- def turn_on_netns(self):
- tag = self.get_tag('netns')
- if (not tag) or (tag.value != '1'):
- tag = self.update_tag('netns', '1', None, 'admin')
- return
-
- def turn_off_netns(self):
- tag = self.get_tag('netns')
- if tag and (tag.value != '0'):
- tag.delete()
- return
-
- def add_cap_net_admin(self):
- tag = self.get_tag('capabilities')
- if tag:
- caps = tag.value.split(',')
- for cap in caps:
- if cap == "CAP_NET_ADMIN":
- newcaps = tag.value
- break
- else:
- newcaps = "CAP_NET_ADMIN," + tag.value
- self.update_tag('capabilities', newcaps, None, 'admin')
- else:
- tag = self.add_tag('capabilities', 'CAP_NET_ADMIN', None, 'admin')
- return
-
- def remove_cap_net_admin(self):
- tag = self.get_tag('capabilities')
- if tag:
- caps = tag.value.split(',')
- newcaps = []
- for cap in caps:
- if cap != "CAP_NET_ADMIN":
- newcaps.append(cap)
- if newcaps:
- value = ','.join(newcaps)
- self.update_tag('capabilities', value, None, 'admin')
- else:
- tag.delete()
- return
-
-class Link:
- def __init__(self, end1, end2, bps = 1000 * 1000000, parent = None):
- self.end1 = end1
- self.end2 = end2
- self.bps = bps
- self.parent = parent
- self.children = []
-
- end1.add_link(self)
- end2.add_link(self)
-
- if self.parent:
- self.parent.children.append(self)
-
- def toxml(self, xml):
- end_ids = "%s %s" % (self.end1.idtag, self.end2.idtag)
-
- if self.parent:
- with xml.vlink(endpoints=end_ids):
- with xml.description:
- xml << "%s -- %s" % (self.end1.name, self.end2.name)
- with xml.kbps:
- xml << str(int(self.bps/1000))
- else:
- with xml.link(endpoints=end_ids):
- with xml.description:
- xml << "%s -- %s" % (self.end1.name, self.end2.name)
- with xml.bw_unallocated(units="kbps"):
- xml << str(int(self.bps/1000))
- for child in self.children:
- child.toxml(xml)
-
-
-
-class ViniNetwork(Network):
- def __init__(self, api, type = "SFA"):
- Network.__init__(self, api, type)
- self.sitelinks = []
- self.nodelinks = []
-
- for (s1, s2) in PhysicalLinks:
- self.sitelinks.append(Link(self.sites[s1], self.sites[s2]))
-
- for t in self.tags:
- tag = self.tags[t]
- if tag.tagname == 'topo_rspec':
- node1 = self.nodes[tag.node_id]
- l = eval(tag.value)
- for (id, realip, bw, lvip, rvip, vnet) in l:
- allocbps = get_tc_rate(bw)
- node1.bps -= allocbps
- try:
- node2 = self.nodes[id]
- if node1.id < node2.id:
- sl = node1.get_sitelink(node2)
- sl.bps -= allocbps
- except:
- pass
-
- def lookupSiteLink(self, node1, node2):
- site1 = self.sites[node1.site_id]
- site2 = self.sites[node2.site_id]
- for link in self.sitelinks:
- if site1 == link.end1 and site2 == link.end2:
- return link
- if site2 == link.end1 and site1 == link.end2:
- return link
- return None
-
-
- """
- Check the requested topology against the available topology and capacity
- """
- def verifyTopology(self):
- for link in self.nodelinks:
- if link.bps <= 0:
- raise InvalidRSpec("must request positive bandwidth")
-
- n1 = link.end1
- n2 = link.end2
- sitelink = self.lookupSiteLink(n1, n2)
- if not sitelink:
- raise InvalidRSpec("nodes %s and %s are not adjacent" %
- (n1.idtag, n2.idtag))
- if sitelink.bps < link.bps:
- raise InvalidRSpec("not enough capacity between %s and %s" %
- (n1.idtag, n2.idtag))
-
- def __add_vlink(self, vlink, parent = None):
- n1 = n2 = None
- endpoints = vlink.get("endpoints")
- if endpoints:
- (end1, end2) = endpoints.split()
- n1 = self.lookupNode(end1)
- n2 = self.lookupNode(end2)
- elif parent:
- """ Try to infer the endpoints for the virtual link """
- site_endpoints = parent.get("endpoints")
- (n1, n2) = self.__infer_endpoints(site_endpoints)
- else:
- raise InvalidRSpec("no endpoints given")
-
- #print "Added virtual link: %s -- %s" % (n1.tag, n2.tag)
- bps = int(vlink.findtext("kbps")) * 1000
- sitelink = self.lookupSiteLink(n1, n2)
- if not sitelink:
- raise InvalidRSpec("nodes %s and %s are not adjacent" %
- (n1.idtag, n2.idtag))
- self.nodelinks.append(Link(n1, n2, bps, sitelink))
- return
-
- """
- Infer the endpoints of the virtual link. If the slice exists on
- only a single node at each end of the physical link, we'll assume that
- the user wants the virtual link to terminate at these nodes.
- """
- def __infer_endpoints(self, endpoints):
- n = []
- ends = endpoints.split()
- for end in ends:
- found = 0
- site = self.lookupSite(end)
- for id in site.node_ids:
- if id in self.nodedict:
- n.append(self.nodedict[id])
- found += 1
- if found != 1:
- raise InvalidRSpec("could not infer endpoint for site %s" %
- site.idtag)
- #print "Inferred endpoints: %s %s" % (n[0].idtag, n[1].idtag)
- return n
-
- def addRSpec(self, xml, schema = None):
- Network.addRSpec(self, xml, schema)
- self.nodedict = {}
- for node in self.nodesWithSlivers():
- self.nodedict[node.id] = node
-
- # Find vlinks under link elements
- for vlink in self.rspec.iterfind("./network/link/vlink"):
- link = vlink.getparent()
- self.__add_vlink(vlink, link)
-
- # Find vlinks that specify endpoints
- for vlink in self.rspec.iterfind("./request/vlink[@endpoints]"):
- self.__add_vlink(vlink)
-
-
- def addSlice(self):
- Network.addSlice(self)
-
- for node in self.slice.get_nodes():
- linktag = self.slice.get_tag('topo_rspec', node)
- if linktag:
- l = eval(linktag.value)
- for (id, realip, bw, lvip, rvip, vnet) in l:
- if node.id < id:
- bps = get_tc_rate(bw)
- remote = self.lookupNode(id)
- sitelink = self.lookupSiteLink(node, remote)
- self.nodelinks.append(Link(node,remote,bps,sitelink))
-
-
- def updateSliceTags(self):
- slice = self.slice
-
- tag = slice.update_tag('vini_topo', 'manual', None, 'admin')
- slice.assign_egre_key()
- slice.turn_on_netns()
- slice.add_cap_net_admin()
-
- for node in self.nodesWithSlivers():
- linkdesc = []
- for link in node.links:
- linkdesc.append(node.get_topo_rspec(link))
- if linkdesc:
- topo_str = "%s" % linkdesc
- tag = slice.update_tag('topo_rspec', topo_str, node, 'admin')
-
- # Expire the un-updated topo_rspec tags
- for tag in self.getSliceTags():
- if tag.tagname in ['topo_rspec']:
- if not tag.was_updated():
- tag.delete()
-
- Network.updateSliceTags(self)
-
- """
- Find a free EGRE key
- """
- def free_egre_key(self):
- used = set()
- for tag in self.getSliceTags():
- if tag.tagname == 'egre_key':
- used.add(int(tag.value))
-
- for i in range(1, 256):
- if i not in used:
- key = i
- break
- else:
- raise KeyError("No more EGRE keys available")
-
- return str(key)
-
- """
- Produce XML directly from the topology specification.
- """
- def toxml(self):
- xml = XMLBuilder(format = True, tab_step = " ")
- with xml.RSpec(type=self.type):
- if self.slice:
- element = xml.network(name=self.api.hrn, slice=self.slice.hrn)
- else:
- element = xml.network(name=self.api.hrn)
-
- with element:
- if self.slice:
- self.slice.toxml(xml)
- for site in self.getSites():
- site.toxml(xml)
- for link in self.sitelinks:
- link.toxml(xml)
-
- header = '<?xml version="1.0"?>\n'
- return header + str(xml)
-
- """
- Create a dictionary of ViniSite objects keyed by site ID
- """
- def get_sites(self, api):
- tmp = []
- for site in api.driver.GetSites({'peer_id': None}):
- t = site['site_id'], ViniSite(self, site)
- tmp.append(t)
- return dict(tmp)
-
-
- """
- Create a dictionary of ViniNode objects keyed by node ID
- """
- def get_nodes(self, api):
- tmp = []
- for node in api.driver.GetNodes({'peer_id': None}):
- t = node['node_id'], ViniNode(self, node)
- tmp.append(t)
- return dict(tmp)
-
- """
- Return a ViniSlice object for a single slice
- """
- def get_slice(self, api, hrn):
- slicename = hrn_to_pl_slicename(hrn)
- slice = api.driver.GetSlices([slicename])
- if slice:
- self.slice = ViniSlice(self, slicename, slice[0])
- return self.slice
- else:
- return None
-
-
-
#!/usr/bin/python
-from sfa.util.xrn import hrn_to_urn, urn_to_hrn
-from sfa.util.plxrn import PlXrn, hostname_to_urn, hrn_to_pl_slicename, urn_to_sliver_id
+from sfa.util.xrn import hrn_to_urn, urn_to_hrn, urn_to_sliver_id
+from sfa.util.plxrn import PlXrn, hostname_to_urn, hrn_to_pl_slicename
from sfa.rspecs.rspec import RSpec
from sfa.rspecs.elements.hardware_type import HardwareType
+from sfa.rspecs.elements.node import Node
from sfa.rspecs.elements.link import Link
from sfa.rspecs.elements.login import Login
+from sfa.rspecs.elements.location import Location
from sfa.rspecs.elements.interface import Interface
from sfa.rspecs.elements.services import Services
from sfa.rspecs.elements.pltag import PLTag
def get_links(self, filter={}):
if not self.api.config.SFA_AGGREGATE_TYPE.lower() == 'vini':
- return
+ return []
topology = Topology()
links = {}
return (slice, slivers)
- def get_nodes(self, slice=None):
+ def get_nodes (self, slice=None,slivers=[]):
filter = {}
+ tags_filter = {}
if slice and 'node_ids' in slice and slice['node_ids']:
filter['node_id'] = slice['node_ids']
+ tags_filter=filter.copy()
filter.update({'peer_id': None})
nodes = self.api.driver.GetNodes(filter)
# get interfaces
interfaces = self.get_interfaces({'interface_id':interface_ids})
# get slivers
- slivers = self.get_slivers(slice)
+ #
+ # thierry: no get_slivers, we have slivers as a result of
+ # get_slice_and_slivers passed as an argument
+ #
+# slivers = self.get_slivers(slice)
+
# get tags
- node_tags = self.get_node_tags({'node_id': node_ids})
+ node_tags = self.get_node_tags(tags_filter)
# get initscripts
pl_initscripts = self.get_pl_initscripts()
if not slice or slice['slice_id'] not in node['slice_ids_whitelist']:
continue
rspec_node = Node()
+ # xxx how to retrieve site['login_base']
+ site_id=node['site_id']
+ site=sites_dict[site_id]
rspec_node['component_id'] = hostname_to_urn(self.api.hrn, site['login_base'], node['hostname'])
rspec_node['component_name'] = node['hostname']
rspec_node['component_manager_id'] = self.api.hrn
rspec_node['interfaces'] = []
for if_id in node['interface_ids']:
interface = Interface(interfaces[if_id])
- interface['ipv4'] = interface['ip']
+ interface['ipv4'] = interface['ipv4']
rspec_node['interfaces'].append(interface)
tags = [PLTag(node_tags[tag_id]) for tag_id in node['node_tag_ids']]
rspec_node['tags'] = tags
slice, slivers = self.get_slice_and_slivers(slice_xrn)
rspec = RSpec(version=rspec_version, user_options=self.user_options)
- rspec.version.add_nodes(self.get_nodes(slice, slivers))
+ rspec.version.add_nodes(self.get_nodes(slice), slivers)
rspec.version.add_links(self.get_links(slice))
# add sliver defaults
except:
self.api.logger.log_exc('Failed to add/remove slice from nodes')
+ def free_egre_key(self):
+ used = set()
+ for tag in self.api.driver.GetSliceTags({'tagname': 'egre_key'}):
+ used.add(int(tag['value']))
+
+ for i in range(1, 256):
+ if i not in used:
+ key = i
+ break
+ else:
+ raise KeyError("No more EGRE keys available")
+
+ return str(key)
+
def verify_slice_links(self, slice, links, aggregate):
# nodes is undefined here
if not links:
- return
+ return
+
+ slice_tags = []
+
+ # set egre key
+ slice_tags.append({'name': 'egre_key', 'value': self.free_egre_key()})
+
+ # set netns
+ slice_tags.append({'name': 'netns', 'value': '1'})
+
+ # set cap_net_admin
+ # need to update the attribute string?
+ slice_tags.append({'name': 'capabilities', 'value': 'CAP_NET_ADMIN'})
for link in links:
# get the ip address of the first node in the link
if1 = aggregate.interfaces[node['interface_ids'][0]]
ipaddr = if1['ip']
topo_rspec = VLink.get_topo_rspec(link, ipaddr)
- self.api.driver.AddSliceTag(slice['name'], 'topo_rspec', str([topo_rspec]), node_id)
+ # set topo_rspec tag
+ slice_tags.append({'name': 'topo_rspec', 'value': str([topo_rspec]), 'node_id': node_id})
+ # set vini_topo tag
+ slice_tags.append({'name': 'vini_topo', 'value': 'manual', 'node_id': node_id})
+ #self.api.driver.AddSliceTag(slice['name'], 'topo_rspec', str([topo_rspec]), node_id)
+
+ self.verify_slice_attributes(slice, slice_tags, append=True, admin=True)
except:
pass
- def verify_slice_attributes(self, slice, requested_slice_attributes):
+ def verify_slice_attributes(self, slice, requested_slice_attributes, append=False, admin=False):
# get list of attributes users ar able to manage
- slice_attributes = self.api.driver.GetTagTypes({'category': '*slice*', '|roles': ['user']})
+ filter = {'category': '*slice*'}
+ if not admin:
+ filter['|roles'] = ['user']
+ slice_attributes = self.api.driver.GetTagTypes(filter)
valid_slice_attribute_names = [attribute['tagname'] for attribute in slice_attributes]
# get sliver attributes
attribute_found=True
break
- if not attribute_found:
+ if not attribute_found and not append:
removed_slice_attributes.append(slice_tag)
# get attributes that should be added:
from sfa.rspecs.elements.disk_image import DiskImage
from sfa.rspecs.elements.interface import Interface
from sfa.rspecs.elements.bwlimit import BWlimit
-from sfa.rspecs.elements.pl_tag import PLTag
+from sfa.rspecs.elements.pltag import PLTag
from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
from sfa.rspecs.elements.versions.pgv2Service import PGv2Service
for services_elem in services_elems:
# services element has no useful info, but the child elements do
for child in services_elem.iterchildren():
+ pass
# set interfaces
interface_elems = node_elem.xpath(PGv2Node.elements['interface'].path, xml.namespaces)
class PGv2Services:
elements = {
'services': RSpecElement(RSpecElements.SERVICES, '//default:services | //services'),
- 'install': RSpecElement(RspecElements.INSTALL, './default:install | ./install'),
- 'execute': RSpecElement(RspecElements.EXECUTE, './default:execute | ./execute'),
- 'login': RSpecElement(RspecElements.LOGIN, './default:login | ./login'),
+ 'install': RSpecElement(RSpecElements.INSTALL, './default:install | ./install'),
+ 'execute': RSpecElement(RSpecElements.EXECUTE, './default:execute | ./execute'),
+ 'login': RSpecElement(RSpecElements.LOGIN, './default:login | ./login'),
}
@staticmethod
from sfa.rspecs.elements.disk_image import DiskImage
from sfa.rspecs.elements.interface import Interface
from sfa.rspecs.elements.bwlimit import BWlimit
-from sfa.rspecs.elements.pl_tag import PLTag
+from sfa.rspecs.elements.pltag import PLTag
from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
from sfa.rspecs.elements.versions.sfav1Network import SFAv1Network
from sfa.rspecs.elements.versions.pgv2Services import PGv2Services
from types import StringTypes
from lxml import etree
from StringIO import StringIO
-
from sfa.util.faults import InvalidXML
class XpathFilter:
@staticmethod
+
+ def filter_value(key, value):
+ xpath = ""
+ if isinstance(value, str):
+ if '*' in value:
+ value = value.replace('*', '')
+ xpath = 'contains(%s, "%s")' % (key, value)
+ else:
+ xpath = '%s="%s"' % (key, value)
+ return xpath
+
+ @staticmethod
def xpath(filter={}):
xpath = ""
if filter:
else:
key = '@'+key
if isinstance(value, str):
- filter_list.append('%s="%s"' % (key, value))
+ filter_list.append(XpathFilter.filter_value(key, value))
elif isinstance(value, list):
- filter_list.append('contains("%s", %s)' % (' '.join(map(str, value)), key))
+ stmt = ' or '.join([XpathFilter.filter_value(key, str(val)) for val in value])
+ filter_list.append(stmt)
if filter_list:
xpath = ' and '.join(filter_list)
xpath = '[' + xpath + ']'
return xpath
+class XmlNode:
+ def __init__(self, node, namespaces):
+ self.node = node
+ self.namespaces = namespaces
+ self.attrib = node.attrib
+
+ def xpath(self, xpath, namespaces=None):
+ if not namespaces:
+ namespaces = self.namespaces
+ return self.node.xpath(xpath, namespaces=namespaces)
+
+ def add_element(name, *args, **kwds):
+ element = etree.SubElement(name, args, kwds)
+ return XmlNode(element, self.namespaces)
+
+ def remove_elements(name):
+ """
+ Removes all occurences of an element from the tree. Start at
+ specified root_node if specified, otherwise start at tree's root.
+ """
+
+ if not element_name.startswith('//'):
+ element_name = '//' + element_name
+ elements = self.node.xpath('%s ' % name, namespaces=self.namespaces)
+ for element in elements:
+ parent = element.getparent()
+ parent.remove(element)
+
+ def set(self, key, value):
+ self.node.set(key, value)
+
+ def set_text(self, text):
+ self.node.text = text
+
+ def unset(self, key):
+ del self.node.attrib[key]
+
+ def toxml(self):
+ return etree.tostring(self.node, encoding='UTF-8', pretty_print=True)
+
+ def __str__(self):
+ return self.toxml()
+
class XML:
- def __init__(self, xml=None):
+ def __init__(self, xml=None, namespaces=None):
self.root = None
- self.namespaces = None
+ self.namespaces = namespaces
self.default_namespace = None
self.schema = None
if isinstance(xml, basestring):
self.parse_xml(xml)
- elif isinstance(xml, etree._ElementTree):
- self.root = xml.getroot()
- elif isinstance(xml, etree._Element):
- self.root = xml
+ if isinstance(xml, XmlNode):
+ self.root = xml
+ self.namespces = xml.namespaces
+ elif isinstance(xml, etree._ElementTree) or isinstance(xml, etree._Element):
+ self.parse_xml(etree.tostring(xml))
def parse_xml(self, xml):
"""
tree = etree.parse(StringIO(xml), parser)
except Exception, e:
raise InvalidXML(str(e))
- self.root = tree.getroot()
+ root = tree.getroot()
+ self.namespaces = dict(root.nsmap)
# set namespaces map
- self.namespaces = dict(self.root.nsmap)
if 'default' not in self.namespaces and None in self.namespaces:
# If the 'None' exist, then it's pointing to the default namespace. This makes
# it hard for us to write xpath queries for the default naemspace because lxml
else:
self.namespaces['default'] = 'default'
+
+ self.root = XmlNode(root, self.namespaces)
# set schema
for key in self.root.attrib.keys():
if key.endswith('schemaLocation'):
namespaces = self.namespaces
return self.root.xpath(xpath, namespaces=namespaces)
- def set(self, key, value):
- return self.root.set(key, value)
+ def set(self, key, value, node=None):
+ if not node:
+ node = self.root
+ return node.set(key, value)
- def add_attribute(self, elem, name, value):
- """
- Add attribute to specified etree element
- """
- opt = etree.SubElement(elem, name)
- opt.text = value
+ def remove_attribute(self, name, node=None):
+ if not node:
+ node = self.root
+ node.remove_attribute(name)
+
def add_element(self, name, attrs={}, parent=None, text=""):
"""
if isinstance(attrs, dict):
for attr in attrs:
element.set(attr, attrs[attr])
- return element
+ return XmlNode(element, self.namespaces)
- def remove_attribute(self, elem, name, value):
- """
- Removes an attribute from an element
- """
- if elem is not None:
- opts = elem.iterfind(name)
- if opts is not None:
- for opt in opts:
- if opt.text == value:
- elem.remove(opt)
-
- def remove_element(self, element_name, root_node = None):
+ def remove_elements(self, name, node = None):
"""
Removes all occurences of an element from the tree. Start at
specified root_node if specified, otherwise start at tree's root.
"""
- if not root_node:
- root_node = self.root
-
- if not element_name.startswith('//'):
- element_name = '//' + element_name
+ if not node:
+ node = self.root
- elements = root_node.xpath('%s ' % element_name, namespaces=self.namespaces)
- for element in elements:
- parent = element.getparent()
- parent.remove(element)
+ node.remove_elements(name)
def attributes_list(self, elem):
# convert a list of attribute tags into list of tuples