first stab at a design where each incoming API call has its own dbsession
[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 # xxx 1-dbsession-per-request
11 from sfa.storage.alchemy import dbsession
12
13 class V2ToV3Adapter:
14
15     def __init__ (self, 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(config)
21         elif flavour == "fd":
22             from sfa.federica.fddriver import FdDriver
23             self.driver = FdDriver(config)
24         elif flavour == "dummy":
25             from sfa.dummy.dummydriver import DummyDriver
26             self.driver = DummyDriver(config)
27         elif flavour == "slab":
28             from sfa.senslab.slabdriver import SlabDriver
29             self.driver = SlabDriver(config)
30         else:
31           logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, dummy, slab")
32          
33         # Caching 
34         if config.SFA_AGGREGATE_CACHING:
35             if self.driver.cache:
36                 self.cache = self.driver.cache
37             else:
38                 self.cache = Cache()
39
40
41     def __getattr__(self, name):
42         def func(*args, **kwds):
43             if name == "list_resources":
44                 (version, options) = args
45                 slice_urn = slice_hrn = None
46                 creds = []
47                 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options) 
48                 result = rspec
49
50             elif name == "describe":
51                 (urns, version, options) = args
52                 slice_urn = urns[0]
53                 slice_hrn, type = urn_to_hrn(slice_urn)
54                 creds = []
55                 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
56     
57                 # SliverAllocation
58                 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
59                     constraint = SliverAllocation.slice_urn.in_(urns)
60                 else:
61                     constraint = SliverAllocation.sliver_id.in_(urns)
62  
63                 sliver_allocations = dbsession.query (SliverAllocation).filter        (constraint)
64                 sliver_status = getattr(self.driver, "sliver_status")(slice_urn, slice_hrn)               
65                 if 'geni_expires' in sliver_status.keys():
66                     geni_expires = sliver_status['geni_expires']
67                 else: 
68                     geni_expires = ''
69                 
70                 geni_slivers = []
71                 for sliver_allocation in sliver_allocations:
72                     geni_sliver = {}
73                     geni_sliver['geni_expires'] = geni_expires
74                     geni_sliver['geni_allocation'] = sliver_allocation.allocation_state
75                     geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
76                     geni_sliver['geni_error'] = ''
77                     if geni_sliver['geni_allocation'] == 'geni_allocated':
78                         geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
79                     else: 
80                         geni_sliver['geni_operational_status'] = 'geni_ready'
81                     geni_slivers.append(geni_sliver)
82
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")(slice_urn, slice_hrn, creds, rspec_string, users, options)
94                 
95                 # slivers allocation
96                 rspec = RSpec(manifest_string)
97                 slivers = rspec.version.get_nodes_with_slivers()
98                 
99                 ##SliverAllocation
100                 for sliver in slivers:
101                      client_id = sliver['client_id']
102                      component_id = sliver['component_id']
103                      component_name = sliver['component_name']
104                      slice_name = slice_hrn.replace('.','-')
105                      component_short_name = component_name.split('.')[0]
106                      # self.driver.hrn
107                      sliver_hrn = '%s.%s-%s' % (self.driver.hrn, slice_name, component_short_name)
108                      sliver_id = Xrn(sliver_hrn, type='sliver').urn
109                      record = SliverAllocation(sliver_id=sliver_id, 
110                                       client_id=client_id,
111                                       component_id=component_id,
112                                       slice_urn = slice_urn,
113                                       allocation_state='geni_allocated')    
114      
115                      record.sync()
116
117                
118                 # return manifest
119                 rspec_version = RSpec(rspec_string).version
120                 rspec_version_str = "%s"%rspec_version
121                 options['geni_rspec_version'] = {'version': rspec_version_str.split(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
122                 result = self.describe([slice_urn], rspec_version, options)
123                 
124             elif name == "provision": 
125                 (urns, options) = args
126                 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
127                    constraint = SliverAllocation.slice_urn.in_(urns)
128                 else:
129                    constraint = SliverAllocation.sliver_id.in_(urns)
130                 
131                 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
132                 for sliver_allocation in sliver_allocations:
133                      sliver_allocation.allocation_state = 'geni_provisioned'
134                 
135                 dbsession.commit()
136                 result = self.describe(urns, '', options)
137
138             elif name == "status":
139                 urns = args
140                 options = {}
141                 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
142                 descr = self.describe(urns[0], '', options)
143                 result = {'geni_urn': descr['geni_urn'],
144                           'geni_slivers': descr['geni_slivers']}
145
146             elif name == "delete":
147                 (urns, options) = args
148                 slice_urn = urns[0]
149                 slice_hrn, type = urn_to_hrn(slice_urn)
150                 creds = []
151                 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
152                 descr = self.describe(urns, '', options)
153                 result = []
154                 for sliver_allocation in descr['geni_slivers']:
155                      geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
156                                     'geni_allocation_status': 'geni_unallocated',
157                                     'geni_expires': sliver_allocation['geni_expires'],
158                                     'geni_error': sliver_allocation['geni_error']}
159                        
160                      result.append(geni_sliver)
161                      
162                 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options) 
163              
164                 #SliverAllocation
165                 constraints = SliverAllocation.slice_urn.in_(urns)
166                 sliver_allocations = dbsession.query(SliverAllocation).filter(constraints)
167                 sliver_ids = [sliver_allocation.sliver_id for sliver_allocation in sliver_allocations]
168                 SliverAllocation.delete_allocations(sliver_ids)
169                 
170
171             elif name == "renew":
172                 (urns, expiration_time, options) = args
173                 slice_urn = urns[0]    
174                 slice_hrn, type = urn_to_hrn(slice_urn)
175                 creds = []
176
177                 getattr(self.driver, "renew_sliver")(slice_urn, slice_hrn, creds, expiration_time, options)
178
179                 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
180                 descr = self.describe(urns, '', options)
181                 result = descr['geni_slivers']
182                 
183
184             elif name == "perform_operational_action":
185                 (urns, action, options) = args
186                 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
187                 result = self.describe(urns, '', options)['geni_slivers']
188
189
190             else: 
191                 # same as v2 ( registry methods) 
192                 result=getattr(self.driver, name)(*args, **kwds)
193             return result
194         return func