1 ### $Id: slices.py 15842 2009-11-22 09:56:13Z anil $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/plc/slices.py $
8 from copy import deepcopy
10 from StringIO import StringIO
11 from types import StringTypes
12 from sfa.util.rspecHelper import merge_rspecs
13 from sfa.util.namespace import *
14 from sfa.util.rspec import *
15 from sfa.util.specdict import *
16 from sfa.util.faults import *
17 from sfa.util.record import SfaRecord
18 from sfa.util.policy import Policy
19 from sfa.util.prefixTree import prefixTree
20 from sfa.util.sfaticket import *
21 from sfa.trust.credential import Credential
22 from sfa.util.threadmanager import ThreadManager
23 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
24 from sfa.util.debug import log
25 import sfa.plc.peers as peers
29 version['geni_api'] = 1
32 def slice_status(api, slice_xrn, creds ):
34 result['geni_urn'] = slice_xrn
35 result['geni_status'] = 'unknown'
36 result['geni_resources'] = {}
39 def create_slice(api, xrn, creds, rspec, users):
40 hrn, type = urn_to_hrn(xrn)
42 # Validate the RSpec against PlanetLab's schema --disabled for now
43 # The schema used here needs to aggregate the PL and VINI schemas
44 # schema = "/var/www/html/schemas/pl.rng"
48 tree = etree.parse(StringIO(rspec))
49 except etree.XMLSyntaxError:
50 message = str(sys.exc_info()[1])
51 raise InvalidRSpec(message)
53 relaxng_doc = etree.parse(schema)
54 relaxng = etree.RelaxNG(relaxng_doc)
57 error = relaxng.error_log.last_error
58 message = "%s (line %s)" % (error.message, error.line)
59 raise InvalidRSpec(message)
62 # XX TODO: Should try to use delegated credential first
64 cred = api.getCredential()
65 threads = ThreadManager()
66 for aggregate in api.aggregates:
67 if aggregate not in [api.auth.client_cred.get_gid_caller().get_hrn()]:
68 server = api.aggregates[aggregate]
69 # Just send entire RSpec to each aggregate
70 threads.run(server.CreateSliver, xrn, cred, rspec, users)
75 def get_ticket(api, xrn, rspec, origin_hrn=None):
76 slice_hrn, type = urn_to_hrn(xrn)
77 # get the netspecs contained within the clients rspec
79 tree= etree.parse(StringIO(rspec))
80 elements = tree.findall('./network')
81 for element in elements:
82 aggregate_hrn = element.values()[0]
83 aggregate_rspecs[aggregate_hrn] = rspec
85 # get a ticket from each aggregate
86 credential = api.getCredential()
87 threads = ThreadManager()
88 for aggregate, aggregate_rspec in aggregate_rspecs.items():
90 if aggregate in api.aggregates:
91 server = api.aggregates[aggregate]
93 net_urn = hrn_to_urn(aggregate, 'authority')
94 # we may have a peer that knows about this aggregate
95 for agg in api.aggregates:
96 agg_info = api.aggregates[agg].get_aggregates(credential, net_urn)
98 # send the request to this address
99 url = 'http://%s:%s' % (agg_info['addr'], agg_info['port'])
100 server = xmlrpcprotocol.get_server(url, api.key_file, api.cert_file)
104 threads.run(server.get_ticket, credential, xrn, aggregate_rspec, origin_hrn)
105 results = threads.get_results()
107 # gather information from each ticket
112 for result in results:
113 agg_ticket = SfaTicket(string=result)
114 attrs = agg_ticket.get_attributes()
116 object_gid = agg_ticket.get_gid_object()
117 rspecs.append(agg_ticket.get_rspec())
118 initscripts.extend(attrs.get('initscripts', []))
119 slivers.extend(attrs.get('slivers', []))
122 attributes = {'initscripts': initscripts,
124 merged_rspec = merge_rspecs(rspecs)
126 # create a new ticket
127 ticket = SfaTicket(subject = slice_hrn)
128 ticket.set_gid_caller(api.auth.client_gid)
129 ticket.set_issuer(key=api.key, subject=api.hrn)
130 ticket.set_gid_object(object_gid)
131 ticket.set_pubkey(object_gid.get_pubkey())
132 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
133 ticket.set_attributes(attributes)
134 ticket.set_rspec(merged_rspec)
137 return ticket.save_to_string(save_parents=True)
140 def delete_slice(api, xrn, origin_hrn=None):
142 # XX TODO: Should try to use delegated credential first
144 credential = api.getCredential()
145 threads = ThreadManager()
146 for aggregate in api.aggregates:
147 server = api.aggregates[aggregate]
148 threads.run(server.DeleteSliver, xrn, credential)
149 threads.get_results()
152 def start_slice(api, xrn, creds):
154 # XX TODO: Should try to use delegated credential first
156 credential = api.getCredential()
157 threads = ThreadManager()
158 for aggregate in api.aggregates:
159 server = api.aggregates[aggregate]
160 threads.run(server.Start, xrn, credential)
161 threads.get_results()
164 def stop_slice(api, xrn, creds):
166 # XX TODO: Should try to use delegated credential first
168 credential = api.getCredential()
169 threads = ThreadManager()
170 for aggregate in api.aggregates:
171 server = api.aggregates[aggregate]
172 threads.run(server.Stop, xrn, credential)
173 threads.get_results()
176 def reset_slice(api, xrn):
182 def shutdown(api, xrn, creds):
188 def status(api, xrn, creds):
195 # look in cache first
197 slices = api.cache.get('slices')
201 # fetch from aggregates
203 credential = api.getCredential()
204 threads = ThreadManager()
205 for aggregate in api.aggregates:
206 server = api.aggregates[aggregate]
207 threads.run(server.get_slices, credential)
210 results = threads.get_results()
212 for result in results:
213 slices.extend(result)
217 api.cache.add('slices', slices)
221 def get_rspec(api, creds, options):
222 # get slice's hrn from options
223 xrn = options.get('geni_slice_urn', None)
224 hrn, type = urn_to_hrn(xrn)
226 # get hrn of the original caller
227 origin_hrn = options.get('origin_hrn', None)
229 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
231 # look in cache first
232 if api.cache and not xrn:
233 rspec = api.cache.get('nodes')
237 hrn, type = urn_to_hrn(xrn)
240 # XX TODO: Should try to use delegated credential first
242 cred = api.getCredential()
243 threads = ThreadManager()
245 for aggregate in api.aggregates:
246 if aggregate not in [api.auth.client_cred.get_gid_caller().get_hrn()]:
247 # get the rspec from the aggregate
248 server = api.aggregates[aggregate]
249 threads.run(server.ListResources, cred, options)
250 #threads.run(server.get_resources, cred, xrn, origin_hrn)
253 results = threads.get_results()
254 # combine the rspecs into a single rspec
255 for agg_rspec in results:
257 tree = etree.parse(StringIO(agg_rspec))
258 except etree.XMLSyntaxError:
259 message = str(agg_rspec) + ": " + str(sys.exc_info()[1])
260 raise InvalidRSpec(message)
262 root = tree.getroot()
263 if root.get("type") in ["SFA"]:
267 for network in root.iterfind("./network"):
268 rspec.append(deepcopy(network))
269 for request in root.iterfind("./request"):
270 rspec.append(deepcopy(request))
272 rspec = etree.tostring(rspec, xml_declaration=True, pretty_print=True)
274 if api.cache and not xrn:
275 api.cache.add('nodes', rspec)
281 r.parseFile(sys.argv[1])
283 create_slice(None,'plc.princeton.tmacktestslice',rspec)
285 if __name__ == "__main__":