2 from sfa.rspecs.version_manager import VersionManager
3 from sfa.util.version import version_core
4 from sfa.util.xrn import Xrn
5 from sfa.util.callids import Callids
6 from sfa.util.sfalogging import logger
7 from sfa.util.faults import SfaInvalidArgument, InvalidRSpecVersion
8 from sfa.server.api_versions import ApiVersions
11 class AggregateManager:
13 def __init__ (self, config): pass
15 # essentially a union of the core version, the generic version (this code) and
16 # whatever the driver needs to expose
18 def rspec_versions(self):
19 version_manager = VersionManager()
20 ad_rspec_versions = []
21 request_rspec_versions = []
22 for rspec_version in version_manager.versions:
23 if rspec_version.content_type in ['*', 'ad']:
24 ad_rspec_versions.append(rspec_version.to_dict())
25 if rspec_version.content_type in ['*', 'request']:
26 request_rspec_versions.append(rspec_version.to_dict())
28 'geni_request_rspec_versions': request_rspec_versions,
29 'geni_ad_rspec_versions': ad_rspec_versions,
32 def get_rspec_version_string(self, rspec_version, options={}):
33 version_string = "rspec_%s" % (rspec_version)
35 #panos adding the info option to the caching key (can be improved)
36 if options.get('info'):
37 version_string = version_string + "_"+options.get('info', 'default')
39 # Adding the list_leases option to the caching key
40 if options.get('list_leases'):
41 version_string = version_string + "_"+options.get('list_leases', 'default')
43 # Adding geni_available to caching key
44 if options.get('geni_available'):
45 version_string = version_string + "_" + str(options.get('geni_available'))
49 def GetVersion(self, api, options):
50 xrn=Xrn(api.hrn, type='authority+am')
51 version = version_core()
52 cred_types = [{'geni_type': 'geni_sfa', 'geni_version': str(i)} for i in range(4)[-2:]]
53 geni_api_versions = ApiVersions().get_versions()
54 geni_api_versions['3'] = 'http://%s:%s' % (api.config.sfa_aggregate_host, api.config.sfa_aggregate_port)
56 'testbed': api.driver.testbed_name(),
57 'interface':'aggregate',
62 'geni_api_versions': geni_api_versions,
63 'geni_single_allocation': 0, # Accept operations that act on as subset of slivers in a given state.
64 'geni_allocate': 'geni_many',# Multiple slivers can exist and be incrementally added, including those which connect or overlap in some way.
65 'geni_credential_types': cred_types,
66 'geni_handles_speaksfor': True, # supports 'speaks for' credentials
68 version.update(version_generic)
69 version.update(self.rspec_versions())
70 testbed_version = api.driver.aggregate_version()
71 version.update(testbed_version)
74 def ListResources(self, api, creds, options):
75 call_id = options.get('call_id')
76 if Callids().already_handled(call_id): return ""
78 # get the rspec's return format from options
79 version_manager = VersionManager()
80 rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
81 version_string = self.get_rspec_version_string(rspec_version, options)
84 cached_requested = options.get('cached', True)
85 if cached_requested and api.driver.cache:
86 rspec = api.driver.cache.get(version_string)
88 logger.debug("%s.ListResources returning cached advertisement" % (api.driver.__module__))
91 rspec = api.driver.list_resources (rspec_version, options)
93 logger.debug("%s.ListResources stores advertisement in cache" % (api.driver.__module__))
94 api.driver.cache.add(version_string, rspec)
97 def Describe(self, api, creds, urns, options):
98 call_id = options.get('call_id')
99 if Callids().already_handled(call_id): return ""
101 version_manager = VersionManager()
102 rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
103 return api.driver.describe(urns, rspec_version, options)
106 def Status (self, api, urns, creds, options):
107 call_id = options.get('call_id')
108 if Callids().already_handled(call_id): return {}
109 return api.driver.status (urns, options=options)
112 def Allocate(self, api, xrn, creds, rspec_string, expiration, options):
114 Allocate resources as described in a request RSpec argument
115 to a slice with the named URN.
117 call_id = options.get('call_id')
118 if Callids().already_handled(call_id): return ""
119 return api.driver.allocate(xrn, rspec_string, expiration, options)
121 def Provision(self, api, xrns, creds, options):
123 Create the sliver[s] (slice) at this aggregate.
124 Verify HRN and initialize the slice record in PLC if necessary.
126 call_id = options.get('call_id')
127 if Callids().already_handled(call_id): return ""
129 # make sure geni_rspec_version is specified in options
130 if 'geni_rspec_version' not in options:
131 msg = 'geni_rspec_version is required and must be set in options struct'
132 raise SfaInvalidArgument(msg, 'geni_rspec_version')
133 # make sure we support the requested rspec version
134 version_manager = VersionManager()
135 rspec_version = version_manager.get_version(options['geni_rspec_version'])
136 if not rspec_version:
137 raise InvalidRSpecVersion(options['geni_rspec_version'])
139 return api.driver.provision(xrns, options)
141 def Delete(self, api, xrns, creds, options):
142 call_id = options.get('call_id')
143 if Callids().already_handled(call_id): return True
144 return api.driver.delete(xrns, options)
146 def Renew(self, api, xrns, creds, expiration_time, options):
147 call_id = options.get('call_id')
148 if Callids().already_handled(call_id): return True
150 # extend as long as possible
151 if options.get('geni_extend_alap'):
152 now = datetime.datetime.now()
153 requested = utcparse(expiration_time)
154 max = adjust_datetime(now, days=int(api.config.SFA_MAX_SLICE_RENEW))
156 expiration_time = max
158 return api.driver.renew(xrns, expiration_time, options)
160 def PerformOperationalAction(self, api, xrns, creds, action, options={}):
161 call_id = options.get('call_id')
162 if Callids().already_handled(call_id): return True
163 return api.driver.perform_operational_action(xrns, action, options)
165 def Shutdown(self, api, xrn, creds, options={}):
166 call_id = options.get('call_id')
167 if Callids().already_handled(call_id): return True
168 return api.driver.shutdown(xrn, options)