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

36

37 38 39
from synnefo.lib.commissioning import (CanonifyException, SpecifyException,
                                       Specificator, Null, Integer, Text,
                                       Tuple, ListOf, Dict, Args)
Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
40
from random import choice, randint
41

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
42
Context             =   Dict(classname='Context', null=True, show=False)
43

44
class Name(Text):
45
    def init(self):
46
        self.opts.update({'regex': "[\w.:@+/-]+", 'maxlen':512})
47
        Text.init(self)
48

Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
49 50
    def _random_choice(self, kw):
        alphabet = u'abcdef_1233490.:@/-αβγδεζ'
51
        length = randint(1, 48)
Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
52 53
        return ''.join(choice(alphabet) for _ in xrange(length))

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
class Nonnegative(Integer):
    def init(self):
        self.opts.update({'minimum': 0})

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

Serial              =   Positive(classname='Serial')

ClientKey           =   Name(classname='ClientKey')
Nothing             =   Null(classname='Nothing')

Entity              =   Name(classname='Entity')
Owner               =   Name(classname='Owner')
69 70 71
Key                 =   Text(classname='Key')
NewKey              =   Text(classname='Newkey')
OwnerKey            =   Text(classname='OwnerKey')
72 73 74
Resource            =   Name(classname='Resource')
Policy              =   Name(classname='Policy')

75
Quantity            =   Integer(classname='Quantity', null=True)
76 77 78
Capacity            =   Nonnegative(classname='Capacity', null=True)
ImportLimit         =   Nonnegative(classname='ImportLimit', null=True)
ExportLimit         =   Nonnegative(classname='ExportLimit', null=True)
79 80
Imported            =   Nonnegative(classname='Imported')
Exported            =   Nonnegative(classname='Exported')
81
Returned            =   Nonnegative(classname='Returned')
Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
82
Released            =   Nonnegative(classname='Released')
83
Flags               =   Nonnegative(classname='Flags')
84
Index               =   Nonnegative(classname='Index')
85

Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
86 87 88 89
Timepoint           =   Text(classname='Timepoint', maxlen=24)
Reason              =   Text(   classname   =   'Reason',
                                regex       =   '(ACCEPT|REJECT):.*',
                                maxlen      =   128         )
90 91 92 93 94 95 96 97

class QuotaholderAPI(Specificator):

    def create_entity   (
                self,
                context         =   Context,
                create_entity   =   ListOf(Entity, Owner, Key, OwnerKey, nonempty=1)
        ):
98
        rejected = ListOf(Index)
99 100
        return rejected

101
    def set_entity_key  (
102 103 104 105 106 107 108
                self,
                context         =   Context,
                set_entity_key  =   ListOf(Entity, Key, NewKey)
        ):
        rejected = ListOf(Entity)
        return rejected

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    def list_entities   (
                self,
                context         =   Context,
                entity          =   Entity,
                key             =   Key
        ):
        entities = ListOf(Entity)
        return entities

    def get_entity  (
                self,
                context     =   Context,
                get_entity  =   ListOf(Entity, Key, nonempty=1)
        ):
        entities = ListOf(Entity, Owner)
        return entities

    def get_limits  (
                self,
                context     =   Context,
129
                get_limits  =   ListOf(Policy, nonempty=1)
130
        ):
131 132
        limits = ListOf(Policy, Quantity, Capacity,
                        ImportLimit, ExportLimit)
133 134 135 136 137
        return limits

    def set_limits  (
                self,
                context     =   Context,
138 139
                set_limits  =   ListOf( Policy, Quantity, Capacity,
                                        ImportLimit, ExportLimit,
140 141
                                        nonempty=1 )
        ):
142
        rejected = ListOf(Policy)
143 144 145 146 147 148 149 150
        return rejected

    def get_holding (
                self,
                context     =   Context,
                get_holding =   ListOf(Entity, Resource, Key)
        ):
        holdings = ListOf(  Entity, Resource, Policy,
151
                            Imported, Exported, Returned, Released, Flags   )
152 153 154 155 156 157 158 159 160 161
        return holdings

    def set_holding (
                self,
                context     =   Context,
                set_holding =   ListOf(Entity, Resource, Key, Policy, Flags)
        ):
        rejected = ListOf(Entity, Resource, Policy)
        return rejected

