Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / endpoints / qh.py @ 4319c408

History | View | Annotate | Download (10.3 kB)

1 fc1e2f02 Sofia Papagiannaki
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 5ce3ce4f Sofia Papagiannaki
#
3 fc1e2f02 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 fc1e2f02 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 fc1e2f02 Sofia Papagiannaki
# conditions are met:
6 5ce3ce4f Sofia Papagiannaki
#
7 fc1e2f02 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 fc1e2f02 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 fc1e2f02 Sofia Papagiannaki
#      disclaimer.
10 5ce3ce4f Sofia Papagiannaki
#
11 fc1e2f02 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 fc1e2f02 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 fc1e2f02 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 fc1e2f02 Sofia Papagiannaki
#      provided with the distribution.
15 5ce3ce4f Sofia Papagiannaki
#
16 fc1e2f02 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 fc1e2f02 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 fc1e2f02 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 fc1e2f02 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 fc1e2f02 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 fc1e2f02 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 fc1e2f02 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 fc1e2f02 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 fc1e2f02 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 fc1e2f02 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 fc1e2f02 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 fc1e2f02 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 5ce3ce4f Sofia Papagiannaki
#
29 fc1e2f02 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 fc1e2f02 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 fc1e2f02 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 fc1e2f02 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 fc1e2f02 Sofia Papagiannaki
34 fc1e2f02 Sofia Papagiannaki
import logging
35 bd4f356c Sofia Papagiannaki
import itertools
36 bd4f356c Sofia Papagiannaki
37 bd4f356c Sofia Papagiannaki
from functools import wraps
38 4fc7d569 Sofia Papagiannaki
from collections import namedtuple
39 fc1e2f02 Sofia Papagiannaki
40 fc1e2f02 Sofia Papagiannaki
from django.utils.translation import ugettext as _
41 fc1e2f02 Sofia Papagiannaki
42 30d92d1e Georgios D. Tsoukalas
from astakos.im.settings import (
43 0ea1f32b Georgios D. Tsoukalas
        QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL)
44 fc1e2f02 Sofia Papagiannaki
45 30d92d1e Georgios D. Tsoukalas
if QUOTAHOLDER_URL:
46 c836d69f Georgios D. Tsoukalas
    from kamaki.clients.quotaholder import QuotaholderClient
47 8b59b8ea Sofia Papagiannaki
48 fc1e2f02 Sofia Papagiannaki
ENTITY_KEY = '1'
49 4319c408 Kostas Papadimitriou
PRACTICALLY_INF = pow(2, 70)
50 fc1e2f02 Sofia Papagiannaki
51 ae497612 Olga Brani
inf = float('inf')
52 ae497612 Olga Brani
53 fc1e2f02 Sofia Papagiannaki
logger = logging.getLogger(__name__)
54 fc1e2f02 Sofia Papagiannaki
55 ae497612 Olga Brani
inf = float('inf')
56 5ce3ce4f Sofia Papagiannaki
57 ee45eb81 Giorgos Korfiatis
clientkey = 'astakos'
58 ee45eb81 Giorgos Korfiatis
59 73fbaec4 Sofia Papagiannaki
_client = None
60 73fbaec4 Sofia Papagiannaki
def get_client():
61 73fbaec4 Sofia Papagiannaki
    global _client
62 73fbaec4 Sofia Papagiannaki
    if _client:
63 73fbaec4 Sofia Papagiannaki
        return _client
64 73fbaec4 Sofia Papagiannaki
    if not QUOTAHOLDER_URL:
65 73fbaec4 Sofia Papagiannaki
        return
66 73fbaec4 Sofia Papagiannaki
    _client = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN)
67 52116521 Sofia Papagiannaki
    return _client
68 73fbaec4 Sofia Papagiannaki
69 8978cfbd Sofia Papagiannaki
def set_quota(payload):
70 8978cfbd Sofia Papagiannaki
    c = get_client()
71 8978cfbd Sofia Papagiannaki
    if not c:
72 8978cfbd Sofia Papagiannaki
        return
73 8978cfbd Sofia Papagiannaki
    result = c.set_quota(context={}, clientkey=clientkey, set_quota=payload)
74 8978cfbd Sofia Papagiannaki
    logger.info('set_quota: %s rejected: %s' % (payload, result))
