'Map a volume to a host and return target iSCSI information.'

@coordination.synchronized('huawei-mapping-{connector[host]}')
def initialize_connection(self, volume, connector):

    (lun_id, lun_type) = self.get_lun_id_and_type(volume)
    initiator_name = connector['initiator']
    LOG.info('initiator    name:    %(initiator_name)s,    LUN    ID:    %(lun_id)s.', {'initiator_name': initiator_name, 'lun_id': lun_id})
    (iscsi_iqns, target_ips, portgroup_id) = self.client.get_iscsi_params(connector)
    LOG.info('initialize_connection,    iscsi_iqn:    %(iscsi_iqn)s,    target_ip:    %(target_ip)s,    portgroup_id:    %(portgroup_id)s.', {'iscsi_iqn': iscsi_iqns, 'target_ip': target_ips, 'portgroup_id': portgroup_id})
    host_id = self.client.add_host_with_check(connector['host'])
    self.client.ensure_initiator_added(initiator_name, host_id)
    hostgroup_id = self.client.add_host_to_hostgroup(host_id)
    self.client.do_mapping(lun_id, hostgroup_id, host_id, portgroup_id, lun_type)
    hostlun_id = self.client.get_host_lun_id(host_id, lun_id, lun_type)
    LOG.info('initialize_connection,    host    lun    id    is:    %s.', hostlun_id)
    chapinfo = self.client.find_chap_info(self.client.iscsi_info, initiator_name)
    properties = {}
    properties['target_discovered'] = False
    properties['volume_id'] = volume.id
    multipath = connector.get('multipath', False)
    hostlun_id = int(hostlun_id)
    if (not multipath):
        properties['target_portal'] = ('%s:3260' % target_ips[0])
        properties['target_iqn'] = iscsi_iqns[0]
        properties['target_lun'] = hostlun_id
    else:
        properties['target_iqns'] = [iqn for iqn in iscsi_iqns]
        properties['target_portals'] = [('%s:3260' % ip) for ip in target_ips]
        properties['target_luns'] = ([hostlun_id] * len(target_ips))
    if chapinfo:
        (chap_username, chap_password) = chapinfo.split(';')
        properties['auth_method'] = 'CHAP'
        properties['auth_username'] = chap_username
        properties['auth_password'] = chap_password
    LOG.info('initialize_connection    success.    Return    data:    %s.', properties)
    return {'driver_volume_type': 'iscsi', 'data': properties}
