3 from StringIO import StringIO
4 from sfa.rspecs.rspec import RSpec
5 from sfa.util.xrn import *
6 from sfa.util.plxrn import hostname_to_urn
7 from sfa.util.config import Config
10 # define some useful xpath queries for this rspec
11 xpath_nodes = '//node'
12 xpath_nodes_hostnames = '//node/hostname/text()'
13 xpath_nodes_with_hostname = '//node[hostname="%s"]/hostname/text()'
14 xpath_nodes_with_network = '//network[@name="%s"]//node/hostname/text()'
15 xpath_networks = '//network'
16 xpath_networks_names = '//network[@name]/@name'
19 class SfaRSpec(RSpec):
21 header = '<?xml version="1.0"?>\n'
27 def get_networks(self):
29 return = self.xml.xpath(xpath_network_names, self.namespaces)
31 def get_network_elements(self):
32 return self.xml.xpath(xpath_networks, self.namespaces)
34 def get_node_elements(self):
35 return self.xml.xpath(xpath_nodes, self.namespaces)
37 def get_nodes(self, network=None, nodes_with_slivers=False):
39 nodes = self.xml.xpath(xpath_nodes_hostnames, self.namespaces)
41 nodes = self.xml.xpath(xpath_nodes_with_network % network, self.namespaces)
48 def add_nodes(self, nodes, check_for_dupes=False):
49 if not isinstance(nodes, list):
53 if check_for_dupes and \
54 self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, self.namespaces):
58 node_tag = etree.SubElement(self.xml, 'node')
59 node_type_tag = etree.SubElement(node_tag, 'node_type', type_name='pcvm', type_slots='100')
60 available_tag = etree.SubElement(node_tag, 'available').text = 'true'
61 exclusive_tag = etree.SubElement(node_tag, 'exclusive').text = 'false'
62 location_tag = etree.SubElement(node_tag, 'location')
63 interface_tag = etree.SubElement(node_tag, 'interface')
66 def add_slivers(self, slivers, check_for_dupes=False):
69 def add_links(self, links, check_for_dupes=False):
73 if __name__ == '__main__':