2015-05-14 20:43:31 +03:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2015-05-14 20:43:31 +03:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
2015-05-19 19:41:23 +03:00
# ifndef SNAPSHOTS_H_
# define SNAPSHOTS_H_
2015-05-14 20:43:31 +03:00
# include <iostream>
# include <string>
# include <map>
# include <libxml/parser.h>
# include "Template.h"
using namespace std ;
class VectorAttribute ;
/**
2015-05-19 19:41:23 +03:00
* This class represents a list of Snapshots associated to an image or Virtual
* Machine Disk . The list is in the form :
* < SNAPSHOTS >
* < DISK_ID > : of the disk the snapshots are taken from ( the initial backing )
* < ACTIVE > : the current snapshot in use by the VM . 0 for the original DISK
* < SNAPSHOT >
* < ID >
2015-07-09 14:13:07 +03:00
* < NAME > : Description
2015-05-19 19:41:23 +03:00
* < DATE > : the snapshot was taken
2015-05-26 14:52:40 +03:00
* < PARENT > : backing for this snapshot , - 1 for the original image
* < CHILDREN > : comma separated list of children snapshots
2015-05-14 20:43:31 +03:00
*/
class Snapshots
{
public :
2018-12-24 15:58:27 +03:00
/**
* ALLOW_ORPHANS : Define how child snapshots are handled .
* - ALLOW : Children can be orphan ( no parent snapshot )
* | - snap_1
* | - snap_2
* | - snap_3
*
* - DENY : New snapshots are set active and child of the previous one
* | - snap_1
* | - snap_2
* | - snap_3
*
* - MIXED : Snapshots are children of last snapshot reverted to
* | - snap_1 ( < - - - revert )
* | - snap_2
* | - snap_3
*/
enum AllowOrphansMode
{
ALLOW = 0 ,
DENY = 1 ,
MIXED = 2
} ;
static string allow_orphans_mode_to_str ( AllowOrphansMode aom )
{
switch ( aom )
{
case ALLOW : return " YES " ;
case DENY : return " NO " ;
case MIXED : return " MIXED " ;
}
return " NO " ;
} ;
static AllowOrphansMode str_to_allow_orphans_mode ( const string & aom )
{
if ( aom = = " YES " )
{
return ALLOW ;
}
else if ( aom = = " MIXED " )
{
return MIXED ;
}
else
{
return DENY ;
}
} ;
Snapshots ( int _disk_id , AllowOrphansMode orphans ) ;
2015-05-14 20:43:31 +03:00
2015-05-30 14:31:25 +03:00
Snapshots ( const Snapshots & s ) ;
2015-06-01 20:25:02 +03:00
Snapshots & operator = ( const Snapshots & s ) ;
2019-07-10 20:36:43 +03:00
~ Snapshots ( ) = default ;
2015-05-14 20:43:31 +03:00
// *************************************************************************
// Inititalization functions
// *************************************************************************
/**
* Builds the snapshot list from its XML representation . This function
* is used when importing it from the DB .
* @ param node xmlNode for the template
* @ return 0 on success
*/
int from_xml_node ( const xmlNodePtr node ) ;
2015-05-19 19:41:23 +03:00
/**
* XML Representation of the Snapshots
*/
string & to_xml ( string & xml ) const
{
return snapshot_template . to_xml ( xml ) ;
} ;
/**
* Creates a new ( empty ) snapshot of the active disk
2015-07-09 14:13:07 +03:00
* @ param name description of this snapshot ( optional )
2015-06-17 00:40:09 +03:00
* @ param size_mb of the snapshot ( virtual size )
2015-05-19 19:41:23 +03:00
* @ return id of the new snapshot
*/
2015-07-23 13:44:17 +03:00
int create_snapshot ( const string & name , long long size_mb ) ;
2015-05-14 20:43:31 +03:00
2015-05-19 19:41:23 +03:00
/**
2015-06-04 20:46:46 +03:00
* Check if an snapshot can be deleted ( no children , no active )
2015-05-19 19:41:23 +03:00
* @ param id of the snapshot
2015-05-26 12:24:34 +03:00
* @ param error if any
2015-06-04 20:46:46 +03:00
* @ return true if can be deleted , false otherwise
2015-05-19 19:41:23 +03:00
*/
2015-07-01 18:50:47 +03:00
bool test_delete ( int id , string & error ) const ;
2015-06-04 20:46:46 +03:00
/**
* Removes the snapshot from the list
* @ param id of the snapshot
*/
2015-07-01 18:50:47 +03:00
void delete_snapshot ( int id ) ;
2015-05-14 20:43:31 +03:00
/**
* Set the given snapshot as active . Updates the values of the current
* snapshot
2018-12-24 15:58:27 +03:00
*
* @ param id id of the snapshot
* @ param revert true if the cause of changing the active snapshot
* is because a revert
2015-05-14 20:43:31 +03:00
*/
2018-12-24 15:58:27 +03:00
int active_snapshot ( int id , bool revert ) ;
2015-05-19 19:41:23 +03:00
2018-10-11 18:01:36 +03:00
/**
* Rename the given snapshot by the given name
*/
int rename_snapshot ( int id , const string & name , string & str_error ) ;
2015-06-05 17:01:06 +03:00
/**
* Clear all the snapshots in the list
*/
void clear ( )
{
active = - 1 ;
disk_id = - 1 ;
snapshot_template . clear ( ) ;
snapshot_pool . clear ( ) ;
}
2015-05-19 19:41:23 +03:00
/**
* Return the disk_id of the snapshot list
*/
2015-06-04 20:46:46 +03:00
int get_disk_id ( ) const
2015-05-19 19:41:23 +03:00
{
return disk_id ;
}
2015-05-14 20:43:31 +03:00
2015-06-04 20:46:46 +03:00
/**
* Return the active snapshot id
*/
int get_active_id ( ) const
{
return active ;
}
2015-05-30 14:31:25 +03:00
/**
* Removes the DISK_ID attribute to link the list to an Image
*/
void clear_disk_id ( )
{
disk_id = - 1 ;
snapshot_template . erase ( " DISK_ID " ) ;
} ;
2015-06-01 20:25:02 +03:00
/**
* Sets the disk id for this snapshot list
* @ param did the id
*/
void set_disk_id ( int did )
{
disk_id = did ;
snapshot_template . replace ( " DISK_ID " , did ) ;
2015-06-04 20:46:46 +03:00
} ;
2015-06-01 20:25:02 +03:00
/**
* @ return number of snapshots in the list
*/
2015-06-04 20:46:46 +03:00
unsigned int size ( ) const
2015-06-01 20:25:02 +03:00
{
return snapshot_pool . size ( ) ;
2015-06-04 20:46:46 +03:00
} ;
2015-06-01 20:25:02 +03:00
2018-03-19 19:03:09 +03:00
/**
* @ return true if snapshot_pool is empty
*/
bool empty ( ) const
{
return snapshot_pool . empty ( ) ;
} ;
2015-06-05 14:32:00 +03:00
/**
* Check if snapshot exists
* @ param snap_id of the snapshot
* @ return true if the snapshot with snap_id exisits
*/
bool exists ( int snap_id ) const
{
const VectorAttribute * snap = get_snapshot ( snap_id ) ;
return ( snap ! = 0 ) ;
}
2015-06-17 00:40:09 +03:00
/**
* @ return total snapshot size ( virtual ) in mb
*/
2015-07-23 13:44:17 +03:00
long long get_total_size ( ) const ;
2015-06-17 00:40:09 +03:00
2015-06-17 18:45:37 +03:00
/**
* Get the size ( virtual ) in mb of the given snapshot
* @ param id of the snapshot
* @ return size or 0 if not found
*/
2015-07-23 13:44:17 +03:00
long long get_snapshot_size ( int id ) const ;
2015-06-17 18:45:37 +03:00
2015-05-20 18:48:27 +03:00
/**
* Get Attribute from the given snapshot
* @ param id of the snapshot
* @ param name of the attribute
*
* @ return value or empty if not found
*/
2015-07-01 18:50:47 +03:00
string get_snapshot_attribute ( int id , const char * name ) const ;
2015-05-20 18:48:27 +03:00
2015-05-14 20:43:31 +03:00
private :
/**
* Get a snapshot from the pool
* @ param id of the snapshot
* @ return pointer to the snapshot ( VectorAttribute ) or null
*/
2015-07-01 18:50:47 +03:00
const VectorAttribute * get_snapshot ( int id ) const ;
2015-06-04 20:46:46 +03:00
2015-07-01 18:50:47 +03:00
VectorAttribute * get_snapshot ( int id )
2015-06-04 20:46:46 +03:00
{
return const_cast < VectorAttribute * > (
static_cast < const Snapshots & > ( * this ) . get_snapshot ( id ) ) ;
} ;
2015-05-14 20:43:31 +03:00
2015-05-30 14:31:25 +03:00
/**
* Build the object internal representation from an initialized
* template
*/
void init ( ) ;
2018-12-24 15:58:27 +03:00
/**
* Updates children list for the current base snapshot in the tree
* @ param snapshot new child to be added
*/
void add_child_mixed ( VectorAttribute * snapshot ) ;
/**
* Updates children list of the active snapshot
* @ param snapshot new child to be added
* @ return - 1 in case of error ( current active does not exist )
*/
int add_child_deny ( VectorAttribute * snapshot ) ;
2015-05-19 19:41:23 +03:00
/**
* Text representation of the snapshot pool . To be stored as part of the
* VM or Image Templates
*/
2015-05-14 20:43:31 +03:00
Template snapshot_template ;
2015-05-19 19:41:23 +03:00
/**
* Next id
*/
2015-07-01 18:50:47 +03:00
int next_snapshot ;
2015-05-14 20:43:31 +03:00
2015-05-19 19:41:23 +03:00
/**
* Id of the active ( current ) snapshot , 0 represents the base image
*/
2015-05-26 13:52:55 +03:00
int active ;
2015-05-19 19:41:23 +03:00
/**
* Id of the disk associated with this snapshot list
*/
2015-07-01 18:50:47 +03:00
int disk_id ;
2015-05-14 20:43:31 +03:00
2017-07-05 19:07:22 +03:00
/**
* Allow to remove parent snapshots and active one
*/
2018-12-24 15:58:27 +03:00
AllowOrphansMode orphans ;
2017-07-05 19:07:22 +03:00
2015-05-19 19:41:23 +03:00
/**
* Snapshot pointer map
*/
2015-07-01 18:50:47 +03:00
map < int , VectorAttribute * > snapshot_pool ;
2018-12-24 15:58:27 +03:00
/**
* Current snaphsot base for mixed mode
*/
int current_base ;
2015-05-14 20:43:31 +03:00
} ;
2015-05-19 19:41:23 +03:00
# endif /*SNAPSHOTS_H_*/