75 8978cfbd Sofia Papagiannaki
    return result
76 8978cfbd Sofia Papagiannaki
77 16cfac45 Sofia Papagiannaki
def get_quota(user):
78 8978cfbd Sofia Papagiannaki
    c = get_client()
79 8978cfbd Sofia Papagiannaki
    if not c:
80 8978cfbd Sofia Papagiannaki
        return
81 16cfac45 Sofia Papagiannaki
    payload = []
82 16cfac45 Sofia Papagiannaki
    append = payload.append
83 16cfac45 Sofia Papagiannaki
    for r in user.quota.keys():
84 16cfac45 Sofia Papagiannaki
        append((user.uuid, r, ENTITY_KEY),)
85 8978cfbd Sofia Papagiannaki
    result = c.get_quota(context={}, clientkey=clientkey, get_quota=payload)
86 8978cfbd Sofia Papagiannaki
    logger.info('get_quota: %s rejected: %s' % (payload, result))
87 8978cfbd Sofia Papagiannaki
    return result
88 8978cfbd Sofia Papagiannaki
89 8978cfbd Sofia Papagiannaki
def create_entity(payload):
90 8978cfbd Sofia Papagiannaki
    c = get_client()
91 8978cfbd Sofia Papagiannaki
    if not c:
92 8978cfbd Sofia Papagiannaki
        return
93 8978cfbd Sofia Papagiannaki
    result = c.create_entity(context={}, clientkey=clientkey, create_entity=payload)
94 8978cfbd Sofia Papagiannaki
    logger.info('create_entity: %s rejected: %s' % (payload, result))
95 8978cfbd Sofia Papagiannaki
    return result
96 5ce3ce4f Sofia Papagiannaki
97 8978cfbd Sofia Papagiannaki
SetQuotaPayload = namedtuple('SetQuotaPayload', ('holder',
98 8978cfbd Sofia Papagiannaki
                                                 'resource',
99 8978cfbd Sofia Papagiannaki
                                                 'key',
100 8978cfbd Sofia Papagiannaki
                                                 'quantity',
101 8978cfbd Sofia Papagiannaki
                                                 'capacity',
102 8978cfbd Sofia Papagiannaki
                                                 'import_limit',
103 8978cfbd Sofia Papagiannaki
                                                 'export_limit',
104 8978cfbd Sofia Papagiannaki
                                                 'flags'))
105 8978cfbd Sofia Papagiannaki
106 8978cfbd Sofia Papagiannaki
GetQuotaPayload = namedtuple('GetQuotaPayload', ('holder',
107 8978cfbd Sofia Papagiannaki
                                                 'resource',
108 8978cfbd Sofia Papagiannaki
                                                 'key'))
109 8978cfbd Sofia Papagiannaki
110 8978cfbd Sofia Papagiannaki
CreateEntityPayload = namedtuple('CreateEntityPayload', ('entity',
111 16cfac45 Sofia Papagiannaki
                                                         'owner',
112 16cfac45 Sofia Papagiannaki
                                                         'key',
113 16cfac45 Sofia Papagiannaki
                                                         'ownerkey'))
114 2a97d93b Giorgos Korfiatis
QuotaLimits = namedtuple('QuotaLimits', ('holder',
115 5cfd4acb Sofia Papagiannaki
                                         'resource',
116 2a97d93b Giorgos Korfiatis
                                         'capacity',
117 2a97d93b Giorgos Korfiatis
                                         'import_limit',
118 2a97d93b Giorgos Korfiatis
                                         'export_limit'))
119 2a97d93b Giorgos Korfiatis
120 8978cfbd Sofia Papagiannaki
def register_users(users):
121 8978cfbd Sofia Papagiannaki
    payload = list(CreateEntityPayload(
122 8978cfbd Sofia Papagiannaki
                    entity=u.uuid,
123 8978cfbd Sofia Papagiannaki
                    owner='system',
124 8978cfbd Sofia Papagiannaki
                    key=ENTITY_KEY,
125 8978cfbd Sofia Papagiannaki
                    ownerkey='') for u in users)
126 8978cfbd Sofia Papagiannaki
    rejected = create_entity(payload)
127 8978cfbd Sofia Papagiannaki
    if not rejected:
128 8978cfbd Sofia Papagiannaki
        payload = []
129 8978cfbd Sofia Papagiannaki
        append = payload.append
130 8978cfbd Sofia Papagiannaki
        for u in users:
131 8978cfbd Sofia Papagiannaki
            for resource, uplimit in u.quota.iteritems():
132 8978cfbd Sofia Papagiannaki
                append( SetQuotaPayload(
133 8978cfbd Sofia Papagiannaki
                                holder=u.uuid,
134 8978cfbd Sofia Papagiannaki
                                resource=resource,
135 8978cfbd Sofia Papagiannaki
                                key=ENTITY_KEY,
136 0b73f8e4 Sofia Papagiannaki
                                quantity=0,
137 8978cfbd Sofia Papagiannaki
                                capacity=uplimit if uplimit != inf else None,
138 de8859f1 Sofia Papagiannaki
                                import_limit=PRACTICALLY_INF,
139 de8859f1 Sofia Papagiannaki
                                export_limit=PRACTICALLY_INF,
140 8978cfbd Sofia Papagiannaki
                                flags=0))
141 8978cfbd Sofia Papagiannaki
        return set_quota(payload)
142 8978cfbd Sofia Papagiannaki
143 8978cfbd Sofia Papagiannaki
144 8978cfbd Sofia Papagiannaki
def register_resources(resources):
145 8978cfbd Sofia Papagiannaki
    rdata = ((r.service, r) for r in resources)
146 8978cfbd Sofia Papagiannaki
    services = set(r.service for r in resources)
147 8978cfbd Sofia Papagiannaki
    payload = list(CreateEntityPayload(
148 8978cfbd Sofia Papagiannaki
                    entity=service,
149 8978cfbd Sofia Papagiannaki
                    owner='system',
150 8978cfbd Sofia Papagiannaki
                    key=ENTITY_KEY,
151 8978cfbd Sofia Papagiannaki
                    ownerkey='') for service in set(services))
152 8978cfbd Sofia Papagiannaki
    rejected = create_entity(payload)
153 8978cfbd Sofia Papagiannaki
    if not rejected:
154 8978cfbd Sofia Papagiannaki
        payload = list(SetQuotaPayload(
155 8978cfbd Sofia Papagiannaki
                        holder=resource.service,
156 8978cfbd Sofia Papagiannaki
                        resource=resource,
157 8978cfbd Sofia Papagiannaki
                        key=ENTITY_KEY,
158 af877ee0 root
                        quantity=PRACTICALLY_INF,
159 de8859f1 Sofia Papagiannaki
                        capacity=0,
160 de8859f1 Sofia Papagiannaki
                        import_limit=PRACTICALLY_INF,
161 de8859f1 Sofia Papagiannaki
                        export_limit=PRACTICALLY_INF,
162 8978cfbd Sofia Papagiannaki
                        flags=0) for resource in resources)
163 8978cfbd Sofia Papagiannaki
        return set_quota(payload)
164 8978cfbd Sofia Papagiannaki
165 d2b32360 Giorgos Korfiatis
def qh_add_quota(serial, sub_list, add_list):
166 ee45eb81 Giorgos Korfiatis
    if not QUOTAHOLDER_URL:
167 ee45eb81 Giorgos Korfiatis
        return ()
168 ee45eb81 Giorgos Korfiatis
169 ee45eb81 Giorgos Korfiatis
    context = {}
170 ee45eb81 Giorgos Korfiatis
    c = get_client()
171 4319c408 Kostas Papadimitriou
172 d2b32360 Giorgos Korfiatis
    sub_quota = []
173 d2b32360 Giorgos Korfiatis
    sub_append = sub_quota.append
174 d2b32360 Giorgos Korfiatis
    add_quota = []
175 d2b32360 Giorgos Korfiatis
    add_append = add_quota.append
176 d2b32360 Giorgos Korfiatis
177 5cfd4acb Sofia Papagiannaki
    for ql in sub_list:
178 d2b32360 Giorgos Korfiatis
        args = (ql.holder, ql.resource, ENTITY_KEY,
179 d2b32360 Giorgos Korfiatis
                0, ql.capacity, ql.import_limit, ql.export_limit)
180 d2b32360 Giorgos Korfiatis
        sub_append(args)
