2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2014-01-09 14:51:20 +04:00
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
2008-06-17 20:27:32 +04: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. */
/* -------------------------------------------------------------------------- */
# ifndef NEBULA_TEMPLATE_H_
# define NEBULA_TEMPLATE_H_
# include "Template.h"
# include <map>
2013-12-21 04:35:51 +04:00
/**
2011-12-14 02:57:56 +04:00
* This class provides the basic abstraction for OpenNebula configuration files
*/
2008-06-17 20:27:32 +04:00
class NebulaTemplate : public Template
2011-12-14 02:57:56 +04:00
{
2008-06-17 20:27:32 +04:00
public :
2011-12-14 02:57:56 +04:00
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
2011-12-14 02:57:56 +04:00
NebulaTemplate ( const string & etc_location , const char * _conf_name )
{
conf_file = etc_location + _conf_name ;
}
2013-12-21 04:35:51 +04:00
2011-12-14 02:57:56 +04:00
virtual ~ NebulaTemplate ( ) { } ;
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
2008-06-17 20:27:32 +04:00
2009-01-02 17:58:51 +03:00
int get ( const char * name , vector < const Attribute * > & values ) const
2008-06-17 20:27:32 +04:00
{
string _name ( name ) ;
2013-12-21 04:35:51 +04:00
return Template : : get ( _name , values ) ;
2008-06-17 20:27:32 +04:00
} ;
2013-12-21 04:35:51 +04:00
2009-01-02 17:58:51 +03:00
void get ( const char * name , string & values ) const
2008-06-18 19:58:44 +04:00
{
string _name ( name ) ;
2013-12-21 04:35:51 +04:00
Template : : get ( _name , values ) ;
2008-06-18 19:58:44 +04:00
} ;
2013-12-21 04:35:51 +04:00
2009-01-02 17:58:51 +03:00
void get ( const char * name , int & values ) const
2008-06-18 19:58:44 +04:00
{
string _name ( name ) ;
2013-12-21 04:35:51 +04:00
Template : : get ( _name , values ) ;
2008-06-18 19:58:44 +04:00
} ;
2011-12-15 04:19:23 +04:00
void get ( const char * name , unsigned int & values ) const
{
int ival ;
NebulaTemplate : : get ( name , ival ) ;
values = static_cast < unsigned int > ( ival ) ;
} ;
2013-12-21 04:35:51 +04:00
2009-01-02 17:58:51 +03:00
void get ( const char * name , time_t & values ) const
{
2011-12-14 02:57:56 +04:00
const SingleAttribute * sattr ;
vector < const Attribute * > attr ;
2013-12-21 04:35:51 +04:00
2011-12-14 02:57:56 +04:00
string _name ( name ) ;
2013-12-21 04:35:51 +04:00
2009-01-02 17:58:51 +03:00
if ( Template : : get ( _name , attr ) = = 0 )
{
2011-12-14 02:57:56 +04:00
values = 0 ;
return ;
2009-01-02 17:58:51 +03:00
}
2013-12-21 04:35:51 +04:00
2009-01-02 17:58:51 +03:00
sattr = dynamic_cast < const SingleAttribute * > ( attr [ 0 ] ) ;
2013-12-21 04:35:51 +04:00
2009-01-02 17:58:51 +03:00
if ( sattr ! = 0 )
{
2011-12-14 02:57:56 +04:00
istringstream is ;
2013-12-21 04:35:51 +04:00
2011-12-14 02:57:56 +04:00
is . str ( sattr - > value ( ) ) ;
is > > values ;
2009-01-02 17:58:51 +03:00
}
else
2013-12-21 04:35:51 +04:00
values = 0 ;
} ;
2009-01-02 17:58:51 +03:00
2012-07-02 18:46:46 +04:00
void get ( const char * name , float & value ) const
{
string _name ( name ) ;
Template : : get ( _name , value ) ;
} ;
2012-12-20 21:21:30 +04:00
void get ( const char * name , bool & value ) const
{
string _name ( name ) ;
Template : : get ( _name , value ) ;
} ;
2013-12-21 04:35:51 +04:00
void get ( const char * name , long long & value ) const
{
string _name ( name ) ;
Template : : get ( _name , value ) ;
}
2011-12-14 02:57:56 +04:00
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
/**
* Parse and loads the configuration in the template
*/
int load_configuration ( ) ;
protected :
/**
2013-12-21 04:35:51 +04:00
* Full path to the configuration file
2011-12-14 02:57:56 +04:00
*/
2008-06-17 20:27:32 +04:00
string conf_file ;
2013-12-21 04:35:51 +04:00
2011-12-14 02:57:56 +04:00
/**
* Defaults for the configuration file
*/
2008-06-17 20:27:32 +04:00
map < string , Attribute * > conf_default ;
2011-12-14 02:57:56 +04:00
/**
* Sets the defaults value for the template
*/
virtual void set_conf_default ( ) = 0 ;
} ;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class OpenNebulaTemplate : public NebulaTemplate
2013-12-21 04:35:51 +04:00
{
2011-12-14 02:57:56 +04:00
public :
OpenNebulaTemplate ( const string & etc_location , const string & _var_location ) :
NebulaTemplate ( etc_location , conf_name ) , var_location ( _var_location )
{ } ;
2013-12-21 04:35:51 +04:00
2011-12-14 02:57:56 +04:00
~ OpenNebulaTemplate ( ) { } ;
private :
/**
* Name for the configuration file , oned . conf
*/
static const char * conf_name ;
/**
* Path for the var directory , for defaults
*/
string var_location ;
2013-12-21 04:35:51 +04:00
2011-12-14 02:57:56 +04:00
/**
* Sets the defaults value for the template
*/
void set_conf_default ( ) ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*NEBULA_TEMPLATE_H_*/