Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / quotaholder / api / quotaholder.py @ d1304043

History | View | Annotate | Download (10.2 kB)

1
# -*- coding: utf-8 -*-
2
#
3
# Copyright 2012 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or
6
# without modification, are permitted provided that the following
7
# conditions are met:
8
#
9
#   1. Redistributions of source code must retain the above
10
#      copyright notice, this list of conditions and the following
11
#      disclaimer.
12
#
13
#   2. Redistributions in binary form must reproduce the above
14
#      copyright notice, this list of conditions and the following
15
#      disclaimer in the documentation and/or other materials
16
#      provided with the distribution.
17
#
18
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
# POSSIBILITY OF SUCH DAMAGE.
30
#
31
# The views and conclusions contained in the software and
32
# documentation are those of the authors and should not be
33
# interpreted as representing official policies, either expressed
34
# or implied, of GRNET S.A.
35

    
36

    
37
from kamaki.clients.commissioning import (
38
    CanonifyException,
39
    SpecifyException,
40
    Specificator,
41
    Null,
42
    Integer,
43
    Text,
44
    Tuple,
45
    ListOf,
46
    Dict,
47
    Args)
48
from random import choice, randint
49

    
50
Context = Dict(classname='Context', null=True, show=False)
51

    
52

    
53
class Name(Text):
54
    def init(self):
55
        self.opts.update({'regex': "[\w.:@+/-]+", 'maxlen': 512})
56
        Text.init(self)
57

    
58
    def _random_choice(self, kw):
59
        alphabet = u'abcdef_1233490.:@/-αβγδεζ'
60
        length = randint(1, 48)
61
        return ''.join(choice(alphabet) for _ in xrange(length))
62

    
63

    
64
class Nonnegative(Integer):
65
    def init(self):
66
        self.opts.update({'minimum': 0})
67

    
68

    
69
class Positive(Integer):
70
    def init(self):
71
        self.opts.update({'minimum': 1})
72

    
73

    
74
QH_PRACTICALLY_INFINITE = 10**32
75

    
76
Serial = Positive(classname='Serial')
77

    
78
ClientKey = Name(classname='ClientKey')
79
Nothing = Null(classname='Nothing')
80

    
81
Entity = Name(classname='Entity')
82
Owner = Name(classname='Owner')
83
Key = Text(classname='Key')
84
NewKey = Text(classname='Newkey')
85
OwnerKey = Text(classname='OwnerKey')
86
Resource = Name(classname='Resource')
87
Policy = Name(classname='Policy')
88

    
89
Quantity = Integer(classname='Quantity')
90
Capacity = Nonnegative(classname='Capacity')
91
ImportLimit = Nonnegative(classname='ImportLimit')
92
ExportLimit = Nonnegative(classname='ExportLimit')
93
QuantityDelta = Integer(classname='QuantityDelta')
94
CapacityDelta = Integer(classname='CapacityDelta')
95
ImportLimitDelta = Integer(classname='ImportLimitDelta')
96
ExportLimitDelta = Integer(classname='ExportLimitDelta')
97
Imported = Nonnegative(classname='Imported')
98
Exported = Nonnegative(classname='Exported')
99
Returned = Nonnegative(classname='Returned')
100
Released = Nonnegative(classname='Released')
101
Flags = Nonnegative(classname='Flags')
102
Index = Nonnegative(classname='Index')
103

    
104
Timepoint = Text(classname='Timepoint', maxlen=24)
105
Reason = Text(classname='Reason', regex='(ACCEPT|REJECT):.*', maxlen=128)
106

    
107

    
108
class QuotaholderAPI(Specificator):
109

    
110
    def create_entity(
111
        self,
112
        context=Context,
113
        create_entity=ListOf(Entity, Owner, Key, OwnerKey,
114
        nonempty=1)
115
    ):
116
        rejected = ListOf(Index)
117
        return rejected
118

    
119
    def set_entity_key(
120
        self,
121
        context=Context,
122
        set_entity_key=ListOf(Entity, Key, NewKey)
123
    ):
124
        rejected = ListOf(Entity)
125
        return rejected
126

    
127
    def list_entities(
128
        self,
129
        context=Context,
130
        entity=Entity,
131
        key=Key
132
    ):
133
        entities = ListOf(Entity)
134
        return entities
135

    
136
    def get_entity(
137
        self,
138
        context=Context,
139
        get_entity=ListOf(Entity, Key, nonempty=1)
140
    ):
141
        entities = ListOf(Entity, Owner)
142
        return entities
143

    
144
    def get_limits(
145
        self,
146
        context=Context,
147
        get_limits=ListOf(Policy, nonempty=1)
148
    ):
149
        limits = ListOf(Policy, Quantity, Capacity,
150
                        ImportLimit, ExportLimit)
151
        return limits
152

    
153
    def set_limits(
154
        self,
155
        context=Context,
156
        set_limits=ListOf(
157
            Policy,
158
            Quantity,
159
            Capacity,
160
            ImportLimit,
161
            ExportLimit,
162
            nonempty=1)
163
    ):
164
        rejected = ListOf(Policy)
165
        return rejected
166

    
167
    def get_holding(
168
        self,
169
        context=Context,
170
        get_holding=ListOf(Entity, Resource, Key)
171
    ):
172
        holdings = ListOf(  Entity, Resource, Policy,
173
                            Imported, Exported, Returned, Released, Flags   )
174
        return holdings
175

    
176
    def set_holding(
177
        self,
178
        context=Context,
179
        set_holding=ListOf(Entity, Resource, Key, Policy, Flags)
180
    ):
181
        rejected = ListOf(Entity, Resource, Policy)
182
        return rejected
