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