cb8b2a6b8b98a41422c5b50474ffa3cd0d3baf3d
[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
9
10 class AggregateManager:
11
12     def __init__ (self, config): pass
13     
14     # essentially a union of the core version, the generic version (this code) and
15     # whatever the driver needs to expose
16
17     def rspec_versions(self):
18         version_manager = VersionManager()
19         ad_rspec_versions = []
20         request_rspec_versions = []
21         for rspec_version in version_manager.versions:
22             if rspec_version.content_type in ['*', 'ad']:
23                 ad_rspec_versions.append(rspec_version.to_dict())
24             if rspec_version.content_type in ['*', 'request']:
25                 request_rspec_versions.append(rspec_version.to_dict())
26         return {
27             'geni_request_rspec_versions': request_rspec_versions,
28             'geni_ad_rspec_versions': ad_rspec_versions,
29             }
30
31     def get_rspec_version_string(self, rspec_version, options={}):
32         version_string = "rspec_%s" % (rspec_version)
33
34         #panos adding the info option to the caching key (can be improved)
35         if options.get('info'):
36             version_string = version_string + "_"+options.get('info', 'default')
37
38         # Adding the list_leases option to the caching key
39         if options.get('list_leases'):
40             version_string = version_string + "_"+options.get('list_leases', 'default')
41
42         # Adding geni_available to caching key
43         if options.get('geni_available'):
44             version_string = version_string + "_" + str(options.get('geni_available'))
45
46         return version_string
47
48     def GetVersion(self, api, options):
49         xrn=Xrn(api.hrn, type='authority')
50         version = version_core()
51         cred_types = [{'geni_type': 'geni_sfa', 'geni_version': str(i)} for i in range(4)[-2:]]
52         version_generic = {
53             'testbed': self.driver.testbed_name(),
54             'interface':'aggregate',
55             'hrn':xrn.get_hrn(),
56             'urn':xrn.get_urn(),
57             'geni_api': 3,
58             'geni_api_versions': {'3': 'http://%s:%s' % (socket.gethostname(), api.config.sfa_aggregate_port)},
59             'geni_single_allocation': 0, # Accept operations that act on as subset of slivers in a given state.
60             'geni_allocate': 'geni_many',# Multiple slivers can exist and be incrementally added, including those which connect or overlap in some way.
61             'geni_credential_types': cred_types,
62         }
63         version.update(version_generic)
64         version.update(self.rspec_versions())
65         testbed_version = self.driver.aggregate_version()
66         version.update(testbed_version)
67         return version
68     
69     def ListResources(self, api, creds, options):
70         call_id = options.get('call_id')
71         if Callids().already_handled(call_id): return ""
72
73         # get the rspec's return format from options
74         version_manager = VersionManager()
75         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
76         version_string = self.get_rspec_version_string(rspec_version, options)
77
78         # look in cache first
79         cached_requested = options.get('cached', True)
80         if cached_requested and self.driver.cache:
81             rspec = self.driver.cache.get(version_string)
82             if rspec:
83                 logger.debug("%s.ListResources returning cached advertisement" % (self.driver.__module__))
84                 return rspec
85        
86         rspec = self.driver.list_resources (rspec_version, options) 
87         if self.driver.cache:
88             logger.debug("%s.ListResources stores advertisement in cache" % (self.driver.__module__))
89             self.driver.cache.add(version_string, rspec)    
90         return rspec
91     
92     def Describe(self, api, creds, urns, options):
93         call_id = options.get('call_id')
94         if Callids().already_handled(call_id): return ""
95
96         version_manager = VersionManager()
97         rspec_version = version_manager.get_version(options.get('geni_rspec_version'))
98         return self.driver.describe(urns, rspec_version, options)
99         
100     
101     def Status (self, api, urns, creds, options):
102         call_id = options.get('call_id')
103         if Callids().already_handled(call_id): return {}
104         return self.driver.status (urns, options=options)
105    
106
107     def Allocate(self, api, xrn, creds, rspec_string, expiration, options):
108         """
109         Allocate resources as described in a request RSpec argument 
110         to a slice with the named URN.
111         """
112         call_id = options.get('call_id')
113         if Callids().already_handled(call_id): return ""
114         return self.driver.allocate(xrn, rspec_string, expiration, options)
115  
116     def Provision(self, api, xrns, creds, options):
117         """
118         Create the sliver[s] (slice) at this aggregate.    
119         Verify HRN and initialize the slice record in PLC if necessary.
120         """
121         call_id = options.get('call_id')
122         if Callids().already_handled(call_id): return ""
123
124         # make sure geni_rspec_version is specified in options
125         if 'geni_rspec_version' not in options:
126             msg = 'geni_rspec_version is required and must be set in options struct'
127             raise SfaInvalidArgument(msg, 'geni_rspec_version')
128         # make sure we support the requested rspec version
129         version_manager = VersionManager()
130         rspec_version = version_manager.get_version(options['geni_rspec_version']) 
131         if not rspec_version:
132             raise InvalidRSpecVersion(options['geni_rspec_version'])
133                        
134         return self.driver.provision(xrns, options)
135     
136     def Delete(self, api, xrns, creds, options):
137         call_id = options.get('call_id')
138         if Callids().already_handled(call_id): return True
139         return self.driver.delete(xrns, options)
140
141     def Renew(self, api, xrns, creds, expiration_time, options):
142         call_id = options.get('call_id')
143         if Callids().already_handled(call_id): return True
144         return self.driver.renew(xrns, expiration_time, options)
145
146     def PerformOperationalAction(self, api, xrns, creds, action, options={}):
147         call_id = options.get('call_id')
148         if Callids().already_handled(call_id): return True
149         return self.driver.perform_operational_action(xrns, action, options) 
150
151     def Shutdown(self, api, xrn, creds, options={}):
152         call_id = options.get('call_id')
153         if Callids().already_handled(call_id): return True
154         return self.driver.shutdown(xrn, options) 
155