summaryrefslogtreecommitdiffstats
path: root/conductor/conductor/common/models/order_lock.py
blob: f9ad465cd0e0c5850d5aec6c57df4b3a7dcb2902 (plain)
1
2
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#
# -------------------------------------------------------------------------
#   Copyright (c) 2015-2018 AT&T Intellectual Property
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# -------------------------------------------------------------------------
#

from conductor.common import db_backend
from conductor.common.music.model import base


class OrderLock(base.Base):

    __tablename__ = "order_locks"
    __keyspace__ = None

    id = None
    plans = None
    is_spinup_completed = None
    spinup_completed_timestamp = None

    # Status
    PARKED = "parked"
    UNDER_SPIN_UP = "under-spin-up"
    COMPLETED = "completed"
    REHOME = "rehome"
    FAILED = "failed"

    SPINING = [PARKED, UNDER_SPIN_UP]
    REHOMABLE = [REHOME, COMPLETED]

    @classmethod
    def schema(cls):
        """Return schema."""
        schema = {
            'id': 'text',
            'plans': 'map<text, text>',
            'is_spinup_completed': 'boolean',
            'spinup_completed_timestamp': 'bigint',
            'PRIMARY KEY': '(id)'
        }
        return schema

    @classmethod
    def atomic(cls):
        """Use atomic operations"""
        return True

    @classmethod
    def pk_name(cls):
        """Primary key name"""
        return 'id'

    def pk_value(self):
        """Primary key value"""
        return self.id

    def values(self):
        """Values"""
        value_dict = {
            'id': self.id,
            'plans': self.plans,
            'is_spinup_completed': self.is_spinup_completed,
            'spinup_completed_timestamp': self.spinup_completed_timestamp
        }
        return value_dict

    def update(self, plan_id, updated_fields, values=None):
        """Update order lock"""
        db_backend.DB_API.row_complex_field_update(
            self.__keyspace__, self.__tablename__, self.pk_name(),
            self.pk_value(), plan_id, updated_fields, values)

    def insert(self):
        return \
            db_backend.DB_API.row_insert_by_condition(
                self.__keyspace__, self.__tablename__, self.pk_name(),
                self.pk_value(), self.values(), self.PARKED)

    def __init__(self, id=None, plans=None, is_spinup_completed=False, spinup_completed_timestamp=None, _insert=False):
        """Initializer"""
        super(OrderLock, self).__init__()
        # Breaking here with error: Can't set attribute
        self.id = id
        self.plans = plans
        self.is_spinup_completed = is_spinup_completed
        self.spinup_completed_timestamp = spinup_completed_timestamp

        if _insert:
            self.insert()

    def __repr__(self):
        """Object representation"""
        return '<OrderLock {}>'.format(self.id)

    def __json__(self):
        """JSON representation"""
        json_ = {}
        json_[id] = self.id,
        json_['plans'] = self.plans
        json_['is_spinup_completed'] = self.is_spinup_completed
        json_['spinup_completed_timestamp'] = self.spinup_completed_timestamp

        return json_