call logging
[sfa.git] / sfa / util / geniclient.py
1 ##
2 # This module implements the client-side of the Geni API. Stubs are provided
3 # that convert the supplied parameters to the necessary format and send them
4 # via XMLRPC to a Geni Server.
5 #
6 # TODO: Investigate ways to combine this with existing PLC API?
7 ##
8
9 ### $Id$
10 ### $URL$
11
12 from sfa.trust.gid import *
13 from sfa.trust.credential import *
14 from sfa.util.record import *
15 from sfa.util.sfaticket import SfaTicket
16
17 ##
18 # The GeniClient class provides stubs for executing Geni operations. A given
19 # client object connects to one server. To connect to multiple servers, create
20 # multiple GeniClient objects.
21 #
22 # The Geni protocol uses an HTTPS connection, and the client's side of the
23 # connection uses his private key. Generally, this private key must match the
24 # public key that is containing in the GID that the client is providing for
25 # those functions that take a GID.
26
27 class GeniClient:
28     ##
29     # Create a new GeniClient object.
30     #
31     # @param url is the url of the server
32     # @param key_file = private key file of client
33     # @param cert_file = x.509 cert containing the client's public key. This
34     #      could be a GID certificate, or any x.509 cert.
35     # @param protocol The ORPC protocol to use. Can be "soap" or "xmlrpc"
36
37     def __init__(self, url, key_file, cert_file, protocol="xmlrpc"):
38        self.url = url
39        self.key_file = key_file
40        self.cert_file = cert_file
41
42        if (protocol=="xmlrpc"):
43            import xmlrpcprotocol  
44            self.server = xmlrpcprotocol.get_server(self.url, self.key_file, self.cert_file)
45        elif (protocol=="soap"):
46            import soapprotocol
47            self.server = soapprotocol.get_server(self.url, self.key_file, self.cert_file)
48        else:
49            raise Exception("Attempted use of undefined protocol %s"%protocol)
50
51
52     # -------------------------------------------------------------------------
53     # Registry Interface
54     # -------------------------------------------------------------------------
55
56     ##
57     # Create a new GID. For MAs and SAs that are physically located on the
58     # registry, this allows a owner/operator/PI to create a new GID and have it
59     # signed by his respective authority.
60     #
61     # @param cred credential of caller
62     # @param name hrn for new GID
63     # @param uuid unique identifier for new GID
64     # @param pkey_string public-key string (TODO: why is this a string and not a keypair object?)
65     #
66     # @return a GID object
67
68     def create_gid(self, cred, name, uuid, pkey_string):
69         gid_str = self.server.create_gid(cred.save_to_string(save_parents=True), name, uuid, pkey_string)
70         return GID(string=gid_str)
71
72     ##
73     # Retrieve the GID for an object. This function looks up a record in the
74     # registry and returns the GID of the record if it exists.
75     # TODO: Is this function needed? It's a shortcut for Resolve()
76     #
77     # @param name hrn to look up
78     #
79     # @return a GID object
80
81     def get_gid(self, name):
82        gid_str_list = self.server.get_gid(name)
83        gid_list = []
84        for str in gid_str_list:
85            gid_list.append(GID(string=str))
86        return gid_list
87
88     ##
89     # Get_self_credential a degenerate version of get_credential used by a
90     # client to get his initial credential when he doesn't have one. This is
91     # the same as get_credential(..., cred=None,...).
92     #
93     # The registry ensures that the client is the principal that is named by
94     # (type, name) by comparing the public key in the record's GID to the
95     # private key used to encrypt the client-side of the HTTPS connection. Thus
96     # it is impossible for one principal to retrieve another principal's
97     # credential without having the appropriate private key.
98     #
99     # @param type type of object (user | slice | sa | ma | node
100     # @param name human readable name of object
101     #
102     # @return a credential object
103
104     def get_self_credential(self, type, name):
105         cred_str = self.server.get_self_credential(type, name)
106         return Credential(string = cred_str)
107
108     ##
109     # Retrieve a credential for an object.
110     #
111     # If cred==None, then the behavior reverts to get_self_credential()
112     #
113     # @param cred credential object specifying rights of the caller
114     # @param type type of object (user | slice | sa | ma | node)
115     # @param name human readable name of object
116     #
117     # @return a credental object
118
119     def get_credential(self, cred, type, name):
120         if cred:
121             cred = cred.save_to_string(save_parents=True) 
122         cred_str = self.server.get_credential(cred, type, name)
123         return Credential(string = cred_str)
124
125     ##
126     # List the records in an authority. The objectGID in the supplied credential
127     # should name the authority that will be listed.
128     #
129     # @param cred credential object specifying rights of the caller
130     #
131     # @return list of record objects
132
133     def list(self, cred, auth_hrn, caller_cred=None):
134         result_dict_list = self.server.list(cred.save_to_string(save_parents=True), auth_hrn, caller_cred)
135         result_rec_list = []
136         for dict in result_dict_list:
137              result_rec_list.append(GeniRecord(dict=dict))
138         return result_rec_list
139
140     ##
141     # Register an object with the registry. In addition to being stored in the
142     # Geni database, the appropriate records will also be created in the
143     # PLC databases.
144     #
145     #
146     #
147     # @param cred credential object specifying rights of the caller
148     # @return record to register
149     #
150     # @return GID object for the newly-registered record
151
152     def register(self, cred, record, caller_cred=None):
153         gid_str = self.server.register(cred.save_to_string(save_parents=True), record.as_dict(), caller_cred)
154         return GID(string = gid_str)
155
156     ##
157     # Remove an object from the registry. If the object represents a PLC object,
158     # then the PLC records will also be removed.
159     #
160     # @param cred credential object specifying rights of the caller
161     # @param type
162     # @param hrn
163
164     def remove(self, cred, type, hrn, caller_cred=None):
165         result = self.server.remove(cred.save_to_string(save_parents=True), type, hrn, caller_cred)
166         return result
167
168     ##
169     # Resolve an object in the registry. A given HRN may have multiple records
170     # associated with it, and therefore multiple records may be returned. The
171     # caller should check the type fields of the records to find the one that
172     # he is interested in.
173     #
174     # @param cred credential object specifying rights of the caller
175     # @param name human readable name of object
176
177     def resolve(self, cred, name, caller_cred=None):
178         result_dict_list = self.server.resolve(cred.save_to_string(save_parents=True), name, caller_cred)
179         result_rec_list = []
180         for dict in result_dict_list:
181             if dict['type'] in ['authority']:
182                 result_rec_list.append(AuthorityRecord(dict=dict))
183             elif dict['type'] in ['node']:
184                 result_rec_list.append(NodeRecord(dict=dict))
185             elif dict['type'] in ['slice']:
186                 result_rec_list.append(SliceRecord(dict=dict))
187             elif dict['type'] in ['user']:
188                 result_rec_list.append(UserRecord(dict=dict))
189             else:
190                 result_rec_list.append(GeniRecord(dict=dict))
191         return result_rec_list
192
193     ##
194     # Update an object in the registry. Currently, this only updates the
195     # PLC information associated with the record. The Geni fields (name, type,
196     # GID) are fixed.
197     #
198     #
199     #
200     # @param cred credential object specifying rights of the caller
201     # @param record a record object to be updated
202
203     def update(self, cred, record, caller_cred=None):
204         result = self.server.update(cred.save_to_string(save_parents=True), record.as_dict(), caller_cred)
205         return result
206
207
208     #-------------------------------------------------------------------------
209     # Aggregate Interface
210     #-------------------------------------------------------------------------
211     
212     ## list resources
213     #
214     # @param cred a credential
215     # @param hrn slice hrn
216
217     def get_resources(self, cred, hrn=None, caller_cred=None):
218         result = self.server.get_resources(cred.save_to_string(save_parents=True), hrn, caller_cred)
219         return result
220
221     def get_aggregates(self, cred, hrn=None):
222         result = self.server.get_aggregates(cred.save_to_string(save_parents=True), hrn)
223         return result
224
225     def get_registries(self, cred, hrn=None):
226         result = self.server.get_registries(cred.save_to_string(save_parents=True), hrn)
227         return result
228
229     ## get policy
230     #
231     # @param cred a credential
232
233     def get_policy(self, cred):
234         result = self.server.get_policy(cred.save_to_string(save_parents=True))
235         return result
236
237     ## create slice
238     #
239     # @param cred a credential
240     # @param rspec resource specification defining how to instantiate the slice
241     
242     def create_slice(self, cred, hrn, rspec, caller_cred=None):
243         result = self.server.create_slice(cred.save_to_string(save_parents=True), hrn, rspec, caller_cred)
244         return result
245
246
247     ## delete slice
248     #
249     # @param cred a credential
250     # @param hrn slice to delete
251     def delete_slice(self, cred, hrn, caller_cred=None):
252         result = self.server.delete_slice(cred.save_to_string(save_parents=True), hrn, caller_cred)
253         return result    
254
255     # ------------------------------------------------------------------------
256     # Slice Interface
257     # ------------------------------------------------------------------------
258
259     ##
260     # Start a slice.
261     #
262     # @param cred a credential identifying the caller (callerGID) and the slice
263     #     (objectGID)
264
265     def start_slice(self, cred, hrn):
266         result = self.server.start_slice(cred.save_to_string(save_parents=True), hrn)
267         return result
268
269     ##
270     # Stop a slice.
271     #
272     # @param cred a credential identifying the caller (callerGID) and the slice
273     #     (objectGID)
274
275     def stop_slice(self, cred, hrn):
276         result = self.server.stop_slice(cred.save_to_string(save_parents=True), hrn)
277         return result
278
279     ##
280     # Reset a slice.
281     #
282     # @param cred a credential identifying the caller (callerGID) and the slice
283     #     (objectGID)
284
285     def reset_slice(self, cred, hrn):
286         result = self.server.reset_slice(cred.save_to_string(save_parents=True), hrn)
287         return result
288
289     ##
290     # Delete a slice.
291     #
292     # @param cred a credential identifying the caller (callerGID) and the slice
293     #     (objectGID)
294
295     def delete_slice(self, cred, hrn, caller_cred=None):
296         result = self.server.delete_slice(cred.save_to_string(save_parents=True), hrn, caller_cred)
297         return result
298
299     ##
300     # List the slices on a component.
301     #
302     # @param cred credential object that authorizes the caller
303     #
304     # @return a list of slice names
305
306     def get_slices(self, cred):
307         result = self.server.get_slices(cred.save_to_string(save_parents=True))
308         return result
309
310     ##
311     # Retrieve a ticket. This operation is currently implemented on the
312     # registry (see SFA, engineering decisions), and is not implemented on
313     # components.
314     #
315     # The ticket is filled in with information from the PLC database. This
316     # information includes resources, and attributes such as user keys and
317     # initscripts.
318     #
319     # @param cred credential object
320     # @param name name of the slice to retrieve a ticket for
321     # @param rspec resource specification dictionary
322     #
323     # @return a ticket object
324
325     def get_ticket(self, cred, name, rspec):
326         ticket_str = self.server.get_ticket(cred.save_to_string(save_parents=True), name, rspec)
327         ticket = SfaTicket(string=ticket_str)
328         return ticket
329
330     ##
331     # Redeem a ticket. This operation is currently implemented on the
332     # component.
333     #
334     # The ticket is submitted to the node manager, and the slice is instantiated
335     # or updated as appropriate.
336     #
337     # TODO: This operation should return a sliver credential and indicate
338     # whether or not the component will accept only sliver credentials, or
339     # will accept both sliver and slice credentials.
340     #
341     # @param ticket a ticket object containing the ticket
342
343     def redeem_ticket(self, ticket):
344         result = self.server.redeem_ticket(ticket.save_to_string(save_parents=True))
345         return result
346
347