181 d2b32360 Giorgos Korfiatis
182 5cfd4acb Sofia Papagiannaki
    for ql in add_list:
183 2a97d93b Giorgos Korfiatis
        args = (ql.holder, ql.resource, ENTITY_KEY,
184 2a97d93b Giorgos Korfiatis
                0, ql.capacity, ql.import_limit, ql.export_limit)
185 d2b32360 Giorgos Korfiatis
        add_append(args)
186 2a97d93b Giorgos Korfiatis
187 ee45eb81 Giorgos Korfiatis
    result = c.add_quota(context=context,
188 ee45eb81 Giorgos Korfiatis
                         clientkey=clientkey,
189 ee45eb81 Giorgos Korfiatis
                         serial=serial,
190 d2b32360 Giorgos Korfiatis
                         sub_quota=sub_quota,
191 d2b32360 Giorgos Korfiatis
                         add_quota=add_quota)
192 ee45eb81 Giorgos Korfiatis
193 ee45eb81 Giorgos Korfiatis
    return result
194 ee45eb81 Giorgos Korfiatis
195 ee45eb81 Giorgos Korfiatis
def qh_query_serials(serials):
196 ee45eb81 Giorgos Korfiatis
    if not QUOTAHOLDER_URL:
197 ee45eb81 Giorgos Korfiatis
        return ()
198 ee45eb81 Giorgos Korfiatis
199 ee45eb81 Giorgos Korfiatis
    context = {}
200 ee45eb81 Giorgos Korfiatis
    c = get_client()
201 ee45eb81 Giorgos Korfiatis
    result = c.query_serials(context=context,
202 ee45eb81 Giorgos Korfiatis
                             clientkey=clientkey,
203 ee45eb81 Giorgos Korfiatis
                             serials=serials)
204 ee45eb81 Giorgos Korfiatis
    return result
205 ee45eb81 Giorgos Korfiatis
206 ee45eb81 Giorgos Korfiatis
def qh_ack_serials(serials):
207 ee45eb81 Giorgos Korfiatis
    if not QUOTAHOLDER_URL:
208 ee45eb81 Giorgos Korfiatis
        return ()
209 ee45eb81 Giorgos Korfiatis
210 ee45eb81 Giorgos Korfiatis
    context = {}
211 ee45eb81 Giorgos Korfiatis
    c = get_client()
212 ee45eb81 Giorgos Korfiatis
    result = c.ack_serials(context=context,
213 ee45eb81 Giorgos Korfiatis
                           clientkey=clientkey,
214 ee45eb81 Giorgos Korfiatis
                           serials=serials)
215 ee45eb81 Giorgos Korfiatis
    return
216 5ce3ce4f Sofia Papagiannaki
217 2925e285 root
from datetime import datetime
218 2925e285 root
219 2925e285 root
strptime = datetime.strptime
220 2925e285 root
timefmt = '%Y-%m-%dT%H:%M:%S.%f'
221 2925e285 root
222 476fdba1 root
SECOND_RESOLUTION = 1
223 476fdba1 root
224 9a06d96f Olga Brani
225 2925e285 root
def total_seconds(timedelta_object):
226 2925e285 root
    return timedelta_object.seconds + timedelta_object.days * 86400
227 2925e285 root
228 9a06d96f Olga Brani
229 476fdba1 root
def iter_timeline(timeline, before):
230 476fdba1 root
    if not timeline:
231 476fdba1 root
        return
232 476fdba1 root
233 476fdba1 root
    for t in timeline:
234 476fdba1 root
        yield t
235 476fdba1 root
236 476fdba1 root
    t = dict(t)
237 476fdba1 root
    t['issue_time'] = before
238 476fdba1 root
    yield t
239 476fdba1 root
240 9a06d96f Olga Brani
241 476fdba1 root
def _usage_units(timeline, after, before, details=0):
242 476fdba1 root
243 2925e285 root
    t_total = 0
244 476fdba1 root
    uu_total = 0
245 476fdba1 root
    t_after = strptime(after, timefmt)
246 476fdba1 root
    t_before = strptime(before, timefmt)
247 476fdba1 root
    t0 = t_after
248 476fdba1 root
    u0 = 0
249 2925e285 root
250 476fdba1 root
    for point in iter_timeline(timeline, before):
