17e4f7ddd814647bb8aa9bd1c6c22618e0e76efe
[sfa.git] / sfa / rspecs / sfa_rspec.py
1 #!/usr/bin/python 
2 from lxml import etree
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  
8
9
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'
17  
18
19 class SfaRSpec(RSpec):
20     xml = None
21     header = '<?xml version="1.0"?>\n'
22     namespaces = {}
23
24     ########
25     # Parser
26     ########
27     def get_networks(self):
28         network = None 
29         return = self.xml.xpath(xpath_network_names, self.namespaces)
30
31     def get_network_elements(self):
32         return self.xml.xpath(xpath_networks, self.namespaces)
33
34     def get_node_elements(self):
35         return self.xml.xpath(xpath_nodes, self.namespaces)
36
37     def get_nodes(self, network=None, nodes_with_slivers=False):
38         if network == None:
39             nodes = self.xml.xpath(xpath_nodes_hostnames, self.namespaces)
40         else:
41             nodes = self.xml.xpath(xpath_nodes_with_network % network, self.namespaces)
42         return nodes
43
44     #########
45     # Builder
46     ########
47
48     def add_nodes(self, nodes, check_for_dupes=False):
49         if not isinstance(nodes, list):
50             nodes = [nodes]
51         for node in nodes:
52             urn = ""
53             if check_for_dupes and \
54               self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, self.namespaces):
55                 # node already exists
56                 continue
57                 
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')
64             
65
66     def add_slivers(self, slivers, check_for_dupes=False): 
67         pass
68
69     def add_links(self, links, check_for_dupes=False):
70         pass
71
72
73 if __name__ == '__main__':
74     rspec = SfaRSpec()
75     rspec.add_nodes([1])
76     print rspec