feature #212: Rank & Requirements are now for ObjectXML

This commit is contained in:
Carlos Martin and Ruben S. Montero authored and Ruben S. Montero committed 2010-05-14 03:32:42 +02:00
1 parent 9faf52101a
commit bb37340deb
12 files changed
+6456 -1

No files matched your search

+24
View File
@@ -76,6 +76,30 @@ public:
*/
int update(const string &xml_doc);
// ---------------------------------------------------------
// Lex & bison parser for requirements and rank expressions
// ---------------------------------------------------------
/**
* Evaluates a requirement expression on the given host.
* @param requirements string
* @param result true if the host matches the requirements
* @param errmsg string describing the error, must be freed by the
* calling function
* @return 0 on success
*/
int eval_bool(const string& expr, bool& result, char **errmsg);
/**
* Evaluates a rank expression on the given host.
* @param rank string
* @param result of the rank evaluation
* @param errmsg string describing the error, must be freed by the
* calling function
* @return 0 on success
*/
int eval_arith(const string& expr, int& result, char **errmsg);
private:
/**
* XML representation of the Object
+95
View File
@@ -16,6 +16,7 @@
#include <ObjectXML.h>
#include <stdexcept>
#include <cstring>
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@@ -212,3 +213,97 @@ void ObjectXML::xml_parse(const string &xml_doc)
throw("Unable to create new XPath context");
}
}
/* ************************************************************************ */
/* Host :: Parse functions to compute rank and evaluate requirements */
/* ************************************************************************ */
extern "C"
{
typedef struct yy_buffer_state * YY_BUFFER_STATE;
int expr_bool_parse(ObjectXML * oxml, bool& result, char ** errmsg);
int expr_arith_parse(ObjectXML * oxml, int& result, char ** errmsg);
int expr_lex_destroy();
YY_BUFFER_STATE expr__scan_string(const char * str);
void expr__delete_buffer(YY_BUFFER_STATE);
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int ObjectXML::eval_bool(const string& expr, bool& result, char **errmsg)
{
YY_BUFFER_STATE str_buffer = 0;
const char * str;
int rc;
*errmsg = 0;
str = expr.c_str();
str_buffer = expr__scan_string(str);
if (str_buffer == 0)
{
goto error_yy;
}
rc = expr_bool_parse(this,result,errmsg);
expr__delete_buffer(str_buffer);
expr_lex_destroy();
return rc;
error_yy:
*errmsg=strdup("Error setting scan buffer");
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int ObjectXML::eval_arith(const string& expr, int& result, char **errmsg)
{
YY_BUFFER_STATE str_buffer = 0;
const char * str;
int rc;
*errmsg = 0;
str = expr.c_str();
str_buffer = expr__scan_string(str);
if (str_buffer == 0)
{
goto error_yy;
}
rc = expr_arith_parse(this,result,errmsg);
expr__delete_buffer(str_buffer);
expr_lex_destroy();
return rc;
error_yy:
*errmsg=strdup("Error setting scan buffer");
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
+23 -1
View File
@@ -20,7 +20,29 @@ Import('env')
lib_name='scheduler_xml'
source_files=['ObjectXML.cc','Client.cc']
if env['parsers']=='yes':
# LEX
parser=env.Lex(
source='expr_parser.l'
)
env.NoClean(parser)
# BISON
parser=env.Bison(
source='expr_arith.y'
)
env.NoClean(parser)
parser=env.Bison(
source='expr_bool.y'
)
env.NoClean(parser)
source_files=['ObjectXML.cc',
'Client.cc',
'expr_parser.c',
'expr_bool.cc',
'expr_arith.cc']
# Build library
env.StaticLibrary(lib_name, source_files)
File diff suppressed because it is too large. Load diff
+86
View File
@@ -0,0 +1,86 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
INTEGER = 258,
STRING = 259,
FLOAT = 260
};
#endif
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 1685 of yacc.c */
#line 54 "expr_arith.y"
char * val_str;
int val_int;
float val_float;
/* Line 1685 of yacc.c */
#line 64 "expr_arith.hh"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
typedef struct YYLTYPE
{
int first_line;
int first_column;
int last_line;
int last_column;
} YYLTYPE;
# define yyltype YYLTYPE /* obsolescent; will be withdrawn */
# define YYLTYPE_IS_DECLARED 1
# define YYLTYPE_IS_TRIVIAL 1
#endif
+142
View File
@@ -0,0 +1,142 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* 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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctype.h>
#include <string.h>
#include <fnmatch.h>
#include "expr_arith.h"
#include "ObjectXML.h"
#define YYERROR_VERBOSE
#define expr_arith_lex expr_lex
extern "C"
{
void expr_arith_error(
YYLTYPE * llocp,
ObjectXML * oxml,
int& result,
char ** error_msg,
const char * str);
int expr_arith_lex (YYSTYPE *lvalp, YYLTYPE *llocp);
int expr_arith_parse(ObjectXML *oxml, int& result, char ** errmsg);
}
%}
%parse-param {ObjectXML * oxml}
%parse-param {int& result}
%parse-param {char ** error_msg}
%union {
char * val_str;
int val_int;
float val_float;
};
%defines
%locations
%pure_parser
%name-prefix = "expr_arith_"
%output = "expr_arith.cc"
%left '+' '-'
%left '*' '/'
%token <val_int> INTEGER
%token <val_str> STRING
%token <val_float> FLOAT
%type <val_int> stmt
%type <val_float> expr
%%
stmt: expr { result = static_cast<int>($1);}
| { result = 0; }
;
//TODO Pass Xpath base for search
expr: STRING { float val = 0.0;
ostringstream xpath_t;
ostringstream xpath_s;
vector<string> results;
xpath_t << "/HOST/TEMPLATE/" << $1;
xpath_s << "/HOST/HOST_SHARE/" << $1;
results = (*oxml)[xpath_t.str().c_str()];
if (results.size() == 0)
{
results = (*oxml)[xpath_s.str().c_str()];
}
if (results.size() != 0)
{
istringstream iss(results[0]);
iss >> val;
}
$$ = val;
free($1);
}
| FLOAT { $$ = $1; }
| INTEGER { $$ = static_cast<float>($1); }
| expr '+' expr { $$ = $1 + $3;}
| expr '-' expr { $$ = $1 - $3;}
| expr '*' expr { $$ = $1 * $3;}
| expr '/' expr { $$ = $1 / $3;}
| '-' expr { $$ = - $2;}
| '(' expr ')' { $$ = $2;}
;
%%
extern "C" void expr_arith_error(
YYLTYPE * llocp,
ObjectXML * oxml,
int& result,
char ** error_msg,
const char * str)
{
int length;
length = strlen(str)+ 64;
*error_msg = (char *) malloc(sizeof(char)*length);
if (*error_msg != 0)
{
snprintf(*error_msg,
length,
"%s at line %i, columns %i:%i",
str,
llocp->first_line,
llocp->first_column,
llocp->last_column);
}
}
File diff suppressed because it is too large. Load diff
+86
View File
@@ -0,0 +1,86 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
INTEGER = 258,
STRING = 259,
FLOAT = 260
};
#endif
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 1685 of yacc.c */
#line 60 "expr_bool.y"
char * val_str;
int val_int;
float val_float;
/* Line 1685 of yacc.c */
#line 64 "expr_bool.hh"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
typedef struct YYLTYPE
{
int first_line;
int first_column;
int last_line;
int last_column;
} YYLTYPE;
# define yyltype YYLTYPE /* obsolescent; will be withdrawn */
# define YYLTYPE_IS_DECLARED 1
# define YYLTYPE_IS_TRIVIAL 1
#endif
+267
View File
@@ -0,0 +1,267 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* 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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctype.h>
#include <string.h>
#include <fnmatch.h>
#include "expr_bool.h"
#include "ObjectXML.h"
#define YYERROR_VERBOSE
#define expr_bool_lex expr_lex
extern "C"
{
void expr_bool_error(
YYLTYPE * llocp,
ObjectXML * oxml,
bool& result,
char ** error_msg,
const char * str);
int expr_bool_lex (YYSTYPE *lvalp, YYLTYPE *llocp);
int expr_bool_parse(ObjectXML * oxml, bool& result, char ** errmsg);
}
void get_xml_attribute(ObjectXML * oxml, const char* attr, int& val);
void get_xml_attribute(ObjectXML * oxml, const char* attr, float& val);
void get_xml_attribute(ObjectXML * oxml, const char* attr, string& val);
%}
%parse-param {ObjectXML * oxml}
%parse-param {bool& result}
%parse-param {char ** error_msg}
%union {
char * val_str;
int val_int;
float val_float;
};
%defines
%locations
%pure_parser
%name-prefix = "expr_bool_"
%output = "expr_bool.cc"
%left '!' '&' '|'
%token <val_int> INTEGER
%token <val_str> STRING
%token <val_float> FLOAT
%type <val_int> stmt expr
%%
stmt: expr { result=$1; }
| { result=true; } /* TRUE BY DEFAULT, ON EMPTY STRINGS */
;
expr: STRING '=' INTEGER { int val;
get_xml_attribute(oxml,$1,val);
$$ = val == $3;
free($1);}
| STRING '!' '=' INTEGER { int val;
get_xml_attribute(oxml,$1,val);
$$ = val != $4;
free($1);}
| STRING '>' INTEGER { int val;
get_xml_attribute(oxml,$1,val);
$$ = val > $3;
free($1);}
| STRING '<' INTEGER { int val;
get_xml_attribute(oxml,$1,val);
$$ = val < $3;
free($1);}
| STRING '=' FLOAT { float val;
get_xml_attribute(oxml,$1,val);
$$ = val == $3;
free($1);}
| STRING '!' '=' FLOAT { float val;
get_xml_attribute(oxml,$1,val);
$$ = val != $4;
free($1);}
| STRING '>' FLOAT {float val;
get_xml_attribute(oxml,$1,val);
$$ = val > $3;
free($1);}
| STRING '<' FLOAT {float val;
get_xml_attribute(oxml,$1,val);
$$ = val < $3;
free($1);}
| STRING '=' STRING { string val;
get_xml_attribute(oxml,$1,val);
$$ = val.empty() ? false :fnmatch($3, val.c_str(), 0) == 0;
free($1);
free($3);}
| STRING '!''=' STRING { string val;
get_xml_attribute(oxml,$1,val);
$$ = val.empty() ? false : fnmatch($4, val.c_str(), 0) != 0;
free($1);
free($4);}
| expr '&' expr { $$ = $1 && $3; }
| expr '|' expr { $$ = $1 || $3; }
| '!' expr { $$ = ! $2; }
| '(' expr ')' { $$ = $2; }
;
%%
extern "C" void expr_bool_error(
YYLTYPE * llocp,
ObjectXML * oxml,
bool& result,
char ** error_msg,
const char * str)
{
int length;
length = strlen(str)+ 64;
*error_msg = (char *) malloc(sizeof(char)*length);
if (*error_msg != 0)
{
snprintf(*error_msg,
length,
"%s at line %i, columns %i:%i",
str,
llocp->first_line,
llocp->first_column,
llocp->last_column);
}
}
void get_xml_attribute(ObjectXML * oxml, const char* attr, int& val)
{
//TODO: pass xpath base
ostringstream xpath_t;
ostringstream xpath_s;
vector<string> results;
xpath_t << "/HOST/TEMPLATE/" << attr;
xpath_s << "/HOST/HOST_SHARE/" << attr;
val = 0;
results = (*oxml)[xpath_t.str().c_str()];
if (results.size() == 0)
{
results = (*oxml)[xpath_s.str().c_str()];
}
if (results.size() != 0)
{
istringstream iss(results[0]);
iss >> val;
}
}
void get_xml_attribute(ObjectXML * oxml, const char* attr, float& val)
{
//TODO: pass xpath base
ostringstream xpath_t;
ostringstream xpath_s;
vector<string> results;
xpath_t << "/HOST/TEMPLATE/" << attr;
xpath_s << "/HOST/HOST_SHARE/" << attr;
val = 0.0;
results = (*oxml)[xpath_t.str().c_str()];
if (results.size() == 0)
{
results = (*oxml)[xpath_s.str().c_str()];
}
if (results.size() != 0)
{
istringstream iss(results[0]);
iss >> val;
}
}
void get_xml_attribute(ObjectXML * oxml, const char* attr, string& val)
{
//TODO: pass xpath base
ostringstream xpath_t;
ostringstream xpath_s;
vector<string> results;
xpath_t << "/HOST/TEMPLATE/" << attr;
xpath_s << "/HOST/HOST_SHARE/" << attr;
val = "";
results = (*oxml)[xpath_t.str().c_str()];
if (results.size() == 0)
{
results = (*oxml)[xpath_s.str().c_str()];
}
if (results.size() != 0)
{
val = results[0];
}
}
File diff suppressed because it is too large. Load diff
+71
View File
@@ -0,0 +1,71 @@
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "expr_bool.h"
#include "expr_arith.h"
#define YY_DECL int expr_lex (YYSTYPE *lvalp, YYLTYPE *llocp)
#define YY_USER_ACTION llocp->first_line = yylineno; \
llocp->first_column = llocp->last_column; \
llocp->last_column += yyleng;
%}
%option nounput
%option prefix="expr_"
%option outfile="expr_parser.c"
%option yylineno
%%
/* --- Tokens --- */
[!&|=><()\*\+/\^-] { return *yytext;}
/* --- Strings, also quoted form --- */
[A-Za-z][0-9A-Za-z_]* { lvalp->val_str = strdup(yytext);
return STRING;}
\"\" { lvalp->val_str = NULL;
return STRING;}
\"[^\"]*\" { lvalp->val_str = strdup(yytext+1);
lvalp->val_str[yyleng-2] = '\0';
return STRING;}
/* --- Numbers --- */
-?[0-9]+ { lvalp->val_int = atoi(yytext);
return INTEGER;}
-?[0-9]+\.[0-9]+ { lvalp->val_float = atof(yytext);
return FLOAT;}
/* --- blanks --- */
[[:blank:]]*
%%
int expr_wrap()
{
return 1;
}
@@ -40,6 +40,7 @@ class ObjectXMLTest : public CppUnit::TestFixture
CPPUNIT_TEST( xpath_access );
CPPUNIT_TEST( node_constructor );
CPPUNIT_TEST( doc_update );
CPPUNIT_TEST( requirements );
CPPUNIT_TEST_SUITE_END ();
@@ -146,6 +147,98 @@ public:
}
};
void requirements()
{
string host = "<HOST>"
"<ID>1</ID>"
"<NAME>ursa12</NAME>"
"<STATE>2</STATE>"
"<IM_MAD>im_kvm</IM_MAD>"
"<VM_MAD>vmm_kvm</VM_MAD>"
"<TM_MAD>tm_nfs</TM_MAD>"
"<LAST_MON_TIME>1273799044</LAST_MON_TIME>"
"<HOST_SHARE>"
" <HID>1</HID>"
" <DISK_USAGE>0</DISK_USAGE>"
" <MEM_USAGE>0</MEM_USAGE>"
" <CPU_USAGE>0</CPU_USAGE>"
" <MAX_DISK>0</MAX_DISK>"
" <MAX_MEM>8194368</MAX_MEM>"
" <MAX_CPU>800</MAX_CPU>"
" <FREE_DISK>0</FREE_DISK>"
" <FREE_MEM>7959232</FREE_MEM>"
" <FREE_CPU>800</FREE_CPU>"
" <USED_DISK>0</USED_DISK>"
" <USED_MEM>523080</USED_MEM>"
" <USED_CPU>0</USED_CPU>"
" <RUNNING_VMS>12</RUNNING_VMS>"
"</HOST_SHARE>"
"<TEMPLATE>"
" <ARCH>x86_64</ARCH>"
" <CPUSPEED>2326</CPUSPEED>"
" <FREECPU>800.0</FREECPU>"
" <FREEMEMORY>7959232</FREEMEMORY>"
" <HOSTNAME>ursa12</HOSTNAME>"
" <HYPERVISOR>kvm</HYPERVISOR>"
" <MODELNAME>Intel(R) Xeon(R) CPU E5410 @ 2.33GHz</MODELNAME>"
" <NETRX>13335836573</NETRX>"
" <NETTX>47959</NETTX>"
" <TOTALCPU>800</TOTALCPU>"
" <TOTALMEMORY>8194368</TOTALMEMORY>"
" <USEDCPU>0.0</USEDCPU>"
" <USEDMEMORY>523080</USEDMEMORY>"
"</TEMPLATE>"
"</HOST>";
try
{
ObjectXML obj(host);
string reqs1 = "NETRX = \"13335836573\"";
string reqs2 = "NETRX != \"13335836573\"";
char *err1;
char *err2;
bool res1;
bool res2;
int rc1;
int rc2;
rc1 = obj.eval_bool(reqs1,res1,&err1);
rc2 = obj.eval_bool(reqs2,res2,&err2);
CPPUNIT_ASSERT(rc1 == 0);
CPPUNIT_ASSERT(rc2 == 0);
CPPUNIT_ASSERT(res1 == true);
CPPUNIT_ASSERT(res2 == false);
string rank1 = "RUNNING_VMS";
string rank2 = "MAX_CPU + NETTX";
int r1, r2;
rc1 = obj.eval_arith(rank1,r1,&err1);
rc2 = obj.eval_arith(rank2,r2,&err2);
CPPUNIT_ASSERT(rc1 == 0);
CPPUNIT_ASSERT(rc2 == 0);
CPPUNIT_ASSERT(r1 == 12);
CPPUNIT_ASSERT(r2 == 47959 + 800 );
}
catch(runtime_error& re)
{
cerr << re.what() << endl;
CPPUNIT_ASSERT(1 == 0);
}
};
static const string xml_history_dump;
static const string xml_history_dump2;
};