2015-05-14 20:43:31 +03:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* 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 >
* < TAG > : Description
* < 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 :
Snapshots ( int _disk_id ) ;
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 ) ;
2015-05-14 20:43:31 +03:00
virtual ~ Snapshots ( ) ;
// *************************************************************************
// 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
* @ param tag 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-06-17 00:40:09 +03:00
int create_snapshot ( const string & tag , unsigned int 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-06-04 20:46:46 +03:00
bool test_delete ( unsigned int id , string & error ) const ;
/**
* Removes the snapshot from the list
* @ param id of the snapshot
*/
void delete_snapshot ( unsigned int id ) ;
2015-05-14 20:43:31 +03:00
/**
* Set the given snapshot as active . Updates the values of the current
* snapshot
*/
2015-06-04 20:46:46 +03:00
int active_snapshot ( unsigned int id ) ;
2015-05-19 19:41:23 +03:00
2015-06-05 17:01:06 +03:00
/**
* Clear all the snapshots in the list
*/
void clear ( )
{
next_snapshot = 0 ;
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
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
*/
unsigned int get_total_size ( ) const ;
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
*/
string get_snapshot_attribute ( unsigned int id , const char * name ) ;
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-06-04 20:46:46 +03:00
const VectorAttribute * get_snapshot ( unsigned int id ) const ;
VectorAttribute * get_snapshot ( unsigned int id )
{
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 ( ) ;
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-05-14 20:43:31 +03:00
unsigned int next_snapshot ;
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-05-14 20:43:31 +03:00
unsigned int disk_id ;
2015-05-19 19:41:23 +03:00
/**
* Snapshot pointer map
*/
2015-05-14 20:43:31 +03:00
map < unsigned int , VectorAttribute * > snapshot_pool ;
} ;
2015-05-19 19:41:23 +03:00
# endif /*SNAPSHOTS_H_*/