1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

F #4809: Add a base class for extended template attributes

This commit is contained in:
Ruben S. Montero 2017-04-10 18:41:43 +02:00
parent ba69ddcdb3
commit e1cb2c928a
7 changed files with 394 additions and 278 deletions

257
include/ExtendedAttribute.h Normal file
View File

@ -0,0 +1,257 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */
/* */
/* 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 EXTENDED_ATTRIBUTE_H_
#define EXTENDED_ATTRIBUTE_H_
#include <vector>
#include <map>
#include "Attribute.h"
#include "Template.h"
/**
* This class represents a generic attribute, it exposes the basic
* VectorAttribute interface and can be decorated with functionality
* for an specific class.
*
* The attribute operates directly on the OriginalTemplate attribute. IT
* IS NOT CLONED OR COPIED
*/
class ExtendedAttribute: public Attribute
{
public:
VectorAttribute * vector_attribute()
{
return va;
}
/* ---------------------------------------------------------------------- */
/* VectorAttribute Interface */
/* ---------------------------------------------------------------------- */
template<typename T>
int vector_value(const std::string& name, T& value) const
{
return va->vector_value(name, value);
}
string vector_value(const std::string& name) const
{
return va->vector_value(name);
}
template<typename T>
void replace(const std::string& name, T value)
{
va->replace(name, value);
}
void remove(const std::string& name)
{
va->remove(name);
}
void merge(VectorAttribute* vattr, bool replace)
{
va->merge(vattr, replace);
}
protected:
/**
* Creates the attribute with a reference to a VectorAttribute. The object
* is shared and WILL BE modified through this interface.
* @param va pointer to the VectorAttribute.
*/
ExtendedAttribute(VectorAttribute *_va):
Attribute(_va->name()) ,va(_va), id(-1) {};
ExtendedAttribute(VectorAttribute *_va, int _id):
Attribute(_va->name()) ,va(_va), id(_id) {};
virtual ~ExtendedAttribute(){};
/* ---------------------------------------------------------------------- */
/* Attribute Interface */
/* ---------------------------------------------------------------------- */
string * marshall(const char * _sep = 0) const
{
return va->marshall(_sep);
};
string * to_xml() const
{
return va->to_xml();
};
void unmarshall(const std::string& sattr, const char * _sep = 0)
{
va->unmarshall(sattr, _sep);
}
AttributeType type()
{
return va->type();
};
Attribute* clone() const
{
return va->clone();
};
/* ---------------------------------------------------------------------- */
/* ExtendedAttribute Interface */
/* ---------------------------------------------------------------------- */
int get_id() const
{
return id;
}
private:
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
friend class ExtendedAttributeSet;
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* The associated VectorAttribute
*/
VectorAttribute * va;
/**
* Set if the attribute can be addressed by an identifier, -1 otherwise
*/
int id;
};
/**
* This class represents a set of ExtendedAttributes it provides fast
* access to individual elements (by ID) and implement collective operations
*/
class ExtendedAttributeSet
{
protected:
/**
* Creates the ExtenededAttribute set
* @param dispose elements upon set destruction
*/
ExtendedAttributeSet(bool _dispose):dispose(_dispose){};
virtual ~ExtendedAttributeSet();
/* ---------------------------------------------------------------------- */
/* Method to access attributes */
/* ---------------------------------------------------------------------- */
/**
* @return attribute by id or 0 if not found
*/
ExtendedAttribute * get_attribute(int id) const;
/* ---------------------------------------------------------------------- */
/* Iterators */
/* ---------------------------------------------------------------------- */
/**
* Generic iterator for the set. Wraps the STL iterator for map, can be
* used to iterate over the attributes
*/
class AttributeIterator
{
public:
AttributeIterator& operator=(const AttributeIterator& rhs)
{
map_it = rhs.map_it;
return *this;
}
AttributeIterator& operator++()
{
++map_it;
return *this;
}
bool operator!=(const AttributeIterator& rhs)
{
return map_it != rhs.map_it;
}
AttributeIterator(){};
AttributeIterator(const AttributeIterator& ait):map_it(ait.map_it){};
AttributeIterator(const std::map<int,
ExtendedAttribute *>::iterator& _map_it):map_it(_map_it){};
virtual ~AttributeIterator(){};
protected:
std::map<int, ExtendedAttribute *>::iterator map_it;
};
AttributeIterator begin()
{
AttributeIterator it(a_set.begin());
return it;
}
AttributeIterator end()
{
AttributeIterator it(a_set.end());
return it;
}
typedef class AttributeIterator attribute_iterator;
/* ---------------------------------------------------------------------- */
/* Attribute map interface */
/* ---------------------------------------------------------------------- */
/**
* Adds a new VirtualMachine attribute to the set
*/
void add_attribute(ExtendedAttribute * a, int id)
{
a_set.insert(make_pair(id, a));
}
/**
* Init the attribute set from a vector
* @param id_name with the ID of the attribute
* @param auto_ids automatically generate ids for the attributes
* @param vas vector of attribute to use
*/
void init_attribute_map(const std::string& id_name,
std::vector<VectorAttribute *>& vas);
/**
* Abstract method to create the VirtualMachineAttributes for this set
*/
virtual ExtendedAttribute * attribute_factory(VectorAttribute * va,
int id) const = 0;
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* Map with the disk attributes
*/
std::map<int, ExtendedAttribute *> a_set;
/**
* Frees the VectorAttribute associated with each VirtualMachineAttribute
* upon object destruction
*/
bool dispose;
};
#endif /*VIRTUAL_MACHINE_ATTRIBUTE_H_*/

