'map volume to host.'

@lockutils.synchronized('ETERNUS-connect', 'cinder-', True)
def _map_lun(self, vol_instance, connector, targetlist=None):

    volumename = vol_instance['ElementName']
    LOG.debug('_map_lun,    volume    name:    %(vname)s,    connector:    %(connector)s.', {'vname': volumename, 'connector': connector})
    volume_uid = vol_instance['Name']
    initiatorlist = self._find_initiator_names(connector)
    aglist = self._find_affinity_group(connector)
    configservice = self._find_eternus_service(CTRL_CONF)
    if (targetlist is None):
        targetlist = self._get_target_port()
    if (configservice is None):
        msg = (_('_map_lun,    vol_instance.path:%(vol)s,    volumename:    %(volumename)s,    volume_uid:    %(uid)s,    initiator:    %(initiator)s,    target:    %(tgt)s,    aglist:    %(aglist)s,    Storage    Configuration    Service    not    found.') % {'vol': vol_instance.path, 'volumename': volumename, 'uid': volume_uid, 'initiator': initiatorlist, 'tgt': targetlist, 'aglist': aglist})
        LOG.error(msg)
        raise exception.VolumeBackendAPIException(data=msg)
    LOG.debug('_map_lun,    vol_instance.path:    %(vol_instance)s,    volumename:%(volumename)s,    initiator:%(initiator)s,    target:%(tgt)s.', {'vol_instance': vol_instance.path, 'volumename': [volumename], 'initiator': initiatorlist, 'tgt': targetlist})
    if (not aglist):
        for target in targetlist:
            LOG.debug('_map_lun,    lun_name:    %(volume_uid)s,    Initiator:    %(initiator)s,    target:    %(target)s.', {'volume_uid': [volume_uid], 'initiator': initiatorlist, 'target': target['Name']})
            (rc, errordesc, job) = self._exec_eternus_service('ExposePaths', configservice, LUNames=[volume_uid], InitiatorPortIDs=initiatorlist, TargetPortIDs=[target['Name']], DeviceAccesses=[self._pywbem_uint(2, '16')])
            LOG.debug('_map_lun,    Error:    %(errordesc)s,    Return    code:    %(rc)lu,    Create    affinitygroup    and    set    host-affinity.', {'errordesc': errordesc, 'rc': rc})
            if ((rc != 0) and (rc != LUNAME_IN_USE)):
                LOG.warning('_map_lun,    lun_name:    %(volume_uid)s,    Initiator:    %(initiator)s,    target:    %(target)s,    Return    code:    %(rc)lu,    Error:    %(errordesc)s.', {'volume_uid': [volume_uid], 'initiator': initiatorlist, 'target': target['Name'], 'rc': rc, 'errordesc': errordesc})
    else:
        for ag in aglist:
            LOG.debug('_map_lun,    ag:    %(ag)s,    lun_name:    %(volume_uid)s.', {'ag': ag, 'volume_uid': volume_uid})
            (rc, errordesc, job) = self._exec_eternus_service('ExposePaths', configservice, LUNames=[volume_uid], DeviceAccesses=[self._pywbem_uint(2, '16')], ProtocolControllers=[ag])
            LOG.debug('_map_lun,    Error:    %(errordesc)s,    Return    code:    %(rc)lu,    Add    lun    to    affinity    group.', {'errordesc': errordesc, 'rc': rc})
            if ((rc != 0) and (rc != LUNAME_IN_USE)):
                LOG.warning('_map_lun,    lun_name:    %(volume_uid)s,    Initiator:    %(initiator)s,    ag:    %(ag)s,    Return    code:    %(rc)lu,    Error:    %(errordesc)s.', {'volume_uid': [volume_uid], 'initiator': initiatorlist, 'ag': ag, 'rc': rc, 'errordesc': errordesc})
