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