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