Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / callpoint.py @ 9b126f13

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_quota(self, holders=None, sources=None, resources=None):
56
        holdings = Holding.objects.all()
57

    
58
        if holders is not None:
59
            holdings = holdings.filter(holder__in=holders)
60

    
61
        if sources is not None:
62
            holdings = holdings.filter(source__in=sources)
63

    
64
        if resources is not None:
65
            holdings = holdings.filter(resource__in=resources)
66

    
67
        quotas = {}
68
        for holding in holdings:
69
            key = (holding.holder, holding.source, holding.resource)
70
            value = (holding.limit, holding.imported_min, holding.imported_max)
71
            quotas[key] = value
72

    
73
        return quotas
74

    
75
    def _get_holdings_for_update(self, holding_keys):
76
        holding_keys = sorted(holding_keys)
77
        holdings = {}
78
        for (holder, source, resource) in holding_keys:
79
            try:
80
                h = Holding.objects.get_for_update(
81
                    holder=holder, source=source, resource=resource)
82
                holdings[(holder, source, resource)] = h
83
            except Holding.DoesNotExist:
84
                pass
85
        return holdings
86

    
87
    def _provisions_to_list(self, provisions):
88
        lst = []
89
        for provision in provisions:
90
            try:
91
                holder = provision['holder']
92
                source = provision['source']
93
                resource = provision['resource']
94
                quantity = provision['quantity']
95
                key = (holder, source, resource)
96
                lst.append((key, quantity))
97
            except KeyError:
98
                raise InvalidDataError("Malformed provision")
99
        return lst
100

    
101
    def _mkProvision(self, key, quantity):
102
        holder, source, resource = key
103
        return {'holder': holder,
104
                'source': source,
105
                'resource': resource,
106
                'quantity': quantity,
107
                }
108

    
109
    def set_quota(self, quotas):
110
        holding_keys = [key for (key, limit) in quotas]
111
        holdings = self._get_holdings_for_update(holding_keys)
112

    
113
        for key, limit in quotas:
114
            try:
115
                h = holdings[key]
116
            except KeyError:
117
                holder, source, resource = key
118
                h = Holding(holder=holder,
119
                            source=source,
120
                            resource=resource)
121
            h.limit = limit
122
            h.save()
123
            holdings[key] = h
124

    
125
    def add_resource_limit(self, holders=None, sources=None, resources=None,
126
                           diff=0):
127
        holdings = Holding.objects.all()
128

    
129
        if holders is not None:
130
            holdings = holdings.filter(holder__in=holders)
131

    
132
        if sources is not None:
133
            holdings = holdings.filter(source__in=sources)
134

    
135
        if resources is not None:
136
            holdings = holdings.filter(resource__in=resources)
137

    
138
        holdings.update(limit=F('limit')+diff)
139

    
140
    def issue_commission(self,
141
                         context=None,
142
                         clientkey=None,
143
                         name=None,
144
                         force=False,
145
                         provisions=()):
146

    
147
        if name is None:
148
            name = ""
149

    
150
        operations = Operations()
151
        provisions_to_create = []
152

    
153
        provisions = self._provisions_to_list(provisions)
154
        keys = [key for (key, value) in provisions]
155
        holdings = self._get_holdings_for_update(keys)
156
        try:
157
            checked = []
158
            for key, quantity in provisions:
159
                if not isinstance(quantity, (int, long)):
160
                    raise InvalidDataError("Malformed provision")
161

    
162
                if key in checked:
163
                    m = "Duplicate provision for %s" % str(key)
164
                    provision = self._mkProvision(key, quantity)
165
                    raise DuplicateError(m,
166
                                         provision=provision)
167
                checked.append(key)
168

    
169
                # Target
170
                try:
171
                    th = holdings[key]
172
                except KeyError:
173
                    m = ("There is no such holding %s" % str(key))
174
                    provision = self._mkProvision(key, quantity)
175
                    raise NoHoldingError(m,
176
                                         provision=provision)
177

    
178
                if quantity >= 0:
179
                    operations.prepare(Import, th, quantity, force)
180

    
181
                else: # release
182
                    abs_quantity = -quantity
183
                    operations.prepare(Release, th, abs_quantity, force)
184

    
185
                holdings[key] = th
186
                provisions_to_create.append((key, quantity))
187

    
188
        except QuotaholderError:
189
            operations.revert()
190
            raise
191

    
192
        commission = Commission.objects.create(clientkey=clientkey, name=name)
193
        for (holder, source, resource), quantity in provisions_to_create:
194
            Provision.objects.create(serial=commission,
195
                                     holder=holder,
196
                                     source=source,
197
                                     resource=resource,
198
                                     quantity=quantity)
