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