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