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