'Finishes the second half of reverting a resize.
Bring the original source instance state back (active/shutoff) and
revert the resized attributes in the database.'

@wrap_exception()
@reverts_task_state
@wrap_instance_event(prefix='compute')
@errors_out_migration
@wrap_instance_fault
def finish_revert_resize(self, context, instance, reservations, migration):

    with self._error_out_instance_on_exception(context, instance):
        self._notify_about_instance_usage(context, instance, 'resize.revert.start')
        old_vm_state = instance.system_metadata.pop('old_vm_state', vm_states.ACTIVE)
        self._set_instance_info(instance, instance.old_flavor)
        instance.old_flavor = None
        instance.new_flavor = None
        instance.host = migration.source_compute
        instance.node = migration.source_node
        instance.save()
        self.network_api.setup_networks_on_host(context, instance, migration.source_compute)
        migration_p = obj_base.obj_to_primitive(migration)
        migration_p['dest_compute'] = migration.source_compute
        self.network_api.migrate_instance_finish(context, instance, migration_p)
        network_info = self.network_api.get_instance_nw_info(context, instance)
        block_device_info = self._get_instance_block_device_info(context, instance, refresh_conn_info=True)
        power_on = (old_vm_state != vm_states.STOPPED)
        self.driver.finish_revert_migration(context, instance, network_info, block_device_info, power_on)
        instance.drop_migration_context()
        instance.launched_at = timeutils.utcnow()
        instance.save(expected_task_state=task_states.RESIZE_REVERTING)
        LOG.info("Updating    instance    to    original    state:    '%s'", old_vm_state, instance=instance)
        if power_on:
            instance.vm_state = vm_states.ACTIVE
            instance.task_state = None
            instance.save()
        else:
            instance.task_state = task_states.POWERING_OFF
            instance.save()
            self.stop_instance(context, instance=instance, clean_shutdown=True)
        self._notify_about_instance_usage(context, instance, 'resize.revert.end')
