0f9b8e2d78121adcd5e6259a4e8791f166fc17d9
[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, xrn_to_hostname
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"/>'
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_id | //node[@component_name]/@component_id'
83         nodes = self.xml.xpath(xpath, namespaces=self.namespaces)
84         nodes = [xrn_to_hostname(node) for node in nodes]
85         return nodes 
86
87     def get_nodes_with_slivers(self, network=None):
88         if network:
89             nodes = self.xml.xpath('//rspecv2:node[@component_manager_id="%s"][sliver_type]/@component_id' % network, namespaces=self.namespaces)
90         else:
91             nodes = self.xml.xpath('//rspecv2:node[rspecv2:sliver_type]/@component_id', namespaces=self.namespaces)
92         nodes = [xrn_to_hostname(node) for node in nodes]
93         return nodes
94
95     def get_nodes_without_slivers(self, network=None):
96         pass
97    
98     def get_slice_attributes(self, network=None):
99         pass
100
101     def get_default_sliver_attributes(self, network=None):
102         pass 
103
104     def add_default_sliver_attribute(self, name, value, network=None):
105         pass
106
107     def add_nodes(self, nodes, check_for_dupes=False):
108         if not isinstance(nodes, list):
109             nodes = [nodes]
110         for node in nodes:
111             urn = ""
112             if check_for_dupes and \
113               self.xml.xpath('//rspecv2:node[@component_uuid="%s"]' % urn, namespaces=self.namespaces):
114                 # node already exists
115                 continue
116                 
117             node_tag = etree.SubElement(self.xml, 'node', exclusive='false')
118             if 'network_urn' in node:
119                 node_tag.set('component_manager_id', node['network_urn'])
120             if 'urn' in node:
121                 node_tag.set('component_id', node['urn'])
122             if 'hostname' in node:
123                 node_tag.set('component_name', node['hostname'])
124             # TODO: should replace plab-pc with pc model 
125             node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='plab-pc')
126             node_type_tag = etree.SubElement(node_tag, 'hardware_type', name='pc')
127             available_tag = etree.SubElement(node_tag, 'available', now='true')
128             sliver_type_tag = etree.SubElement(node_tag, 'sliver_type', name='plab-vnode')
129             # protogeni uses the <sliver_type> tag to identify the types of
130             # vms available at the node. 
131             # only add location tag if longitude and latitude are not null
132             if 'site' in node:
133                 longitude = node['site'].get('longitude', None)
134                 latitude = node['site'].get('latitude', None)
135                 if longitude and latitude:
136                     location_tag = etree.SubElement(node_tag, 'location', country="us", \
137                                                     longitude=longitude, latitude=latitude)
138
139
140     def add_slivers(self, slivers, sliver_urn=None, no_dupes=False): 
141         slivers = self._process_slivers(slivers)
142         nodes_with_slivers = self.get_nodes_with_slivers()
143         for sliver in slivers:
144             hostname = sliver['hostname']
145             if hostname in nodes_with_slivers:
146                 continue
147             nodes = self.xml.xpath('//rspecv2:node[@component_name="%s"] | //node[@component_name="%s"]' % (hostname, hostname), namespaces=self.namespaces)
148             if nodes:
149                 node = nodes[0]
150                 node.set('client_id', hostname)
151                 if sliver_urn:
152                     node.set('sliver_id', sliver_urn)
153                 etree.SubElement(node, 'sliver_type', name='plab-vnode')
154
155     def add_default_sliver_attribute(self, name, value, network=None):
156         pass
157
158     def add_interfaces(self, interfaces, no_dupes=False):
159         pass
160
161     def add_links(self, links, no_dupes=False):
162         pass
163
164
165     def merge(self, in_rspec):
166         """
167         Merge contents for specified rspec with current rspec
168         """
169         
170         # just copy over all the child elements under the root element
171         tree = etree.parse(StringIO(in_rspec))
172         root = tree.getroot()
173         for child in root.getchildren():
174             self.xml.append(child)
175                   
176     def cleanup(self):
177         # remove unncecessary elements, attributes
178         if self.type in ['request', 'manifest']:
179             # remove nodes without slivers
180             nodes = self.get_node_elements()
181             for node in nodes:
182                 delete = True
183                 hostname = node.get('component_name')
184                 parent = node.getparent()
185                 children = node.getchildren()
186                 for child in children:
187                     if child.tag.endswith('sliver_type'):
188                         delete = False
189                 if delete:
190                     parent.remove(node)
191
192             # remove 'available' element from remaining node elements
193             self.remove_element('//rspecv2:available | //available')
194
195 if __name__ == '__main__':
196     rspec = PGRSpec()
197     rspec.add_nodes([1])
198     print rspec