cec702d6aefafed4d2c2076d8bf0b8b2469ab32f
[sfa.git] / sfa / federica / fddriver.py
1 from sfa.util.sfalogging import logger
2 from sfa.util.faults import SfaFault
3
4 # this is probably too big to swallow but for a starting point..
5 from sfa.planetlab.pldriver import PlDriver
6
7 from sfa.federica.fdshell import FdShell
8
9 # hardwired for now
10 # this could/should be obtained by issuing getRSpecVersion
11 federica_version_string="RSpecV2"
12
13 #### avail. methods on the federica side as of 2012/02/13
14 # listAvailableResources(String credentials, String rspecVersion) 
15 # listSliceResources(String credentials, String rspecVersion, String sliceUrn) 
16 # createSlice(String credentials, String sliceUrn, String rspecVersion, String rspecString) 
17 # deleteSlice(String credentials, String sliceUrn) 
18 # listSlices() 
19 # getRSpecVersion()
20 ##### all return
21 # Result: {'code': 0, 'value': RSpec} if success
22 #       {'code': code_id, 'output': Error message} if error
23
24 class FdDriver (PlDriver):
25
26     def __init__ (self,api): 
27         PlDriver.__init__ (self, api)
28         config = api.config
29         self.shell=FdShell(config)
30
31     # the agreement with the federica driver is for them to expose results in a way
32     # compliant with the avpi v2 return code, i.e. a dict with 'code' 'value' 'output'
33     # essentially, either 'code'==0, then 'value' is set to the actual result
34     # otherwise, 'code' is set to an error code and 'output' holds an error message
35     def response (self, from_xmlrpc):
36         if isinstance (from_xmlrpc, dict) and 'code' in from_xmlrpc:
37             if from_xmlrpc['code']==0:
38                 return from_xmlrpc['value']
39             else:
40                 raise SfaFault(from_xmlrpc['code'],from_xmlrpc['output'])
41         else:
42             logger.warning("unexpected result from federica xmlrpc api")
43             return from_xmlrpc
44
45     def aggregate_version (self):
46         result={}
47         federica_version_string_api = self.response(self.shell.getRSpecVersion())
48         result ['federica_version_string_api']=federica_version_string_api
49         if federica_version_string_api != federica_version_string:
50             result['WARNING']="hard-wired rspec version %d differs from what the API currently exposes"%\
51                         federica_version_string
52         return result
53
54     def testbed_name (self):
55         return "federica"
56
57     def list_slices (self, creds, options):
58         # the issue is that federica returns the list of slice's urn in a string format
59         # this is why this dirty hack is needed until federica fixes it. 
60         slices_str = self.shell.listSlices()['value'][1:-1]
61         slices_list = slices_str.split(", ")
62         return slices_list
63
64     def sliver_status (self, slice_urn, slice_hrn):
65         return "fddriver.sliver_status: undefined/todo for slice %s"%slice_hrn
66
67     def list_resources (self, slice_urn, slice_hrn, creds, options):
68         # right now rspec_version is ignored on the federica side
69         # we normally derive it from options
70         # look in cache if client has requested so
71         cached_requested = options.get('cached', True) 
72         # global advertisement
73         if not slice_hrn:
74             # self.cache is initialized unless the global config has it turned off
75             if cached_requested and self.cache:
76                 # using federica_version_string as the key into the cache
77                 rspec = self.cache.get(federica_version_string)
78                 if rspec:
79                     logger.debug("FdDriver.ListResources: returning cached advertisement")
80                     return self.response(rspec)
81             # otherwise, need to get it
82                 # java code expects creds as a String
83 #            rspec = self.shell.listAvailableResources (creds, federica_version_string)
84             rspec = self.shell.listAvailableResources ("", federica_version_string)
85 #            rspec = self.shell.listAvailableResources (federica_version_string)
86             # cache it for future use
87             if self.cache:
88                 logger.debug("FdDriver.ListResources: stores advertisement in cache")
89                 self.cache.add(federica_version_string, rspec)
90             return self.response(rspec)
91         # about a given slice : don't cache
92         else:
93                 # java code expects creds as a String
94 #            return self.response(self.shell.listSliceResources(creds, federica_version_string, slice_urn))
95             return self.response(self.shell.listSliceResources("", federica_version_string, slice_urn))
96
97     def create_sliver (self, slice_urn, slice_hrn, creds, rspec_string, users, options):
98         # right now version_string is ignored on the federica side
99         # we normally derive it from options
100                 # java code expects creds as a String
101 #        return self.response(self.shell.createSlice(creds, slice_urn, federica_version_string, rspec_string))
102         return self.response(self.shell.createSlice("", slice_urn, federica_version_string, rspec_string))
103
104     def delete_sliver (self, slice_urn, slice_hrn, creds, options):
105         # right now version_string is ignored on the federica side
106         # we normally derive it from options
107         # xxx not sure if that's currentl supported at all
108                 # java code expects creds as a String
109 #        return self.response(self.shell.deleteSlice(creds, slice_urn))
110         return self.response(self.shell.deleteSlice("", slice_urn))
111
112     # for the the following methods we use what is provided by the default driver class
113     #def renew_sliver (self, slice_urn, slice_hrn, creds, expiration_time, options):
114     #def start_slice (self, slice_urn, slice_xrn, creds):
115     #def stop_slice (self, slice_urn, slice_xrn, creds):
116     #def reset_slice (self, slice_urn, slice_xrn, creds):
117     #def get_ticket (self, slice_urn, slice_xrn, creds, rspec, options):