'find affinity group from connector.'

def _find_affinity_group(self, connector, vol_instance=None):

    LOG.debug('_find_affinity_group,    vol_instance:    %s.', vol_instance)
    affinity_grouplist = []
    initiatorlist = self._find_initiator_names(connector)
    if (vol_instance is None):
        try:
            aglist = self._enum_eternus_instance_names('FUJITSU_AffinityGroupController')
        except Exception:
            msg = (_('_find_affinity_group,    connector:    %(connector)s,    EnumerateInstanceNames,    cannot    connect    to    ETERNUS.') % {'connector': connector})
            LOG.error(msg)
            raise exception.VolumeBackendAPIException(data=msg)
        LOG.debug('_find_affinity_group,affinity_groups:%s', aglist)
    else:
        try:
            aglist = self._assoc_eternus_names(vol_instance.path, AssocClass='FUJITSU_ProtocolControllerForUnit', ResultClass='FUJITSU_AffinityGroupController')
        except Exception:
            msg = (_('_find_affinity_group,connector:    %(connector)s,AssocNames:    FUJITSU_ProtocolControllerForUnit,    cannot    connect    to    ETERNUS.') % {'connector': connector})
            LOG.error(msg)
            raise exception.VolumeBackendAPIException(data=msg)
        LOG.debug('_find_affinity_group,    vol_instance.path:    %(volume)s,    affinity_groups:    %(aglist)s.', {'volume': vol_instance.path, 'aglist': aglist})
    for ag in aglist:
        try:
            hostaglist = self._assoc_eternus(ag, AssocClass='FUJITSU_AuthorizedTarget', ResultClass='FUJITSU_AuthorizedPrivilege')
        except Exception:
            msg = (_('_find_affinity_group,    connector:    %(connector)s,    Associators:    FUJITSU_AuthorizedTarget,    cannot    connect    to    ETERNUS.') % {'connector': connector})
            LOG.error(msg)
            raise exception.VolumeBackendAPIException(data=msg)
        for hostag in hostaglist:
            for initiator in initiatorlist:
                if (initiator.lower() not in hostag['InstanceID'].lower()):
                    continue
                LOG.debug('_find_affinity_group,    AffinityGroup:    %(ag)s.', {'ag': ag})
                affinity_grouplist.append(ag)
                break
            break
    LOG.debug('_find_affinity_group,    initiators:    %(initiator)s,    affinity_group:    %(affinity_group)s.', {'initiator': initiatorlist, 'affinity_group': affinity_grouplist})
    return affinity_grouplist
