better support for reporting available api versions
[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')
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.append({'3': 'http://%s:%s' % (socket.gethostname(), api.config.sfa_aggregate_port)})
55         version_generic = {
56             'testbed': self.driver.testbed_name(),
57             'interface':'aggregate',
58             'hrn':xrn.get_hrn(),
59             'urn':xrn.get_urn(),
60             'geni_api': 3,
61             'geni_api_versions': geni_api_versions,
62             'geni_single_allocation': 0, # Accept operations that act on as subset of slivers in a given state.
63             'geni_allocate': 'geni_many',# Multiple slivers can exist and be incrementally added, including those which connect or overlap in some way.
64             'geni_credential_types': cred_types,
65         }
66         version.update(version_generic)
67         version.update(self.rspec_versions())
68         testbed_version = self.driver.aggregate_version()
69         version.update(testbed_version)
70         return version
71     
72     def ListResources(self, api, creds, options):
73         call_id = options.get('call_id')
74         if Callids().already_handled(call_id): return ""
75
76         # get the rspec's return format from options
77         version_manager = VersionManager()
78         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
79         version_string = self.get_rspec_version_string(rspec_version, options)
80
81         # look in cache first
82         cached_requested = options.get('cached', True)
83         if cached_requested and self.driver.cache:
84             rspec = self.driver.cache.get(version_string)
85             if rspec:
86                 logger.debug("%s.ListResources returning cached advertisement" % (self.driver.__module__))
87                 return rspec
88        
89         rspec = self.driver.list_resources (rspec_version, options) 
90         if self.driver.cache:
91             logger.debug("%s.ListResources stores advertisement in cache" % (self.driver.__module__))
92             self.driver.cache.add(version_string, rspec)    
93         return rspec
94     
95     def Describe(self, api, creds, urns, options):
96         call_id = options.get('call_id')
97         if Callids().already_handled(call_id): return ""
98
99         version_manager = VersionManager()
100         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
101         return self.driver.describe(urns, rspec_version, options)
102         
103     
104     def Status (self, api, urns, creds, options):
105         call_id = options.get('call_id')
106         if Callids().already_handled(call_id): return {}
107         return self.driver.status (urns, options=options)
108    
109
110     def Allocate(self, api, xrn, creds, rspec_string, expiration, options):
111         """
112         Allocate resources as described in a request RSpec argument 
113         to a slice with the named URN.
114         """
115         call_id = options.get('call_id')
116         if Callids().already_handled(call_id): return ""
117         return self.driver.allocate(xrn, rspec_string, expiration, options)
118  
119     def Provision(self, api, xrns, creds, options):
120         """
121         Create the sliver[s] (slice) at this aggregate.    
122         Verify HRN and initialize the slice record in PLC if necessary.
123         """
124         call_id = options.get('call_id')
125         if Callids().already_handled(call_id): return ""
126
127         # make sure geni_rspec_version is specified in options
128         if 'geni_rspec_version' not in options:
129             msg = 'geni_rspec_version is required and must be set in options struct'
130             raise SfaInvalidArgument(msg, 'geni_rspec_version')
131         # make sure we support the requested rspec version
132         version_manager = VersionManager()
133         rspec_version = version_manager.get_version(options['geni_rspec_version']) 
134         if not rspec_version:
135             raise InvalidRSpecVersion(options['geni_rspec_version'])
136                        
137         return self.driver.provision(xrns, options)
138     
139     def Delete(self, api, xrns, creds, options):
140         call_id = options.get('call_id')
141         if Callids().already_handled(call_id): return True
142         return self.driver.delete(xrns, options)
143
144     def Renew(self, api, xrns, creds, expiration_time, options):
145         call_id = options.get('call_id')
146         if Callids().already_handled(call_id): return True
147         return self.driver.renew(xrns, expiration_time, options)
148
149     def PerformOperationalAction(self, api, xrns, creds, action, options={}):
150         call_id = options.get('call_id')
151         if Callids().already_handled(call_id): return True
152         return self.driver.perform_operational_action(xrns, action, options) 
153
154     def Shutdown(self, api, xrn, creds, options={}):
155         call_id = options.get('call_id')
156         if Callids().already_handled(call_id): return True
157         return self.driver.shutdown(xrn, options) 
158