root / snf-astakos-app / astakos / quotaholder / callpoint.py @ 9747707e
History | View | Annotate | Download (11.8 kB)
1 |
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from django.db.models import F |
35 |
from astakos.quotaholder.exception import ( |
36 |
QuotaholderError, |
37 |
NoCommissionError, |
38 |
CorruptedError, InvalidDataError, |
39 |
NoHoldingError, |
40 |
DuplicateError) |
41 |
|
42 |
from astakos.quotaholder.commission import ( |
43 |
Import, Release, Operations, finalize, undo) |
44 |
|
45 |
from astakos.quotaholder.utils.newname import newname |
46 |
from astakos.quotaholder.api import QH_PRACTICALLY_INFINITE |
47 |
|
48 |
from .models import (Holding, |
49 |
Commission, Provision, ProvisionLog, |
50 |
now) |
51 |
|
52 |
|
53 |
class QuotaholderDjangoDBCallpoint(object): |
54 |
|
55 |
def get_holder_quota(self, holders=None, sources=None, resources=None): |
56 |
holdings = Holding.objects.filter(holder__in=holders) |
57 |
|
58 |
if sources is not None: |
59 |
holdings = holdings.filter(source__in=sources) |
60 |
|
61 |
if resources is not None: |
62 |
holdings = holdings.filter(resource__in=resources) |
63 |
|
64 |
quotas = {} |
65 |
for holding in holdings: |
66 |
key = (holding.holder, holding.source, holding.resource) |
67 |
value = (holding.limit, holding.imported_min, holding.imported_max) |
68 |
quotas[key] = value |
69 |
|
70 |
return quotas
|
71 |
|
72 |
def _get_holdings_for_update(self, holding_keys): |
73 |
holding_keys = sorted(holding_keys)
|
74 |
holdings = {} |
75 |
for (holder, source, resource) in holding_keys: |
76 |
try:
|
77 |
h = Holding.objects.get_for_update( |
78 |
holder=holder, source=source, resource=resource) |
79 |
holdings[(holder, source, resource)] = h |
80 |
except Holding.DoesNotExist:
|
81 |
pass
|
82 |
return holdings
|
83 |
|
84 |
def _provisions_to_list(self, provisions): |
85 |
lst = [] |
86 |
for provision in provisions: |
87 |
try:
|
88 |
holder = provision['holder']
|
89 |
source = provision['source']
|
90 |
resource = provision['resource']
|
91 |
quantity = provision['quantity']
|
92 |
key = (holder, source, resource) |
93 |
lst.append((key, quantity)) |
94 |
except KeyError: |
95 |
raise InvalidDataError("Malformed provision") |
96 |
return lst
|
97 |
|
98 |
def _mkProvision(self, key, quantity): |
99 |
holder, source, resource = key |
100 |
return {'holder': holder, |
101 |
'source': source,
|
102 |
'resource': resource,
|
103 |
'quantity': quantity,
|
104 |
} |
105 |
|
106 |
def set_holder_quota(self, quotas): |
107 |
q = self._level_quota_dict(quotas)
|
108 |
self.set_quota(q)
|
109 |
|
110 |
def _level_quota_dict(self, quotas): |
111 |
lst = [] |
112 |
for holder, holder_quota in quotas.iteritems(): |
113 |
for source, source_quota in holder_quota.iteritems(): |
114 |
for resource, limit in source_quota.iteritems(): |
115 |
key = (holder, source, resource) |
116 |
lst.append((key, limit)) |
117 |
return lst
|
118 |
|
119 |
def set_quota(self, quotas): |
120 |
holding_keys = [key for (key, limit) in quotas] |
121 |
holdings = self._get_holdings_for_update(holding_keys)
|
122 |
|
123 |
for key, limit in quotas: |
124 |
try:
|
125 |
h = holdings[key] |
126 |
except KeyError: |
127 |
holder, source, resource = key |
128 |
h = Holding(holder=holder, |
129 |
source=source, |
130 |
resource=resource) |
131 |
h.limit = limit |
132 |
h.save() |
133 |
holdings[key] = h |
134 |
|
135 |
def add_resource_limit(self, source, resource, diff): |
136 |
objs = Holding.objects.filter(source=source, resource=resource) |
137 |
objs.update(limit=F('limit')+diff)
|
138 |
|
139 |
def issue_commission(self, |
140 |
context=None,
|
141 |
clientkey=None,
|
142 |
name=None,
|
143 |
force=False,
|
144 |
provisions=()): |
145 |
|
146 |
if name is None: |
147 |
name = ""
|
148 |
create = Commission.objects.create |
149 |
commission = create(clientkey=clientkey, name=name) |
150 |
serial = commission.serial |
151 |
|
152 |
operations = Operations() |
153 |
|
154 |
provisions = self._provisions_to_list(provisions)
|
155 |
keys = [key for (key, value) in provisions] |
156 |
holdings = self._get_holdings_for_update(keys)
|
157 |
try:
|
158 |
checked = [] |
159 |
for key, quantity in provisions: |
160 |
if not isinstance(quantity, (int, long)): |
161 |
raise InvalidDataError("Malformed provision") |
162 |
|
163 |
if key in checked: |
164 |
m = "Duplicate provision for %s" % str(key) |
165 |
provision = self._mkProvision(key, quantity)
|
166 |
raise DuplicateError(m,
|
167 |
provision=provision) |
168 |
checked.append(key) |
169 |
|
170 |
# Target
|
171 |
try:
|
172 |
th = holdings[key] |
173 |
except KeyError: |
174 |
m = ("There is no such holding %s" % key)
|
175 |
provision = self._mkProvision(key, quantity)
|
176 |
raise NoHoldingError(m,
|
177 |
provision=provision) |
178 |
|
179 |
if quantity >= 0: |
180 |
operations.prepare(Import, th, quantity, force) |
181 |
|
182 |
else: # release |
183 |
abs_quantity = -quantity |
184 |
operations.prepare(Release, th, abs_quantity, force) |
185 |
|
186 |
holdings[key] = th |
187 |
Provision.objects.create(serial=commission, |
188 |
holder=th.holder, |
189 |
source=th.source, |
190 |
resource=th.resource, |
191 |
quantity=quantity) |
192 |
|
193 |
except QuotaholderError:
|
194 |
operations.revert() |
195 |
raise
|
196 |
|
197 |
return serial
|
198 |
|
199 |
def _log_provision(self, |
200 |
commission, provision, holding, log_time, reason): |
201 |
|
202 |
kwargs = { |
203 |
'serial': commission.serial,
|
204 |
'name': commission.name,
|
205 |
'holder': holding.holder,
|
206 |
'source': holding.source,
|
207 |
'resource': holding.resource,
|
208 |
'limit': holding.limit,
|
209 |
'imported_min': holding.imported_min,
|
210 |
'imported_max': holding.imported_max,
|
211 |
'delta_quantity': provision.quantity,
|
212 |
'issue_time': commission.issue_time,
|
213 |
'log_time': log_time,
|
214 |
'reason': reason,
|
215 |
} |
216 |
|
217 |
ProvisionLog.objects.create(**kwargs) |
218 |
|
219 |
def _get_commissions_for_update(self, clientkey, serials): |
220 |
cs = Commission.objects.filter( |
221 |
clientkey=clientkey, serial__in=serials).select_for_update() |
222 |
|
223 |
commissions = {} |
224 |
for c in cs: |
225 |
commissions[c.serial] = c |
226 |
return commissions
|
227 |
|
228 |
def _partition_by(self, f, l): |
229 |
d = {} |
230 |
for x in l: |
231 |
group = f(x) |
232 |
group_l = d.get(group, []) |
233 |
group_l.append(x) |
234 |
d[group] = group_l |
235 |
return d
|
236 |
|
237 |
def resolve_pending_commissions(self, |
238 |
context=None, clientkey=None, |
239 |
accept_set=[], reject_set=[], |
240 |
reason=''):
|
241 |
actions = dict.fromkeys(accept_set, True) |
242 |
conflicting = set()
|
243 |
for serial in reject_set: |
244 |
if actions.get(serial) is True: |
245 |
actions.pop(serial) |
246 |
conflicting.add(serial) |
247 |
else:
|
248 |
actions[serial] = False
|
249 |
|
250 |
conflicting = list(conflicting)
|
251 |
serials = actions.keys() |
252 |
commissions = self._get_commissions_for_update(clientkey, serials)
|
253 |
ps = Provision.objects.filter(serial__in=serials).select_for_update() |
254 |
holding_keys = sorted(p.holding_key() for p in ps) |
255 |
holdings = self._get_holdings_for_update(holding_keys)
|
256 |
provisions = self._partition_by(lambda p: p.serial_id, ps) |
257 |
|
258 |
log_time = now() |
259 |
|
260 |
accepted, rejected, notFound = [], [], [] |
261 |
for serial, accept in actions.iteritems(): |
262 |
commission = commissions.get(serial) |
263 |
if commission is None: |
264 |
notFound.append(serial) |
265 |
continue
|
266 |
|
267 |
accepted.append(serial) if accept else rejected.append(serial) |
268 |
|
269 |
ps = provisions.get(serial) |
270 |
assert ps is not None |
271 |
for pv in ps: |
272 |
key = pv.holding_key() |
273 |
h = holdings.get(key) |
274 |
if h is None: |
275 |
raise CorruptedError("Corrupted provision") |
276 |
|
277 |
quantity = pv.quantity |
278 |
action = finalize if accept else undo |
279 |
if quantity >= 0: |
280 |
action(Import, h, quantity) |
281 |
else: # release |
282 |
action(Release, h, -quantity) |
283 |
|
284 |
prefix = 'ACCEPT:' if accept else 'REJECT:' |
285 |
comm_reason = prefix + reason[-121:]
|
286 |
self._log_provision(commission, pv, h, log_time, comm_reason)
|
287 |
pv.delete() |
288 |
commission.delete() |
289 |
return accepted, rejected, notFound, conflicting
|
290 |
|
291 |
def resolve_pending_commission(self, clientkey, serial, accept=True): |
292 |
if accept:
|
293 |
ok, notOk, notF, confl = self.resolve_pending_commissions(
|
294 |
clientkey=clientkey, accept_set=[serial]) |
295 |
else:
|
296 |
notOk, ok, notF, confl = self.resolve_pending_commissions(
|
297 |
clientkey=clientkey, reject_set=[serial]) |
298 |
|
299 |
assert notOk == confl == []
|
300 |
assert ok + notF == [serial]
|
301 |
return bool(ok) |
302 |
|
303 |
def get_pending_commissions(self, context=None, clientkey=None): |
304 |
pending = Commission.objects.filter(clientkey=clientkey) |
305 |
pending_list = pending.values_list('serial', flat=True) |
306 |
return list(pending_list) |
307 |
|
308 |
def get_commission(self, clientkey=None, serial=None): |
309 |
try:
|
310 |
commission = Commission.objects.get(clientkey=clientkey, |
311 |
serial=serial) |
312 |
except Commission.DoesNotExist:
|
313 |
raise NoCommissionError(serial)
|
314 |
|
315 |
objs = Provision.objects.select_related('holding')
|
316 |
provisions = objs.filter(serial=commission) |
317 |
|
318 |
ps = [p.todict() for p in provisions] |
319 |
|
320 |
response = {'serial': serial,
|
321 |
'provisions': ps,
|
322 |
'issue_time': commission.issue_time,
|
323 |
} |
324 |
return response
|
325 |
|
326 |
|
327 |
API_Callpoint = QuotaholderDjangoDBCallpoint |