'Delete map between a volume and a host.'

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

    (lun_id, lun_type) = self.get_lun_id_and_type(volume)
    initiator_name = connector['initiator']
    host_name = connector['host']
    lungroup_id = None
    LOG.info('terminate_connection:    initiator    name:    %(ini)s,    LUN    ID:    %(lunid)s.', {'ini': initiator_name, 'lunid': lun_id})
    portgroup = None
    portgroup_id = None
    view_id = None
    left_lunnum = (-1)
    for ini in self.client.iscsi_info:
        if (ini['Name'] == initiator_name):
            for key in ini:
                if (key == 'TargetPortGroup'):
                    portgroup = ini['TargetPortGroup']
                    break
    if portgroup:
        portgroup_id = self.client.get_tgt_port_group(portgroup)
    host_id = huawei_utils.get_host_id(self.client, host_name)
    if host_id:
        mapping_view_name = (constants.MAPPING_VIEW_PREFIX + host_id)
        view_id = self.client.find_mapping_view(mapping_view_name)
        if view_id:
            lungroup_id = self.client.find_lungroup_from_map(view_id)
    if (lun_id and lungroup_id):
        lungroup_ids = self.client.get_lungroupids_by_lunid(lun_id, lun_type)
        if (lungroup_id in lungroup_ids):
            self.client.remove_lun_from_lungroup(lungroup_id, lun_id, lun_type)
        else:
            LOG.warning('LUN    is    not    in    lungroup.    LUN    ID:    %(lun_id)s.    Lungroup    id:    %(lungroup_id)s.', {'lun_id': lun_id, 'lungroup_id': lungroup_id})
    if lungroup_id:
        left_lunnum = self.client.get_obj_count_from_lungroup(lungroup_id)
    if (portgroup_id and view_id and (int(left_lunnum) <= 0)):
        if self.client.is_portgroup_associated_to_view(view_id, portgroup_id):
            self.client.delete_portgroup_mapping_view(view_id, portgroup_id)
    if (view_id and (int(left_lunnum) <= 0)):
        self.client.remove_chap(initiator_name)
        if self.client.lungroup_associated(view_id, lungroup_id):
            self.client.delete_lungroup_mapping_view(view_id, lungroup_id)
        self.client.delete_lungroup(lungroup_id)
        if self.client.is_initiator_associated_to_host(initiator_name, host_id):
            self.client.remove_iscsi_from_host(initiator_name)
        hostgroup_name = (constants.HOSTGROUP_PREFIX + host_id)
        hostgroup_id = self.client.find_hostgroup(hostgroup_name)
        if hostgroup_id:
            if self.client.hostgroup_associated(view_id, hostgroup_id):
                self.client.delete_hostgoup_mapping_view(view_id, hostgroup_id)
            self.client.remove_host_from_hostgroup(hostgroup_id, host_id)
            self.client.delete_hostgroup(hostgroup_id)
        self.client.remove_host(host_id)
        self.client.delete_mapping_view(view_id)