162
    def init_holding (
163
                self,
164
                context      =   Context,
165 166 167
                init_holding =   ListOf(Entity, Resource, Key, Policy,
                                        Imported, Exported, Returned, Released,
                                        Flags)
168 169 170 171
        ):
        rejected = ListOf(Index)
        return rejected

172 173 174 175 176 177 178 179 180
    def reset_holding (
                self,
                context       =   Context,
                reset_holding =   ListOf(Entity, Resource, Key,
                                        Imported, Exported, Returned, Released)
        ):
        rejected = ListOf(Index)
        return rejected

Giorgos Korfiatis's avatar
Giorgos Korfiatis committed
181 182 183 184 185 186 187 188
    def release_holding (
                self,
                context         =   Context,
                release_holding =   ListOf(Entity, Resource, Key)
        ):
        rejected = ListOf(Index)
        return rejected

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    def list_resources  (
                self,
                context     =   Context,
                entity      =   Entity,
                key         =   Key
        ):
        resources = ListOf(Resource)
        return resources

    def get_quota   (
                self,
                context     =   Context,
                get_quota   =   ListOf(Entity, Resource, Key)
        ):
        quotas = ListOf(Entity, Resource,
                        Quantity, Capacity,
                        ImportLimit, ExportLimit,
                        Imported, Exported,
207
                        Returned, Released,
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                        Flags)
        return quotas

    def set_quota   (
                self,
                context     =   Context,
                set_quota   =   ListOf( Entity, Resource, Key,
                                        Quantity, Capacity,
                                        ImportLimit, ExportLimit, Flags )
        ):
        rejected = ListOf(Entity, Resource)
        return rejected

    def issue_commission    (
                self,
                context     =   Context,
224
                target      =   Entity,
225 226
                key         =   Key,
                clientkey   =   ClientKey,
227
                name        =   Text(default=''),
228 229 230 231 232 233 234 235
                provisions  =   ListOf(Entity, Resource, Quantity)
        ):
        return Serial

    def accept_commission   (
                self,
                context     =   Context,
                clientkey   =   ClientKey,
236 237
                serials     =   ListOf(Serial),
                reason      =   Text(default='ACCEPT')
238 239 240 241 242 243 244
        ):
        return Nothing

    def reject_commission   (
                self,
                context     =   Context,
                clientkey   =   ClientKey,
245 246
                serials     =   ListOf(Serial),
                reason      =   Text(default='REJECT')
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
        ):
        return Nothing

    def get_pending_commissions (
                    self,
                    context     =   Context,
                    clientkey   =   ClientKey
        ):
        pending = ListOf(Serial)
        return pending

    def resolve_pending_commissions (
                    self,
                    context     =   Context,
                    clientkey   =   ClientKey,
                    max_serial  =   Serial,
                    accept_set  =   ListOf(Serial)
        ):
        return Nothing

    def release_entity  (
                self,
                context         =   Context,
                release_entity  =   ListOf(Entity, Key, nonempty=1)
        ):
        rejected = ListOf(Entity)
        return rejected

Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
275 276 277 278 279
    def get_timeline    (
                self,
                context         =   Context,
                after           =   Timepoint,
                before          =   Timepoint,
280
                get_timeline    =   ListOf(Entity, Resource, Key)
Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
281
        ):
282 283 284 285 286 287 288
        timeline = ListOf(Dict(
                            serial                      =   Serial,
                            source                      =   Entity,
                            target                      =   Entity,
                            resource                    =   Resource,
                            name                        =   Name(),
                            quantity                    =   Quantity,
289 290 291 292 293 294 295 296 297 298 299 300
                            source_allocated            =   Quantity,
                            source_allocated_through    =   Quantity,
                            source_inbound              =   Quantity,
                            source_inbound_through      =   Quantity,
                            source_outbound             =   Quantity,
                            source_outbound_through     =   Quantity,
                            target_allocated            =   Quantity,
                            target_allocated_through    =   Quantity,
                            target_inbound              =   Quantity,
                            target_inbound_through      =   Quantity,
                            target_outbound             =   Quantity,
                            target_outbound_through     =   Quantity,
301 302
                            issue_time                  =   Timepoint,
                            log_time                    =   Timepoint,
303 304
                            reason                      =   Reason,

305
                            strict  =   True))
Georgios D. Tsoukalas's avatar
Georgios D. Tsoukalas committed
306 307
        return timeline