'Enable replication.'

def enable_replication(self, ctxt, group):

    group.refresh()
    if (group.replication_status != fields.ReplicationStatus.ENABLING):
        msg = (_('Replication    status    in    group    %s    is    not    enabling.    Cannot    enable    replication.') % group.id)
        LOG.error(msg)
        raise exception.InvalidGroup(reason=msg)
    volumes = group.volumes
    for vol in volumes:
        vol.refresh()
        if (vol.replication_status != fields.ReplicationStatus.ENABLING):
            msg = (_('Replication    status    in    volume    %s    is    not    enabling.    Cannot    enable    replication.') % vol.id)
            LOG.error(msg)
            raise exception.InvalidVolume(reason=msg)
    self._notify_about_group_usage(ctxt, group, 'enable_replication.start')
    volumes_model_update = None
    model_update = None
    try:
        utils.require_driver_initialized(self.driver)
        (model_update, volumes_model_update) = self.driver.enable_replication(ctxt, group, volumes)
        if volumes_model_update:
            for update in volumes_model_update:
                vol_obj = objects.Volume.get_by_id(ctxt, update['id'])
                vol_obj.update(update)
                vol_obj.save()
                if ((update.get('replication_status') == fields.ReplicationStatus.ERROR) and (model_update.get('replication_status') != fields.ReplicationStatus.ERROR)):
                    model_update['replication_status'] = update.get('replication_status')
        if model_update:
            if (model_update.get('replication_status') == fields.ReplicationStatus.ERROR):
                msg = _('Enable    replication    failed.')
                LOG.error(msg, resource={'type': 'group', 'id': group.id})
                raise exception.VolumeDriverException(message=msg)
            else:
                group.update(model_update)
                group.save()
    except exception.CinderException as ex:
        group.status = fields.GroupStatus.ERROR
        group.replication_status = fields.ReplicationStatus.ERROR
        group.save()
        if (not volumes_model_update):
            for vol in volumes:
                vol.status = 'error'
                vol.replication_status = fields.ReplicationStatus.ERROR
                vol.save()
        err_msg = (_('Enable    replication    group    failed:    %s.') % six.text_type(ex))
        raise exception.ReplicationGroupError(reason=err_msg, group_id=group.id)
    for vol in volumes:
        vol.replication_status = fields.ReplicationStatus.ENABLED
        vol.save()
    group.replication_status = fields.ReplicationStatus.ENABLED
    group.save()
    self._notify_about_group_usage(ctxt, group, 'enable_replication.end', volumes)
    LOG.info('Enable    replication    completed    successfully.', resource={'type': 'group', 'id': group.id})
