summaryrefslogtreecommitdiff
path: root/glance_store/capabilities.py
blob: 6a45d7dafe89ba7237573c1452d45694d841b0ce (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Copyright (c) 2015 IBM, Inc.
#
# 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.

"""Glance Store capability"""

import logging
import threading

import enum
from oslo_utils import reflection

from glance_store import exceptions
from glance_store.i18n import _LW

_STORE_CAPABILITES_UPDATE_SCHEDULING_BOOK = {}
_STORE_CAPABILITES_UPDATE_SCHEDULING_LOCK = threading.Lock()
LOG = logging.getLogger(__name__)


class BitMasks(enum.IntEnum):
    NONE = 0b00000000
    ALL = 0b11111111
    READ_ACCESS = 0b00000001
    # Included READ_ACCESS
    READ_OFFSET = 0b00000011
    # Included READ_ACCESS
    READ_CHUNK = 0b00000101
    # READ_OFFSET | READ_CHUNK
    READ_RANDOM = 0b00000111
    WRITE_ACCESS = 0b00001000
    # Included WRITE_ACCESS
    WRITE_OFFSET = 0b00011000
    # Included WRITE_ACCESS
    WRITE_CHUNK = 0b00101000
    # WRITE_OFFSET | WRITE_CHUNK
    WRITE_RANDOM = 0b00111000
    # READ_ACCESS | WRITE_ACCESS
    RW_ACCESS = 0b00001001
    # READ_OFFSET | WRITE_OFFSET
    RW_OFFSET = 0b00011011
    # READ_CHUNK | WRITE_CHUNK
    RW_CHUNK = 0b00101101
    # RW_OFFSET | RW_CHUNK
    RW_RANDOM = 0b00111111
    # driver is stateless and can be reused safely
    DRIVER_REUSABLE = 0b01000000


class StoreCapability(object):

    def __init__(self):
        # Set static store capabilities base on
        # current driver implementation.
        self._capabilities = getattr(self.__class__, "_CAPABILITIES", 0)

    @property
    def capabilities(self):
        return self._capabilities

    @staticmethod
    def contains(x, y):
        return x & y == y

    def update_capabilities(self):
        """
        Update dynamic storage capabilities based on current
        driver configuration and backend status when needed.

        As a hook, the function will be triggered in two cases:
        calling once after store driver get configured, it was
        used to update dynamic storage capabilities based on
        current driver configuration, or calling when the
        capabilities checking of an operation failed every time,
        this was used to refresh dynamic storage capabilities
        based on backend status then.

        This function shouldn't raise any exception out.
        """
        LOG.debug(("Store %s doesn't support updating dynamic "
                   "storage capabilities. Please overwrite "
                   "'update_capabilities' method of the store to "
                   "implement updating logics if needed.") %
                  reflection.get_class_name(self))

    def is_capable(self, *capabilities):
        """
        Check if requested capability(s) are supported by
        current driver instance.

        :param capabilities: required capability(s).
        """
        caps = 0

        for cap in capabilities:
            caps |= int(cap)

        return self.contains(self.capabilities, caps)

    def set_capabilities(self, *dynamic_capabilites):
        """
        Set dynamic storage capabilities based on current
        driver configuration and backend status.

        :param dynamic_capabilites: dynamic storage capability(s).
        """
        for cap in dynamic_capabilites:
            self._capabilities |= int(cap)

    def unset_capabilities(self, *dynamic_capabilites):
        """
        Unset dynamic storage capabilities.

        :param dynamic_capabilites: dynamic storage capability(s).
        """
        caps = 0

        for cap in dynamic_capabilites:
            caps |= int(cap)

        # TODO(zhiyan): Cascaded capability removal is
        # skipped currently, we can add it back later
        # when a concrete requirement comes out.
        # For example, when removing READ_ACCESS, all
        # read related capabilities need to be removed
        # together, e.g. READ_RANDOM.

        self._capabilities &= ~caps


def check(store_op_fun):

    def op_checker(store, *args, **kwargs):
        get_capabilities = [
            BitMasks.READ_ACCESS,
            BitMasks.READ_OFFSET if kwargs.get('offset') else BitMasks.NONE,
            BitMasks.READ_CHUNK if kwargs.get('chunk_size') else BitMasks.NONE
        ]

        op_cap_map = {
            'get': get_capabilities,
            'add': [BitMasks.WRITE_ACCESS],
            'delete': [BitMasks.WRITE_ACCESS]}

        op_exec_map = {
            'get': (exceptions.StoreRandomGetNotSupported
                    if kwargs.get('offset') or kwargs.get('chunk_size') else
                    exceptions.StoreGetNotSupported),
            'add': exceptions.StoreAddDisabled,
            'delete': exceptions.StoreDeleteNotSupported}

        op = store_op_fun.__name__.lower()

        try:
            req_cap = op_cap_map[op]
        except KeyError:
            LOG.warning(_LW('The capability of operation "%s" '
                            'could not be checked.'), op)
        else:
            if not store.is_capable(*req_cap):
                kwargs.setdefault('offset', 0)
                kwargs.setdefault('chunk_size', None)
                raise op_exec_map[op](**kwargs)

        return store_op_fun(store, *args, **kwargs)

    return op_checker