15c89e08a9ebdb4ae0a273c7548e6ef691486024
[sfa.git] / sfa / managers / v2_to_v3_adapter.py
1 #
2 # an adapter on top of driver implementing AM API v2 to be AM API v3 compliant
3 #
4 import sys
5 from sfa.util.sfalogging import logger
6 from sfa.util.xrn import Xrn, urn_to_hrn, hrn_to_urn, get_leaf, get_authority
7 from sfa.util.cache import Cache
8 from sfa.rspecs.rspec import RSpec
9 from sfa.storage.model import SliverAllocation
10
11
12 class V2ToV3Adapter:
13
14     def __init__(self, api):
15         config = api.config
16         flavour = config.SFA_GENERIC_FLAVOUR
17         # to be cleaned
18         if flavour == "nitos":
19             from sfa.nitos.nitosdriver import NitosDriver
20             self.driver = NitosDriver(api)
21         elif flavour == "fd":
22             from sfa.federica.fddriver import FdDriver
23             self.driver = FdDriver(api)
24         else:
25             logger.error(
26                 "V2ToV3Adapter: Unknown Flavour !!!\n Supported Flavours: nitos, fd")
27
28         # Caching
29         if config.SFA_AGGREGATE_CACHING:
30             if self.driver.cache:
31                 self.cache = self.driver.cache
32             else:
33                 self.cache = Cache()
34
35     def __getattr__(self, name):
36         def func(*args, **kwds):
37             if name == "list_resources":
38                 (version, options) = args
39                 slice_urn = slice_hrn = None
40                 creds = []
41                 rspec = getattr(self.driver, "list_resources")(
42                     slice_urn, slice_hrn, [], options)
43                 result = rspec
44
45             elif name == "describe":
46                 (urns, version, options) = args
47                 slice_urn = urns[0]
48                 slice_hrn, type = urn_to_hrn(slice_urn)
49                 creds = []
50                 rspec = getattr(self.driver, "list_resources")(
51                     slice_urn, slice_hrn, creds, options)
52
53                 # SliverAllocation
54                 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
55                     constraint = SliverAllocation.slice_urn.in_(urns)
56                 else:
57                     constraint = SliverAllocation.sliver_id.in_(urns)
58
59                 sliver_allocations = self.driver.api.dbsession().query(
60                     SliverAllocation).filter(constraint)
61                 sliver_status = getattr(self.driver, "sliver_status")(
62                     slice_urn, slice_hrn)
63                 if 'geni_expires' in sliver_status.keys():
64                     geni_expires = sliver_status['geni_expires']
65                 else:
66                     geni_expires = ''
67
68                 geni_slivers = []
69                 for sliver_allocation in sliver_allocations:
70                     geni_sliver = {}
71                     geni_sliver['geni_expires'] = geni_expires
72                     geni_sliver[
73                         'geni_allocation'] = sliver_allocation.allocation_state
74                     geni_sliver[
75                         'geni_sliver_urn'] = sliver_allocation.sliver_id
76                     geni_sliver['geni_error'] = ''
77                     if geni_sliver['geni_allocation'] == 'geni_allocated':
78                         geni_sliver[
79                             'geni_operational_status'] = 'geni_pending_allocation'
80                     else:
81                         geni_sliver['geni_operational_status'] = 'geni_ready'
82                     geni_slivers.append(geni_sliver)
83
84                 result = {'geni_urn': slice_urn,
85                           'geni_rspec': rspec,
86                           'geni_slivers': geni_slivers}
87
88             elif name == "allocate":
89                 (slice_urn, rspec_string, expiration, options) = args
90                 slice_hrn, type = urn_to_hrn(slice_urn)
91                 creds = []
92                 users = options.get('sfa_users', [])
93                 manifest_string = getattr(self.driver, "create_sliver")(
94                     slice_urn, slice_hrn, creds, rspec_string, users, options)
95
96                 # slivers allocation
97                 rspec = RSpec(manifest_string)
98                 slivers = rspec.version.get_nodes_with_slivers()
99
100                 # SliverAllocation
101                 for sliver in slivers:
102                     client_id = sliver['client_id']
103                     component_id = sliver['component_id']
104                     component_name = sliver['component_name']
105                     slice_name = slice_hrn.replace('.', '-')
106                     component_short_name = component_name.split('.')[0]
107                     # self.driver.hrn
108                     sliver_hrn = '%s.%s-%s' % (self.driver.hrn,
109                                                slice_name, component_short_name)
110                     sliver_id = Xrn(sliver_hrn, type='sliver').urn
111                     record = SliverAllocation(sliver_id=sliver_id,
112                                               client_id=client_id,
113                                               component_id=component_id,
114                                               slice_urn=slice_urn,
115                                               allocation_state='geni_allocated')
116
117                     record.sync(self.driver.api.dbsession())
118
119                 # return manifest
120                 rspec_version = RSpec(rspec_string).version
121                 rspec_version_str = "%s" % rspec_version
122                 options['geni_rspec_version'] = {'version': rspec_version_str.split(
123                     ' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
124                 result = self.describe([slice_urn], rspec_version, options)
125
126             elif name == "provision":
127                 (urns, options) = args
128                 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
129                     constraint = SliverAllocation.slice_urn.in_(urns)
130                 else:
131                     constraint = SliverAllocation.sliver_id.in_(urns)
132
133                 dbsession = self.driver.api.dbsession()
134                 sliver_allocations = dbsession.query(
135                     SliverAllocation).filter(constraint)
136                 for sliver_allocation in sliver_allocations:
137                     sliver_allocation.allocation_state = 'geni_provisioned'
138
139                 dbsession.commit()
140                 result = self.describe(urns, '', options)
141
142             elif name == "status":
143                 urns = args
144                 options = {}
145                 options['geni_rspec_version'] = {
146                     'version': '3', 'type': 'GENI'}
147                 descr = self.describe(urns[0], '', options)
148                 result = {'geni_urn': descr['geni_urn'],
149                           'geni_slivers': descr['geni_slivers']}
150
151             elif name == "delete":
152                 (urns, options) = args
153                 slice_urn = urns[0]
154                 slice_hrn, type = urn_to_hrn(slice_urn)
155                 creds = []
156                 options['geni_rspec_version'] = {
157                     'version': '3', 'type': 'GENI'}
158                 descr = self.describe(urns, '', options)
159                 result = []
160                 for sliver_allocation in descr['geni_slivers']:
161                     geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
162                                    'geni_allocation_status': 'geni_unallocated',
163                                    'geni_expires': sliver_allocation['geni_expires'],
164                                    'geni_error': sliver_allocation['geni_error']}
165
166                     result.append(geni_sliver)
167
168                 getattr(self.driver, "delete_sliver")(
169                     slice_urn, slice_hrn, creds, options)
170
171                 # SliverAllocation
172                 constraints = SliverAllocation.slice_urn.in_(urns)
173                 dbsession = self.driver.api.dbsession()
174                 sliver_allocations = dbsession.query(
175                     SliverAllocation).filter(constraints)
176                 sliver_ids = [
177                     sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
178                 SliverAllocation.delete_allocations(sliver_ids, dbsession)
179
180             elif name == "renew":
181                 (urns, expiration_time, options) = args
182                 slice_urn = urns[0]
183                 slice_hrn, type = urn_to_hrn(slice_urn)
184                 creds = []
185
186                 getattr(self.driver, "renew_sliver")(
187                     slice_urn, slice_hrn, creds, expiration_time, options)
188
189                 options['geni_rspec_version'] = {
190                     'version': '3', 'type': 'GENI'}
191                 descr = self.describe(urns, '', options)
192                 result = descr['geni_slivers']
193
194             elif name == "perform_operational_action":
195                 (urns, action, options) = args
196                 options['geni_rspec_version'] = {
197                     'version': '3', 'type': 'GENI'}
198                 result = self.describe(urns, '', options)['geni_slivers']
199
200             else:
201                 # same as v2 ( registry methods)
202                 result = getattr(self.driver, name)(*args, **kwds)
203             return result
204         return func