7299f27d63eb6d1ac5cbc3157094a5ecea68488a
[sfa.git] / sfa / util / faults.py
1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
13 #
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS 
21 # IN THE WORK.
22 #----------------------------------------------------------------------
23 #
24 # SFA API faults
25 #
26
27 try:
28     from xmlrpc.client import Fault as xmlrpcFault
29 except:
30     from xmlrpclib import Fault as xmlrpcFault
31     
32 from sfa.util.genicode import GENICODE
33
34 class SfaFault(xmlrpcFault):
35     def __init__(self, faultCode, faultString, extra = None):
36         if extra:
37             faultString += ": " + str(extra)
38         xmlrpcFault.__init__(self, faultCode, faultString)
39
40 class Forbidden(SfaFault):
41     def __init__(self,  extra = None):
42         faultString = "FORBIDDEN" 
43         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)   
44
45 class BadArgs(SfaFault):
46     def __init__(self,  extra = None):
47         faultString = "BADARGS"
48         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
49
50
51 class CredentialMismatch(SfaFault):
52     def __init__(self,  extra = None):
53         faultString = "Credential mismatch"
54         SfaFault.__init__(self, GENICODE.CREDENTIAL_MISMATCH, faultString, extra) 
55
56 class SfaInvalidAPIMethod(SfaFault):
57     def __init__(self, method, interface = None, extra = None):
58         faultString = "Invalid method " + method
59         if interface:
60             faultString += " for interface " + interface
61         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
62
63 class SfaInvalidArgumentCount(SfaFault):
64     def __init__(self, got, min, max = min, extra = None):
65         if min != max:
66             expected = "%d-%d" % (min, max)
67         else:
68             expected = "%d" % min
69         faultString = "Expected %s arguments, got %d" % \
70                       (expected, got)
71         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
72
73 class SfaInvalidArgument(SfaFault):
74     def __init__(self, extra = None, name = None):
75         if name is not None:
76             faultString = "Invalid %s value" % name
77         else:
78             faultString = "Invalid argument"
79         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
80
81 class SfaAuthenticationFailure(SfaFault):
82     def __init__(self, extra = None):
83         faultString = "Failed to authenticate call"
84         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
85
86 class SfaDBError(SfaFault):
87     def __init__(self, extra = None):
88         faultString = "Database error"
89         SfaFault.__init__(self, GENICODE.DBERROR, faultString, extra)
90
91 class SfaPermissionDenied(SfaFault):
92     def __init__(self, extra = None):
93         faultString = "Permission denied"
94         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
95
96 class SfaNotImplemented(SfaFault):
97     def __init__(self, interface=None, extra = None):
98         faultString = "Not implemented"
99         if interface:
100             faultString += " at interface " + interface 
101         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
102
103 class SfaAPIError(SfaFault):
104     def __init__(self, extra = None):
105         faultString = "Internal SFA API error"
106         SfaFault.__init__(self, GENICODE.SERVERERROR, faultString, extra)
107
108 class MalformedHrnException(SfaFault):
109     def __init__(self, value, extra = None):
110         self.value = value
111         faultString = "Malformed HRN: %(value)s" % locals()
112         SfaFault.__init__(self, GENICODE.ERROR, extra)
113     def __str__(self):
114         return repr(self.value)
115
116 class TreeException(SfaFault):
117     def __init__(self, value, extra = None):
118         self.value = value
119         faultString = "Tree Exception: %(value)s, " % locals()
120         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
121     def __str__(self):
122         return repr(self.value)
123
124 class SearchFailed(SfaFault):
125     def __init__(self, value, extra = None):
126         self.value = value
127         faultString = "%s does not exist here " % self.value
128         SfaFault.__init__(self, GENICODE.SEARCHFAILED, faultString, extra)
129     def __str__(self):
130         return repr(self.value)
131
132 class NonExistingRecord(SfaFault):
133     def __init__(self, value, extra = None):
134         self.value = value
135         faultString = "Non exsiting record %(value)s, " % locals()
136         SfaFault.__init__(self, GENICODE.SEARCHFAILED, faultString, extra)
137     def __str__(self):
138         return repr(self.value)
139
140 class ExistingRecord(SfaFault):
141     def __init__(self, value, extra = None):
142         self.value = value
143         faultString = "Existing record: %(value)s, " % locals()
144         SfaFault.__init__(self, GENICODE.REFUSED, faultString, extra)
145     def __str__(self):
146         return repr(self.value)
147
148     
149 class InvalidRPCParams(SfaFault):
150     def __init__(self, value, extra = None):
151         self.value = value
152         faultString = "Invalid RPC Params: %(value)s, " % locals()
153         SfaFault.__init__(self, GENICODE.RPCERROR, faultString, extra)
154     def __str__(self):
155         return repr(self.value)
156
157 # SMBAKER exceptions follow
158
159 class ConnectionKeyGIDMismatch(SfaFault):
160     def __init__(self, value, extra = None):
161         self.value = value
162         faultString = "Connection Key GID mismatch: %(value)s" % locals()
163         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra) 
164     def __str__(self):
165         return repr(self.value)
166
167 class MissingCallerGID(SfaFault):
168     def __init__(self, value, extra = None):
169         self.value = value
170         faultString = "Missing Caller GID: %(value)s" % locals()
171         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra) 
172     def __str__(self):
173         return repr(self.value)
174
175 class RecordNotFound(SfaFault):
176     def __init__(self, value, extra = None):
177         self.value = value
178         faultString = "Record not found: %(value)s" % locals()
179         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
180     def __str__(self):
181         return repr(self.value)
182
183 class UnknownSfaType(SfaFault):
184     def __init__(self, value, extra = None):
185         self.value = value
186         faultString = "Unknown SFA Type: %(value)s" % locals()
187         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
188     def __str__(self):
189         return repr(self.value)
190
191 class MissingAuthority(SfaFault):
192     def __init__(self, value, extra = None):
193         self.value = value
194         faultString = "Missing authority: %(value)s" % locals()
195         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
196     def __str__(self):
197         return repr(self.value)
198
199 class PlanetLabRecordDoesNotExist(SfaFault):
200     def __init__(self, value, extra = None):
201         self.value = value
202         faultString = "PlanetLab record does not exist : %(value)s" % locals()
203         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
204     def __str__(self):
205         return repr(self.value)
206
207 class PermissionError(SfaFault):
208     def __init__(self, value, extra = None):
209         self.value = value
210         faultString = "Permission error: %(value)s" % locals()
211         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
212     def __str__(self):
213         return repr(self.value)
214
215 class InsufficientRights(SfaFault):
216     def __init__(self, value, extra = None):
217         self.value = value
218         faultString = "Insufficient rights: %(value)s" % locals()
219         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
220     def __str__(self):
221         return repr(self.value)
222
223 class MissingDelegateBit(SfaFault):
224     def __init__(self, value, extra = None):
225         self.value = value
226         faultString = "Missing delegate bit: %(value)s" % locals()
227         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
228     def __str__(self):
229         return repr(self.value)
230
231 class ChildRightsNotSubsetOfParent(SfaFault):
232     def __init__(self, value, extra = None):
233         self.value = value
234         faultString = "Child rights not subset of parent: %(value)s" % locals()
235         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra)
236     def __str__(self):
237         return repr(self.value)
238
239 class CertMissingParent(SfaFault):
240     def __init__(self, value, extra = None):
241         self.value = value
242         faultString = "Cert missing parent: %(value)s" % locals()
243         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
244     def __str__(self):
245         return repr(self.value)
246
247 class CertNotSignedByParent(SfaFault):
248     def __init__(self, value, extra = None):
249         self.value = value
250         faultString = "Cert not signed by parent: %(value)s" % locals()
251         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
252     def __str__(self):
253         return repr(self.value)
254     
255 class GidParentHrn(SfaFault):
256     def __init__(self, value, extra = None):
257         self.value = value
258         faultString = "Cert URN is not an extension of its parent: %(value)s" % locals()
259         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
260     def __str__(self):
261         return repr(self.value)
262         
263 class GidInvalidParentHrn(SfaFault):
264     def __init__(self, value, extra = None):
265         self.value = value
266         faultString = "GID invalid parent hrn: %(value)s" % locals()
267         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
268     def __str__(self):
269         return repr(self.value)
270
271 class SliverDoesNotExist(SfaFault):
272     def __init__(self, value, extra = None):
273         self.value = value
274         faultString = "Sliver does not exist : %(value)s" % locals()
275         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
276     def __str__(self):
277         return repr(self.value)
278
279 class BadRequestHash(xmlrpcFault):
280     def __init__(self, hash = None, extra = None):
281         faultString = "bad request hash: " + str(hash)
282         xmlrpcFault.__init__(self, GENICODE.ERROR, faultString)
283
284 class MissingTrustedRoots(SfaFault):
285     def __init__(self, value, extra = None):
286         self.value = value
287         faultString = "Trusted root directory does not exist: %(value)s" % locals()
288         SfaFault.__init__(self, GENICODE.SERVERERROR, faultString, extra) 
289     def __str__(self):
290         return repr(self.value)
291
292 class MissingSfaInfo(SfaFault):
293     def __init__(self, value, extra = None):
294         self.value = value
295         faultString = "Missing information: %(value)s" % locals()
296         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra) 
297     def __str__(self):
298         return repr(self.value)
299
300 class InvalidRSpec(SfaFault):
301     def __init__(self, value, extra = None):
302         self.value = value
303         faultString = "Invalid RSpec: %(value)s" % locals()
304         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
305     def __str__(self):
306         return repr(self.value)
307
308 class InvalidRSpecVersion(SfaFault):
309     def __init__(self, value, extra = None):
310         self.value = value
311         faultString = "Invalid RSpec version: %(value)s" % locals()
312         SfaFault.__init__(self, GENICODE.BADVERSION, faultString, extra)
313     def __str__(self):
314         return repr(self.value)
315
316 class UnsupportedRSpecVersion(SfaFault):
317     def __init__(self, value, extra = None):
318         self.value = value
319         faultString = "Unsupported RSpec version: %(value)s" % locals()
320         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra)
321     def __str__(self):
322         return repr(self.value)
323
324 class InvalidRSpecElement(SfaFault):
325     def __init__(self, value, extra = None):
326         self.value = value
327         faultString = "Invalid RSpec Element: %(value)s" % locals()
328         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
329     def __str__(self):
330         return repr(self.value)
331
332 class InvalidXML(SfaFault):
333     def __init__(self, value, extra = None):
334         self.value = value
335         faultString = "Invalid XML Document: %(value)s" % locals()
336         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
337     def __str__(self):
338         return repr(self.value)
339
340 class AccountNotEnabled(SfaFault):
341     def __init__(self,  extra = None):
342         faultString = "Account Disabled"
343         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
344     def __str__(self):
345         return repr(self.value)
346
347 class CredentialNotVerifiable(SfaFault):
348     def __init__(self, value=None, extra = None):
349         self.value = value
350         faultString = "Unable to verify credential" %locals()
351         if value:
352             faultString += ": %s" % value
353         faultString += ", " 
354         SfaFault.__init__(self, GENICODE.BADARGS, faultString, extra)
355     def __str__(self):
356         return repr(self.value)
357
358 class CertExpired(SfaFault):
359     def __init__(self, value, extra=None):
360         self.value = value
361         faultString = "%s cert is expired" % value
362         SfaFault.__init__(self, GENICODE.ERROR, faultString, extra)
363   
364 class SfatablesRejected(SfaFault):
365     def __init__(self, value, extra=None):
366         self.value =value
367         faultString = "%s rejected by sfatables"
368         SfaFault.__init__(self, GENICODE.FORBIDDEN, faultString, extra) 
369
370 class UnsupportedOperation(SfaFault):
371     def __init__(self, value, extra=None):
372         self.value = value
373         faultString = "Unsupported operation: %s" % value
374         SfaFault.__init__(self, GENICODE.UNSUPPORTED, faultString, extra) 
375