199

    
200
        return commission.serial
201

    
202
    def _log_provision(self,
203
                       commission, provision, holding, log_time, reason):
204

    
205
        kwargs = {
206
            'serial':              commission.serial,
207
            'name':                commission.name,
208
            'holder':              holding.holder,
209
            'source':              holding.source,
210
            'resource':            holding.resource,
211
            'limit':               holding.limit,
212
            'imported_min':        holding.imported_min,
213
            'imported_max':        holding.imported_max,
214
            'delta_quantity':      provision.quantity,
215
            'issue_time':          commission.issue_time,
216
            'log_time':            log_time,
217
            'reason':              reason,
218
        }
219

    
220
        ProvisionLog.objects.create(**kwargs)
221

    
222
    def _get_commissions_for_update(self, clientkey, serials):
223
        cs = Commission.objects.filter(
224
            clientkey=clientkey, serial__in=serials).select_for_update()
225

    
226
        commissions = {}
227
        for c in cs:
228
            commissions[c.serial] = c
229
        return commissions
230

    
231
    def _partition_by(self, f, l):
232
        d = {}
233
        for x in l:
234
            group = f(x)
235
            group_l = d.get(group, [])
236
            group_l.append(x)
237
            d[group] = group_l
238
        return d
239

    
240
    def resolve_pending_commissions(self,
241
                                    context=None, clientkey=None,
242
                                    accept_set=[], reject_set=[],
243
                                    reason=''):
244
        actions = dict.fromkeys(accept_set, True)
245
        conflicting = set()
246
        for serial in reject_set:
247
            if actions.get(serial) is True:
248
                actions.pop(serial)
249
                conflicting.add(serial)
250
            else:
251
                actions[serial] = False
252

    
253
        conflicting = list(conflicting)
254
        serials = actions.keys()
255
        commissions = self._get_commissions_for_update(clientkey, serials)
256
        ps = Provision.objects.filter(serial__in=serials).select_for_update()
257
        holding_keys = sorted(p.holding_key() for p in ps)
258
        holdings = self._get_holdings_for_update(holding_keys)
259
        provisions = self._partition_by(lambda p: p.serial_id, ps)
260

    
261
        log_time = now()
262

    
263
        accepted, rejected, notFound = [], [], []
264
        for serial, accept in actions.iteritems():
265
            commission = commissions.get(serial)
266
            if commission is None:
267
                notFound.append(serial)
268
                continue
269

    
270
            accepted.append(serial) if accept else rejected.append(serial)
271

    
272
            ps = provisions.get(serial)
273
            assert ps is not None
274
            for pv in ps:
275
                key = pv.holding_key()
276
                h = holdings.get(key)
277
                if h is None:
278
                    raise CorruptedError("Corrupted provision")
279

    
280
                quantity = pv.quantity
281
                action = finalize if accept else undo
282
                if quantity >= 0:
283
                    action(Import, h, quantity)
284
                else:  # release
285
                    action(Release, h, -quantity)
286

    
287
                prefix = 'ACCEPT:' if accept else 'REJECT:'
288
                comm_reason = prefix + reason[-121:]
289
                self._log_provision(commission, pv, h, log_time, comm_reason)
290
                pv.delete()
291
            commission.delete()
292
        return accepted, rejected, notFound, conflicting
293

    
294
    def resolve_pending_commission(self, clientkey, serial, accept=True):
295
        if accept:
296
            ok, notOk, notF, confl = self.resolve_pending_commissions(
297
                clientkey=clientkey, accept_set=[serial])
298
        else:
299
            notOk, ok, notF, confl = self.resolve_pending_commissions(
300
                clientkey=clientkey, reject_set=[serial])
301

    
302
        assert notOk == confl == []
303
        assert ok + notF == [serial]
304
        return bool(ok)
305

    
306
    def get_pending_commissions(self, context=None, clientkey=None):
307
        pending = Commission.objects.filter(clientkey=clientkey)
308
        pending_list = pending.values_list('serial', flat=True)
309
        return list(pending_list)
310

    
311
    def get_commission(self, clientkey=None, serial=None):
312
        try:
313
            commission = Commission.objects.get(clientkey=clientkey,
314
                                                serial=serial)
315
        except Commission.DoesNotExist:
316
            raise NoCommissionError(serial)
317

    
318
        objs = Provision.objects.select_related('holding')
319
        provisions = objs.filter(serial=commission)
320

    
321
        ps = [p.todict() for p in provisions]
322

    
323
        response = {'serial':     serial,
324
                    'provisions': ps,
325
                    'issue_time': commission.issue_time,
326
                    }
327
        return response
328

    
329

    
330
API_Callpoint = QuotaholderDjangoDBCallpoint