root / commissioning / controllers / django_controller / models.py @ 9f1a1bd0
History | View | Annotate | Download (3.7 kB)
1 |
|
---|---|
2 |
from commissioning import Controller |
3 |
|
4 |
from django.db.models import Model, BigIntegerField, CharField, IntegerField |
5 |
from django.db import transaction |
6 |
from json import dumps as json_dumps, loads as json_loads |
7 |
|
8 |
|
9 |
class ControllerCommission(Model): |
10 |
|
11 |
serial = BigIntegerField(primary_key=True)
|
12 |
clientkey = CharField(null=False, max_length=72) |
13 |
physical_description = CharField(null=False, max_length=4096) |
14 |
status = IntegerField(null=False)
|
15 |
|
16 |
|
17 |
class DjangoController(Controller): |
18 |
|
19 |
def get_commission_issue(self, commission_spec): |
20 |
"""Prepare and return the arguments for the
|
21 |
quotaholder's issue_commission call,
|
22 |
containing the provisions required and
|
23 |
the target entity for their allocation.
|
24 |
"""
|
25 |
raise NotImplementedError |
26 |
|
27 |
def register_commission(self, serial, |
28 |
clientkey, |
29 |
physical_description ): |
30 |
|
31 |
"""Register a commission to the controller's stable storage,
|
32 |
along with the quotaholder serial and clientkey,
|
33 |
and the target physical description.
|
34 |
This information is needed to co-ordinate the commission
|
35 |
execution among the quotaholder server, the controller,
|
36 |
and the physical layer implementing the resource.
|
37 |
"""
|
38 |
physical_description = json_dumps(physical_description) |
39 |
create = ControllerCommission.objects.create |
40 |
create( serial=serial, clientkey=clientkey, |
41 |
physical_description=physical_description ) |
42 |
|
43 |
def get_commission(self, serial): |
44 |
"""Retrieve the commission registered with serial"""
|
45 |
try:
|
46 |
commission = ControllerCommission.objects.get(serial=serial) |
47 |
except ControllerCommission.DoesNotExist:
|
48 |
return None |
49 |
|
50 |
return (commission.serial,
|
51 |
commission.clientkey, |
52 |
commission.physical_description, |
53 |
commission.status) |
54 |
|
55 |
def complete_commission(self, serial): |
56 |
"""Mark and commit in stable storage the commission identified by
|
57 |
a serial as to-be-completed-successfully,
|
58 |
i.e that it has succeeded in producing a physical resource
|
59 |
and is to be removed from being tracked by the holder server,
|
60 |
controller, and physical layers.
|
61 |
"""
|
62 |
commission = ControllerCommission.objects.get(serial=serial) |
63 |
commission.status = 1
|
64 |
commission.save() |
65 |
|
66 |
def is_commission_complete(self, serial): |
67 |
"""Return true if the serial is marked as
|
68 |
completed by complete_commission()
|
69 |
"""
|
70 |
commission = ControllerCommission.objects.get(serial=serial) |
71 |
return commission.status > 0 |
72 |
|
73 |
def fail_commission(self, serial): |
74 |
"""Mark and commit in stable storage the commission identified by
|
75 |
a serial as to-be-completed-unsuccessfully,
|
76 |
i.e. that it has failed in producing a physical resource
|
77 |
and is to be removed from being tracked by the holder server,
|
78 |
controller, and physical layers.
|
79 |
"""
|
80 |
commission = ControllerCommission.objects.get(serial=serial) |
81 |
commission.status = -1
|
82 |
commission.save() |
83 |
|
84 |
def is_commission_failing(self, serial): |
85 |
"""Return true if the serial is marked as
|
86 |
failing by fail_commission()
|
87 |
"""
|
88 |
commission = ControllerCommission.objects.get(serial=serial) |
89 |
return commission.status < 0 |
90 |
|
91 |
def retire_commission(self, serial): |
92 |
"""Stop tracking the commission identified by a serial"""
|
93 |
|
94 |
try:
|
95 |
commission = ControllerCommission.objects.get(serial=serial) |
96 |
except ControllerCommission.DoesNotExist:
|
97 |
return
|
98 |
|
99 |
commission.delete() |
100 |
|