a8c3af4b89b6d104ae5fb0e83078d93b7def77ee
[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=None):
33         if options is None:
34             options = {}
35         version_string = "rspec_%s" % (rspec_version)
36
37         # panos adding the info option to the caching key (can be improved)
38         if options.get('info'):
39             version_string = version_string + \
40                 "_" + options.get('info', 'default')
41
42         # Adding the list_leases option to the caching key
43         if options.get('list_leases'):
44             version_string = version_string + "_" + \
45                 options.get('list_leases', 'default')
46
47         # Adding geni_available to caching key
48         if options.get('geni_available'):
49             version_string = version_string + "_" + \
50                 str(options.get('geni_available'))
51
52         return version_string
53
54     def GetVersion(self, api, options):
55         xrn = Xrn(api.hrn, type='authority+am')
56         version = version_core()
57         cred_types = [{'geni_type': 'geni_sfa',
58                        'geni_version': str(i)} for i in range(4)[-2:]]
59         geni_api_versions = ApiVersions().get_versions()
60         geni_api_versions[
61             '3'] = 'http://%s:%s' % (api.config.sfa_aggregate_host, api.config.sfa_aggregate_port)
62         version_generic = {
63             'testbed': api.driver.testbed_name(),
64             'interface': 'aggregate',
65             'sfa': 3,
66             'hrn': xrn.get_hrn(),
67             'urn': xrn.get_urn(),
68             'geni_api': 3,
69             'geni_api_versions': geni_api_versions,
70             # Accept operations that act on as subset of slivers in a given
71             # state.
72             'geni_single_allocation': 0,
73             # Multiple slivers can exist and be incrementally added, including
74             # those which connect or overlap in some way.
75             'geni_allocate': 'geni_many',
76             'geni_credential_types': cred_types,
77             'geni_handles_speaksfor': True,     # supports 'speaks for' credentials
78         }
79         version.update(version_generic)
80         version.update(self.rspec_versions())
81         testbed_version = api.driver.aggregate_version()
82         version.update(testbed_version)
83         return version
84
85     def ListResources(self, api, creds, options):
86         call_id = options.get('call_id')
87         if Callids().already_handled(call_id):
88             return ""
89
90         # get the rspec's return format from options
91         version_manager = VersionManager()
92         rspec_version = version_manager.get_version(
93             options.get('geni_rspec_version'))
94         version_string = self.get_rspec_version_string(rspec_version, options)
95
96         # look in cache first
97         cached_requested = options.get('cached', True)
98         if cached_requested and api.driver.cache:
99             rspec = api.driver.cache.get(version_string)
100             if rspec:
101                 logger.debug("%s.ListResources returning cached advertisement" % (
102                     api.driver.__module__))
103                 return rspec
104
105         rspec = api.driver.list_resources(rspec_version, options)
106         if api.driver.cache:
107             logger.debug("%s.ListResources stores advertisement in cache" % (
108                 api.driver.__module__))
109             api.driver.cache.add(version_string, rspec)
110         return rspec
111
112     def Describe(self, api, creds, urns, options):
113         call_id = options.get('call_id')
114         if Callids().already_handled(call_id):
115             return ""
116
117         version_manager = VersionManager()
118         rspec_version = version_manager.get_version(
119             options.get('geni_rspec_version'))
120         return api.driver.describe(urns, rspec_version, options)
121
122     def Status(self, api, urns, creds, options):
123         call_id = options.get('call_id')
124         if Callids().already_handled(call_id):
125             return {}
126         return api.driver.status(urns, options=options)
127
128     def Allocate(self, api, xrn, creds, rspec_string, expiration, options):
129         """
130         Allocate resources as described in a request RSpec argument 
131         to a slice with the named URN.
132         """
133         call_id = options.get('call_id')
134         if Callids().already_handled(call_id):
135             return ""
136         return api.driver.allocate(xrn, rspec_string, expiration, options)
137
138     def Provision(self, api, xrns, creds, options):
139         """
140         Create the sliver[s] (slice) at this aggregate.    
141         Verify HRN and initialize the slice record in PLC if necessary.
142         """
143         call_id = options.get('call_id')
144         if Callids().already_handled(call_id):
145             return ""
146
147         # make sure geni_rspec_version is specified in options
148         if 'geni_rspec_version' not in options:
149             msg = 'geni_rspec_version is required and must be set in options struct'
150             raise SfaInvalidArgument(msg, 'geni_rspec_version')
151         # make sure we support the requested rspec version
152         version_manager = VersionManager()
153         rspec_version = version_manager.get_version(
154             options['geni_rspec_version'])
155         if not rspec_version:
156             raise InvalidRSpecVersion(options['geni_rspec_version'])
157
158         return api.driver.provision(xrns, options)
159
160     def Delete(self, api, xrns, creds, options):
161         call_id = options.get('call_id')
162         if Callids().already_handled(call_id):
163             return True
164         return api.driver.delete(xrns, options)
165
166     def Renew(self, api, xrns, creds, expiration_time, options):
167         call_id = options.get('call_id')
168         if Callids().already_handled(call_id):
169             return True
170
171         return api.driver.renew(xrns, expiration_time, options)
172
173     def PerformOperationalAction(self, api, xrns, creds, action, options=None):
174         if options is None:
175             options = {}
176         call_id = options.get('call_id')
177         if Callids().already_handled(call_id):
178             return True
179         return api.driver.perform_operational_action(xrns, action, options)
180
181     def Shutdown(self, api, xrn, creds, options=None):
182         if options is None:
183             options = {}
184         call_id = options.get('call_id')
185         if Callids().already_handled(call_id):
186             return True
187         return api.driver.shutdown(xrn, options)