summaryrefslogtreecommitdiff
path: root/cinder/volume/drivers/dell_emc/powerstore/utils.py
blob: 52c74a58703e7ae2bed10f334d71724a2eaf58b8 (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
179
180
181
182
183
184
185
186
187
188
189
# Copyright (c) 2020 Dell Inc. or its subsidiaries.
# All Rights Reserved.
#
# 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.

"""Utilities for Dell EMC PowerStore Cinder driver."""

from distutils import version
import functools
import re

from oslo_log import log as logging
from oslo_utils import units

from cinder.common import constants
from cinder import exception
from cinder.i18n import _
from cinder.objects import fields
from cinder.volume.drivers.dell_emc.powerstore import driver
from cinder.volume import volume_utils


LOG = logging.getLogger(__name__)
CHAP_DEFAULT_USERNAME = "PowerStore_iSCSI_CHAP_Username"
CHAP_DEFAULT_SECRET_LENGTH = 60
PROTOCOL_FC = constants.FC
PROTOCOL_ISCSI = constants.ISCSI
PROTOCOL_NVME = "NVMe"


def bytes_to_gib(size_in_bytes):
    """Convert size in bytes to GiB.

    :param size_in_bytes: size in bytes
    :return: size in GiB
    """

    return size_in_bytes // units.Gi


def gib_to_bytes(size_in_gb):
    """Convert size in GiB to bytes.

    :param size_in_gb: size in GiB
    :return: size in bytes
    """

    return size_in_gb * units.Gi


def extract_fc_wwpns(connector):
    """Convert connector FC ports to appropriate format with colons.

    :param connector: connection properties
    :return: FC ports in appropriate format with colons
    """

    if "wwnns" not in connector or "wwpns" not in connector:
        msg = _("Host %s does not have FC initiators.") % connector["host"]
        LOG.error(msg)
        raise exception.VolumeBackendAPIException(data=msg)
    return [":".join(re.findall("..", wwpn)) for wwpn in connector["wwpns"]]


def fc_wwn_to_string(wwn):
    """Convert FC WWN to string without colons.

    :param wwn: FC WWN
    :return: FC WWN without colons
    """

    return wwn.replace(":", "")


def iscsi_portal_with_port(address):
    """Add default port 3260 to iSCSI portal

    :param address: iSCSI portal without port
    :return: iSCSI portal with default port 3260
    """

    return "%(address)s:3260" % {"address": address}


def powerstore_host_name(connector, protocol):
    """Generate PowerStore host name for connector.

    :param connector: connection properties
    :param protocol: storage protocol (FC, iSCSI or NVMe)
    :return: unique host name
    """

    return ("%(host)s-%(protocol)s" %
            {"host": connector["host"],
             "protocol": protocol, })


def filter_hosts_by_initiators(hosts, initiators):
    """Filter hosts by given list of initiators.

    :param hosts: list of PowerStore host objects
    :param initiators: list of initiators
    :return: PowerStore hosts list
    """

    hosts_names_found = set()
    for host in hosts:
        for initiator in host["host_initiators"]:
            if initiator["port_name"] in initiators:
                hosts_names_found.add(host["name"])
    return list(filter(lambda host: host["name"] in hosts_names_found, hosts))


def is_multiattached_to_host(volume_attachment, host_name):
    """Check if volume is attached to multiple instances on one host.

    When multiattach is enabled, a volume could be attached to two or more
    instances which are hosted on one nova host.
    Because PowerStore cannot recognize the volume is attached to two or more
    instances, we should keep the volume attached to the nova host until
    the volume is detached from the last instance.

    :param volume_attachment: list of VolumeAttachment objects
    :param host_name: OpenStack host name
    :return: multiattach flag
    """

    if not volume_attachment:
        return False

    attachments = [
        attachment for attachment in volume_attachment
        if (attachment.attach_status == fields.VolumeAttachStatus.ATTACHED and
            attachment.attached_host == host_name)
    ]
    return len(attachments) > 1


def get_chap_credentials():
    """Generate CHAP credentials.

    :return: CHAP username and secret
    """

    return {
        "chap_single_username": CHAP_DEFAULT_USERNAME,
        "chap_single_password": volume_utils.generate_password(
            CHAP_DEFAULT_SECRET_LENGTH
        )
    }


def get_protection_policy_from_volume(volume):
    """Get PowerStore Protection policy name from volume type.

    :param volume: OpenStack Volume object
    :return: Protection policy name
    """

    return volume.volume_type.extra_specs.get(driver.POWERSTORE_PP_KEY)


def is_group_a_cg_snapshot_type(func):
    """Check if group is a consistent snapshot group.

    Fallback to generic volume group implementation if consistent group
    snapshot is not enabled.
    """

    @functools.wraps(func)
    def inner(self, *args, **kwargs):
        if not volume_utils.is_group_a_cg_snapshot_type(args[0]):
            raise NotImplementedError
        return func(self, *args, **kwargs)
    return inner


def version_gte(ver1, ver2):
    return version.LooseVersion(ver1) >= version.LooseVersion(ver2)