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