add support for merging PG rspecs
[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 from sfa.rspecs.rspec_version import RSpecVersion 
9
10 _version = {'type':  'protogeni',
11             'version': '2',
12             'schema': 'http://www.protogeni.net/resources/rspec/2/request.xsd',
13             'namespace': 'http://www.protogeni.net/resources/rspec/2',
14             'extenstions':  [
15                 'http://www.protogeni.net/resources/rspec/ext/gre-tunnel/1',
16                 'http://www.protogeni.net/resources/rspec/ext/other-ext/3'
17             ]
18 }
19 pg_rspec_version = RSpecVersion(_version)
20
21 class PGRSpec(RSpec):
22     xml = None
23     header = '<?xml version="1.0"?>\n'
24     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>"""
25     version = pg_rspec_version
26     namespaces = {'rspecv2': version['namespace']}
27
28     def get_network(self):
29         network = None 
30         nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
31         if nodes:
32             network  = nodes[0].get('component_manager_uuid')
33         return network
34
35     def get_networks(self):
36         networks = self.xml.xpath('//rspecv2:node[@component_manager_uuid]/@component_manager_uuid', namespaces=self.namespaces)
37         return set(networks)
38
39     def get_node_elements(self):
40         nodes = self.xml.xpath('//rspecv2:node', namespaces=self.namespaces)
41         return nodes
42
43     def get_nodes(self, network=None):
44         return self.xml.xpath('//rspecv2:node[@component_name]/@component_name', namespaces=self.namespaces) 
45
46     def get_nodes_with_slivers(self, network=None):
47         if network:
48             return self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_name' % network, namespaces=self.namespaces)
49         else:
50             return self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_name', namespaces=self.namespaces)
51
52     def get_nodes_without_slivers(self, network=None):
53         pass
54
55     def add_nodes(self, nodes, check_for_dupes=False):
56         if not isinstance(nodes, list):
57             nodes = [nodes]
58         for node in nodes:
59             urn = ""
60             if check_for_dupes and \
61               self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
62                 # node already exists
63                 continue
64                 
65             node_tag = etree.SubElement(self.xml, 'node')
66             if 'network_urn' in node:
67                 node_tag.set('component_manager_id', node['network_urn'])
68             if 'urn' in node:
69                 node_tag.set('component_id', node['urn'])
70             if 'hostname' in node:
71                 node_tag.set('component_name', node['hostname'])
72             node_type_tag = etree.SubElement(node_tag, 'node_type', type_name='pcvm', type_slots='100')
73             available_tag = etree.SubElement(node_tag, 'available').text = 'true'
74             exclusive_tag = etree.SubElement(node_tag, 'exclusive').text = 'false'
75             location_tag = etree.SubElement(node_tag, 'location', location="US")
76             if 'site' in node:
77                 if 'longitude' in node['site']:
78                     location_tag.set('longitude', str(node['site']['longitude']))
79                 if 'latitude' in node['site']:
80                     location_tag.set('latitude', str(node['site']['latitude']))
81             #if 'interfaces' in node:
82             
83
84     def add_slivers(self, slivers, check_for_dupes=False): 
85         if not isinstance(slivers, list):
86             slivers = [slivers]
87
88         nodes_with_slivers = self.get_nodes_with_slivers()
89         for sliver in slivers:
90             hostname = sliver['hostname']
91             if hostname in nodes_with_slivers:
92                 continue
93             nodes = self.xml.xpath('//rspecv2:node[@component_name="%s"] | //node[@component_name="%s"]' % (hostname, hostname), namespaces=self.namespaces)
94             if nodes:
95                 node = nodes[0]
96                 node.set('client_id', hostname)
97                 etree.SubElement(node, 'sliver_type', name='planetlab-vnode')
98
99     def add_interfaces(self, interfaces, check_for_dupes=False):
100         pass
101
102     def add_links(self, links, check_for_dupes=False):
103         pass
104
105
106     def merge(self, in_rspec):
107         """
108         Merge contents for specified rspec with current rspec
109         """
110         
111         # just copy over all the child elements under the root element
112         tree = etree.parse(StringIO(in_rspec))
113         root = tree.getroot()
114         for child in root.getchildren():
115             self.xml.append(child)
116                   
117     
118
119 if __name__ == '__main__':
120     rspec = PGRSpec()
121     rspec.add_nodes([1])
122     print rspec