Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / callpoint.py @ e03ccd07

History | View | Annotate | Download (11.9 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_holder_quota(self, quotas):
110
        q = self._level_quota_dict(quotas)
111
        self.set_quota(q)
112

    
113
    def _level_quota_dict(self, quotas):
114
        lst = []
115
        for holder, holder_quota in quotas.iteritems():
116
            for source, source_quota in holder_quota.iteritems():
117
                for resource, limit in source_quota.iteritems():
118
                    key = (holder, source, resource)
119
                    lst.append((key, limit))
120
        return lst
121

    
122
    def set_quota(self, quotas):
123
        holding_keys = [key for (key, limit) in quotas]
124
        holdings = self._get_holdings_for_update(holding_keys)
125

    
126
        for key, limit in quotas:
127
            try:
128
                h = holdings[key]
129
            except KeyError:
130
                holder, source, resource = key
131
                h = Holding(holder=holder,
132
                            source=source,
133
                            resource=resource)
134
            h.limit = limit
135
            h.save()
136
            holdings[key] = h
137

    
138
    def add_resource_limit(self, source, resource, diff):
139
        objs = Holding.objects.filter(source=source, resource=resource)
140
        objs.update(limit=F('limit')+diff)
141

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

    
149
        if name is None:
150
            name = ""
151
        create = Commission.objects.create
152
        commission = create(clientkey=clientkey, name=name)
153
        serial = commission.serial
154

    
155
        operations = Operations()
156

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

    
166
                if key in checked:
167
                    m = "Duplicate provision for %s" % str(key)
168
                    provision = self._mkProvision(key, quantity)
169
                    raise DuplicateError(m,
170
                                         provision=provision)
171
                checked.append(key)
172

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

    
182
                if quantity >= 0:
183
                    operations.prepare(Import, th, quantity, force)
184

    
185
                else: # release
186
                    abs_quantity = -quantity
187
                    operations.prepare(Release, th, abs_quantity, force)
188

    
189
                holdings[key] = th
190
                Provision.objects.create(serial=commission,
191
                                         holder=th.holder,
192
                                         source=th.source,
193
                                         resource=th.resource,
194
                                         quantity=quantity)
195

    
196
        except QuotaholderError:
197
            operations.revert()
198
            raise
199

    
200
        return 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