update v2_to_v3_adapter constructor
[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, api):
16         config = api.config
17         flavour = config.SFA_GENERIC_FLAVOUR
18         # to be cleaned
19         if flavour == "nitos":
20             from sfa.nitos.nitosdriver import NitosDriver
21             self.driver = NitosDriver(api)
22         elif flavour == "fd":
23             from sfa.federica.fddriver import FdDriver
24             self.driver = FdDriver(api)
25         elif flavour == "iotlab":
26             from sfa.iotlab.iotlabdriver import IotlabDriver
27             self.driver = IotlabDriver(api)
28         else:
29           logger.info("DriverV2Adapter unknown flavour !!!\n supported flavours: pl, nitos, fd, iotlab")
30          
31         # Caching 
32         if config.SFA_AGGREGATE_CACHING:
33             if self.driver.cache:
34                 self.cache = self.driver.cache
35             else:
36                 self.cache = Cache()
37
38
39     def __getattr__(self, name):
40         def func(*args, **kwds):
41             if name == "list_resources":
42                 (version, options) = args
43                 slice_urn = slice_hrn = None
44                 creds = []
45                 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, [], options) 
46                 result = rspec
47
48             elif name == "describe":
49                 (urns, version, options) = args
50                 slice_urn = urns[0]
51                 slice_hrn, type = urn_to_hrn(slice_urn)
52                 creds = []
53                 rspec = getattr(self.driver, "list_resources")(slice_urn, slice_hrn, creds, options)
54     
55                 # SliverAllocation
56                 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
57                     constraint = SliverAllocation.slice_urn.in_(urns)
58                 else:
59                     constraint = SliverAllocation.sliver_id.in_(urns)
60  
61                 sliver_allocations = dbsession.query (SliverAllocation).filter        (constraint)
62                 sliver_status = getattr(self.driver, "sliver_status")(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['geni_allocation'] = sliver_allocation.allocation_state
73                     geni_sliver['geni_sliver_urn'] = sliver_allocation.sliver_id
74                     geni_sliver['geni_error'] = ''
75                     if geni_sliver['geni_allocation'] == 'geni_allocated':
76                         geni_sliver['geni_operational_status'] = 'geni_pending_allocation'
77                     else: 
78                         geni_sliver['geni_operational_status'] = 'geni_ready'
79                     geni_slivers.append(geni_sliver)
80
81
82                 result = {'geni_urn': slice_urn,
83                 'geni_rspec': rspec,
84                 'geni_slivers': geni_slivers}
85
86             elif name == "allocate":
87                 (slice_urn, rspec_string, expiration, options) = args
88                 slice_hrn, type = urn_to_hrn(slice_urn)
89                 creds = []
90                 users = options.get('sfa_users', [])
91                 manifest_string = getattr(self.driver, "create_sliver")(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, slice_name, component_short_name)
106                      sliver_id = Xrn(sliver_hrn, type='sliver').urn
107                      record = SliverAllocation(sliver_id=sliver_id, 
108                                       client_id=client_id,
109                                       component_id=component_id,
110                                       slice_urn = slice_urn,
111                                       allocation_state='geni_allocated')    
112      
113                      record.sync()
114
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(' ')[1], 'type': rspec_version_str.lower().split(' ')[0]}
120                 result = self.describe([slice_urn], rspec_version, options)
121                 
122             elif name == "provision": 
123                 (urns, options) = args
124                 if len(urns) == 1 and Xrn(xrn=urns[0]).type == 'slice':
125                    constraint = SliverAllocation.slice_urn.in_(urns)
126                 else:
127                    constraint = SliverAllocation.sliver_id.in_(urns)
128                 
129                 sliver_allocations = dbsession.query (SliverAllocation).filter(constraint)
130                 for sliver_allocation in sliver_allocations:
131                      sliver_allocation.allocation_state = 'geni_provisioned'
132                 
133                 dbsession.commit()
134                 result = self.describe(urns, '', options)
135
136             elif name == "status":
137                 urns = args
138                 options = {}
139                 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
140                 descr = self.describe(urns[0], '', options)
141                 result = {'geni_urn': descr['geni_urn'],
142                           'geni_slivers': descr['geni_slivers']}
143
144             elif name == "delete":
145                 (urns, options) = args
146                 slice_urn = urns[0]
147                 slice_hrn, type = urn_to_hrn(slice_urn)
148                 creds = []
149                 options['geni_rspec_version'] = {'version': '3', 'type': 'GENI'}
150                 descr = self.describe(urns, '', options)
151                 result = []
152                 for sliver_allocation in descr['geni_slivers']:
153                      geni_sliver = {'geni_sliver_urn': sliver_allocation['geni_sliver_urn'],
154                                     'geni_allocation_status': 'geni_unallocated',
155                                     'geni_expires': sliver_allocation['geni_expires'],
156                                     'geni_error': sliver_allocation['geni_error']}
157                        
158                      result.append(geni_sliver)
159                      
160                 getattr(self.driver, "delete_sliver")(slice_urn, slice_hrn, creds, options) 
161              
162                 #SliverAllocation
163                 constraints = SliverAllocation.slice_urn.in_(urns)
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)
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