Refactored ListResources, ListSlices. ListResources no longer returns slice resources...
[sfa.git] / sfa / managers / aggregate_manager.py
1 import soctet
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
8 class AggregateManager:
9
10     def __init__ (self, config): pass
11     
12     # essentially a union of the core version, the generic version (this code) and
13     # whatever the driver needs to expose
14
15     def _rspec_versions(self):
16         version_manager = VersionManager()
17         ad_rspec_versions = []
18         request_rspec_versions = []
19         for rspec_version in version_manager.versions:
20             if rspec_version.content_type in ['*', 'ad']:
21                 ad_rspec_versions.append(rspec_version.to_dict())
22             if rspec_version.content_type in ['*', 'request']:
23                 request_rspec_versions.append(rspec_version.to_dict())
24         return {
25             'testbed':self.testbed_name(),
26             'geni_request_rspec_versions': request_rspec_versions,
27             'geni_ad_rspec_versions': ad_rspec_versions,
28             }
29
30     def GetVersion(self, api, options):
31         xrn=Xrn(api.hrn)
32         version = version_core()
33         version_generic = {
34             'testbed': self.driver.testbed_name(),
35             'interface':'aggregate',
36             'hrn':xrn.get_hrn(),
37             'urn':xrn.get_urn(),
38             'geni_api': 3,
39             'geni_api_versions': {'3': 'http://%s:%s' % (socket.gethostname(), api.config.sfa_aggregate_port)},
40             'geni_single_allocation': 0, # Accept operations that act on as subset of slivers in a given state.
41             'geni_allocate': 'geni_many',# Multiple slivers can exist and be incrementally added, including those which connect or overlap in some way.
42             'geni_best_effort': 'true',
43             'geni_credential_types': [{
44                 'geni_type': 'geni_sfa',
45                 'geni_version': 3,
46             }],
47         }
48         version.update(version_generic)
49         testbed_version = self.driver.aggregate_version()
50         version.update(testbed_version)
51         return version
52     
53     def ListSlices(self, api, creds, options):
54         call_id = options.get('call_id')
55         if Callids().already_handled(call_id): return []
56         
57         # look in cache first
58         if self.driver.cache:
59             slices = self.driver.cache.get('slices')
60             if slices:
61                 logger.debug("%s.list_slices returns from cache" % (self.driver.__module__))
62                 return slices
63
64         # call driver
65         slices = self.driver.list_slices(creds, options)
66
67         # cache the result
68         if self.driver.cache:
69             logger.debug ("%s.list_slices stores value in cache" % (self.driver.__module__))
70             self.driver.cache.add('slices', instance_urns)
71
72         return self.driver.list_slices (creds, options)
73
74     def ListResources(self, api, creds, options):
75         call_id = options.get('call_id')
76         if Callids().already_handled(call_id): return ""
77
78         version_manager = VersionManager()
79         # get the rspec's return format from options
80         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
81         version_string = "rspec_%s" % (rspec_version)
82
83         #panos adding the info option to the caching key (can be improved)
84         if options.get('info'):
85             version_string = version_string + "_"+options.get('info', 'default')
86
87         # Adding the list_leases option to the caching key
88         if options.get('list_leases'):
89             version_string = version_string + "_"+options.get('list_leases', 'default')
90
91         # Adding geni_available to caching key
92         if options.get('geni_available'):
93             version_string = version_string + "_" + str(options.get('geni_available'))
94
95         # look in cache first
96         cached_requested = options.get('cached', True)
97         if cached_requested and self.driver.cache and not slice_hrn:
98             rspec = self.driver.cache.get(version_string)
99             if rspec:
100                 logger.debug("%s.ListResources returning cached advertisement" % (self.driver.__module__))
101                 return rspec
102        
103         rspec = self.driver.list_resources (creds, options) 
104         if self.driver.cache:
105             logger.debug("%s.ListResources stores advertisement in cache" % (self.driver.__module__))
106             self.driver.cache.add(version_string, rspec)    
107         return self.driver.list_resources (creds, options)
108     
109     def Describe(self, api, creds, urns, options):
110         call_id = options.get('call_id')
111         if Callids().already_handled(call_id): return ""
112
113         return self.driver.describe (creds, urns, options)
114         
115     
116     def SliverStatus (self, api, xrn, creds, options):
117         call_id = options.get('call_id')
118         if Callids().already_handled(call_id): return {}
119     
120         xrn = Xrn(xrn,'slice')
121         slice_urn=xrn.get_urn()
122         slice_hrn=xrn.get_hrn()
123         return self.driver.sliver_status (slice_urn, slice_hrn)
124     
125     def CreateSliver(self, api, xrn, creds, rspec_string, users, options):
126         """
127         Create the sliver[s] (slice) at this aggregate.    
128         Verify HRN and initialize the slice record in PLC if necessary.
129         """
130         call_id = options.get('call_id')
131         if Callids().already_handled(call_id): return ""
132     
133         xrn = Xrn(xrn, 'slice')
134         slice_urn=xrn.get_urn()
135         slice_hrn=xrn.get_hrn()
136
137         return self.driver.create_sliver (slice_urn, slice_hrn, creds, rspec_string, users, options)
138     
139     def DeleteSliver(self, api, xrn, creds, options):
140         call_id = options.get('call_id')
141         if Callids().already_handled(call_id): return True
142
143         xrn = Xrn(xrn, 'slice')
144         slice_urn=xrn.get_urn()
145         slice_hrn=xrn.get_hrn()
146         return self.driver.delete_sliver (slice_urn, slice_hrn, creds, options)
147
148     def RenewSliver(self, api, xrn, creds, expiration_time, options):
149         call_id = options.get('call_id')
150         if Callids().already_handled(call_id): return True
151         
152         xrn = Xrn(xrn, 'slice')
153         slice_urn=xrn.get_urn()
154         slice_hrn=xrn.get_hrn()
155         return self.driver.renew_sliver (slice_urn, slice_hrn, creds, expiration_time, options)
156     
157     ### these methods could use an options extension for at least call_id
158     def start_slice(self, api, xrn, creds):
159         xrn = Xrn(xrn)
160         slice_urn=xrn.get_urn()
161         slice_hrn=xrn.get_hrn()
162         return self.driver.start_slice (slice_urn, slice_hrn, creds)
163      
164     def stop_slice(self, api, xrn, creds):
165         xrn = Xrn(xrn)
166         slice_urn=xrn.get_urn()
167         slice_hrn=xrn.get_hrn()
168         return self.driver.stop_slice (slice_urn, slice_hrn, creds)
169
170     def reset_slice(self, api, xrn):
171         xrn = Xrn(xrn)
172         slice_urn=xrn.get_urn()
173         slice_hrn=xrn.get_hrn()
174         return self.driver.reset_slice (slice_urn, slice_hrn)
175
176     def GetTicket(self, api, xrn, creds, rspec, users, options):
177     
178         xrn = Xrn(xrn)
179         slice_urn=xrn.get_urn()
180         slice_hrn=xrn.get_hrn()
181
182         return self.driver.get_ticket (slice_urn, slice_hrn, creds, rspec, options)
183