183

    
184
    def init_holding(
185
        self,
186
        context=Context,
187
        init_holding=ListOf(
188
            Entity,
189
            Resource,
190
            Key,
191
            Policy,
192
            Imported,
193
            Exported,
194
            Returned,
195
            Released,
196
            Flags)
197
    ):
198
        rejected = ListOf(Index)
199
        return rejected
200

    
201
    def reset_holding(
202
        self,
203
        context=Context,
204
        reset_holding=ListOf(
205
            Entity,
206
            Resource,
207
            Key,
208
            Imported,
209
            Exported,
210
            Returned,
211
            Released)
212
    ):
213
        rejected = ListOf(Index)
214
        return rejected
215

    
216
    def release_holding(
217
        self,
218
        context=Context,
219
        release_holding=ListOf(Entity, Resource, Key)
220
    ):
221
        rejected = ListOf(Index)
222
        return rejected
223

    
224
    def list_resources(self, context=Context, entity=Entity, key=Key):
225
        resources = ListOf(Resource)
226
        return resources
227

    
228
    def list_holdings(
229
        self,
230
        context=Context,
231
        list_holdings=ListOf(Entity, Key)
232
    ):
233
        rejected = ListOf(Entity)
234
        holdings_list = ListOf(ListOf(Entity, Resource,
235
                                      Imported, Exported,
236
                                      Returned, Released))
237
        return Tuple(holdings_list, rejected)
238

    
239
    def get_quota(
240
        self,
241
        context=Context,
242
        get_quota=ListOf(Entity, Resource, Key)
243
    ):
244
        quotas = ListOf(Entity, Resource,
245
                        Quantity, Capacity,
246
                        ImportLimit, ExportLimit,
247
                        Imported, Exported,
248
                        Returned, Released,
249
                        Flags)
250
        return quotas
251

    
252
    def set_quota(
253
        self,
254
        context=Context,
255
        set_quota=ListOf(
256
            Entity,
257
            Resource,
258
            Key,
259
            Quantity,
260
            Capacity,
261
            ImportLimit,
262
            ExportLimit,
263
            Flags)
264
    ):
265
        rejected = ListOf(Entity, Resource)
266
        return rejected
267

    
268
    def add_quota(
269
        self,
270
        context=Context,
271
        clientkey=ClientKey,
272
        serial=Serial,
273
        sub_quota=ListOf(
274
            Entity,
275
            Resource,
276
            Key,
277
            QuantityDelta,
278
            CapacityDelta,
279
            ImportLimitDelta,
280
            ExportLimitDelta),
281
        add_quota=ListOf(
282
            Entity,
283
            Resource,
284
            Key,
285
            QuantityDelta,
286
            CapacityDelta,
287
            ImportLimitDelta,
288
            ExportLimitDelta)
289
    ):
290
        rejected = ListOf(Entity, Resource)
291
        return rejected
292

    
293
    def query_serials(
294
        self,
295
        context=Context,
296
        clientkey=ClientKey,
297
        serials=ListOf(Serial)
298
    ):
299
        return ListOf(Serial)
300

    
301
    def ack_serials(
302
        self,
303
        context=Context,
304
        clientkey=ClientKey,
305
        serials=ListOf(Serial)
306
    ):
307
        return Nothing
308

    
309
    def issue_commission(
310
        self,
311
        context=Context,
312
        target=Entity,
313
        key=Key,
314
        clientkey=ClientKey,
315
        name=Text(default=''),
316
        provisions=ListOf(Entity, Resource, Quantity)
317
    ):
318
        return Serial
319

    
320
    def accept_commission(
321
        self,
322
        context=Context,
323
        clientkey=ClientKey,
324
        serials=ListOf(Serial),
325
        reason=Text(default='ACCEPT')
326
    ):
327
        return Nothing
328

    
329
    def reject_commission(
330
        self,
331
        context=Context,
332
        clientkey=ClientKey,
333
        serials=ListOf(Serial),
334
        reason=Text(default='REJECT')
335
    ):
336
        return Nothing
337

    
338
    def get_pending_commissions(
339
        self,
340
        context=Context,
341
        clientkey=ClientKey
342
    ):
343
        pending = ListOf(Serial)
344
        return pending
345

    
346
    def resolve_pending_commissions(
347
        self,
348
        context=Context,
349
        clientkey=ClientKey,
350
        max_serial=Serial,
351
        accept_set=ListOf(Serial)
352
    ):
353
        return Nothing
354

    
355
    def release_entity(
356
        self,
357
        context=Context,
358
        release_entity=ListOf(Entity, Key, nonempty=1)
359
    ):
360
        rejected = ListOf(Entity)
361
        return rejected
362

    
363
    def get_timeline(
364
        self,
365
        context=Context,
366
        after=Timepoint,
367
        before=Timepoint,
368
        get_timeline=ListOf(Entity, Resource, Key)
369
    ):
370
        timeline = ListOf(Dict(
371
            serial=Serial,
372
            source=Entity,
373
            target=Entity,
374
            resource=Resource,
375
            name=Name(),
376
            quantity=Quantity,
377
            source_allocated=Quantity,
378
            source_allocated_through=Quantity,
379
            source_inbound=Quantity,
380
            source_inbound_through=Quantity,
381
            source_outbound=Quantity,
382
            source_outbound_through=Quantity,
383
            target_allocated=Quantity,
384
            target_allocated_through=Quantity,
385
            target_inbound=Quantity,
386
            target_inbound_through=Quantity,
387
            target_outbound=Quantity,
388
            target_outbound_through=Quantity,
389
            issue_time=Timepoint,
390
            log_time=Timepoint,
391
            reason=Reason,
392
            strict=True))
393
        return timeline