1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-11 04:58:16 +03:00

bug #4355: CURRENT_VMS can be now of any type. Better handling of

ObjectXML::search templates
This commit is contained in:
Ruben S. Montero 2016-03-03 15:39:41 +01:00
parent 66bc3ec399
commit 1add1cfb52
4 changed files with 117 additions and 164 deletions

View File

@ -172,19 +172,20 @@ public:
*
* @return -1 if the element was not found
*/
virtual int search(const char *name, std::string& value);
virtual int search(const char *name, std::string& value)
{
return __search(name, value);
}
/**
* Search the Object for a given attribute in a set of object specific
* routes. integer version
*/
virtual int search(const char *name, int& value);
virtual int search(const char *name, int& value)
{
return __search(name, value);
}
/**
* Search the Object for a given attribute in a set of object specific
* routes. float version
*/
virtual int search(const char *name, float& value);
virtual int search(const char *name, float& value)
{
return __search(name, value);
}
/**
* Get xml nodes by Xpath
@ -327,9 +328,57 @@ private:
* @param name of the attribute
* @results vector of attributes that matches the query
*/
void search(const char* name, std::vector<std::string>& results);
template<typename T>
void __search(const char* name, std::vector<T>& results)
{
if (name[0] == '/')
{
xpaths(results, name);
}
else if (num_paths == 0)
{
results.clear();
}
else
{
std::ostringstream xpath;
xpath << paths[0] << name;
for (int i = 1; i < num_paths ; i++)
{
xpath << '|' << paths[i] << name;
}
xpaths(results, xpath.str().c_str());
}
}
/**
* Search the Object for a given attribute in a set of object specific
* routes.
* @param name of the attribute
* @param value of the attribute
*
* @return -1 if the element was not found
*/
template<typename T>
int __search(const char *name, T& value)
{
std::vector<T> results;
__search(name, results);
if (results.size() != 0)
{
value = results[0];
return 0;
}
return -1;
};
};
#endif /*OBJECT_XML_H_*/

View File

@ -122,12 +122,26 @@ public:
/**
* Search the Object for a given attribute in a set of object specific
* routes. Overwrite ObjectXML function to deal with pseudo-attributes
* - CURRENT_VMS. value is the VM ID to search in the set of VMS
* running VMs in the host. If the VM_ID is found value is not modified
* otherwise is set to -1
* routes.
* @param name of the attribute
* @param value of the attribute
*
* @return -1 if the element was not found
*/
int search(const char *name, int& value);
virtual int search(const char *name, std::string& value)
{
return __search(name, value);
}
virtual int search(const char *name, int& value)
{
return __search(name, value);
}
virtual int search(const char *name, float& value)
{
return __search(name, value);
}
/**
* Checks if the host is a remote public cloud
@ -171,6 +185,43 @@ private:
static int host_num_paths; /**< number of paths*/
void init_attributes();
/**
* Search the Object for a given attribute in a set of object specific
* routes. Overrite ObjectXML function to deal with pseudo-attributes
* - CURRENT_VMS. value is the VM ID to search in the set of VMS
* running VMs in the host. If the VM_ID is found value is not modified
* otherwise is set to -1
*/
template<typename T>
int __search(const char *name, T& value)
{
string s_name(name);
if (s_name == "CURRENT_VMS")
{
typename std::vector<T>::iterator it;
std::vector<T> results;
xpaths(results, "/HOST/VMS/ID");
for (it=results.begin(); it!=results.end(); it++)
{
if (*it == value)
{
return 0; //VMID found in VMS value is VMID
}
}
value = -1; //VMID not found in VMS value is -1
return 0;
}
else
{
return ObjectXML::search(name, value);
}
};
};
#endif /* HOST_XML_H_ */

View File

@ -92,46 +92,6 @@ void HostXML::init_attributes()
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HostXML::search(const char *name, int& value)
{
string s_name(name);
if (s_name == "CURRENT_VMS")
{
vector<string>::iterator it;
istringstream iss;
int id;
vector<string> results;
xpaths(results, "/HOST/VMS/ID");
for (it=results.begin(); it!=results.end(); it++)
{
iss.clear();
iss.str(*it);
iss >> id;
if (!iss.fail() && id == value)
{
return 0; //VMID found in VMS value is VMID
}
}
value = -1; //VMID not found in VMS value is -1
return 0;
}
else
{
return ObjectXML::search(name, value);
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
bool HostXML::test_capacity(long long cpu, long long mem,
vector<VectorAttribute *>& p, string & error)
{

View File

@ -543,110 +543,3 @@ error_yy:
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
void ObjectXML::search(const char* name, vector<string>& results)
{
if (name[0] == '/')
{
xpaths(results, name);
}
else if (num_paths == 0)
{
results.clear();
}
else
{
ostringstream xpath;
xpath << paths[0] << name;
for (int i = 1; i < num_paths ; i++)
{
xpath << '|' << paths[i] << name;
}
xpaths(results, xpath.str().c_str());
}
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int ObjectXML::search(const char *name, string& value)
{
vector<string> results;
value = "";
search(name, results);
if (results.size() != 0)
{
value = results[0];
return 0;
}
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int ObjectXML::search(const char *name, int& value)
{
vector<string> results;
value = 0;
search(name, results);
if (results.size() != 0)
{
istringstream iss(results[0]);
iss >> value;
if (iss.fail())
{
value = 0;
return -1;
}
return 0;
}
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int ObjectXML::search(const char *name, float& value)
{
vector<string> results;
value = 0.0;
search(name, results);
if (results.size() != 0)
{
istringstream iss(results[0]);
iss >> value;
if (iss.fail())
{
value = 0;
return -1;
}
return 0;
}
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */