'Create a target with initiator iqn to attach a volume.'

def initialize_connection(self, volume, connector):

    start_time = time.time()
    LOG.debug('in    initialize_connection')
    LOG.debug('volume:    %s', volume.__dict__)
    LOG.debug('connector:    %s', connector)
    lun_status = self.enum('createing', 'unmapped', 'mapped')
    ret = self.api_executor.get_iscsi_portal_info()
    root = ET.fromstring(ret['data'])
    iscsi_port = root.find('iSCSIPortal').find('servicePort').text
    LOG.debug('iscsiPort:    %s', iscsi_port)
    target_iqn_prefix = root.find('iSCSIPortal').find('targetIQNPrefix').text
    LOG.debug('targetIQNPrefix:    %s', target_iqn_prefix)
    target_iqn_postfix = root.find('iSCSIPortal').find('targetIQNPostfix').text
    LOG.debug('target_iqn_postfix:    %s', target_iqn_postfix)
    lun_naa = self._get_lun_naa_from_volume_metadata(volume)
    if (lun_naa == ''):
        msg = (_('Volume    %s    does    not    exist.') % volume.id)
        LOG.error(msg)
        raise exception.VolumeDriverException(message=msg)
    LOG.debug('volume[name]:    %s', volume['name'])
    LOG.debug('volume[display_name]:    %s', volume['display_name'])
    selected_lun = self.api_executor.get_lun_info(LUNNAA=lun_naa)
    lun_index = selected_lun.find('LUNIndex').text
    LOG.debug('LUNIndex:    %s', lun_index)
    lun_owner = ''
    lun_slot_id = ''
    if (selected_lun.find('lun_owner') is not None):
        lun_owner = selected_lun.find('lun_owner').text
        LOG.debug('lun_owner:    %s', lun_owner)
        lun_slot_id = ('0' if (lun_owner == 'SCA') else '1')
        LOG.debug('lun_slot_id:    %s', lun_slot_id)
    ret = self.api_executor.get_all_iscsi_portal_setting()
    root = ET.fromstring(ret['data'])
    target_index = ''
    target_iqn = ''
    target_with_initiator_list = []
    target_acl_tree = root.find('targetACL')
    target_acl_list = target_acl_tree.findall('row')
    tmp_target_iqn = ''
    for targetACL in target_acl_list:
        tmp_target_iqn = targetACL.find('targetIQN').text
        LOG.debug('lun_slot_id:    %s', lun_slot_id)
        LOG.debug('tmp_target_iqn[-1]:    %s', tmp_target_iqn[(-1)])
        if (lun_slot_id != ''):
            if (lun_slot_id != tmp_target_iqn[(-1)]):
                LOG.debug('skip    the    targetiqn')
                continue
        target_init_info_list = targetACL.findall('targetInitInfo')
        for targetInitInfo in target_init_info_list:
            if (targetInitInfo.find('initiatorIQN').text == connector['initiator']):
                target_with_initiator_list.append(targetACL.find('targetIndex').text)
    target_tree = root.find('iSCSITargetList')
    target_list = target_tree.findall('targetInfo')
    for target_with_initiator in target_with_initiator_list:
        for target in target_list:
            if (target_with_initiator == target.find('targetIndex').text):
                if (int(target.find('targetStatus').text) >= 0):
                    target_index = target_with_initiator
                    target_iqn = target.find('targetIQN').text
    LOG.debug('exist    target_index:    %s', target_index)
    if (not target_index):
        target_name = self._gen_random_name()
        LOG.debug('target_name:    %s', target_name)
        target_index = self.api_executor.create_target(target_name, lun_owner)
        LOG.debug('targetIndex:    %s', target_index)
        target_info = self.api_executor.get_target_info(target_index)
        target_iqn = target_info.find('targetIQN').text
        LOG.debug('target_iqn:    %s', target_iqn)
        default_acl = target_iqn_prefix[:(target_iqn_prefix.find(':') + 1)]
        default_acl = (default_acl + 'all:iscsi.default.ffffff')
        LOG.debug('default_acl:    %s', default_acl)
        self.api_executor.remove_target_init(target_iqn, default_acl)
        self.api_executor.add_target_init(target_iqn, connector['initiator'])
    LOG.debug('LUNStatus:    %s', selected_lun.find('LUNStatus').text)
    if (selected_lun.find('LUNStatus').text == str(lun_status.unmapped)):
        self.api_executor.map_lun(lun_index, target_index)
    properties = {}
    properties['target_discovered'] = True
    properties['target_portal'] = ((self.configuration.iscsi_ip_address + ':') + iscsi_port)
    properties['target_iqn'] = target_iqn
    LOG.debug('properties[target_iqn]:    %s', properties['target_iqn'])
    lun_naa = self._get_lun_naa_from_volume_metadata(volume)
    LOG.debug('LUNNAA:    %s', lun_naa)
    mapped_lun = self.api_executor.get_lun_info(LUNNAA=lun_naa)
    target_lun_id = int(mapped_lun.find('LUNTargetList').find('row').find('LUNNumber').text)
    LOG.debug('target_lun_id:    %s', target_lun_id)
    properties['target_lun'] = target_lun_id
    properties['volume_id'] = volume['id']
    'Below    are    settings    for    multipath'
    target_iqns = []
    eth_list = self.api_executor.get_ethernet_ip(type='data')
    target_portals = []
    target_portals.append(((self.configuration.iscsi_ip_address + ':') + iscsi_port))
    target_iqns.append(target_iqn)
    for eth in eth_list:
        if (eth == self.configuration.iscsi_ip_address):
            continue
        target_portals.append(((eth + ':') + iscsi_port))
        target_iqns.append(target_iqn)
    properties['target_portals'] = target_portals
    properties['target_iqns'] = target_iqns
    properties['target_luns'] = ([target_lun_id] * len(target_portals))
    LOG.debug('properties:    %s', properties)
    elapsed_time = (time.time() - start_time)
    LOG.debug('initialize_connection    elapsed_time:    %s', elapsed_time)
    return {'driver_volume_type': 'iscsi', 'data': properties}
