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