2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2021-02-09 18:07:56 +03:00
/* Copyright 2002-2021, OpenNebula Project, OpenNebula Systems */
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 ATTRIBUTE_H_
# define ATTRIBUTE_H_
# include <string>
# include <map>
# include <sstream>
2009-03-06 15:10:15 +03:00
# include <algorithm>
2008-06-17 20:27:32 +04:00
2016-03-03 14:32:36 +03:00
# include "NebulaUtil.h"
2008-06-17 20:27:32 +04:00
/**
* Attribute base class for name - value pairs . This class provides a generic
2009-03-06 15:10:15 +03:00
* interface to implement
2008-06-17 20:27:32 +04:00
*/
class Attribute
{
public :
2020-07-02 23:42:10 +03:00
Attribute ( const std : : string & aname ) : attribute_name ( aname )
2008-06-17 20:27:32 +04:00
{
transform (
attribute_name . begin ( ) ,
attribute_name . end ( ) ,
attribute_name . begin ( ) ,
( int ( * ) ( int ) ) toupper ) ;
2011-05-08 04:13:37 +04:00
// FIX Attribute name if it does not conform XML element
// naming conventions
int size = attribute_name . size ( ) ;
2017-06-15 18:06:43 +03:00
if ( ( size > 0 & & ! ( isalpha ( aname [ 0 ] ) | | aname [ 0 ] = = ' _ ' ) ) | |
2011-05-08 04:13:37 +04:00
( size > = 3 & & ( aname [ 0 ] = = ' X ' & & aname [ 1 ] = = ' M ' & & aname [ 2 ] = = ' L ' ) ) )
{
2012-12-24 05:41:17 +04:00
attribute_name . insert ( 0 , " ONE_ " ) ;
2011-05-08 04:13:37 +04:00
}
2008-06-17 20:27:32 +04:00
} ;
virtual ~ Attribute ( ) { } ;
enum AttributeType
{
SIMPLE = 0 ,
VECTOR = 1
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Gets the name of the attribute .
* @ return the attribute name
*/
2020-07-02 23:42:10 +03:00
const std : : string & name ( ) const
2008-06-17 20:27:32 +04:00
{
return attribute_name ;
} ;
/**
2020-09-15 12:16:00 +03:00
* Marshall the attribute in a single string .
* @ return a string holding the attribute value .
2008-06-17 20:27:32 +04:00
*/
2020-09-15 12:16:00 +03:00
virtual std : : string marshall ( const char * _sep = 0 ) const = 0 ;
2009-03-06 15:10:15 +03:00
2009-01-26 21:25:15 +03:00
/**
* Write the attribute using a simple XML format . The string MUST be freed
* by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
*/
2019-01-30 02:10:18 +03:00
virtual void to_xml ( std : : ostringstream & s ) const = 0 ;
virtual void to_json ( std : : ostringstream & s ) const = 0 ;
virtual void to_token ( std : : ostringstream & s ) const = 0 ;
2008-06-17 20:27:32 +04:00
/**
* Builds a new attribute from a string .
*/
2020-07-02 23:42:10 +03:00
virtual void unmarshall ( const std : : string & sattr , const char * _sep = 0 ) = 0 ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute type
*/
virtual AttributeType type ( ) = 0 ;
2011-04-11 01:55:49 +04:00
/**
* Clones the current attribute
*/
virtual Attribute * clone ( ) const = 0 ;
2008-06-17 20:27:32 +04:00
2019-09-16 16:51:38 +03:00
/**
* Encrypt all secret attributes
*/
2020-07-02 23:42:10 +03:00
virtual void encrypt ( const std : : string & one_key , const std : : set < std : : string > & eas ) { } ;
2019-09-16 16:51:38 +03:00
/**
* Decrypt all secret attributes
*/
2020-07-02 23:42:10 +03:00
virtual void decrypt ( const std : : string & one_key , const std : : set < std : : string > & eas ) { } ;
2019-09-16 16:51:38 +03:00
2011-04-11 01:55:49 +04:00
protected :
2019-09-16 16:51:38 +03:00
2008-06-17 20:27:32 +04:00
/**
* The attribute name .
*/
2020-07-02 23:42:10 +03:00
std : : string attribute_name ;
2008-06-17 20:27:32 +04:00
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
2009-03-06 15:10:15 +03:00
* The SingleAttribute class represents a simple attribute in the form
* NAME = VALUE .
2008-06-17 20:27:32 +04:00
*/
class SingleAttribute : public Attribute
{
public :
2020-07-02 23:42:10 +03:00
SingleAttribute ( const std : : string & name ) : Attribute ( name ) { } ;
2008-06-17 20:27:32 +04:00
2020-07-02 23:42:10 +03:00
SingleAttribute ( const std : : string & name , const std : : string & value ) :
2008-06-17 20:27:32 +04:00
Attribute ( name ) , attribute_value ( value ) { } ;
2009-03-06 15:10:15 +03:00
2011-04-11 01:55:49 +04:00
SingleAttribute ( const SingleAttribute & sa ) : Attribute ( sa . attribute_name )
{
attribute_value = sa . attribute_value ;
} ;
2008-06-17 20:27:32 +04:00
~ SingleAttribute ( ) { } ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute value , a string .
*/
2020-07-02 23:42:10 +03:00
const std : : string & value ( ) const
2008-06-17 20:27:32 +04:00
{
return attribute_value ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2020-09-15 12:16:00 +03:00
* Marshall the attribute in a single string .
* @ return a string holding the attribute value .
2009-03-06 15:10:15 +03:00
*/
2020-09-15 12:16:00 +03:00
std : : string marshall ( const char * _sep = 0 ) const override
2008-06-17 20:27:32 +04:00
{
2020-09-15 12:16:00 +03:00
return attribute_value ;
2008-06-17 20:27:32 +04:00
} ;
2009-03-06 15:10:15 +03:00
2009-01-26 21:25:15 +03:00
/**
* Write the attribute using a simple XML format :
2009-03-06 15:10:15 +03:00
*
* < attribute_name > attribute_value < / attribute_name >
*
2019-01-30 02:10:18 +03:00
* @ paran s the stream to write the attribute .
2009-01-26 21:25:15 +03:00
*/
2020-03-04 18:05:57 +03:00
void to_xml ( std : : ostringstream & s ) const override
2009-01-26 21:25:15 +03:00
{
2019-01-30 02:10:18 +03:00
s < < " < " < < attribute_name < < " > " < < one_util : : escape_xml ( attribute_value )
< < " </ " < < attribute_name < < " > " ;
2009-03-06 15:10:15 +03:00
2019-01-30 02:10:18 +03:00
}
2009-03-06 15:10:15 +03:00
2020-03-04 18:05:57 +03:00
void to_json ( std : : ostringstream & s ) const override
2019-01-30 02:10:18 +03:00
{
2019-09-16 16:51:38 +03:00
one_util : : escape_json ( attribute_value , s ) ;
2019-01-30 02:10:18 +03:00
}
2020-03-04 18:05:57 +03:00
void to_token ( std : : ostringstream & s ) const override
2019-01-30 02:10:18 +03:00
{
if ( attribute_name . empty ( ) | | attribute_value . empty ( ) )
{
return ;
}
one_util : : escape_token ( attribute_name , s ) ;
s < < " = " ;
one_util : : escape_token ( attribute_value , s ) ;
s < < std : : endl ;
2009-01-26 21:25:15 +03:00
}
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Builds a new attribute from a string .
2009-03-06 15:10:15 +03:00
*/
2020-07-02 23:42:10 +03:00
void unmarshall ( const std : : string & sattr , const char * _sep = 0 ) override
2008-06-17 20:27:32 +04:00
{
attribute_value = sattr ;
} ;
2008-11-13 19:21:17 +03:00
/**
* Replaces the attribute value from a string .
*/
2020-07-02 23:42:10 +03:00
void replace ( const std : : string & sattr )
2008-11-13 19:21:17 +03:00
{
attribute_value = sattr ;
} ;
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute type
2009-03-06 15:10:15 +03:00
*/
2020-03-04 18:05:57 +03:00
AttributeType type ( ) override
2008-06-17 20:27:32 +04:00
{
return SIMPLE ;
} ;
2009-03-06 15:10:15 +03:00
2011-04-11 01:55:49 +04:00
/**
* Clones the current attribute
*/
2020-03-04 18:05:57 +03:00
Attribute * clone ( ) const override
2011-04-11 01:55:49 +04:00
{
2012-12-24 05:41:17 +04:00
return new SingleAttribute ( * this ) ;
2011-04-11 01:55:49 +04:00
} ;
2019-09-16 16:51:38 +03:00
/**
* Encrypt all secret attributes
*/
2020-07-02 23:42:10 +03:00
void encrypt ( const std : : string & one_key ,
const std : : set < std : : string > & eas ) override ;
2019-09-16 16:51:38 +03:00
/**
* Decrypt all secret attributes
*/
2020-07-02 23:42:10 +03:00
void decrypt ( const std : : string & one_key ,
const std : : set < std : : string > & eas ) override ;
2019-09-16 16:51:38 +03:00
2008-06-17 20:27:32 +04:00
private :
2020-07-02 23:42:10 +03:00
std : : string attribute_value ;
2008-06-17 20:27:32 +04:00
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
2009-03-06 15:10:15 +03:00
* The VectorAttribute class represents an array attribute in the form
* NAME = [ VAL_NAME_1 = VAL_VALUE_1 , . . . , VAL_NAME_N = VAL_VALUE_N ] .
2008-06-17 20:27:32 +04:00
*/
class VectorAttribute : public Attribute
{
public :
2020-07-02 23:42:10 +03:00
VectorAttribute ( const std : : string & name ) : Attribute ( name ) { } ;
2008-06-17 20:27:32 +04:00
2020-07-02 23:42:10 +03:00
VectorAttribute ( const std : : string & name ,
const std : : map < std : : string , std : : string > & value ) :
2008-06-17 20:27:32 +04:00
Attribute ( name ) , attribute_value ( value ) { } ;
2011-04-11 01:55:49 +04:00
VectorAttribute ( const VectorAttribute & va ) : Attribute ( va . attribute_name )
{
attribute_value = va . attribute_value ;
} ;
2016-03-01 14:09:15 +03:00
VectorAttribute ( const VectorAttribute * va ) : Attribute ( va - > attribute_name )
{
attribute_value = va - > attribute_value ;
} ;
2008-06-17 20:27:32 +04:00
~ VectorAttribute ( ) { } ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute value , a string .
*/
2020-07-02 23:42:10 +03:00
const std : : map < std : : string , std : : string > & value ( ) const
2008-06-17 20:27:32 +04:00
{
return attribute_value ;
} ;
2009-03-06 15:10:15 +03:00
2008-06-17 20:27:32 +04:00
/**
2012-06-02 04:58:46 +04:00
* Returns the string value
* @ param name of the attribute
2008-06-17 20:27:32 +04:00
*
2012-06-02 04:58:46 +04:00
* @ return the value of the attribute if found , empty otherwise
2008-06-17 20:27:32 +04:00
*/
2020-07-02 23:42:10 +03:00
std : : string vector_value ( const std : : string & name ) const ;
2009-03-06 15:10:15 +03:00
2015-02-18 14:16:16 +03:00
/**
2016-02-04 15:10:42 +03:00
* Returns the value of the given element of the VectorAttribute
2012-12-24 05:41:17 +04:00
*
2016-02-04 15:10:42 +03:00
* @ param name of the attribute
* @ param value , not set if the element is not found of has invalid type
2012-03-06 21:44:22 +04:00
*
* @ return 0 on success , - 1 otherwise
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
int vector_value ( const std : : string & name , T & value ) const
2016-02-04 15:10:42 +03:00
{
2020-07-02 23:42:10 +03:00
auto it = attribute_value . find ( name ) ;
2014-05-02 16:11:36 +04:00
2016-02-04 15:10:42 +03:00
if ( it = = attribute_value . end ( ) )
{
return - 1 ;
}
2013-10-17 14:35:19 +04:00
2016-02-04 15:10:42 +03:00
if ( it - > second . empty ( ) )
{
return - 1 ;
}
2020-07-02 23:42:10 +03:00
std : : istringstream iss ( it - > second ) ;
2016-02-04 15:10:42 +03:00
iss > > value ;
if ( iss . fail ( ) | | ! iss . eof ( ) )
{
return - 1 ;
}
return 0 ;
}
2020-03-04 18:05:57 +03:00
/**
* Returns the value of the given element of the VectorAttribute .
* If element is invalid , returns default value
*
* @ param name of the attribute
* @ param value always set , if element is invalid set default_value
* @ param default_value used if element is invalid
*/
template < typename T >
2020-07-02 23:42:10 +03:00
void vector_value ( const std : : string & name ,
T & value ,
const T & default_value ) const
2020-03-04 18:05:57 +03:00
{
if ( vector_value ( name , value ) ! = 0 )
{
value = default_value ;
}
}
2020-07-02 23:42:10 +03:00
int vector_value ( const std : : string & name , std : : string & value ) const ;
2016-02-04 15:10:42 +03:00
2020-07-02 23:42:10 +03:00
int vector_value ( const std : : string & name , bool & value ) const ;
2012-07-16 19:15:22 +04:00
2012-05-29 02:36:13 +04:00
/**
2016-04-19 17:22:37 +03:00
* Returns the value of the given element of the VectorAttribute
2012-05-29 02:36:13 +04:00
*
* @ param name Name of the attribute
2012-07-16 19:15:22 +04:00
* @ param value Integer value , if an error occurred the string returned is
2016-02-04 15:10:42 +03:00
* empty and value is not set
2012-05-29 02:36:13 +04:00
*
* @ return the value in string form on success , " " otherwise
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
std : : string vector_value_str ( const std : : string & name , T & value ) const
2016-02-04 15:10:42 +03:00
{
2020-07-02 23:42:10 +03:00
auto it = attribute_value . find ( name ) ;
2016-02-04 15:10:42 +03:00
if ( it = = attribute_value . end ( ) )
{
return " " ;
}
if ( it - > second . empty ( ) )
{
return " " ;
}
2020-07-02 23:42:10 +03:00
std : : istringstream iss ( it - > second ) ;
2016-02-04 15:10:42 +03:00
iss > > value ;
if ( iss . fail ( ) | | ! iss . eof ( ) )
{
return " " ;
}
return it - > second ;
}
2012-07-16 20:28:58 +04:00
2008-06-17 20:27:32 +04:00
/**
* Marshall the attribute in a single string . The string MUST be freed
2009-03-06 15:10:15 +03:00
* by the calling function . The string is in the form :
2008-06-17 20:27:32 +04:00
* " VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N " .
* @ return a string ( allocated in the heap ) holding the attribute value .
2009-03-06 15:10:15 +03:00
*/
2020-09-15 12:16:00 +03:00
std : : string marshall ( const char * _sep = 0 ) const override ;
2009-03-06 15:10:15 +03:00
2009-01-26 21:25:15 +03:00
/**
* Write the attribute using a simple XML format :
2009-03-06 15:10:15 +03:00
*
2009-01-26 21:25:15 +03:00
* < attribute_name >
* < val_name_1 > val_value_1 < / val_name_1 >
* . . .
* < val_name_n > val_value_n < / val_name_n >
2009-03-06 15:10:15 +03:00
* < / attribute_name >
*
2009-01-26 21:25:15 +03:00
* The string MUST be freed by the calling function .
* @ return a string ( allocated in the heap ) holding the attribute value .
*/
2020-03-04 18:05:57 +03:00
void to_xml ( std : : ostringstream & s ) const override ;
2008-06-17 20:27:32 +04:00
2020-03-04 18:05:57 +03:00
void to_json ( std : : ostringstream & s ) const override ;
2019-01-30 02:10:18 +03:00
2020-03-04 18:05:57 +03:00
void to_token ( std : : ostringstream & s ) const override ;
2014-05-08 17:48:16 +04:00
2008-06-17 20:27:32 +04:00
/**
* Builds a new attribute from a string of the form :
* " VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N " .
2009-03-06 15:10:15 +03:00
*/
2020-07-02 23:42:10 +03:00
void unmarshall ( const std : : string & sattr , const char * _sep = 0 ) override ;
2008-11-13 19:21:17 +03:00
/**
* Replace the value of the given attribute with the provided map
*/
2020-07-02 23:42:10 +03:00
void replace ( const std : : map < std : : string , std : : string > & attr ) ;
2008-11-13 19:21:17 +03:00
2014-07-10 18:24:25 +04:00
/**
* The attributes from vattr will be copied to this vector
* @ param attr Vector attribute to merge
* @ param replace True to replace existing values , false to copy values
* only if they don ' t exist in this vector attribute
*/
void merge ( VectorAttribute * vattr , bool replace ) ;
2009-04-04 03:34:33 +04:00
/**
* Replace the value of the given vector attribute
*/
2016-02-04 15:10:42 +03:00
template < typename T >
2020-07-02 23:42:10 +03:00
void replace ( const std : : string & name , T value )
2013-10-17 14:35:19 +04:00
{
2020-07-02 23:42:10 +03:00
std : : ostringstream oss ;
2013-10-17 14:35:19 +04:00
oss < < value ;
replace ( name , oss . str ( ) ) ;
}
2020-07-02 23:42:10 +03:00
void replace ( const std : : string & name , bool value )
2013-01-18 02:14:34 +04:00
{
if ( value = = true )
{
2016-02-04 15:10:42 +03:00
replace ( name , " YES " ) ;
2013-01-18 02:14:34 +04:00
}
else
{
2016-02-04 15:10:42 +03:00
replace ( name , " NO " ) ;
2013-01-18 02:14:34 +04:00
}
}
2020-07-02 23:42:10 +03:00
void replace ( const std : : string & name , const std : : string & value ) ;
2016-02-04 15:10:42 +03:00
2012-06-13 20:42:42 +04:00
/**
2020-10-16 13:33:20 +03:00
* Removes the given attribute from the vector
* @ param name of the attribute
2012-06-13 20:42:42 +04:00
*/
2020-07-02 23:42:10 +03:00
void remove ( const std : : string & name ) ;
2012-06-13 20:42:42 +04:00
2008-06-17 20:27:32 +04:00
/**
* Returns the attribute type
2009-03-06 15:10:15 +03:00
*/
2020-03-04 18:05:57 +03:00
AttributeType type ( ) override
2008-06-17 20:27:32 +04:00
{
return VECTOR ;
} ;
2011-04-11 01:55:49 +04:00
/**
* Clones the current attribute
*/
2020-03-04 18:05:57 +03:00
VectorAttribute * clone ( ) const override
2011-04-11 01:55:49 +04:00
{
2012-12-24 05:41:17 +04:00
return new VectorAttribute ( * this ) ;
2011-04-11 01:55:49 +04:00
} ;
2014-05-08 17:48:16 +04:00
/**
* Clear the vector attribute values
*/
void clear ( )
{
attribute_value . clear ( ) ;
}
2018-03-26 19:58:04 +03:00
/**
* @ return true if the vector attribute contains no values
*/
2020-09-25 13:08:42 +03:00
bool empty ( ) const
2018-03-26 19:58:04 +03:00
{
return attribute_value . empty ( ) ;
}
2019-09-16 16:51:38 +03:00
/**
* Encrypt all secret attributes
*/
2020-07-02 23:42:10 +03:00
void encrypt ( const std : : string & one_key ,
const std : : set < std : : string > & eas ) override ;
2019-09-16 16:51:38 +03:00
/**
* Decrypt all secret attributes
*/
2020-07-02 23:42:10 +03:00
void decrypt ( const std : : string & one_key ,
const std : : set < std : : string > & eas ) override ;
2019-09-16 16:51:38 +03:00
2008-06-17 20:27:32 +04:00
private :
2009-03-06 15:10:15 +03:00
2015-07-01 22:15:40 +03:00
static const char * magic_sep ;
2009-03-06 15:10:15 +03:00
2015-07-01 22:15:40 +03:00
static const int magic_sep_size ;
2008-06-17 20:27:32 +04:00
2020-07-02 23:42:10 +03:00
std : : map < std : : string , std : : string > attribute_value ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*ATTRIBUTE_H_*/