the big cleanup: remove max aggregate manager
[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
25 class FdDriver (PlDriver):
26
27     def __init__(self, api):
28         PlDriver.__init__(self, api)
29         config = api.config
30         self.shell = FdShell(config)
31
32     # the agreement with the federica driver is for them to expose results in a way
33     # compliant with the avpi v2 return code, i.e. a dict with 'code' 'value' 'output'
34     # essentially, either 'code'==0, then 'value' is set to the actual result
35     # otherwise, 'code' is set to an error code and 'output' holds an error
36     # message
37     def response(self, from_xmlrpc):
38         if isinstance(from_xmlrpc, dict) and 'code' in from_xmlrpc:
39             if from_xmlrpc['code'] == 0:
40                 return from_xmlrpc['value']
41             else:
42                 raise SfaFault(from_xmlrpc['code'], from_xmlrpc['output'])
43         else:
44             logger.warning("unexpected result from federica xmlrpc api")
45             return from_xmlrpc
46
47     def aggregate_version(self):
48         result = {}
49         federica_version_string_api = self.response(
50             self.shell.getRSpecVersion())
51         result['federica_version_string_api'] = federica_version_string_api
52         if federica_version_string_api != federica_version_string:
53             result['WARNING'] = "hard-wired rspec version %d differs from what the API currently exposes" %\
54                 federica_version_string
55         return result
56
57     def testbed_name(self):
58         return "federica"
59
60     def list_slices(self, creds, options):
61         # the issue is that federica returns the list of slice's urn in a string format
62         # this is why this dirty hack is needed until federica fixes it.
63         slices_str = self.shell.listSlices()['value'][1:-1]
64         slices_list = slices_str.split(", ")
65         return slices_list
66
67     def sliver_status(self, slice_urn, slice_hrn):
68         return "fddriver.sliver_status: undefined/todo for slice %s" % slice_hrn
69
70     def list_resources(self, slice_urn, slice_hrn, creds, options):
71         # right now rspec_version is ignored on the federica side
72         # we normally derive it from options
73         # look in cache if client has requested so
74         cached_requested = options.get('cached', True)
75         # global advertisement
76         if not slice_hrn:
77             # self.cache is initialized unless the global config has it turned
78             # off
79             if cached_requested and self.cache:
80                 # using federica_version_string as the key into the cache
81                 rspec = self.cache.get(federica_version_string)
82                 if rspec:
83                     logger.debug(
84                         "FdDriver.ListResources: returning cached advertisement")
85                     return self.response(rspec)
86             # otherwise, need to get it
87                 # java code expects creds as a String
88 #            rspec = self.shell.listAvailableResources (creds, federica_version_string)
89             rspec = self.shell.listAvailableResources(
90                 "", federica_version_string)
91 #            rspec = self.shell.listAvailableResources (federica_version_string)
92             # cache it for future use
93             if self.cache:
94                 logger.debug(
95                     "FdDriver.ListResources: stores advertisement in cache")
96                 self.cache.add(federica_version_string, rspec)
97             return self.response(rspec)
98         # about a given slice : don't cache
99         else:
100                 # java code expects creds as a String
101             # return self.response(self.shell.listSliceResources(creds,
102             # federica_version_string, slice_urn))
103             return self.response(self.shell.listSliceResources("", federica_version_string, slice_urn))
104
105     def create_sliver(self, slice_urn, slice_hrn, creds, rspec_string, users, options):
106         # right now version_string is ignored on the federica side
107         # we normally derive it from options
108                 # java code expects creds as a String
109         # return self.response(self.shell.createSlice(creds, slice_urn,
110         # federica_version_string, rspec_string))
111         return self.response(self.shell.createSlice("", slice_urn, federica_version_string, rspec_string))
112
113     def delete_sliver(self, slice_urn, slice_hrn, creds, options):
114         # right now version_string is ignored on the federica side
115         # we normally derive it from options
116         # xxx not sure if that's currentl supported at all
117                 # java code expects creds as a String
118         #        return self.response(self.shell.deleteSlice(creds, slice_urn))
119         return self.response(self.shell.deleteSlice("", slice_urn))
120
121     # for the the following methods we use what is provided by the default driver class
122     # def renew_sliver (self, slice_urn, slice_hrn, creds, expiration_time, options):
123     # def start_slice (self, slice_urn, slice_xrn, creds):
124     # def stop_slice (self, slice_urn, slice_xrn, creds):
125     # def reset_slice (self, slice_urn, slice_xrn, creds):
126     # def get_ticket (self, slice_urn, slice_xrn, creds, rspec, options):