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