4 from StringIO import StringIO
5 from types import StringTypes
6 from copy import deepcopy
10 from sfa.util.sfalogging import sfa_logger
11 from sfa.util.rspecHelper import merge_rspecs
12 from sfa.util.xrn import Xrn, urn_to_hrn, hrn_to_urn
13 from sfa.util.plxrn import hrn_to_pl_slicename
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 import sfa.plc.peers as peers
25 from sfa.util.version import version_core
26 from sfa.util.callids import Callids
28 # XX FIX ME: should merge result from multiple aggregates instead of
29 # calling aggregate implementation
30 from sfa.managers.aggregate_manager_pl import slice_status
32 # we have specialized xmlrpclib.ServerProxy to remember the input url
33 # OTOH it's not clear if we're only dealing with XMLRPCServerProxy instances
34 def get_serverproxy_url (server):
38 sfa_logger().warning("GetVersion, falling back to xmlrpclib.ServerProxy internals")
39 return server._ServerProxy__host + server._ServerProxy__handler
42 # peers explicitly in aggregates.xml
43 peers =dict ([ (peername,get_serverproxy_url(v)) for (peername,v) in api.aggregates.iteritems()
44 if peername != api.hrn])
46 sm_version=version_core({'interface':'slicemgr',
47 'hrn' : xrn.get_hrn(),
48 'urn' : xrn.get_urn(),
51 # local aggregate if present needs to have localhost resolved
52 if api.hrn in api.aggregates:
53 local_am_url=get_serverproxy_url(api.aggregates[api.hrn])
54 sm_version['peers'][api.hrn]=local_am_url.replace('localhost',sm_version['hostname'])
57 def create_slice(api, xrn, creds, rspec, users):
58 hrn, type = urn_to_hrn(xrn)
60 # Validate the RSpec against PlanetLab's schema --disabled for now
61 # The schema used here needs to aggregate the PL and VINI schemas
62 # schema = "/var/www/html/schemas/pl.rng"
66 tree = etree.parse(StringIO(rspec))
67 except etree.XMLSyntaxError:
68 message = str(sys.exc_info()[1])
69 raise InvalidRSpec(message)
71 relaxng_doc = etree.parse(schema)
72 relaxng = etree.RelaxNG(relaxng_doc)
75 error = relaxng.error_log.last_error
76 message = "%s (line %s)" % (error.message, error.line)
77 raise InvalidRSpec(message)
80 valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
81 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
83 # attempt to use delegated credential first
84 credential = api.getDelegatedCredential(creds)
86 credential = api.getCredential()
87 threads = ThreadManager()
88 for aggregate in api.aggregates:
89 # prevent infinite loop. Dont send request back to caller
90 # unless the caller is the aggregate's SM
91 if caller_hrn == aggregate and aggregate != api.hrn:
94 # Just send entire RSpec to each aggregate
95 server = api.aggregates[aggregate]
96 threads.run(server.CreateSliver, xrn, credential, rspec, users)
98 results = threads.get_results()
99 merged_rspec = merge_rspecs(results)
102 def renew_slice(api, xrn, creds, expiration_time):
103 hrn, type = urn_to_hrn(xrn)
105 # get the callers hrn
106 valid_cred = api.auth.checkCredentials(creds, 'renewsliver', hrn)[0]
107 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
109 # attempt to use delegated credential first
110 credential = api.getDelegatedCredential(creds)
112 credential = api.getCredential()
113 threads = ThreadManager()
114 for aggregate in api.aggregates:
115 # prevent infinite loop. Dont send request back to caller
116 # unless the caller is the aggregate's SM
117 if caller_hrn == aggregate and aggregate != api.hrn:
120 server = api.aggregates[aggregate]
121 threads.run(server.RenewSliver, xrn, [credential], expiration_time)
122 threads.get_results()
125 def get_ticket(api, xrn, creds, rspec, users):
126 slice_hrn, type = urn_to_hrn(xrn)
127 # get the netspecs contained within the clients rspec
128 aggregate_rspecs = {}
129 tree= etree.parse(StringIO(rspec))
130 elements = tree.findall('./network')
131 for element in elements:
132 aggregate_hrn = element.values()[0]
133 aggregate_rspecs[aggregate_hrn] = rspec
135 # get the callers hrn
136 valid_cred = api.auth.checkCredentials(creds, 'getticket', slice_hrn)[0]
137 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
139 # attempt to use delegated credential first
140 credential = api.getDelegatedCredential(creds)
142 credential = api.getCredential()
143 threads = ThreadManager()
144 for (aggregate, aggregate_rspec) in aggregate_rspecs.iteritems():
145 # prevent infinite loop. Dont send request back to caller
146 # unless the caller is the aggregate's SM
147 if caller_hrn == aggregate and aggregate != api.hrn:
150 if aggregate in api.aggregates:
151 server = api.aggregates[aggregate]
153 net_urn = hrn_to_urn(aggregate, 'authority')
154 # we may have a peer that knows about this aggregate
155 for agg in api.aggregates:
156 target_aggs = api.aggregates[agg].get_aggregates(credential, net_urn)
157 if not target_aggs or not 'hrn' in target_aggs[0]:
159 # send the request to this address
160 url = target_aggs[0]['url']
161 server = xmlrpcprotocol.get_server(url, api.key_file, api.cert_file)
162 # aggregate found, no need to keep looping
166 threads.run(server.GetTicket, xrn, credential, aggregate_rspec, users)
168 results = threads.get_results()
170 # gather information from each ticket
175 for result in results:
176 agg_ticket = SfaTicket(string=result)
177 attrs = agg_ticket.get_attributes()
179 object_gid = agg_ticket.get_gid_object()
180 rspecs.append(agg_ticket.get_rspec())
181 initscripts.extend(attrs.get('initscripts', []))
182 slivers.extend(attrs.get('slivers', []))
185 attributes = {'initscripts': initscripts,
187 merged_rspec = merge_rspecs(rspecs)
189 # create a new ticket
190 ticket = SfaTicket(subject = slice_hrn)
191 ticket.set_gid_caller(api.auth.client_gid)
192 ticket.set_issuer(key=api.key, subject=api.hrn)
193 ticket.set_gid_object(object_gid)
194 ticket.set_pubkey(object_gid.get_pubkey())
195 #new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
196 ticket.set_attributes(attributes)
197 ticket.set_rspec(merged_rspec)
200 return ticket.save_to_string(save_parents=True)
203 def delete_slice(api, xrn, creds):
204 hrn, type = urn_to_hrn(xrn)
206 # get the callers hrn
207 valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
208 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
210 # attempt to use delegated credential first
211 credential = api.getDelegatedCredential(creds)
213 credential = api.getCredential()
214 threads = ThreadManager()
215 for aggregate in api.aggregates:
216 # prevent infinite loop. Dont send request back to caller
217 # unless the caller is the aggregate's SM
218 if caller_hrn == aggregate and aggregate != api.hrn:
220 server = api.aggregates[aggregate]
221 threads.run(server.DeleteSliver, xrn, credential)
222 threads.get_results()
225 def start_slice(api, xrn, creds):
226 hrn, type = urn_to_hrn(xrn)
228 # get the callers hrn
229 valid_cred = api.auth.checkCredentials(creds, 'startslice', hrn)[0]
230 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
232 # attempt to use delegated credential first
233 credential = api.getDelegatedCredential(creds)
235 credential = api.getCredential()
236 threads = ThreadManager()
237 for aggregate in api.aggregates:
238 # prevent infinite loop. Dont send request back to caller
239 # unless the caller is the aggregate's SM
240 if caller_hrn == aggregate and aggregate != api.hrn:
242 server = api.aggregates[aggregate]
243 threads.run(server.Start, xrn, credential)
244 threads.get_results()
247 def stop_slice(api, xrn, creds):
248 hrn, type = urn_to_hrn(xrn)
250 # get the callers hrn
251 valid_cred = api.auth.checkCredentials(creds, 'stopslice', hrn)[0]
252 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
254 # attempt to use delegated credential first
255 credential = api.getDelegatedCredential(creds)
257 credential = api.getCredential()
258 threads = ThreadManager()
259 for aggregate in api.aggregates:
260 # prevent infinite loop. Dont send request back to caller
261 # unless the caller is the aggregate's SM
262 if caller_hrn == aggregate and aggregate != api.hrn:
264 server = api.aggregates[aggregate]
265 threads.run(server.Stop, xrn, credential)
266 threads.get_results()
269 def reset_slice(api, xrn):
275 def shutdown(api, xrn, creds):
281 def status(api, xrn, creds):
287 def get_slices(api, creds):
289 # look in cache first
291 slices = api.cache.get('slices')
295 # get the callers hrn
296 valid_cred = api.auth.checkCredentials(creds, 'listslices', None)[0]
297 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
299 # attempt to use delegated credential first
300 credential = api.getDelegatedCredential(creds)
302 credential = api.getCredential()
303 threads = ThreadManager()
304 # fetch from aggregates
305 for aggregate in api.aggregates:
306 # prevent infinite loop. Dont send request back to caller
307 # unless the caller is the aggregate's SM
308 if caller_hrn == aggregate and aggregate != api.hrn:
310 server = api.aggregates[aggregate]
311 threads.run(server.ListSlices, credential)
314 results = threads.get_results()
316 for result in results:
317 slices.extend(result)
321 api.cache.add('slices', slices)
326 # Thierry : caching at the slicemgr level makes sense to some extent
328 def get_rspec(api, creds, options, call_id):
330 if not Callids().should_handle_call_id(call_id):
331 api.logger.info("%d received get_rspec with known call_id %s"%(api.interface,call_id))
334 # get slice's hrn from options
335 xrn = options.get('geni_slice_urn', '')
336 (hrn, type) = urn_to_hrn(xrn)
338 # get hrn of the original caller
339 origin_hrn = options.get('origin_hrn', None)
341 if isinstance(creds, list):
342 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
344 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
346 # look in cache first
347 if caching and api.cache and not xrn:
348 rspec = api.cache.get('nodes')
352 # get the callers hrn
353 valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
354 caller_hrn = Credential(string=valid_cred).get_gid_caller().get_hrn()
356 # attempt to use delegated credential first
357 credential = api.getDelegatedCredential(creds)
359 credential = api.getCredential()
360 threads = ThreadManager()
361 for aggregate in api.aggregates:
362 # prevent infinite loop. Dont send request back to caller
363 # unless the caller is the aggregate's SM
364 if caller_hrn == aggregate and aggregate != api.hrn:
366 # get the rspec from the aggregate
367 server = api.aggregates[aggregate]
368 my_opts = copy(options)
369 my_opts['geni_compressed'] = False
370 threads.run(server.ListResources, credential, my_opts, call_id)
371 #threads.run(server.get_resources, cred, xrn, origin_hrn)
373 results = threads.get_results()
374 merged_rspec = merge_rspecs(results)
377 if caching and api.cache and not xrn:
378 api.cache.add('nodes', merged_rspec)
384 r.parseFile(sys.argv[1])
386 create_slice(None,'plc.princeton.tmacktestslice',rspec)
388 if __name__ == "__main__":