Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[unfold.git] / manifold / core / result_value.py
1 # Inspired from GENI error codes
2
3 import time
4 import pprint
5
6 class ResultValue(dict):
7
8     # type
9     SUCCESS     = 0
10     WARNING     = 1
11     ERROR       = 2
12
13     # origin
14     CORE        = 0
15     GATEWAY     = 1
16
17     # code
18     SUCCESS     = 0
19     SERVERBUSY  = 32001
20     BADARGS     = 1
21     ERROR       = 2
22     FORBIDDEN   = 3
23     BADVERSION  = 4
24     SERVERERROR = 5
25     TOOBIG      = 6
26     REFUSED     = 7
27     TIMEDOUT    = 8
28     DBERROR     = 9
29     RPCERROR    = 10
30
31     # description
32     ERRSTR = {
33         SUCCESS     : 'Success',
34         SERVERBUSY  : 'Server is (temporarily) too busy; try again later',
35         BADARGS     : 'Bad Arguments: malformed',
36         ERROR       : 'Error (other)',
37         FORBIDDEN   : 'Operation Forbidden: eg supplied credentials do not provide sufficient privileges (on the given slice)',
38         BADVERSION  : 'Bad Version (eg of RSpec)',
39         SERVERERROR : 'Server Error',
40         TOOBIG      : 'Too Big (eg request RSpec)',
41         REFUSED     : 'Operation Refused',
42         TIMEDOUT    : 'Operation Timed Out',
43         DBERROR     : 'Database Error',
44         RPCERROR    : ''
45     }
46
47     ALLOWED_FIELDS = set(['origin', 'type', 'code', 'value', 'description', 'traceback', 'ts'])
48
49     def __init__(self, **kwargs):
50         
51         # Checks
52         given = set(kwargs.keys())
53         cstr_success = set(['code', 'origin', 'value']) <= given
54         cstr_error   = set(['code', 'type', 'origin', 'description']) <= given
55         assert given <= self.ALLOWED_FIELDS, "Wrong fields in ResultValue constructor: %r" % (given - self.ALLOWED_FIELDS)
56         assert cstr_success or cstr_error, 'Incomplete set of fields in ResultValue constructor: %r' % given
57         
58         dict.__init__(self, **kwargs)
59
60         # Set missing fields to None
61         for field in self.ALLOWED_FIELDS - given:
62             self[field] = None
63         if not 'ts' in self:
64             self['ts'] = time.time()
65             
66
67     # Internal MySlice errors   : return ERROR
68     # Internal MySlice warnings : return RESULT WITH WARNINGS
69     # Debug                     : add DEBUG INFORMATION
70     # Gateway errors            : return RESULT WITH WARNING
71     # all Gateways errors       : return ERROR
72     
73     @classmethod
74     def get_result_value(self, results, result_value_array):
75         # let's analyze the results of the query plan
76         # XXX we should inspect all errors to determine whether to return a
77         # result or not
78         if not result_value_array:
79             # No error
80             return ResultValue(code=self.SUCCESS, origin=[self.CORE, 0], value=results)
81         else:
82             # Handle errors
83             return ResultValue(code=self.WARNING, origin=[self.CORE, 0], description=result_value_array, value=results)
84
85     @classmethod
86     def get_error(self, error):
87         return ResultValue(code=error, origin=[self.CORE, 0], value=self.ERRSTR[error])
88
89     @classmethod
90     def get_success(self, result):
91         return ResultValue(code=self.SUCCESS, origin=[self.CORE, 0], value=result) 
92
93     def ok_value(self):
94         return self['value']
95
96     def error(self):
97         err = "%r" % self['description']
98
99     @staticmethod
100     def to_html (raw_dict):
101         return pprint.pformat (raw_dict).replace("\\n","<br/>")
102
103 # 67    <code>
104 # 68      <value>9</value>
105 # 69      <label>DBERROR</label>
106 # 70      <description>Database Error</description>
107 # 71    </code>
108 # 72    <code>
109 # 73      <value>10</value>
110 # 74      <label>RPCERROR</label>
111 # 75      <description>RPC Error</description>
112 # 76    </code>
113 # 77    <code>
114 # 78      <value>11</value>
115 # 79      <label>UNAVAILABLE</label>
116 # 80      <description>Unavailable (eg server in lockdown)</description>
117 # 81    </code>
118 # 82    <code>
119 # 83      <value>12</value>
120 # 84      <label>SEARCHFAILED</label>
121 # 85      <description>Search Failed (eg for slice)</description>
122 # 86    </code>
123 # 87    <code>
124 # 88      <value>13</value>
125 # 89      <label>UNSUPPORTED</label>
126 # 90      <description>Operation Unsupported</description>
127 # 91    </code>
128 # 92    <code>
129 # 93      <value>14</value>
130 # 94      <label>BUSY</label>
131 # 95      <description>Busy (resource, slice, or server); try again
132 # later</description>
133 # 96    </code>
134 # 97    <code>
135 # 98      <value>15</value>
136 # 99      <label>EXPIRED</label>
137 # 100     <description>Expired (eg slice)</description>
138 # 101   </code>
139 # 102   <code>
140 # 103     <value>16</value>
141 # 104     <label>INPROGRESS</label>
142 # 105     <description>In Progress</description>
143 # 106   </code>
144 # 107   <code>
145 # 108     <value>17</value>
146 # 109     <label>ALREADYEXISTS</label>
147 # 110     <description>Already Exists (eg slice)</description>
148 # 111   </code>
149 # 112 <!-- 18+ not in original ProtoGENI implementation or Error Code --
150 # 113   -- proposal. -->
151 # 114   <code>
152 # 115     <value>18</value>
153 # 116     <label>MISSINGARGS</label>
154 # 117     <description>Required argument(s) missing</description>
155 # 118   </code>
156 # 119   <code>
157 # 120     <value>19</value>
158 # 121     <label>OUTOFRANGE</label>
159 # 122     <description>Input Argument outside of legal range</description>
160 # 123   </code>
161 # 124   <code>
162 # 125     <value>20</value>
163 # 126     <label>CREDENTIAL_INVALID</label>
164 # 127     <description>Not authorized: Supplied credential is
165 # invalid</description>
166 # 128   </code>
167 # 129   <code>
168 # 130     <value>21</value>
169 # 131     <label>CREDENTIAL_EXPIRED</label>
170 # 132     <description>Not authorized: Supplied credential expired</description>
171 # 133   </code>
172 # 134   <code>
173 # 135     <value>22</value>
174 # 136     <label>CREDENTIAL_MISMATCH</label>
175 # 137     <description>Not authorized: Supplied credential does not match client
176 # certificate or does not match the given slice URN</description>
177 # 138   </code>
178 # 139   <code>
179 # 140     <value>23</value>
180 # 141     <label>CREDENTIAL_SIGNER_UNTRUSTED</label>
181 # 142     <description>Not authorized: Supplied credential not signed by a trusted
182 # authority</description>
183 # 143   </code>
184 # 144   <code>
185 # 145     <value>24</value>
186 # 146     <label>VLAN_UNAVAILABLE</label>
187 # 147     <description>VLAN tag(s) requested not available (likely stitching
188 # failure)</description>
189 # 148   </code>
190 # 149 </geni-error-codes>
191 # 150 
192 # <!--
193 # || 0    || SUCCESS      || "Success" ||
194 # || 1    || BADARGS      || "Bad Arguments: malformed arguments" ||
195 # || 2    || ERROR        || "Error (other)" ||
196 # || 3    || FORBIDDEN    || "Operation Forbidden: eg supplied credentials do # not provide sufficient privileges (on given slice)" ||
197 # || 4    || BADVERSION   || "Bad Version (eg of RSpec)" ||
198 # || 5    || SERVERERROR  || "Server Error" ||
199 # || 6    || TOOBIG       || "Too Big (eg request RSpec)" ||
200 # || 7    || REFUSED      || "Operation Refused" ||
201 # || 8    || TIMEDOUT     || "Operation Timed Out" ||
202 # || 9    || DBERROR      || "Database Error" ||
203 # || 10   || RPCERROR     || "RPC Error" ||
204 # || 11   || UNAVAILABLE  || "Unavailable (eg server in lockdown)" ||
205 # || 12   || SEARCHFAILED         || "Search Failed (eg for slice)" ||
206 # || 13   || UNSUPPORTED  || "Operation Unsupported" ||
207 # || 14   || BUSY         || "Busy (resource, slice, or server); try again # later" ||
208 # || 15   || EXPIRED      || "Expired (eg slice)" ||
209 # || 16   || INPROGRESS   || "In Progress" ||
210 # || 17   || ALREADYEXISTS        || "Already Exists (eg the slice}" ||
211 # || 18   || MISSINGARGS  || "Required argument(s) missing" ||
212 # || 19   || OUTOFRANGE   || "Requested expiration time or other argument not # valid" ||
213 # || 20   || CREDENTIAL_INVALID   || "Not authorized: Supplied credential is # invalid" ||
214 # || 21   || CREDENTIAL_EXPIRED   || "Not authorized: Supplied credential # expired" ||
215 # || 22   || CREDENTIAL_MISMATCH   || "Not authorized: Supplied credential # does not match the supplied client certificate or does not match the given slice # URN" ||
216 # || 23   || CREDENTIAL_SIGNER_UNTRUSTED   || "Not authorized: Supplied # credential not signed by trusted authority" ||
217 # || 24   || VLAN_UNAVAILABLE     || "VLAN tag(s) requested not available # (likely stitching failure)" ||
218
219 # 18+ not in original ProtoGENI implementation or Error Code proposal.
220
221 # Maping to SFA Faults:
222 # SfaAuthenticationFailure: FORBIDDEN
223 # SfaDBErrr: DBERROR
224 # SfaFault: ERROR
225 # SfaPermissionDenied: FORBIDDEN
226 # SfaNotImplemented: UNSUPPORTED
227 # SfaAPIError: SERVERERROR
228 # MalformedHrnException: BADARGS
229 # NonExistingRecord: SEARCHFAILED
230 # ExistingRecord: ALREADYEXISTS
231 # NonexistingCredType: SEARCHFAILED? FORBIDDEN? CREDENTIAL_INVALID?
232 # NonexitingFile: SEARCHFAILED
233 # InvalidRPCParams: RPCERROR
234 # ConnectionKeyGIDMismatch: FORBIDDEN? CREDENTIAL_MISMATCH?
235 # MissingCallerGID: SEARCHFAILED? CREDENTIAL_MISMATCH?
236 # RecordNotFound: SEARCHFAILED
237 # PlanetLAbRecordDoesNotExist: SEARCHFAILED
238 # PermissionError: FORBIDDEN
239 # InsufficientRights: FORBIDDEN
240 # MissingDelegateBit: CREDENTIAL_INVALID? FORBIDDEN?
241 # ChildRightsNotSubsetOfParent: CREDENTIAL_INVALID
242 # CertMissingParent: FORBIDDEN? CREDENTIAL_INVALID?
243 # CertNotSignedByParent: FORBIDDEN
244 # GidParentHrn: FORBIDDEN? CREDENTIAL_INVALID?
245 # GidInvalidParentHrn: FORBIDDEN? CREDENTIAL_INVALID?
246 # SliverDoesNotExist: SEARCHFAILED
247 # MissingTrustedRoots: SERVERERROR
248 # MissingSfaInfo: SERVERERROR
249 # InvalidRSpec: BADARGS
250 # InvalidRSpecElement: BADARGS
251 # AccountNotEnabled: REFUSED? FORBIDDEN?
252 # CredentialNotVerifiable: CREDENTIAL_INVALID
253 # CertExpired: EXPIRED? FORBIDDEN?
254 # -->