'Add hostgroup and lungroup to mapping view.'

def do_mapping(self, lun_id, hostgroup_id, host_id, portgroup_id=None, lun_type=constants.LUN_TYPE, hypermetro_lun=False):

    lungroup_name = (constants.LUNGROUP_PREFIX + host_id)
    mapping_view_name = (constants.MAPPING_VIEW_PREFIX + host_id)
    lungroup_id = self._find_lungroup(lungroup_name)
    view_id = self.find_mapping_view(mapping_view_name)
    map_info = {}
    LOG.info('do_mapping,    lun_group:    %(lun_group)s,    view_id:    %(view_id)s,    lun_id:    %(lun_id)s.', {'lun_group': lungroup_id, 'view_id': view_id, 'lun_id': lun_id})
    try:
        if (lungroup_id is None):
            lungroup_id = self._create_lungroup(lungroup_name)
        is_associated = self._is_lun_associated_to_lungroup(lungroup_id, lun_id, lun_type)
        if (not is_associated):
            self.associate_lun_to_lungroup(lungroup_id, lun_id, lun_type)
        if (view_id is None):
            view_id = self._add_mapping_view(mapping_view_name)
            self._associate_hostgroup_to_view(view_id, hostgroup_id)
            self._associate_lungroup_to_view(view_id, lungroup_id)
            if portgroup_id:
                self._associate_portgroup_to_view(view_id, portgroup_id)
        else:
            if (not self.hostgroup_associated(view_id, hostgroup_id)):
                self._associate_hostgroup_to_view(view_id, hostgroup_id)
            if (not self.lungroup_associated(view_id, lungroup_id)):
                self._associate_lungroup_to_view(view_id, lungroup_id)
            if portgroup_id:
                if (not self._portgroup_associated(view_id, portgroup_id)):
                    self._associate_portgroup_to_view(view_id, portgroup_id)
        if hypermetro_lun:
            aval_luns = self.find_view_by_id(view_id)
            map_info['lun_id'] = lun_id
            map_info['view_id'] = view_id
            map_info['aval_luns'] = aval_luns
    except Exception:
        with excutils.save_and_reraise_exception():
            LOG.error('Error    occurred    when    adding    hostgroup    and    lungroup    to    view.    Remove    lun    from    lungroup    now.')
            self.remove_lun_from_lungroup(lungroup_id, lun_id, lun_type)
    return map_info
