added support for geni_extend_alap (as long as possible) in RenewSliver
[sfa.git] / sfa / managers / aggregate_manager.py
1 import socket
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
9
10
11 class AggregateManager:
12
13     def __init__ (self, config): pass
14     
15     # essentially a union of the core version, the generic version (this code) and
16     # whatever the driver needs to expose
17
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())
27         return {
28             'geni_request_rspec_versions': request_rspec_versions,
29             'geni_ad_rspec_versions': ad_rspec_versions,
30             }
31
32     def get_rspec_version_string(self, rspec_version, options={}):
33         version_string = "rspec_%s" % (rspec_version)
34
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')
38
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')
42
43         # Adding geni_available to caching key
44         if options.get('geni_available'):
45             version_string = version_string + "_" + str(options.get('geni_available'))
46
47         return version_string
48
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)
55         version_generic = {
56             'testbed': api.driver.testbed_name(),
57             'interface':'aggregate',
58             'sfa': 3,
59             'hrn':xrn.get_hrn(),
60             'urn':xrn.get_urn(),
61             'geni_api': 3,
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         }
67         version.update(version_generic)
68         version.update(self.rspec_versions())
69         testbed_version = api.driver.aggregate_version()
70         version.update(testbed_version)
71         return version
72     
73     def ListResources(self, api, creds, options):
74         call_id = options.get('call_id')
75         if Callids().already_handled(call_id): return ""
76
77         # get the rspec's return format from options
78         version_manager = VersionManager()
79         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
80         version_string = self.get_rspec_version_string(rspec_version, options)
81
82         # look in cache first
83         cached_requested = options.get('cached', True)
84         if cached_requested and api.driver.cache:
85             rspec = api.driver.cache.get(version_string)
86             if rspec:
87                 logger.debug("%s.ListResources returning cached advertisement" % (api.driver.__module__))
88                 return rspec
89        
90         rspec = api.driver.list_resources (rspec_version, options) 
91         if api.driver.cache:
92             logger.debug("%s.ListResources stores advertisement in cache" % (api.driver.__module__))
93             api.driver.cache.add(version_string, rspec)    
94         return rspec
95     
96     def Describe(self, api, creds, urns, options):
97         call_id = options.get('call_id')
98         if Callids().already_handled(call_id): return ""
99
100         version_manager = VersionManager()
101         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
102         return api.driver.describe(urns, rspec_version, options)
103         
104     
105     def Status (self, api, urns, creds, options):
106         call_id = options.get('call_id')
107         if Callids().already_handled(call_id): return {}
108         return api.driver.status (urns, options=options)
109    
110
111     def Allocate(self, api, xrn, creds, rspec_string, expiration, options):
112         """
113         Allocate resources as described in a request RSpec argument 
114         to a slice with the named URN.
115         """
116         call_id = options.get('call_id')
117         if Callids().already_handled(call_id): return ""
118         return api.driver.allocate(xrn, rspec_string, expiration, options)
119  
120     def Provision(self, api, xrns, creds, options):
121         """
122         Create the sliver[s] (slice) at this aggregate.    
123         Verify HRN and initialize the slice record in PLC if necessary.
124         """
125         call_id = options.get('call_id')
126         if Callids().already_handled(call_id): return ""
127
128         # make sure geni_rspec_version is specified in options
129         if 'geni_rspec_version' not in options:
130             msg = 'geni_rspec_version is required and must be set in options struct'
131             raise SfaInvalidArgument(msg, 'geni_rspec_version')
132         # make sure we support the requested rspec version
133         version_manager = VersionManager()
134         rspec_version = version_manager.get_version(options['geni_rspec_version']) 
135         if not rspec_version:
136             raise InvalidRSpecVersion(options['geni_rspec_version'])
137                        
138         return api.driver.provision(xrns, options)
139     
140     def Delete(self, api, xrns, creds, options):
141         call_id = options.get('call_id')
142         if Callids().already_handled(call_id): return True
143         return api.driver.delete(xrns, options)
144
145     def Renew(self, api, xrns, creds, expiration_time, options):
146         call_id = options.get('call_id')
147         if Callids().already_handled(call_id): return True
148
149         # extend as long as possible
150         if options.get('geni_extend_alap'):
151             now = datetime.datetime.now()
152             requested = utcparse(expiration_time)
153             max = adjust_datetime(now, days=30)
154             if requested > max:
155                 expiration_time = max
156
157         return api.driver.renew(xrns, expiration_time, options)
158
159     def PerformOperationalAction(self, api, xrns, creds, action, options={}):
160         call_id = options.get('call_id')
161         if Callids().already_handled(call_id): return True
162         return api.driver.perform_operational_action(xrns, action, options) 
163
164     def Shutdown(self, api, xrn, creds, options={}):
165         call_id = options.get('call_id')
166         if Callids().already_handled(call_id): return True
167         return api.driver.shutdown(xrn, options) 
168