<commit_before># Copyright (c) 2014 ProphetStor, Inc.
# 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.

import errno

import six

from cinder import exception
from cinder.i18n import _
from cinder.openstack.common import log as logging
from cinder.volume import driver
from cinder.volume.drivers.prophetstor import dplcommon
from cinder.zonemanager import utils as fczm_utils

LOG = logging.getLogger(__name__)


class DPLFCDriver(dplcommon.DPLCOMMONDriver,
                  driver.FibreChannelDriver):
    def __init__(self, *args, **kwargs):
        super(DPLFCDriver, self).__init__(*args, **kwargs)

    def _get_fc_channel(self):
        """return :
                fcInfos[uuid]
                    fcInfo[uuid]['display_name']
                    fcInfo[uuid]['display_description']
                    fcInfo[uuid]['hardware_address']
                    fcInfo[uuid]['type']
                    fcInfo[uuid]['speed']
                    fcInfo[uuid]['state']
        """
        output = None
        fcInfos = {}
        try:
            retCode, output = self.dpl.get_server_info()
            if retCode == 0 and output:
                fcUuids = output.get('metadata',
                                     {}).get('storage_adapter', {}).keys()
                for fcUuid in fcUuids:
                    fcInfo = output.get('metadata',
                                        {}).get('storage_adapter',
                                                {}).get(fcUuid)
                    if fcInfo['type'] == 'fc':
                        fcInfos[fcUuid] = fcInfo
        except Exception as e:
            msg = _("Failed to get fiber channel info from storage due "
                    "to %(stat)s") % {'stat': six.string_types(e)}
            LOG.error(msg)
        return fcInfos

    def _get_targets(self):
        """return::
                targetInfos[uuid] = targetInfo
                targetInfo['targetUuid']
                targetInfo['targetName']
                targetInfo['targetAddr']
        """
        output = None
        targetInfos = {}
        try:
            retCode, output = self.dpl.get_target_list('target')
            if retCode == 0 and output:
                for targetInfo in output.get('children', []):
                    targetI = {}
                    targetI['targetUuid'] = targetInfo[0]
                    targetI['targetName'] = targetInfo[1]
                    targetI['targetAddr'] = targetInfo[2]
                    targetInfos[str(targetInfo[0])] = targetI
        except Exception as e:
            msg = _("Failed to get fiber channel target from storage server"
                    " due to %(stat)s") % {'stat': six.text_type(e)}
            targetInfos = {}
            LOG.error(msg)
        return targetInfos

    def _get_targetwpns(self, volumeid, initiatorWwpns):
        lstargetWwpns = []
        try:
            ret, output = self.dpl.get_vdev(volumeid)
            if ret == 0 and output:
                exports = output.get('exports', {})
                fc_infos = exports.get('Network/FC', {})
                for fc_info in fc_infos:
                    for p in fc_info.get('permissions', []):
                        if p.get(initiatorWwpns, None):
                            targetWwpns = fc_info.get('target_identifier', '')
                            lstargetWwpns.append(targetWwpns)
        except Exception as e:
            msg = _("Failed to get target wwpns from storage due "
                    "to %(stat)s") % {'stat': six.text_type(e)}
            LOG.error(msg)
            lstargetWwpns = []
        return lstargetWwpns

    def _is_initiator_wwpn_active(self, targetWwpn, initiatorWwpn):
        fActive = False
        output = None
        try:
            retCode, output = self.dpl.get_sns_table(targetWwpn)
            if retCode == 0 and output:
                for fdwwpn, fcport in output.get('metadata',
                                                 {}).get('sns_table',
                                                         []):
                    if fdwwpn == initiatorWwpn:
                        fActive = True
                        break
        except Exception:
            LOG.error(_('Failed to get sns table'))
        return fActive

    def _convertHex2String(self, wwpns):
        szwwpns = ''
        if len(str(wwpns)) == 16:
            szwwpns = '%2s:%2s:%2s:%2s:%2s:%2s:%2s:%2s' % (
                str(wwpns)[0:2],
                str(wwpns)[2:4],
                str(wwpns)[4:6],
                str(wwpns)[6:8],
                str(wwpns)[8:10],
                str(wwpns)[10:12],
                str(wwpns)[12:14],
                str(wwpns)[14:16])
        return szwwpns

    def _export_fc(self, volumeid, targetwwpns, initiatorwwpns, volumename):
        ret = 0
        output = ''
        msg = _('Export fc: %(volume)s, %(wwpns)s, %(iqn)s, %(volumename)s') \
            % {'volume': volumeid, 'wwpns': targetwwpns,
               'iqn': initiatorwwpns, 'volumename': volumename}
        LOG.debug(msg)
        try:
            ret, output = self.dpl.assign_vdev_fc(
                self._conver_uuid2hex(volumeid), targetwwpns,
                initiatorwwpns, volumename)
        except Exception:
            msg = _('Volume %(volumeid) failed to send assign command, '
                    'ret: %(status)s output: %(output)s') % \
                {'volumeid': volumeid, 'status': ret, 'output': output}
            LOG.error(msg)
            ret = errno.EFAULT

        if ret == errno.EAGAIN:
            ret, event_uuid = self._get_event_uuid(output)
            if len(event_uuid):
                ret = 0
                status = self._wait_event(
                    self.dpl.get_vdev_status,
                    self._conver_uuid2hex(volumeid), event_uuid)
                if status['state'] == 'error':
                    ret = errno.EFAULT
                    msg = _('Flexvisor failed to assign volume %(id)s: '
                            '%(status)s.') % {'id': volumeid,
                                              'status': status}
                    LOG.error(msg)
                    raise exception.VolumeBackendAPIException(data=msg)
            else:
                ret = errno.EFAULT
                msg = _('Flexvisor failed to assign volume %(id)s due to '
                        'unable to query status by event '
                        'id.') % {'id': volumeid}<commit_msg>Correct the message string<commit_after># Copyright (c) 2014 ProphetStor, Inc.
