bdb1716eaf6e58322542d56b12679be19dce949b
[sfa.git] / sfa / rspecs / pg_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 class PGRSpec(RSpec):
11     header = '<?xml version="1.0"?>\n'
12     template = """
13 <rspec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.protogeni.net/resources/rspec/0.2" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/0.2 http://www.protogeni.net/resources/rspec/0.2/ad.xsd">\n
14 </rspec>
15 """
16     namespaces = {'rspecv2':'http://www.protogeni.net/resources/rspec/0.2',
17                   'xsi': 'http://www.w3.org/2001/XMLSchema-instance'
18                  }
19     schemas =  {'xsi': 'http://www.protogeni.net/resources/rspec/0.2 http://www.protogeni.net/resources/rspec/0.2/ad.xsd'
20             }
21     format = 'pg'
22     xml = None
23
24     def get_network(self):
25         network = None 
26         nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
27         if nodes:
28             network  = nodes[0].get('component_manager_uuid')
29         return network
30
31     def get_networks(self):
32         networks = self.xml.xpath('//rspecv2:node[@component_manager_uuid]/@component_manager_uuid')
33         return set(networks)
34
35     def get_node_elements(self):
36         nodes = self.xml.xpath('//rspecv2:node', namespaces=self.namespaces)
37         return nodes
38
39     def get_nodes(self, network=None):
40         return self.xml.xpath('//rspecv2:node[@component_uuid]/@component_uuid', namespaces=self.namespaces) 
41
42     def get_nodes_with_slivers(self, network=None):
43         if network:
44             return self.xml.xpath('//node[@component_manager_uuid="%s"][sliver_type]/@component_uuid' % network, namespaces=self.namespaces)
45         else:
46             return self.xml.xpath('//node[sliver_type]/@component_uuid' % network, namespaces=self.namespaces)
47
48     def get_nodes_without_slivers(self, network=None):
49         pass
50
51     def add_nodes(self, nodes, check_for_dupes=False):
52         if not isinstance(nodes, list):
53             nodes = [nodes]
54         for node in nodes:
55             urn = ""
56             if check_for_dupes and \
57               self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
58                 # node already exists
59                 continue
60                 
61             node_tag = etree.SubElement(self.xml, 'node')
62             if 'network' in node:
63                 node_tag.set('component_manager_id', node['network'])
64             if 'urn' in node:
65                 node_tag.set('component_id', node['urn'])
66             if 'hostname' in node:
67                 node_tag.set('name', node['hostname'])
68             node_type_tag = etree.SubElement(node_tag, 'node_type', type_name='pcvm', type_slots='100')
69             available_tag = etree.SubElement(node_tag, 'available').text = 'true'
70             exclusive_tag = etree.SubElement(node_tag, 'exclusive').text = 'false'
71             location_tag = etree.SubElement(node_tag, 'location', location="US")
72             if 'site' in node:
73                 if 'longitude' in node['site']:
74                     location_tag.set('longitude', str(node['site']['longitude']))
75                 if 'latitude' in node['site']:
76                     location_tag.set('latitude', str(node['site']['latitude']))
77             #if 'interfaces' in node:
78             
79
80     def add_slivers(self, slivers, check_for_dupes=False): 
81         pass
82
83     def add_interfaces(self, interfaces, check_for_dupes=False):
84         pass
85
86     def add_links(self, links, check_for_dupes=False):
87         pass
88
89
90 if __name__ == '__main__':
91     rspec = PGRSpec()
92     rspec.add_nodes([1])
93     print rspec