251 2925e285 root
        issue_time = point['issue_time']
252 2925e285 root
253 476fdba1 root
        if issue_time <= after:
254 476fdba1 root
            u0 = point['target_allocated_through']
255 2925e285 root
            continue
256 2925e285 root
257 476fdba1 root
        t = strptime(issue_time, timefmt) if issue_time <= before else t_before
258 476fdba1 root
        t_diff = int(total_seconds(t - t0) * SECOND_RESOLUTION)
259 2925e285 root
        t_total += t_diff
260 2925e285 root
        uu_cost = u0 * t_diff
261 2925e285 root
        uu_total += uu_cost
262 476fdba1 root
        t0 = t
263 476fdba1 root
        u0 = point['target_allocated_through']
264 2925e285 root
265 476fdba1 root
        target = point['target']
266 2925e285 root
        if details:
267 2925e285 root
            yield  (target,
268 2925e285 root
                    point['resource'],
269 2925e285 root
                    point['name'],
270 2925e285 root
                    issue_time,
271 2925e285 root
                    uu_cost,
272 2925e285 root
                    uu_total)
273 2925e285 root
274 2925e285 root
    if not t_total:
275 2925e285 root
        return
276 2925e285 root
277 2925e285 root
    yield  (target,
278 2925e285 root
            'total',
279 2925e285 root
            point['resource'],
280 2925e285 root
            issue_time,
281 9a06d96f Olga Brani
            uu_total / t_total,
282 2925e285 root
            uu_total)
283 2925e285 root
284 9a06d96f Olga Brani
285 476fdba1 root
def usage_units(timeline, after, before, details=0):
286 476fdba1 root
    return list(_usage_units(timeline, after, before, details=details))
287 2925e285 root
288 9a06d96f Olga Brani
289 476fdba1 root
def traffic_units(timeline, after, before, details=0):
290 82b05401 root
    tu_total = 0
291 82b05401 root
    target = None
292 82b05401 root
    issue_time = None
293 82b05401 root
294 82b05401 root
    for point in timeline:
295 82b05401 root
        issue_time = point['issue_time']
296 476fdba1 root
        if issue_time <= after:
297 476fdba1 root
            continue
298 476fdba1 root
        if issue_time > before:
299 476fdba1 root
            break
300 476fdba1 root
301 476fdba1 root
        target = point['target']
302 82b05401 root
        tu = point['target_allocated_through']
303 82b05401 root
        tu_total += tu
304 82b05401 root
305 82b05401 root
        if details:
306 82b05401 root
            yield  (target,
307 82b05401 root
                    point['resource'],
308 82b05401 root
                    point['name'],
309 82b05401 root
                    issue_time,
310 82b05401 root
                    tu,
311 82b05401 root
                    tu_total)
312 82b05401 root
313 82b05401 root
    if not tu_total:
314 82b05401 root
        return
315 82b05401 root
316 82b05401 root
    yield  (target,
317 82b05401 root
            'total',
318 82b05401 root
            point['resource'],
319 82b05401 root
            issue_time,
320 9a06d96f Olga Brani
            tu_total // len(timeline),
321 82b05401 root
            tu_total)
322 2925e285 root
323 9a06d96f Olga Brani
324 2925e285 root
def timeline_charge(entity, resource, after, before, details, charge_type):
325 2925e285 root
    key = '1'
326 2925e285 root
    if charge_type == 'charge_usage':
327 2925e285 root
        charge_units = usage_units
328 2925e285 root
    elif charge_type == 'charge_traffic':
329 2925e285 root
        charge_units = traffic_units
330 2925e285 root
    else:
331 2925e285 root
        m = 'charge type %s not supported' % charge_type
332 2925e285 root
        raise ValueError(m)
333 2925e285 root
334 30d92d1e Georgios D. Tsoukalas
    quotaholder = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN)
335 2925e285 root
    timeline = quotaholder.get_timeline(
336 9a06d96f Olga Brani
        context={},
337 9a06d96f Olga Brani
        after=after,
338 9a06d96f Olga Brani
        before=before,
339 9a06d96f Olga Brani
        get_timeline=[[entity, resource, key]])
340 476fdba1 root
    cu = charge_units(timeline, after, before, details=details)
341 82b05401 root
    return cu