View File

@ -17,11 +17,7 @@
#ifndef VIRTUAL_MACHINE_ATTRIBUTE_H_
#define VIRTUAL_MACHINE_ATTRIBUTE_H_
#include <vector>
#include <map>
#include "Attribute.h"
#include "Template.h"
#include "ExtendedAttribute.h"
/**
* This class represents a generic VirtualMachine attribute, it exposes the
@ -31,49 +27,8 @@
* The attribute operates directly on the VirtualMachineTemplate attribute. IT
* IS NOT CLONED OR COPIED
*/
class VirtualMachineAttribute: public Attribute
class VirtualMachineAttribute: public ExtendedAttribute
{
public:
/**
* Return the associated VectorAttribute to interface with vector attribute
* functions
*/
VectorAttribute * vector_attribute()
{
return va;
}
/* ---------------------------------------------------------------------- */
/* VectorAttribute Interface */
/* ---------------------------------------------------------------------- */
template<typename T>
int vector_value(const std::string& name, T& value) const
{
return va->vector_value(name, value);
}
string vector_value(const std::string& name) const
{
return va->vector_value(name);
}
template<typename T>
void replace(const std::string& name, T value)
{
va->replace(name, value);
}
void remove(const std::string& name)
{
va->remove(name);
}
void merge(VectorAttribute* vattr, bool replace)
{
va->merge(vattr, replace);
}
protected:
/**
* Creates the attribute with a reference to a VectorAttribute. The object
@ -81,41 +36,13 @@ protected:
* @param va pointer to the VectorAttribute.
*/
VirtualMachineAttribute(VectorAttribute *_va):
Attribute(_va->name()) ,va(_va), id(-1) {};
ExtendedAttribute(_va){};
VirtualMachineAttribute(VectorAttribute *_va, int _id):
Attribute(_va->name()) ,va(_va), id(_id) {};
ExtendedAttribute(_va, _id) {};
virtual ~VirtualMachineAttribute(){};
/* ---------------------------------------------------------------------- */
/* Attribute Interface */
/* ---------------------------------------------------------------------- */
string * marshall(const char * _sep = 0) const
{
return va->marshall(_sep);
};
string * to_xml() const
{
return va->to_xml();
};
void unmarshall(const std::string& sattr, const char * _sep = 0)
{
va->unmarshall(sattr, _sep);
}
AttributeType type()
{
return va->type();
};
Attribute* clone() const
{
return va->clone();
};
/* ---------------------------------------------------------------------- */
/* VirtualMachineAttribute Interface */
/* ---------------------------------------------------------------------- */
@ -139,49 +66,29 @@ protected:
{
bool value;
va->vector_value(flag, value);
vector_value(flag, value);
return value;
}
int get_id() const
{
return id;
}
private:
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
friend class VirtualMachineAttributeSet;
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* The associated VectorAttribute
*/
VectorAttribute * va;
/**
* Set if the attribute can be addressed by an identifier, -1 otherwise
*/
int id;
};
/**
* This class represents a set of VirtualMachineAttributes it provides fast
* access to individual elements (by ID) and implement collective operations
*/
class VirtualMachineAttributeSet
class VirtualMachineAttributeSet : public ExtendedAttributeSet
{
protected:
/**
* Creates the VirtualMachineAttribute set
* @param dispose elements upon set destruction
*/
VirtualMachineAttributeSet(bool _dispose):dispose(_dispose){};
VirtualMachineAttributeSet(bool _dispose):ExtendedAttributeSet(_dispose){};
virtual ~VirtualMachineAttributeSet();
virtual ~VirtualMachineAttributeSet(){};
/* ---------------------------------------------------------------------- */
/* Methods to access attributes */
@ -189,7 +96,10 @@ protected:
/**
* @return attribute by id or 0 if not found
*/
VirtualMachineAttribute * get_attribute(int id) const;
VirtualMachineAttribute * get_attribute(int id) const
{
return static_cast<VirtualMachineAttribute *>(get_attribute(id));
}
/**
* @return attribute with the given flag set or 0 if not found
@ -216,96 +126,14 @@ protected:
*/
VirtualMachineAttribute * clear_flag(const string& flag_name);
/* ---------------------------------------------------------------------- */
/* Iterators */
/* ---------------------------------------------------------------------- */
/**
* Generic iterator for the set. Wraps the STL iterator for map, can be
* used to iterate over the attributes
*/
class AttributeIterator
{
public:
AttributeIterator& operator=(const AttributeIterator& rhs)
{
map_it = rhs.map_it;
return *this;
}
AttributeIterator& operator++()
{
++map_it;
return *this;
}
bool operator!=(const AttributeIterator& rhs)
{
return map_it != rhs.map_it;
}
AttributeIterator(){};
AttributeIterator(const AttributeIterator& ait):map_it(ait.map_it){};
AttributeIterator(const std::map<int,
VirtualMachineAttribute *>::iterator& _map_it):map_it(_map_it){};
virtual ~AttributeIterator(){};
protected:
std::map<int, VirtualMachineAttribute *>::iterator map_it;
};
AttributeIterator begin()
{
AttributeIterator it(a_set.begin());
return it;
}
AttributeIterator end()
{
AttributeIterator it(a_set.end());
return it;
}
typedef class AttributeIterator attribute_iterator;
/* ---------------------------------------------------------------------- */
/* Attribute map interface */
/* ---------------------------------------------------------------------- */
/**
* Adds a new VirtualMachine attribute to the set
*/
void add_attribute(VirtualMachineAttribute * a, int id)
{
a_set.insert(make_pair(id, a));
}
/**
* Init the attribute set from a vector
* @param id_name with the ID of the attribute
* @param auto_ids automatically generate ids for the attributes
* @param vas vector of attribute to use
*/
void init_attribute_map(const std::string& id_name,
std::vector<VectorAttribute *>& vas);
/**
* Abstract method to create the VirtualMachineAttributes for this set
*/
virtual VirtualMachineAttribute * attribute_factory(VectorAttribute * va,
virtual ExtendedAttribute * attribute_factory(VectorAttribute * va,
int id) const = 0;
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* Map with the disk attributes
*/
std::map<int, VirtualMachineAttribute *> a_set;
/**
* Frees the VectorAttribute associated with each VirtualMachineAttribute
* upon object destruction
*/
bool dispose;
};
#endif /*VIRTUAL_MACHINE_ATTRIBUTE_H_*/

View File

@ -379,13 +379,13 @@ public:
DiskIterator begin()
{
DiskIterator it(VirtualMachineAttributeSet::begin());
DiskIterator it(ExtendedAttributeSet::begin());
return it;
}
DiskIterator end()
{
DiskIterator it(VirtualMachineAttributeSet::end());
DiskIterator it(ExtendedAttributeSet::end());
return it;
}

View File

@ -200,13 +200,13 @@ public:
NicIterator begin()
{
NicIterator it(VirtualMachineAttributeSet::begin());
NicIterator it(ExtendedAttributeSet::begin());
return it;
}
NicIterator end()
{
NicIterator it(VirtualMachineAttributeSet::end());
NicIterator it(ExtendedAttributeSet::end());
return it;
}

View File

@ -0,0 +1,77 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "ExtendedAttribute.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ExtendedAttributeSet::~ExtendedAttributeSet()
{
std::map<int, ExtendedAttribute *>::iterator it;
for (it = a_set.begin(); it != a_set.end(); ++it)
{
if ( dispose )
{
delete it->second->va;
}
delete it->second;
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ExtendedAttribute * ExtendedAttributeSet::get_attribute(int id) const
{
std::map<int, ExtendedAttribute*>::const_iterator it = a_set.find(id);
if ( it == a_set.end() )
{
return 0;
}
return it->second;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ExtendedAttributeSet::init_attribute_map(const std::string& id_name,
std::vector<VectorAttribute *>& vas)
{
std::vector<VectorAttribute *>::iterator it;
int id, auto_id;
for (it = vas.begin(), auto_id = 0; it != vas.end(); ++it, ++auto_id)
{
if (id_name.empty())
{
id = auto_id;
}
else if ( (*it)->vector_value(id_name, id) != 0 )
{
continue;
}
ExtendedAttribute * a = attribute_factory(*it, id);
a_set.insert(make_pair(id, a));
}
};

View File

@ -24,6 +24,7 @@ lib_name='nebula_common'
source_files=[
'ActionManager.cc',
'Attribute.cc',
'ExtendedAttribute.cc',
'mem_collector.c',
'NebulaUtil.cc'
]

View File

@ -19,49 +19,20 @@
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualMachineAttributeSet::~VirtualMachineAttributeSet()
{
std::map<int, VirtualMachineAttribute *>::iterator it;
for (it = a_set.begin(); it != a_set.end(); ++it)
{
if ( dispose )
{
delete it->second->va;
}
delete it->second;
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualMachineAttribute * VirtualMachineAttributeSet::get_attribute(int id) const
{
std::map<int, VirtualMachineAttribute*>::const_iterator it = a_set.find(id);
if ( it == a_set.end() )
{
return 0;
}
return it->second;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualMachineAttribute * VirtualMachineAttributeSet::get_attribute(
const string& flag) const
{
std::map<int, VirtualMachineAttribute*>::const_iterator it;
std::map<int, ExtendedAttribute*>::const_iterator it;
VirtualMachineAttribute * vma;
for( it = a_set.begin(); it != a_set.end(); ++it)
{
if ( it->second->is_flag(flag) == true )
vma = static_cast<VirtualMachineAttribute *>(it->second);
if ( vma->is_flag(flag) == true )
{
return it->second;
return vma;
}
}
@ -71,53 +42,6 @@ VirtualMachineAttribute * VirtualMachineAttributeSet::get_attribute(
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualMachineAttribute * VirtualMachineAttributeSet::remove_attribute(
const string& flag)
{
std::map<int, VirtualMachineAttribute*>::const_iterator it;
VirtualMachineAttribute * tmp = 0;
for( it = a_set.begin(); it != a_set.end(); ++it)
{
if ( it->second->is_flag(flag) == true )
{
tmp = it->second;
a_set.erase(it);
}
}
return tmp;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachineAttributeSet::init_attribute_map(const std::string& id_name,
std::vector<VectorAttribute *>& vas)
{
std::vector<VectorAttribute *>::iterator it;
int id, auto_id;
for (it = vas.begin(), auto_id = 0; it != vas.end(); ++it, ++auto_id)
{
if (id_name.empty())
{
id = auto_id;
}
else if ( (*it)->vector_value(id_name, id) != 0 )
{
continue;
}
VirtualMachineAttribute * a = attribute_factory(*it, id);
a_set.insert(make_pair(id, a));
}
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachineAttributeSet::set_flag(int a_id, const string& flag_name)
{
VirtualMachineAttribute * va = get_attribute(a_id);
@ -135,18 +59,47 @@ int VirtualMachineAttributeSet::set_flag(int a_id, const string& flag_name)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualMachineAttribute * VirtualMachineAttributeSet::clear_flag(
VirtualMachineAttribute * VirtualMachineAttributeSet::remove_attribute(
const string& flag)
{
std::map<int, VirtualMachineAttribute*>::iterator it;
std::map<int, ExtendedAttribute*>::const_iterator it;
VirtualMachineAttribute * vma;
VirtualMachineAttribute * tmp = 0;
for( it = a_set.begin(); it != a_set.end(); ++it)
{
if ( it->second->is_flag(flag) == true )
{
it->second->clear_flag(flag);
vma = static_cast<VirtualMachineAttribute *>(it->second);
return it->second;
if ( vma->is_flag(flag) == true )
{
tmp = vma;
a_set.erase(it);
}
}
return tmp;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualMachineAttribute * VirtualMachineAttributeSet::clear_flag(
const string& flag)
{
std::map<int, ExtendedAttribute *>::iterator it;
VirtualMachineAttribute * vma;
for( it = a_set.begin(); it != a_set.end(); ++it)
{
vma = static_cast<VirtualMachineAttribute *>(it->second);
if ( vma->is_flag(flag) == true )
{
vma->clear_flag(flag);
return vma;
}
}