expose geni_api_versions URL as https since this it does not make sense to run sfa...
[sfa.git] / sfa / managers / slice_manager.py
1 import sys
2 import time
3 import traceback
4 from copy import copy
5 from lxml import etree
6
7 from sfa.trust.sfaticket import SfaTicket
8 from sfa.trust.credential import Credential
9
10 from sfa.util.sfalogging import logger
11 from sfa.util.xrn import Xrn, urn_to_hrn
12 from sfa.util.version import version_core
13 from sfa.util.callids import Callids
14 from sfa.util.cache import Cache
15
16 from sfa.client.multiclient import MultiClient
17
18 from sfa.rspecs.rspec_converter import RSpecConverter
19 from sfa.rspecs.version_manager import VersionManager
20 from sfa.rspecs.rspec import RSpec
21
22 from sfa.client.client_helper import sfa_to_pg_users_arg
23 from sfa.client.return_value import ReturnValue
24
25
26 class SliceManager:
27
28     # the cache instance is a class member so it survives across incoming
29     # requests
30     cache = None
31
32     def __init__(self, config):
33         self.cache = None
34         if config.SFA_SM_CACHING:
35             if SliceManager.cache is None:
36                 SliceManager.cache = Cache()
37             self.cache = SliceManager.cache
38
39     def GetVersion(self, api, options):
40         # peers explicitly in aggregates.xml
41         peers = dict([(peername, interface.get_url()) for (peername, interface) in api.aggregates.iteritems()
42                       if peername != api.hrn])
43         version_manager = VersionManager()
44         ad_rspec_versions = []
45         request_rspec_versions = []
46         cred_types = [{'geni_type': 'geni_sfa',
47                        'geni_version': str(i)} for i in range(4)[-2:]]
48         for rspec_version in version_manager.versions:
49             if rspec_version.content_type in ['*', 'ad']:
50                 ad_rspec_versions.append(rspec_version.to_dict())
51             if rspec_version.content_type in ['*', 'request']:
52                 request_rspec_versions.append(rspec_version.to_dict())
53         xrn = Xrn(api.hrn, 'authority+sm')
54         version_more = {
55             'interface': 'slicemgr',
56             'sfa': 2,
57             'geni_api': 3,
58             'geni_api_versions': {'3': 'https://%s:%s' % (api.config.SFA_SM_HOST, api.config.SFA_SM_PORT)},
59             'hrn': xrn.get_hrn(),
60             'urn': xrn.get_urn(),
61             'peers': peers,
62             # Accept operations that act on as subset of slivers in a given
63             # state.
64             'geni_single_allocation': 0,
65             # Multiple slivers can exist and be incrementally added, including
66             # those which connect or overlap in some way.
67             'geni_allocate': 'geni_many',
68             'geni_credential_types': cred_types,
69         }
70         sm_version = version_core(version_more)
71         # local aggregate if present needs to have localhost resolved
72         if api.hrn in api.aggregates:
73             local_am_url = api.aggregates[api.hrn].get_url()
74             sm_version['peers'][api.hrn] = local_am_url.replace(
75                 'localhost', sm_version['hostname'])
76         return sm_version
77
78     def drop_slicemgr_stats(self, rspec):
79         try:
80             stats_elements = rspec.xml.xpath('//statistics')
81             for node in stats_elements:
82                 node.getparent().remove(node)
83         except Exception as e:
84             logger.warn("drop_slicemgr_stats failed: %s " % (str(e)))
85
86     def add_slicemgr_stat(self, rspec, callname, aggname, elapsed, status, exc_info=None):
87         try:
88             stats_tags = rspec.xml.xpath('//statistics[@call="%s"]' % callname)
89             if stats_tags:
90                 stats_tag = stats_tags[0]
91             else:
92                 stats_tag = rspec.xml.root.add_element(
93                     "statistics", call=callname)
94
95             stat_tag = stats_tag.add_element("aggregate", name=str(aggname),
96                                              elapsed=str(elapsed), status=str(status))
97
98             if exc_info:
99                 exc_tag = stat_tag.add_element(
100                     "exc_info", name=str(exc_info[1]))
101
102                 # formats the traceback as one big text blob
103                 #exc_tag.text = "\n".join(traceback.format_exception(exc_info[0], exc_info[1], exc_info[2]))
104
105                 # formats the traceback as a set of xml elements
106                 tb = traceback.extract_tb(exc_info[2])
107                 for item in tb:
108                     exc_frame = exc_tag.add_element("tb_frame", filename=str(item[0]),
109                                                     line=str(item[1]), func=str(item[2]), code=str(item[3]))
110
111         except Exception as e:
112             logger.warn("add_slicemgr_stat failed on  %s: %s" %
113                         (aggname, str(e)))
114
115     def ListResources(self, api, creds, options):
116         call_id = options.get('call_id')
117         if Callids().already_handled(call_id):
118             return ""
119
120         version_manager = VersionManager()
121
122         def _ListResources(aggregate, server, credential, options):
123             forward_options = copy(options)
124             tStart = time.time()
125             try:
126                 version = api.get_cached_server_version(server)
127                 # force ProtoGENI aggregates to give us a v2 RSpec
128                 forward_options['geni_rspec_version'] = options.get(
129                     'geni_rspec_version')
130                 result = server.ListResources(credential, forward_options)
131                 return {"aggregate": aggregate, "result": result, "elapsed": time.time() - tStart, "status": "success"}
132             except Exception as e:
133                 api.logger.log_exc("ListResources failed at %s" % (server.url))
134                 return {"aggregate": aggregate, "elapsed": time.time() - tStart, "status": "exception", "exc_info": sys.exc_info()}
135
136         # get slice's hrn from options
137         xrn = options.get('geni_slice_urn', '')
138         (hrn, type) = urn_to_hrn(xrn)
139         if 'geni_compressed' in options:
140             del(options['geni_compressed'])
141
142         # get the rspec's return format from options
143         rspec_version = version_manager.get_version(
144             options.get('geni_rspec_version'))
145         version_string = "rspec_%s" % (rspec_version)
146
147         # look in cache first
148         cached_requested = options.get('cached', True)
149         if not xrn and self.cache and cached_requested:
150             rspec = self.cache.get(version_string)
151             if rspec:
152                 api.logger.debug(
153                     "SliceManager.ListResources returns cached advertisement")
154                 return rspec
155
156         # get the callers hrn
157         valid_cred = api.auth.checkCredentials(creds, 'listnodes', hrn)[0]
158         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
159
160         # attempt to use delegated credential first
161         cred = api.getDelegatedCredential(creds)
162         if not cred:
163             cred = api.getCredential()
164         multiclient = MultiClient()
165         for aggregate in api.aggregates:
166             # prevent infinite loop. Dont send request back to caller
167             # unless the caller is the aggregate's SM
168             if caller_hrn == aggregate and aggregate != api.hrn:
169                 continue
170
171             # get the rspec from the aggregate
172             interface = api.aggregates[aggregate]
173             server = api.server_proxy(interface, cred)
174             multiclient.run(_ListResources, aggregate, server, [cred], options)
175
176         results = multiclient.get_results()
177         rspec_version = version_manager.get_version(
178             options.get('geni_rspec_version'))
179         if xrn:
180             result_version = version_manager._get_version(
181                 rspec_version.type, rspec_version.version, 'manifest')
182         else:
183             result_version = version_manager._get_version(
184                 rspec_version.type, rspec_version.version, 'ad')
185         rspec = RSpec(version=result_version)
186         for result in results:
187             self.add_slicemgr_stat(rspec, "ListResources", result["aggregate"], result["elapsed"],
188                                    result["status"], result.get("exc_info", None))
189             if result["status"] == "success":
190                 res = result['result']['value']
191                 try:
192                     rspec.version.merge(ReturnValue.get_value(res))
193                 except:
194                     api.logger.log_exc(
195                         "SM.ListResources: Failed to merge aggregate rspec")
196
197         # cache the result
198         if self.cache and not xrn:
199             api.logger.debug("SliceManager.ListResources caches advertisement")
200             self.cache.add(version_string, rspec.toxml())
201
202         return rspec.toxml()
203
204     def Allocate(self, api, xrn, creds, rspec_str, expiration, options):
205         call_id = options.get('call_id')
206         if Callids().already_handled(call_id):
207             return ""
208
209         version_manager = VersionManager()
210
211         def _Allocate(aggregate, server, xrn, credential, rspec, options):
212             tStart = time.time()
213             try:
214                 # Need to call GetVersion at an aggregate to determine the supported
215                 # rspec type/format beofre calling CreateSliver at an Aggregate.
216                 #server_version = api.get_cached_server_version(server)
217                 # if 'sfa' not in server_version and 'geni_api' in server_version:
218                     # sfa aggregtes support both sfa and pg rspecs, no need to convert
219                     # if aggregate supports sfa rspecs. otherwise convert to pg rspec
220                     #rspec = RSpec(RSpecConverter.to_pg_rspec(rspec, 'request'))
221                     #filter = {'component_manager_id': server_version['urn']}
222                     # rspec.filter(filter)
223                     #rspec = rspec.toxml()
224                 result = server.Allocate(xrn, credential, rspec, options)
225                 return {"aggregate": aggregate, "result": result, "elapsed": time.time() - tStart, "status": "success"}
226             except:
227                 logger.log_exc(
228                     'Something wrong in _Allocate with URL %s' % server.url)
229                 return {"aggregate": aggregate, "elapsed": time.time() - tStart, "status": "exception", "exc_info": sys.exc_info()}
230
231         # Validate the RSpec against PlanetLab's schema --disabled for now
232         # The schema used here needs to aggregate the PL and VINI schemas
233         # schema = "/var/www/html/schemas/pl.rng"
234         rspec = RSpec(rspec_str)
235     #    schema = None
236     #    if schema:
237     #        rspec.validate(schema)
238
239         # if there is a <statistics> section, the aggregates don't care about it,
240         # so delete it.
241         self.drop_slicemgr_stats(rspec)
242
243         # attempt to use delegated credential first
244         cred = api.getDelegatedCredential(creds)
245         if not cred:
246             cred = api.getCredential()
247
248         # get the callers hrn
249         hrn, type = urn_to_hrn(xrn)
250         valid_cred = api.auth.checkCredentials(creds, 'createsliver', hrn)[0]
251         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
252         multiclient = MultiClient()
253         for aggregate in api.aggregates:
254             # prevent infinite loop. Dont send request back to caller
255             # unless the caller is the aggregate's SM
256             if caller_hrn == aggregate and aggregate != api.hrn:
257                 continue
258             interface = api.aggregates[aggregate]
259             server = api.server_proxy(interface, cred)
260             # Just send entire RSpec to each aggregate
261             multiclient.run(_Allocate, aggregate, server, xrn,
262                             [cred], rspec.toxml(), options)
263
264         results = multiclient.get_results()
265         manifest_version = version_manager._get_version(
266             rspec.version.type, rspec.version.version, 'manifest')
267         result_rspec = RSpec(version=manifest_version)
268         geni_urn = None
269         geni_slivers = []
270
271         for result in results:
272             self.add_slicemgr_stat(result_rspec, "Allocate", result["aggregate"], result["elapsed"],
273                                    result["status"], result.get("exc_info", None))
274             if result["status"] == "success":
275                 try:
276                     res = result['result']['value']
277                     geni_urn = res['geni_urn']
278                     result_rspec.version.merge(
279                         ReturnValue.get_value(res['geni_rspec']))
280                     geni_slivers.extend(res['geni_slivers'])
281                 except:
282                     api.logger.log_exc(
283                         "SM.Allocate: Failed to merge aggregate rspec")
284         return {
285             'geni_urn': geni_urn,
286             'geni_rspec': result_rspec.toxml(),
287             'geni_slivers': geni_slivers
288         }
289
290     def Provision(self, api, xrn, creds, options):
291         call_id = options.get('call_id')
292         if Callids().already_handled(call_id):
293             return ""
294
295         version_manager = VersionManager()
296
297         def _Provision(aggregate, server, xrn, credential, options):
298             tStart = time.time()
299             try:
300                 # Need to call GetVersion at an aggregate to determine the supported
301                 # rspec type/format beofre calling CreateSliver at an
302                 # Aggregate.
303                 server_version = api.get_cached_server_version(server)
304                 result = server.Provision(xrn, credential, options)
305                 return {"aggregate": aggregate, "result": result, "elapsed": time.time() - tStart, "status": "success"}
306             except:
307                 logger.log_exc(
308                     'Something wrong in _Allocate with URL %s' % server.url)
309                 return {"aggregate": aggregate, "elapsed": time.time() - tStart, "status": "exception", "exc_info": sys.exc_info()}
310
311         # attempt to use delegated credential first
312         cred = api.getDelegatedCredential(creds)
313         if not cred:
314             cred = api.getCredential()
315
316         # get the callers hrn
317         valid_cred = api.auth.checkCredentials(creds, 'createsliver', xrn)[0]
318         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
319         multiclient = MultiClient()
320         for aggregate in api.aggregates:
321             # prevent infinite loop. Dont send request back to caller
322             # unless the caller is the aggregate's SM
323             if caller_hrn == aggregate and aggregate != api.hrn:
324                 continue
325             interface = api.aggregates[aggregate]
326             server = api.server_proxy(interface, cred)
327             # Just send entire RSpec to each aggregate
328             multiclient.run(_Provision, aggregate,
329                             server, xrn, [cred], options)
330
331         results = multiclient.get_results()
332         manifest_version = version_manager._get_version(
333             'GENI', '3', 'manifest')
334         result_rspec = RSpec(version=manifest_version)
335         geni_slivers = []
336         geni_urn = None
337         for result in results:
338             self.add_slicemgr_stat(result_rspec, "Provision", result["aggregate"], result["elapsed"],
339                                    result["status"], result.get("exc_info", None))
340             if result["status"] == "success":
341                 try:
342                     res = result['result']['value']
343                     geni_urn = res['geni_urn']
344                     result_rspec.version.merge(
345                         ReturnValue.get_value(res['geni_rspec']))
346                     geni_slivers.extend(res['geni_slivers'])
347                 except:
348                     api.logger.log_exc(
349                         "SM.Provision: Failed to merge aggregate rspec")
350         return {
351             'geni_urn': geni_urn,
352             'geni_rspec': result_rspec.toxml(),
353             'geni_slivers': geni_slivers
354         }
355
356     def Renew(self, api, xrn, creds, expiration_time, options):
357         call_id = options.get('call_id')
358         if Callids().already_handled(call_id):
359             return True
360
361         def _Renew(aggregate, server, xrn, creds, expiration_time, options):
362             try:
363                 result = server.Renew(xrn, creds, expiration_time, options)
364                 if type(result) != dict:
365                     result = {'code': {'geni_code': 0}, 'value': result}
366                 result['aggregate'] = aggregate
367                 return result
368             except:
369                 logger.log_exc(
370                     'Something wrong in _Renew with URL %s' % server.url)
371                 return {'aggregate': aggregate, 'exc_info': traceback.format_exc(),
372                         'code': {'geni_code': -1},
373                         'value': False, 'output': ""}
374
375         # get the callers hrn
376         valid_cred = api.auth.checkCredentials(creds, 'renewsliver', xrn)[0]
377         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
378
379         # attempt to use delegated credential first
380         cred = api.getDelegatedCredential(creds)
381         if not cred:
382             cred = api.getCredential(minimumExpiration=31 * 86400)
383         multiclient = MultiClient()
384         for aggregate in api.aggregates:
385             # prevent infinite loop. Dont send request back to caller
386             # unless the caller is the aggregate's SM
387             if caller_hrn == aggregate and aggregate != api.hrn:
388                 continue
389             interface = api.aggregates[aggregate]
390             server = api.server_proxy(interface, cred)
391             multiclient.run(_Renew, aggregate, server, xrn, [
392                             cred], expiration_time, options)
393
394         results = multiclient.get_results()
395
396         geni_code = 0
397         geni_output = ",".join([x.get('output', "") for x in results])
398         geni_value = reduce(lambda x, y: x and y, [result.get(
399             'value', False) for result in results], True)
400         for agg_result in results:
401             agg_geni_code = agg_result['code'].get('geni_code', 0)
402             if agg_geni_code:
403                 geni_code = agg_geni_code
404
405         results = {'aggregates': results, 'code': {
406             'geni_code': geni_code}, 'value': geni_value, 'output': geni_output}
407
408         return results
409
410     def Delete(self, api, xrn, creds, options):
411         call_id = options.get('call_id')
412         if Callids().already_handled(call_id):
413             return ""
414
415         def _Delete(server, xrn, creds, options):
416             return server.Delete(xrn, creds, options)
417
418         (hrn, type) = urn_to_hrn(xrn[0])
419         # get the callers hrn
420         valid_cred = api.auth.checkCredentials(creds, 'deletesliver', hrn)[0]
421         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
422
423         # attempt to use delegated credential first
424         cred = api.getDelegatedCredential(creds)
425         if not cred:
426             cred = api.getCredential()
427         multiclient = MultiClient()
428         for aggregate in api.aggregates:
429             # prevent infinite loop. Dont send request back to caller
430             # unless the caller is the aggregate's SM
431             if caller_hrn == aggregate and aggregate != api.hrn:
432                 continue
433             interface = api.aggregates[aggregate]
434             server = api.server_proxy(interface, cred)
435             multiclient.run(_Delete, server, xrn, [cred], options)
436
437         results = []
438         for result in multiclient.get_results():
439             results += ReturnValue.get_value(result)
440         return results
441
442     # first draft at a merging SliverStatus
443     def Status(self, api, slice_xrn, creds, options):
444         def _Status(server, xrn, creds, options):
445             return server.Status(xrn, creds, options)
446
447         call_id = options.get('call_id')
448         if Callids().already_handled(call_id):
449             return {}
450         # attempt to use delegated credential first
451         cred = api.getDelegatedCredential(creds)
452         if not cred:
453             cred = api.getCredential()
454         multiclient = MultiClient()
455         for aggregate in api.aggregates:
456             interface = api.aggregates[aggregate]
457             server = api.server_proxy(interface, cred)
458             multiclient.run(_Status, server, slice_xrn, [cred], options)
459         results = [ReturnValue.get_value(result)
460                    for result in multiclient.get_results()]
461
462         # get rid of any void result - e.g. when call_id was hit, where by
463         # convention we return {}
464         results = [
465             result for result in results if result and result['geni_slivers']]
466
467         # do not try to combine if there's no result
468         if not results:
469             return {}
470
471         # otherwise let's merge stuff
472         geni_slivers = []
473         geni_urn = None
474         for result in results:
475             try:
476                 geni_urn = result['geni_urn']
477                 geni_slivers.extend(result['geni_slivers'])
478             except:
479                 api.logger.log_exc(
480                     "SM.Provision: Failed to merge aggregate rspec")
481         return {
482             'geni_urn': geni_urn,
483             'geni_slivers': geni_slivers
484         }
485
486     def Describe(self, api, creds, xrns, options):
487         def _Describe(server, xrn, creds, options):
488             return server.Describe(xrn, creds, options)
489
490         call_id = options.get('call_id')
491         if Callids().already_handled(call_id):
492             return {}
493         # attempt to use delegated credential first
494         cred = api.getDelegatedCredential(creds)
495         if not cred:
496             cred = api.getCredential()
497         multiclient = MultiClient()
498         for aggregate in api.aggregates:
499             interface = api.aggregates[aggregate]
500             server = api.server_proxy(interface, cred)
501             multiclient.run(_Describe, server, xrns, [cred], options)
502         results = [ReturnValue.get_value(result)
503                    for result in multiclient.get_results()]
504
505         # get rid of any void result - e.g. when call_id was hit, where by
506         # convention we return {}
507         results = [
508             result for result in results if result and result.get('geni_urn')]
509
510         # do not try to combine if there's no result
511         if not results:
512             return {}
513
514         # otherwise let's merge stuff
515         version_manager = VersionManager()
516         manifest_version = version_manager._get_version(
517             'GENI', '3', 'manifest')
518         result_rspec = RSpec(version=manifest_version)
519         geni_slivers = []
520         geni_urn = None
521         for result in results:
522             try:
523                 geni_urn = result['geni_urn']
524                 result_rspec.version.merge(
525                     ReturnValue.get_value(result['geni_rspec']))
526                 geni_slivers.extend(result['geni_slivers'])
527             except:
528                 api.logger.log_exc(
529                     "SM.Provision: Failed to merge aggregate rspec")
530         return {
531             'geni_urn': geni_urn,
532             'geni_rspec': result_rspec.toxml(),
533             'geni_slivers': geni_slivers
534         }
535
536     def PerformOperationalAction(self, api, xrn, creds, action, options):
537         # get the callers hrn
538         valid_cred = api.auth.checkCredentials(creds, 'createsliver', xrn)[0]
539         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
540
541         # attempt to use delegated credential first
542         cred = api.getDelegatedCredential(creds)
543         if not cred:
544             cred = api.getCredential()
545         multiclient = MultiClient()
546         for aggregate in api.aggregates:
547             # prevent infinite loop. Dont send request back to caller
548             # unless the caller is the aggregate's SM
549             if caller_hrn == aggregate and aggregate != api.hrn:
550                 continue
551             interface = api.aggregates[aggregate]
552             server = api.server_proxy(interface, cred)
553             multiclient.run(server.PerformOperationalAction,
554                             xrn, [cred], action, options)
555         multiclient.get_results()
556         return 1
557
558     def Shutdown(self, api, xrn, creds, options=None):
559         if options is None:
560             options = {}
561         xrn = Xrn(xrn)
562         # get the callers hrn
563         valid_cred = api.auth.checkCredentials(creds, 'stopslice', xrn.hrn)[0]
564         caller_hrn = Credential(cred=valid_cred).get_gid_caller().get_hrn()
565
566         # attempt to use delegated credential first
567         cred = api.getDelegatedCredential(creds)
568         if not cred:
569             cred = api.getCredential()
570         multiclient = MultiClient()
571         for aggregate in api.aggregates:
572             # prevent infinite loop. Dont send request back to caller
573             # unless the caller is the aggregate's SM
574             if caller_hrn == aggregate and aggregate != api.hrn:
575                 continue
576             interface = api.aggregates[aggregate]
577             server = api.server_proxy(interface, cred)
578             multiclient.run(server.Shutdown, xrn.urn, cred)
579         multiclient.get_results()
580         return 1