# 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.

import errno

import six

from cinder import exception
from cinder.i18n import _
from cinder.openstack.common import log as logging
from cinder.volume import driver
from cinder.volume.drivers.prophetstor import dplcommon
from cinder.zonemanager import utils as fczm_utils

LOG = logging.getLogger(__name__)


class DPLFCDriver(dplcommon.DPLCOMMONDriver,
                  driver.FibreChannelDriver):
    def __init__(self, *args, **kwargs):
        super(DPLFCDriver, self).__init__(*args, **kwargs)

    def _get_fc_channel(self):
        """return :
                fcInfos[uuid]
                    fcInfo[uuid]['display_name']
                    fcInfo[uuid]['display_description']
                    fcInfo[uuid]['hardware_address']
                    fcInfo[uuid]['type']
                    fcInfo[uuid]['speed']
                    fcInfo[uuid]['state']
        """
        output = None
        fcInfos = {}
        try:
            retCode, output = self.dpl.get_server_info()
            if retCode == 0 and output:
                fcUuids = output.get('metadata',
                                     {}).get('storage_adapter', {}).keys()
                for fcUuid in fcUuids:
                    fcInfo = output.get('metadata',
                                        {}).get('storage_adapter',
                                                {}).get(fcUuid)
                    if fcInfo['type'] == 'fc':
                        fcInfos[fcUuid] = fcInfo
        except Exception as e:
            msg = _("Failed to get fiber channel info from storage due "
                    "to %(stat)s") % {'stat': six.string_types(e)}
            LOG.error(msg)
        return fcInfos

    def _get_targets(self):
        """return::
                targetInfos[uuid] = targetInfo
                targetInfo['targetUuid']
                targetInfo['targetName']
                targetInfo['targetAddr']
        """
        output = None
        targetInfos = {}
        try:
            retCode, output = self.dpl.get_target_list('target')
            if retCode == 0 and output:
                for targetInfo in output.get('children', []):
                    targetI = {}
                    targetI['targetUuid'] = targetInfo[0]
                    targetI['targetName'] = targetInfo[1]
                    targetI['targetAddr'] = targetInfo[2]
                    targetInfos[str(targetInfo[0])] = targetI
        except Exception as e:
            msg = _("Failed to get fiber channel target from storage server"
                    " due to %(stat)s") % {'stat': six.text_type(e)}
            targetInfos = {}
            LOG.error(msg)
        return targetInfos

    def _get_targetwpns(self, volumeid, initiatorWwpns):
        lstargetWwpns = []
        try:
            ret, output = self.dpl.get_vdev(volumeid)
            if ret == 0 and output:
                exports = output.get('exports', {})
                fc_infos = exports.get('Network/FC', {})
                for fc_info in fc_infos:
                    for p in fc_info.get('permissions', []):
                        if p.get(initiatorWwpns, None):
                            targetWwpns = fc_info.get('target_identifier', '')
                            lstargetWwpns.append(targetWwpns)
        except Exception as e:
            msg = _("Failed to get target wwpns from storage due "
                    "to %(stat)s") % {'stat': six.text_type(e)}
            LOG.error(msg)
            lstargetWwpns = []
        return lstargetWwpns

    def _is_initiator_wwpn_active(self, targetWwpn, initiatorWwpn):
        fActive = False
        output = None
        try:
            retCode, output = self.dpl.get_sns_table(targetWwpn)
            if retCode == 0 and output:
                for fdwwpn, fcport in output.get('metadata',
                                                 {}).get('sns_table',
                                                         []):
                    if fdwwpn == initiatorWwpn:
                        fActive = True
                        break
        except Exception:
            LOG.error(_('Failed to get sns table'))
        return fActive

    def _convertHex2String(self, wwpns):
        szwwpns = ''
        if len(str(wwpns)) == 16:
            szwwpns = '%2s:%2s:%2s:%2s:%2s:%2s:%2s:%2s' % (
                str(wwpns)[0:2],
                str(wwpns)[2:4],
                str(wwpns)[4:6],
                str(wwpns)[6:8],
                str(wwpns)[8:10],
                str(wwpns)[10:12],
                str(wwpns)[12:14],
                str(wwpns)[14:16])
        return szwwpns

    def _export_fc(self, volumeid, targetwwpns, initiatorwwpns, volumename):
        ret = 0
        output = ''
        msg = _('Export fc: %(volume)s, %(wwpns)s, %(iqn)s, %(volumename)s') \
            % {'volume': volumeid, 'wwpns': targetwwpns,
               'iqn': initiatorwwpns, 'volumename': volumename}
        LOG.debug(msg)
        try:
            ret, output = self.dpl.assign_vdev_fc(
                self._conver_uuid2hex(volumeid), targetwwpns,
                initiatorwwpns, volumename)
        except Exception:
            msg = _('Volume %(volumeid)s failed to send assign command, '
                    'ret: %(status)s output: %(output)s') % \
                {'volumeid': volumeid, 'status': ret, 'output': output}
            LOG.error(msg)
            ret = errno.EFAULT

        if ret == errno.EAGAIN:
            ret, event_uuid = self._get_event_uuid(output)
            if len(event_uuid):
                ret = 0
                status = self._wait_event(
                    self.dpl.get_vdev_status,
                    self._conver_uuid2hex(volumeid), event_uuid)
                if status['state'] == 'error':
                    ret = errno.EFAULT
                    msg = _('Flexvisor failed to assign volume %(id)s: '
                            '%(status)s.') % {'id': volumeid,
                                              'status': status}
                    LOG.error(msg)
                    raise exception.VolumeBackendAPIException(data=msg)
            else:
                ret = errno.EFAULT
                msg = _('Flexvisor failed to assign volume %(id)s due to '
                        'unable to query status by event '
                        'id.') % {'id': volumeid}
{'fraction_non_alphanumeric': 0.10585553497649237, 'fraction_numerical': 0.007978344493517595, 'mean_word_length': 1.514147564469914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6529816', 'n_tokens_mistral': 4058, 'n_tokens_neox': 3835, 'n_words': 938}