Merge branch 'master' of ssh://git.planet-lab.org/git/sfa
[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 _ad_version = {'type':  'ProtoGENI',
11             'version': '2',
12             'schema': 'http://www.protogeni.net/resources/rspec/2/ad.xsd',
13             'namespace': 'http://www.protogeni.net/resources/rspec/2',
14             'extensions':  [
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
20 _request_version = {'type':  'ProtoGENI',
21             'version': '2',
22             'schema': 'http://www.protogeni.net/resources/rspec/2/request.xsd',
23             'namespace': 'http://www.protogeni.net/resources/rspec/2',
24             'extensions':  [
25                 'http://www.protogeni.net/resources/rspec/ext/gre-tunnel/1',
26                 'http://www.protogeni.net/resources/rspec/ext/other-ext/3'
27             ]
28 }
29 pg_rspec_ad_version = RSpecVersion(_ad_version)
30 pg_rspec_request_version = RSpecVersion(_request_version)
31
32 class PGRSpec(RSpec):
33     xml = None
34     header = '<?xml version="1.0"?>\n'
35     template = """<rspec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.protogeni.net/resources/rspec/2" xsi:schemaLocation="http://www.protogeni.net/resources/rspec/2 http://www.protogeni.net/resources/rspec/2/%(rspec_type)s.xsd"></rspec>"""
36
37     def __init__(self, rspec="", namespaces={}, type=None):
38         if not type:
39             type = 'advertisement'
40         self.type = type
41
42         if type == 'advertisement':
43             self.version = pg_rspec_ad_version
44             rspec_type = 'ad'
45         else:
46             self.version = pg_rspec_request_version
47             rspec_type = type
48         
49         self.template = self.template % {'rspec_type': rspec_type}
50
51         if not namespaces:
52             self.namespaces = {'rspecv2': self.version['namespace']}
53         else:
54             self.namespaces = namespaces 
55
56         if rspec:
57             self.parse_rspec(rspec, self.namespaces)
58         else: 
59             self.create()
60
61     def create(self):
62         RSpec.create(self)
63         if self.type:
64             self.xml.set('type', self.type) 
65        
66     def get_network(self):
67         network = None 
68         nodes = self.xml.xpath('//rspecv2:node[@component_manager_uuid][1]', namespaces=self.namespaces)
69         if nodes:
70             network  = nodes[0].get('component_manager_uuid')
71         return network
72
73     def get_networks(self):
74         networks = self.xml.xpath('//rspecv2:node[@component_manager_uuid]/@component_manager_uuid', namespaces=self.namespaces)
75         return set(networks)
76
77     def get_node_elements(self):
78         nodes = self.xml.xpath('//rspecv2:node | //node', namespaces=self.namespaces)
79         return nodes
80
81     def get_nodes(self, network=None):
82         xpath = '//rspecv2:node[@component_name]/@component_name | //node[@component_name]/@component_name'
83         return self.xml.xpath(xpath, namespaces=self.namespaces) 
84
85     def get_nodes_with_slivers(self, network=None):
86         if network:
87             return self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_name' % network, namespaces=self.namespaces)
88         else:
89             return self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_name', namespaces=self.namespaces)
90
91     def get_nodes_without_slivers(self, network=None):
92         pass
93
94     def add_nodes(self, nodes, check_for_dupes=False):
95         if not isinstance(nodes, list):
96             nodes = [nodes]
97         for node in nodes:
98             urn = ""
99             if check_for_dupes and \
100               self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
101                 # node already exists
102                 continue
103                 
104             node_tag = etree.SubElement(self.xml, 'node', exclusive='false')
105             if 'network_urn' in node:
106                 node_tag.set('component_manager_id', node['network_urn'])
107             if 'urn' in node:
108                 node_tag.set('component_id', node['urn'])
109             if 'hostname' in node:
110                 node_tag.set('component_name', node['hostname'])
111             # TODO: should replace plab-pc with pc model 
112             node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='plab-pc')
113             node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='pc')
114             available_tag = etree.SubElement(node_tag, 'available', now='true')
115             location_tag = etree.SubElement(node_tag, 'location', country="us")
116             if 'site' in node:
117                 if 'longitude' in node['site']:
118                     location_tag.set('longitude', str(node['site']['longitude']))
119                 if 'latitude' in node['site']:
120                     location_tag.set('latitude', str(node['site']['latitude']))
121             #if 'interfaces' in node:
122             
123
124     def add_slivers(self, slivers, sliver_urn=None, no_dupes=False): 
125         slivers = self.__process_slivers(slivers)
126         nodes_with_slivers = self.get_nodes_with_slivers()
127         for sliver in slivers:
128             hostname = sliver['hostname']
129             if hostname in nodes_with_slivers:
130                 continue
131             nodes = self.xml.xpath('//rspecv2:node[@component_name="%s"] | //node[@component_name="%s"]' % (hostname, hostname), namespaces=self.namespaces)
132             if nodes:
133                 node = nodes[0]
134                 node.set('client_id', hostname)
135                 if sliver_urn:
136                     node.set('sliver_id', sliver_urn)
137                 etree.SubElement(node, 'sliver_type', name='plab-vnode')
138
139     def add_interfaces(self, interfaces, no_dupes=False):
140         pass
141
142     def add_links(self, links, no_dupes=False):
143         pass
144
145
146     def merge(self, in_rspec):
147         """
148         Merge contents for specified rspec with current rspec
149         """
150         
151         # just copy over all the child elements under the root element
152         tree = etree.parse(StringIO(in_rspec))
153         root = tree.getroot()
154         for child in root.getchildren():
155             self.xml.append(child)
156                   
157     def cleanup(self):
158         # remove unncecessary elements, attributes
159         if self.type in ['request', 'manifest']:
160             # remove nodes without slivers
161             nodes = self.get_node_elements()
162             for node in nodes:
163                 delete = True
164                 hostname = node.get('component_name')
165                 parent = node.getparent()
166                 children = node.getchildren()
167                 for child in children:
168                     if child.tag.endswith('sliver_type'):
169                         delete = False
170                 if delete:
171                     parent.remove(node)
172
173             # remove 'available' element from remaining node elements
174             self.remove_element('//rspecv2:available | //available')
175
176 if __name__ == '__main__':
177     rspec = PGRSpec()
178     rspec.add_nodes([1])
179     print rspec