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