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